From 6d4791c6e956981291e3ef74f3385865706febc8 Mon Sep 17 00:00:00 2001 From: Gabor Date: Fri, 28 Jul 2017 13:57:56 +0200 Subject: [PATCH] style animation added --- lv_examples/1_5_animations/animations.c | 28 ++++++++ lv_obj/lv_obj.c | 4 +- lv_obj/lv_style.c | 91 +++++++++++++++++++++++++ lv_obj/lv_style.h | 43 ++++++++++++ 4 files changed, 163 insertions(+), 3 deletions(-) diff --git a/lv_examples/1_5_animations/animations.c b/lv_examples/1_5_animations/animations.c index e69de29bb..41c8edcb0 100644 --- a/lv_examples/1_5_animations/animations.c +++ b/lv_examples/1_5_animations/animations.c @@ -0,0 +1,28 @@ +/* + * + static lv_style_t s1; + lv_style_get(LV_STYLE_BTN_REL, &s1); + + static lv_style_t s2; + lv_style_get(LV_STYLE_BTN_REL, &s2); + s2.radius = 30; + s2.mcolor = COLOR_RED; + s2.bwidth = 8; + s2.opa = 50; + s2.hpad = 80; + //s2.font = font_get(FONT_DEJAVU_60); + + + lv_style_anim_t a; + a.act_time = -1000; + a.time = 1000; + a.playback = 1; + a.playback_pause = 300; + a.repeat = 1; + a.repeat_pause = 300; + a.end_cb = NULL; + a.style_anim = lv_style_get(LV_STYLE_BTN_REL, NULL); + a.style_start = &s1; + a.style_end = &s2; + lv_style_anim_create(&a); + */ diff --git a/lv_obj/lv_obj.c b/lv_obj/lv_obj.c index 5282193cc..0b9bf896b 100644 --- a/lv_obj/lv_obj.c +++ b/lv_obj/lv_obj.c @@ -1508,9 +1508,7 @@ static void lv_style_refr_core(void * style_p, lv_obj_t * obj) lv_obj_t * i; LL_READ(obj->child_ll, i) { if(i->style_p == style_p || style_p == NULL) { - lv_obj_inv(i); - i->signal_f(i, LV_SIGNAL_STYLE_CHG, NULL); - lv_obj_inv(i); + lv_child_refr_style(i); } lv_style_refr_core(style_p, i); diff --git a/lv_obj/lv_style.c b/lv_obj/lv_style.c index a2a85f9d0..65a3b0f1c 100644 --- a/lv_obj/lv_style.c +++ b/lv_obj/lv_style.c @@ -8,18 +8,31 @@ *********************/ #include "lv_conf.h" #include "lv_style.h" +#include "lv_obj.h" +#include "misc/mem/dyn_mem.h" /********************* * DEFINES *********************/ +#define LV_STYLE_ANIM_RES 255 /*Animation max in 1024 steps*/ +#define LV_STYLE_ANIM_SHIFT 8 /*log2(LV_STYLE_ANIM_RES)*/ + +#define VAL_PROP(v1, v2, r) v1 + (((v2-v1) * r) >> LV_STYLE_ANIM_SHIFT) +#define STYLE_ATTR_ANIM(attr, r) if(start->attr != end->attr) act->attr = VAL_PROP(start->attr, end->attr, r) /********************** * TYPEDEFS **********************/ +typedef struct { + const lv_style_t * style_start; + const lv_style_t * style_end; + lv_style_t * style_anim; +}lv_style_anim_dsc_t; /********************** * STATIC PROTOTYPES **********************/ +static void lv_style_aimator(lv_style_anim_dsc_t * dsc, int32_t val); /********************** * STATIC VARIABLES @@ -229,6 +242,84 @@ void lv_style_cpy(lv_style_t * dest, const lv_style_t * src) memcpy(dest, src, sizeof(lv_style_t)); } +/** + * Create an animation from a pre-configured 'lv_style_anim_t' variable + * @param anim pointer to a pre-configured 'lv_style_anim_t' variable (will be copied) + */ +void lv_style_anim_create(lv_style_anim_t * anim) +{ + lv_style_anim_dsc_t * dsc; + dsc = dm_alloc(sizeof(lv_style_anim_dsc_t)); + dsc->style_anim = anim->style_anim; + dsc->style_start = anim->style_start; + dsc->style_end = anim->style_end; + + anim_t a; + a.var = (void*)dsc; + a.start = 0; + a.end = LV_STYLE_ANIM_RES; + a.fp = (anim_fp_t)lv_style_aimator; + a.path = anim_get_path(ANIM_PATH_LIN); + a.end_cb = anim->end_cb; + a.act_time = anim->act_time; + a.time = anim->time; + a.playback = anim->playback; + a.playback_pause = anim->playback_pause; + a.repeat = anim->repeat; + a.repeat_pause = anim->repeat_pause; + + anim_create(&a); +} + /********************** * STATIC FUNCTIONS **********************/ + +/** + * Used by the style animations to set the values of a style according to start and end style. + * @param dsc the 'animated variable' set by lv_style_anim_create() + * @param val the current state of the animation between 0 and LV_STYLE_ANIM_RES + */ +static void lv_style_aimator(lv_style_anim_dsc_t * dsc, int32_t val) +{ + const lv_style_t * start = dsc->style_start; + const lv_style_t * end = dsc->style_end; + lv_style_t * act = dsc->style_anim; + + STYLE_ATTR_ANIM(opa, val); + STYLE_ATTR_ANIM(radius, val); + STYLE_ATTR_ANIM(bwidth, val); + STYLE_ATTR_ANIM(swidth, val); + STYLE_ATTR_ANIM(hpad, val); + STYLE_ATTR_ANIM(vpad, val); + STYLE_ATTR_ANIM(opad, val); + STYLE_ATTR_ANIM(line_space, val); + STYLE_ATTR_ANIM(letter_space, val); + STYLE_ATTR_ANIM(line_width, val); + STYLE_ATTR_ANIM(img_recolor, val); + + act->mcolor = color_mix(end->mcolor, start->mcolor, val); + act->gcolor = color_mix(end->gcolor, start->gcolor, val); + act->bcolor = color_mix(end->bcolor, start->bcolor, val); + act->scolor = color_mix(end->scolor, start->scolor, val); + act->ccolor = color_mix(end->ccolor, start->ccolor, val); + + + if(val == 0) { + act->empty = start->empty; + act->glass = start->glass; + act->font = start->font; + act->stype = start->stype; + act->txt_align = start->txt_align; + } + + if(val == LV_STYLE_ANIM_RES) { + act->empty = end->empty; + act->glass = end->glass; + act->font = end->font; + act->stype = end->stype; + act->txt_align = end->txt_align; + } + + lv_style_refr_objs(dsc->style_anim); +} diff --git a/lv_obj/lv_style.h b/lv_obj/lv_style.h index bb4de2080..34120045e 100644 --- a/lv_obj/lv_style.h +++ b/lv_obj/lv_style.h @@ -17,6 +17,7 @@ extern "C" { #include "misc/gfx/color.h" #include "misc/gfx/area.h" #include "misc/gfx/font.h" +#include "misc/gfx/anim.h" /********************* * DEFINES @@ -83,6 +84,34 @@ typedef enum { }lv_style_name_t; +typedef struct { + const lv_style_t * style_start; /*Pointer to the starting style*/ + const lv_style_t * style_end; /*Pointer to the destination style*/ + lv_style_t * style_anim; /*Pointer to a style to animate*/ + anim_cb_t end_cb; /*Call it when the animation is ready*/ + int16_t time; /*Animation time in ms*/ + int16_t act_time; /*Current time in animation. Set to negative to make delay.*/ + uint16_t playback_pause; /*Wait before play back*/ + uint16_t repeat_pause; /*Wait before repeat*/ + uint8_t playback :1; /*When the animation is ready play it back*/ + uint8_t repeat :1; /*Repeat the animation infinitely*/ +}lv_style_anim_t; + +/* Example initialization +lv_style_anim_t a; +a.style_anim = &style_to_anim; +a.style_start = &style_1; +a.style_end = &style_2; +a.act_time = 0; +a.time = 1000; +a.playback = 0; +a.playback_pause = 0; +a.repeat = 0; +a.repeat_pause = 0; +a.end_cb = NULL; +lv_style_anim_create(&a); + */ + /********************** * GLOBAL PROTOTYPES **********************/ @@ -99,8 +128,22 @@ void lv_style_init (void); */ lv_style_t * lv_style_get(lv_style_name_t style_name, lv_style_t * copy); + +/** + * Copy a style to an other + * @param dest pointer to the destination style + * @param src pointer to the source style + */ void lv_style_cpy(lv_style_t * dest, const lv_style_t * src); +void lv_style_anim_create(lv_style_anim_t * anim); + +/** + * Used by the style animations to set the values of a style according to start and end style. + * @param dsc the 'animated variable' set by lv_style_anim_create() + * @param val the current state of the animation between 0 and LV_STYLE_ANIM_RES + */ +static void lv_style_aimator(void * dsc, int32_t val); /********************** * MACROS