From 8a7f9cb13163da158cfc9fe25d8cbe3dc672a290 Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Wed, 24 Feb 2021 15:12:36 +0100 Subject: [PATCH] fix(style): use struct as color filter instead of pure callback for Micropython compatibility --- .../get_started/lv_example_get_started_2.c | 8 +++---- examples/widgets/btn/lv_example_btn_1.c | 4 ++-- examples/widgets/btn/lv_example_btn_2.c | 11 +++++----- src/extra/themes/default/lv_theme_default.c | 18 +++++++++++---- src/lv_core/lv_obj_style.c | 22 +++++++++---------- src/lv_core/lv_obj_style_gen.h | 12 +++++----- src/lv_misc/lv_color.h | 13 ++++++++++- src/lv_misc/lv_style.h | 3 +-- src/lv_misc/lv_style_gen.h | 6 ++--- 9 files changed, 58 insertions(+), 39 deletions(-) diff --git a/examples/get_started/lv_example_get_started_2.c b/examples/get_started/lv_example_get_started_2.c index b222004ed..fb0feb89c 100644 --- a/examples/get_started/lv_example_get_started_2.c +++ b/examples/get_started/lv_example_get_started_2.c @@ -28,13 +28,13 @@ void lv_example_get_started_2(void) /*Create a red style. Change only some colors.*/ lv_style_init(&style_btn_red); - lv_style_set_bg_color(&style_btn_red, lv_color_red()); - lv_style_set_bg_grad_color(&style_btn_red, lv_color_red_darken_3()); + lv_style_set_bg_color(&style_btn_red, lv_color_light_blue()); + lv_style_set_bg_grad_color(&style_btn_red, lv_color_light_blue_darken_3()); /*Create a style for the pressed state. Add color filter to make every color darker*/ lv_style_init(&style_btn_pressed); - lv_style_set_color_filter_cb(&style_btn_pressed, lv_color_darken); - lv_style_set_color_filter_opa(&style_btn_pressed, LV_OPA_30); + lv_style_set_bg_color(&style_btn_red, lv_color_blue()); + lv_style_set_bg_grad_color(&style_btn_red, lv_color_blue_darken_3()); /*Create a button and use the new styles*/ lv_obj_t * btn = lv_btn_create(lv_scr_act(), NULL); /*Add a button the current screen*/ diff --git a/examples/widgets/btn/lv_example_btn_1.c b/examples/widgets/btn/lv_example_btn_1.c index 56cf9b8ac..9ad53e746 100644 --- a/examples/widgets/btn/lv_example_btn_1.c +++ b/examples/widgets/btn/lv_example_btn_1.c @@ -5,10 +5,10 @@ static void event_handler(lv_obj_t * obj, lv_event_t event) { LV_UNUSED(obj); if(event == LV_EVENT_CLICKED) { - LV_LOG_USER("Clicked\n"); + LV_LOG_USER("Clicked"); } else if(event == LV_EVENT_VALUE_CHANGED) { - LV_LOG_USER("Toggled\n"); + LV_LOG_USER("Toggled"); } } diff --git a/examples/widgets/btn/lv_example_btn_2.c b/examples/widgets/btn/lv_example_btn_2.c index c345ec595..544e6056e 100644 --- a/examples/widgets/btn/lv_example_btn_2.c +++ b/examples/widgets/btn/lv_example_btn_2.c @@ -18,7 +18,7 @@ void lv_example_btn_2(void) lv_style_set_bg_opa(&style, LV_OPA_70); lv_style_set_bg_color(&style, lv_color_blue()); - lv_style_set_bg_grad_color(&style, lv_color_blue_darken_4()); + lv_style_set_bg_grad_color(&style, lv_color_blue_darken_2()); lv_style_set_bg_grad_dir(&style, LV_GRAD_DIR_VER); lv_style_set_border_opa(&style, LV_OPA_40); @@ -27,16 +27,15 @@ void lv_example_btn_2(void) lv_style_set_shadow_width(&style, 8); lv_style_set_shadow_color(&style, lv_color_grey()); - lv_style_set_shadow_ofs_x(&style, 8); lv_style_set_shadow_ofs_y(&style, 8); lv_style_set_text_color(&style, lv_color_white()); + /*Init the pressed style*/ - lv_style_set_shadow_ofs_x(&style_pr, 4); - lv_style_set_shadow_ofs_y(&style_pr, 4); - lv_style_set_color_filter_cb(&style_pr, lv_color_darken); /*Darken every color*/ - lv_style_set_color_filter_opa(&style_pr, LV_OPA_30); + lv_style_set_shadow_ofs_y(&style_pr, 3); + lv_style_set_bg_color(&style_pr, lv_color_blue_darken_2()); + lv_style_set_bg_grad_color(&style_pr, lv_color_blue_darken_4()); lv_obj_t * btn1 = lv_btn_create(lv_scr_act(), NULL); lv_obj_remove_style(btn1, LV_PART_ANY, LV_STATE_ANY, NULL); diff --git a/src/extra/themes/default/lv_theme_default.c b/src/extra/themes/default/lv_theme_default.c index eeb22fc69..ca7d336b7 100644 --- a/src/extra/themes/default/lv_theme_default.c +++ b/src/extra/themes/default/lv_theme_default.c @@ -194,6 +194,12 @@ static bool inited; * STATIC FUNCTIONS **********************/ +static lv_color_t dark_color_filter_cb(const lv_color_filter_dsc_t * f, lv_color_t c, lv_opa_t opa) +{ + LV_UNUSED(f); + return lv_color_darken(c, opa); +} + static void style_init(void) { static const lv_style_prop_t trans_props[] = { @@ -201,7 +207,7 @@ static void style_init(void) LV_STYLE_TRANSFORM_WIDTH, LV_STYLE_TRANSFORM_HEIGHT, LV_STYLE_TRANSFORM_ZOOM, LV_STYLE_TRANSFORM_ANGLE, LV_STYLE_CONTENT_OFS_X, LV_STYLE_CONTENT_OFS_Y, - LV_STYLE_COLOR_FILTER_OPA, LV_STYLE_COLOR_FILTER_CB, + LV_STYLE_COLOR_FILTER_OPA, LV_STYLE_COLOR_FILTER_DSC, 0 }; @@ -271,13 +277,17 @@ static void style_init(void) lv_style_set_pad_column(&styles->btn, LV_DPX(5)); lv_style_set_pad_row(&styles->btn, LV_DPX(5)); + + static lv_color_filter_dsc_t dark_filter; + lv_color_filter_dsc_init(&dark_filter, dark_color_filter_cb); + style_init_reset(&styles->pressed); - lv_style_set_color_filter_cb(&styles->pressed, lv_color_darken); + lv_style_set_color_filter_dsc(&styles->pressed, &dark_filter); lv_style_set_color_filter_opa(&styles->pressed, 35); style_init_reset(&styles->disabled); - lv_style_set_color_filter_cb(&styles->disabled, gray_filter); - lv_style_set_color_filter_opa(&styles->disabled, LV_OPA_50); +// lv_style_set_color_filter_dsc(&styles->disabled, gray_filter); +// lv_style_set_color_filter_opa(&styles->disabled, LV_OPA_50); style_init_reset(&styles->clip_corner); lv_style_set_clip_corner(&styles->clip_corner, true); diff --git a/src/lv_core/lv_obj_style.c b/src/lv_core/lv_obj_style.c index 0d6670d32..291ba4ddf 100644 --- a/src/lv_core/lv_obj_style.c +++ b/src/lv_core/lv_obj_style.c @@ -548,10 +548,10 @@ static bool get_prop_core(const lv_obj_t * obj, uint8_t part, lv_style_prop_t pr static lv_style_value_t apply_color_filter(const lv_obj_t * obj, uint32_t part, lv_style_value_t v) { if(obj == NULL) return v; - lv_color_filter_cb_t f = lv_obj_get_style_color_filter_cb(obj, part); - if(f) { + const lv_color_filter_dsc_t * f = lv_obj_get_style_color_filter_dsc(obj, part); + if(f && f->filter_cb) { lv_opa_t f_opa = lv_obj_get_style_color_filter_opa(obj, part); - if(f_opa != 0) v.color = f(v.color, f_opa); + if(f_opa != 0) v.color = f->filter_cb(f, v.color, f_opa); } return v; } @@ -640,11 +640,11 @@ static void update_cache(lv_obj_t * obj, lv_part_t part, lv_style_prop_t prop) if(v.ptr == NULL) list->cache_content_text_zero = 1; else list->cache_content_text_zero = 0; } - if(prop == LV_STYLE_PROP_ALL || prop == LV_STYLE_COLOR_FILTER_CB || prop == LV_STYLE_COLOR_FILTER_OPA) { + if(prop == LV_STYLE_PROP_ALL || prop == LV_STYLE_COLOR_FILTER_DSC || prop == LV_STYLE_COLOR_FILTER_OPA) { lv_style_value_t vf[2]; - if(get_prop_core(obj, part, LV_STYLE_COLOR_FILTER_CB, &vf[0]) == false) vf[0].func = 0; + if(get_prop_core(obj, part, LV_STYLE_COLOR_FILTER_DSC, &vf[0]) == false) vf[0].ptr = NULL; if(get_prop_core(obj, part, LV_STYLE_COLOR_FILTER_OPA, &vf[1]) == false) vf[1].num = 0; - if(vf[0].func == NULL || vf[1].num == 0) list->cache_filter_zero = 1; + if(vf[0].ptr == NULL || vf[1].num == 0) list->cache_filter_zero = 1; else list->cache_filter_zero = 0; } @@ -737,7 +737,7 @@ static cache_t read_cache(const lv_obj_t * obj, lv_part_t part, lv_style_prop_t if(list->cache_content_text_zero ) return CACHE_ZERO; else return CACHE_NEED_CHECK; break; - case LV_STYLE_COLOR_FILTER_CB: + case LV_STYLE_COLOR_FILTER_DSC: case LV_STYLE_COLOR_FILTER_OPA: if(list->cache_filter_zero ) return CACHE_ZERO; else return CACHE_NEED_CHECK; @@ -871,9 +871,9 @@ static void trans_anim_cb(void * _tr, int32_t v) if(v < 255) value_final.ptr = tr->start_value.ptr; else value_final.ptr = tr->end_value.ptr; break; - case LV_STYLE_COLOR_FILTER_CB: - if(tr->start_value.func == NULL) value_final.func = tr->end_value.func; - else if(tr->end_value.func == NULL) value_final.func = tr->start_value.func; + case LV_STYLE_COLOR_FILTER_DSC: + if(tr->start_value.ptr == NULL) value_final.ptr = tr->end_value.ptr; + else if(tr->end_value.ptr == NULL) value_final.ptr = tr->start_value.ptr; else if(v < 128) value_final.ptr = tr->start_value.ptr; else value_final.ptr = tr->end_value.ptr; break; @@ -898,7 +898,7 @@ static void trans_anim_cb(void * _tr, int32_t v) lv_style_value_t old_value; bool refr = true; if(lv_style_get_prop(list->styles[i].style, tr->prop, &old_value)) { - if(value_final.ptr == old_value.ptr && value_final.func == old_value.func && value_final.color.full == old_value.color.full && value_final.num == old_value.num) { + if(value_final.ptr == old_value.ptr && value_final.color.full == old_value.color.full && value_final.num == old_value.num) { refr = false; } } diff --git a/src/lv_core/lv_obj_style_gen.h b/src/lv_core/lv_obj_style_gen.h index d05b68826..004f63805 100644 --- a/src/lv_core/lv_obj_style_gen.h +++ b/src/lv_core/lv_obj_style_gen.h @@ -40,10 +40,10 @@ static inline lv_opa_t lv_obj_get_style_opa(const struct _lv_obj_t * obj, uint32 return (lv_opa_t)v.num; } -static inline lv_color_filter_cb_t lv_obj_get_style_color_filter_cb(const struct _lv_obj_t * obj, uint32_t part) +static inline const lv_color_filter_dsc_t * lv_obj_get_style_color_filter_dsc(const struct _lv_obj_t * obj, uint32_t part) { - lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_COLOR_FILTER_CB); - return (lv_color_filter_cb_t)v.func; + lv_style_value_t v = lv_obj_get_style_prop(obj, part, LV_STYLE_COLOR_FILTER_DSC); + return (const lv_color_filter_dsc_t *) v.ptr; } static inline lv_opa_t lv_obj_get_style_color_filter_opa(const struct _lv_obj_t * obj, uint32_t part) @@ -576,12 +576,12 @@ static inline void lv_obj_set_style_opa(struct _lv_obj_t * obj, uint32_t part, u lv_obj_set_local_style_prop(obj, part, state, LV_STYLE_OPA, v); } -static inline void lv_obj_set_style_color_filter_cb(struct _lv_obj_t * obj, uint32_t part, uint32_t state, lv_color_filter_cb_t value) +static inline void lv_obj_set_style_color_filter_dsc(struct _lv_obj_t * obj, uint32_t part, uint32_t state, const lv_color_filter_dsc_t * value) { lv_style_value_t v = { - .func = (void (*)(void))value + .ptr = (void *) value }; - lv_obj_set_local_style_prop(obj, part, state, LV_STYLE_COLOR_FILTER_CB, v); + lv_obj_set_local_style_prop(obj, part, state, LV_STYLE_COLOR_FILTER_DSC, v); } static inline void lv_obj_set_style_color_filter_opa(struct _lv_obj_t * obj, uint32_t part, uint32_t state, lv_opa_t value) diff --git a/src/lv_misc/lv_color.h b/src/lv_misc/lv_color.h index d2190188a..07847926b 100644 --- a/src/lv_misc/lv_color.h +++ b/src/lv_misc/lv_color.h @@ -265,8 +265,14 @@ typedef struct { typedef uint8_t lv_opa_t; //! @endcond +struct _lv_color_filter_dsc_t; -typedef lv_color_t (*lv_color_filter_cb_t)(lv_color_t, lv_opa_t); +typedef lv_color_t (*lv_color_filter_cb_t)(const struct _lv_color_filter_dsc_t *, lv_color_t, lv_opa_t); + +typedef struct _lv_color_filter_dsc_t { + lv_color_filter_cb_t filter_cb; + void * user_data; +}lv_color_filter_dsc_t; /********************** * GLOBAL PROTOTYPES @@ -584,6 +590,11 @@ static inline lv_color_t lv_color_hex3(uint32_t c) (uint8_t)((c & 0xF) | ((c & 0xF) << 4))); } +static inline void lv_color_filter_dsc_init(lv_color_filter_dsc_t * dsc, lv_color_filter_cb_t cb) +{ + dsc->filter_cb = cb; +} + //! @cond Doxygen_Suppress //! LV_ATTRIBUTE_FAST_MEM void lv_color_fill(lv_color_t * buf, lv_color_t color, uint32_t px_num); diff --git a/src/lv_misc/lv_style.h b/src/lv_misc/lv_style.h index d65cbc29f..114eeced3 100644 --- a/src/lv_misc/lv_style.h +++ b/src/lv_misc/lv_style.h @@ -102,7 +102,6 @@ typedef union { int32_t num; /**< Number integer number (opacity, enums, booleans or "normal" numbers)*/ const void * ptr; /**< Constant pointers (font, cone text, etc)*/ lv_color_t color; /**< Colors */ - void (* func)(void); /**< Function pointers */ }lv_style_value_t; /** @@ -120,7 +119,7 @@ typedef enum { LV_STYLE_TRANSFORM_ANGLE = 6 | LV_STYLE_PROP_EXT_DRAW, LV_STYLE_OPA = 7 | LV_STYLE_PROP_INHERIT, - LV_STYLE_COLOR_FILTER_CB = 8, + LV_STYLE_COLOR_FILTER_DSC = 8, LV_STYLE_COLOR_FILTER_OPA = 9, LV_STYLE_ANIM_TIME = 10, LV_STYLE_TRANSITION = 11, diff --git a/src/lv_misc/lv_style_gen.h b/src/lv_misc/lv_style_gen.h index 5527e8dcd..0f401f214 100644 --- a/src/lv_misc/lv_style_gen.h +++ b/src/lv_misc/lv_style_gen.h @@ -54,12 +54,12 @@ static inline void lv_style_set_opa(lv_style_t * style, lv_opa_t value) lv_style_set_prop(style, LV_STYLE_OPA, v); } -static inline void lv_style_set_color_filter_cb(lv_style_t * style, lv_color_filter_cb_t value) +static inline void lv_style_set_color_filter_dsc(lv_style_t * style, const lv_color_filter_dsc_t * value) { lv_style_value_t v = { - .func = (void (*)(void))value + .ptr = (void *)value }; - lv_style_set_prop(style, LV_STYLE_COLOR_FILTER_CB, v); + lv_style_set_prop(style, LV_STYLE_COLOR_FILTER_DSC, v); } static inline void lv_style_set_color_filter_opa(lv_style_t * style, lv_opa_t value)