1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-21 06:53:01 +08:00
lvgl/src/lv_core/lv_style.c

941 lines
30 KiB
C
Raw Normal View History

/**
* @file lv_style.c
*
*/
/*********************
* INCLUDES
*********************/
2017-07-28 13:57:56 +02:00
#include "lv_obj.h"
2019-09-24 16:30:38 +02:00
#include "../lv_core/lv_debug.h"
2017-11-23 20:42:14 +01:00
#include "../lv_misc/lv_mem.h"
#include "../lv_misc/lv_anim.h"
/*********************
* DEFINES
*********************/
2019-04-04 07:15:40 +02:00
#define STYLE_MIX_MAX 256
#define STYLE_MIX_SHIFT 8 /*log2(STYLE_MIX_MAX)*/
#define VAL_PROP(v1, v2, r) v1 + (((v2 - v1) * r) >> STYLE_MIX_SHIFT)
2019-06-06 06:05:40 +02:00
#define STYLE_ATTR_MIX(attr, r) \
if(start->attr != end->attr) { \
res->attr = VAL_PROP(start->attr, end->attr, r); \
} else { \
res->attr = start->attr; \
2019-04-04 07:15:40 +02:00
}
2019-12-14 23:39:26 +01:00
#define LV_STYLE_PROP_TO_ID(prop) (prop & 0xFF);
#define LV_STYLE_PROP_GET_TYPE(prop) ((prop >> 8) & 0xFF);
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
2019-12-14 23:39:26 +01:00
static inline int32_t get_property_index(const lv_style_t * style, lv_style_property_t prop);
#if LV_USE_ANIMATION
static void style_animator(lv_style_anim_dsc_t * dsc, lv_anim_value_t val);
2019-04-22 08:45:07 +02:00
static void style_animation_common_end_cb(lv_anim_t * a);
2017-11-27 17:48:54 +01:00
#endif
/**********************
* GLOABAL VARIABLES
**********************/
2020-01-01 18:46:22 +01:00
lv_style_t lv_style_transp_tight;
/**********************
* STATIC VARIABLES
**********************/
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
2020-01-01 18:46:22 +01:00
void lv_style_built_in_init(void)
{
lv_style_init(&lv_style_transp_tight);
lv_style_set_opa(&lv_style_transp_tight, LV_STYLE_BG_OPA, LV_OPA_TRANSP);
lv_style_set_opa(&lv_style_transp_tight, LV_STYLE_BORDER_OPA, LV_OPA_TRANSP);
lv_style_set_opa(&lv_style_transp_tight, LV_STYLE_SHADOW_OPA, LV_OPA_TRANSP);
lv_style_set_opa(&lv_style_transp_tight, LV_STYLE_PATTERN_OPA, LV_OPA_TRANSP);
2020-01-07 00:49:47 +01:00
lv_style_set_int(&lv_style_transp_tight, LV_STYLE_PAD_LEFT, 0);
lv_style_set_int(&lv_style_transp_tight, LV_STYLE_PAD_RIGHT, 0);
lv_style_set_int(&lv_style_transp_tight, LV_STYLE_PAD_TOP, 0);
lv_style_set_int(&lv_style_transp_tight, LV_STYLE_PAD_BOTTOM, 0);
lv_style_set_int(&lv_style_transp_tight, LV_STYLE_PAD_INNER, 0);
2020-01-01 18:46:22 +01:00
}
2019-12-14 23:39:26 +01:00
void lv_style_init(lv_style_t * style)
{
style->map = NULL;
style->size = 0;
2019-12-31 22:13:32 +01:00
}
void lv_style_copy(lv_style_t * style_dest, const lv_style_t * style_src)
{
lv_style_init(style_dest);
style_dest->map = lv_mem_alloc(style_src->size);
memcpy(style_dest->map, style_src->map, style_src->size);
style_dest->size = style_src->size;
}
void lv_style_dsc_init(lv_style_dsc_t * style_dsc)
{
lv_style_init(&style_dsc->local);
style_dsc->classes = NULL;
style_dsc->class_cnt = 0;
2020-01-08 21:31:05 +01:00
memset(&style_dsc->cache, 0xff, sizeof(lv_style_cache_t));
2019-12-26 01:30:20 +01:00
style_dsc->cache.enabled = 1;
}
2020-01-08 21:31:05 +01:00
void lv_style_dsc_add_class(lv_style_dsc_t * dsc, lv_style_t * class)
{
2019-12-22 23:19:51 +01:00
/* Do not allocate memory for the first class.
* It can be simply stored as a pointer.*/
2020-01-08 21:31:05 +01:00
if(dsc->class_cnt == 0) {
dsc->classes = (lv_style_t**)class;
dsc->class_cnt = 1;
2019-12-22 23:19:51 +01:00
} else {
lv_style_t ** new_classes;
2020-01-08 21:31:05 +01:00
if(dsc->class_cnt == 1) new_classes = lv_mem_alloc(sizeof(lv_style_t *) * (dsc->class_cnt + 1));
else new_classes = lv_mem_realloc(dsc->classes, sizeof(lv_style_t *) * (dsc->class_cnt + 1));
2019-12-22 23:19:51 +01:00
LV_ASSERT_MEM(new_classes);
if(new_classes == NULL) {
LV_LOG_WARN("lv_style_dsc_add_class: couldn't add the class");
return;
}
2020-01-08 21:31:05 +01:00
if(dsc->class_cnt == 1) new_classes[0] = (lv_style_t*)dsc->classes;
new_classes[dsc->class_cnt] = class;
2019-12-22 23:19:51 +01:00
2020-01-08 21:31:05 +01:00
dsc->class_cnt++;
dsc->classes = new_classes;
}
2020-01-08 21:31:05 +01:00
lv_style_cache_update(dsc);
}
2020-01-08 21:31:05 +01:00
void lv_style_dsc_remove_class(lv_style_dsc_t * dsc, lv_style_t * class)
{
2020-01-08 21:31:05 +01:00
if(dsc->class_cnt == 0) return;
if(dsc->class_cnt == 1) {
if((lv_style_t*)dsc->classes == class) {
dsc->classes = NULL;
dsc->class_cnt = 0;
2019-12-22 23:19:51 +01:00
}
} else {
2020-01-08 21:31:05 +01:00
lv_style_t ** new_classes = lv_mem_realloc(dsc->classes, sizeof(lv_style_t *) * (dsc->class_cnt - 1));
2019-12-22 23:19:51 +01:00
LV_ASSERT_MEM(new_classes);
if(new_classes == NULL) {
LV_LOG_WARN("lv_style_dsc_remove_class: couldn't remove the class");
return;
}
uint8_t i,j;
2020-01-08 21:31:05 +01:00
for(i = 0, j = 0; i < dsc->class_cnt; i++) {
if(dsc->classes[i] == class) continue;
new_classes[j] = dsc->classes[i];
2019-12-22 23:19:51 +01:00
j++;
}
2020-01-08 21:31:05 +01:00
dsc->class_cnt--;
dsc->classes = new_classes;
}
2020-01-08 21:31:05 +01:00
lv_style_cache_update(dsc);
}
void lv_style_dsc_reset(lv_style_dsc_t * style_dsc)
{
2019-12-22 23:19:51 +01:00
if(style_dsc->class_cnt > 1) lv_mem_free(style_dsc->classes);
style_dsc->classes = NULL;
style_dsc->class_cnt = 0;
lv_style_reset(&style_dsc->local);
2020-01-08 21:31:05 +01:00
memset(&style_dsc->cache, 0xff, sizeof(lv_style_cache_t));
2019-12-22 23:19:51 +01:00
}
2019-12-26 01:30:20 +01:00
void lv_style_reset(lv_style_t * style)
{
lv_mem_free(style->map);
style->map = NULL;
style->size = 0;
2017-05-08 10:09:41 +02:00
}
2020-01-07 00:49:47 +01:00
void lv_style_set_int(lv_style_t * style, lv_style_property_t prop, lv_style_int_t value)
2019-12-14 23:39:26 +01:00
{
int32_t id = get_property_index(style, prop);
2019-12-19 11:05:04 +01:00
/*The property already exists but not sure it's state is the same*/
2019-12-14 23:39:26 +01:00
if(id >= 0) {
2019-12-19 11:05:04 +01:00
lv_style_attr_t attr_found;
lv_style_attr_t attr_goal;
attr_found.full = *(style->map + id + 1);
attr_goal.full = (prop >> 8) & 0xFFU;
if(attr_found.bits.state == attr_goal.bits.state) {
2020-01-06 22:14:04 +01:00
memcpy(style->map + id + sizeof(lv_style_property_t), &value, sizeof(lv_style_int_t));
2019-12-19 11:05:04 +01:00
return;
}
2019-12-14 23:39:26 +01:00
}
2019-12-19 11:05:04 +01:00
2019-12-14 23:39:26 +01:00
/*Add new property if not exists yet*/
2020-01-06 22:14:04 +01:00
style->size += sizeof(lv_style_property_t) + sizeof(lv_style_int_t);
2019-12-19 11:05:04 +01:00
style->map = lv_mem_realloc(style->map, style->size);
LV_ASSERT_MEM(style->map);
if(style == NULL) return;
2020-01-06 22:14:04 +01:00
memcpy(style->map + style->size - (sizeof(lv_style_property_t) + sizeof(lv_style_int_t)), &prop, sizeof(lv_style_property_t));
memcpy(style->map + style->size - sizeof(lv_style_int_t), &value, sizeof(lv_style_int_t));
2019-12-14 23:39:26 +01:00
}
void lv_style_set_color(lv_style_t * style, lv_style_property_t prop, lv_color_t color)
{
int32_t id = get_property_index(style, prop);
2019-12-19 11:05:04 +01:00
/*The property already exists but not sure it's state is the same*/
2019-12-14 23:39:26 +01:00
if(id >= 0) {
2019-12-19 11:05:04 +01:00
lv_style_attr_t attr_found;
lv_style_attr_t attr_goal;
attr_found.full = *(style->map + id + 1);
attr_goal.full = (prop >> 8) & 0xFFU;
if(attr_found.bits.state == attr_goal.bits.state) {
memcpy(style->map + id + sizeof(lv_style_property_t), &color, sizeof(lv_color_t));
return;
}
2019-12-14 23:39:26 +01:00
}
2019-12-19 11:05:04 +01:00
2019-12-14 23:39:26 +01:00
/*Add new property if not exists yet*/
2019-12-19 11:05:04 +01:00
style->size += sizeof(lv_style_property_t) + sizeof(lv_color_t);
style->map = lv_mem_realloc(style->map, style->size);
LV_ASSERT_MEM(style->map);
if(style == NULL) return;
memcpy(style->map + style->size - (sizeof(lv_style_property_t) + sizeof(lv_color_t)), &prop, sizeof(lv_style_property_t));
memcpy(style->map + style->size - sizeof(lv_color_t), &color, sizeof(lv_color_t));
2019-12-14 23:39:26 +01:00
}
void lv_style_set_opa(lv_style_t * style, lv_style_property_t prop, lv_opa_t opa)
{
int32_t id = get_property_index(style, prop);
2019-12-19 11:05:04 +01:00
/*The property already exists but not sure it's state is the same*/
2019-12-14 23:39:26 +01:00
if(id >= 0) {
2019-12-19 11:05:04 +01:00
lv_style_attr_t attr_found;
lv_style_attr_t attr_goal;
attr_found.full = *(style->map + id + 1);
attr_goal.full = (prop >> 8) & 0xFFU;
if(attr_found.bits.state == attr_goal.bits.state) {
memcpy(style->map + id + sizeof(lv_style_property_t), &opa, sizeof(lv_opa_t));
return;
}
2019-12-14 23:39:26 +01:00
}
2019-12-19 11:05:04 +01:00
2019-12-14 23:39:26 +01:00
/*Add new property if not exists yet*/
2019-12-19 11:05:04 +01:00
style->size += sizeof(lv_style_property_t) + sizeof(lv_opa_t);
style->map = lv_mem_realloc(style->map, style->size);
LV_ASSERT_MEM(style->map);
if(style == NULL) return;
2019-12-14 23:39:26 +01:00
2019-12-19 11:05:04 +01:00
memcpy(style->map + style->size - (sizeof(lv_style_property_t) + sizeof(lv_opa_t)), &prop, sizeof(lv_style_property_t));
memcpy(style->map + style->size - sizeof(lv_opa_t), &opa, sizeof(lv_opa_t));
}
2019-12-14 23:39:26 +01:00
2019-12-19 22:44:18 +01:00
void lv_style_set_ptr(lv_style_t * style, lv_style_property_t prop, void * p)
{
int32_t id = get_property_index(style, prop);
/*The property already exists but not sure it's state is the same*/
if(id >= 0) {
lv_style_attr_t attr_found;
lv_style_attr_t attr_goal;
attr_found.full = *(style->map + id + 1);
attr_goal.full = (prop >> 8) & 0xFFU;
if(attr_found.bits.state == attr_goal.bits.state) {
memcpy(style->map + id + sizeof(lv_style_property_t), &p, sizeof(void *));
return;
}
}
/*Add new property if not exists yet*/
style->size += sizeof(lv_style_property_t) + sizeof(void *);
style->map = lv_mem_realloc(style->map, style->size);
LV_ASSERT_MEM(style->map);
if(style == NULL) return;
memcpy(style->map + style->size - (sizeof(lv_style_property_t) + sizeof(void *)), &prop, sizeof(lv_style_property_t));
memcpy(style->map + style->size - sizeof(void *), &p, sizeof(void *));
}
2019-12-19 11:05:04 +01:00
/**
* Get the a property from a style.
* Take into account the style state and return the property which matches the best.
* @param style pointer to a style where to search
* @param prop the property, might contain ORed style states too
* @param res buffer to store the result
* @return the weight of the found property (how well it fits to the style state).
* Higher number is means better fit
* -1 if the not found (`res` will be undefined)
*/
2020-01-07 00:49:47 +01:00
int16_t lv_style_get_int(const lv_style_t * style, lv_style_property_t prop, lv_style_int_t * res)
2019-12-14 23:39:26 +01:00
{
2020-01-06 22:14:04 +01:00
if(style == NULL) return -1;
2019-12-14 23:39:26 +01:00
int32_t id = get_property_index(style, prop);
if(id < 0) {
2019-12-19 11:05:04 +01:00
return -1;
2019-12-14 23:39:26 +01:00
} else {
2020-01-06 22:14:04 +01:00
memcpy(res, &style->map[id + sizeof(lv_style_property_t)], sizeof(lv_style_int_t));
2019-12-19 11:05:04 +01:00
lv_style_attr_t attr_act;
attr_act.full = style->map[id + 1];
lv_style_attr_t attr_goal;
attr_goal.full = (prop >> 8) & 0xFF;
return attr_act.bits.state & attr_goal.bits.state;
2019-12-14 23:39:26 +01:00
}
}
2019-12-19 11:05:04 +01:00
int16_t lv_style_get_opa(const lv_style_t * style, lv_style_property_t prop, lv_opa_t * res)
2019-12-14 23:39:26 +01:00
{
2020-01-06 22:14:04 +01:00
if(style == NULL) return -1;
2019-12-14 23:39:26 +01:00
int32_t id = get_property_index(style, prop);
if(id < 0) {
2019-12-19 11:05:04 +01:00
return -1;
2019-12-14 23:39:26 +01:00
} else {
memcpy(res, &style->map[id + sizeof(lv_style_property_t)], sizeof(lv_opa_t));
2019-12-19 11:05:04 +01:00
lv_style_attr_t attr_act;
attr_act.full = style->map[id + 1];
lv_style_attr_t attr_goal;
attr_goal.full = (prop >> 8) & 0xFF;
return attr_act.bits.state & attr_goal.bits.state;
2019-12-14 23:39:26 +01:00
}
}
2019-12-19 11:05:04 +01:00
int16_t lv_style_get_color(const lv_style_t * style, lv_style_property_t prop, lv_color_t * res)
2019-12-14 23:39:26 +01:00
{
2020-01-06 22:14:04 +01:00
if(style == NULL) return -1;
2019-12-14 23:39:26 +01:00
int32_t id = get_property_index(style, prop);
if(id < 0) {
2019-12-19 11:05:04 +01:00
return -1;
2019-12-14 23:39:26 +01:00
} else {
memcpy(res, &style->map[id + sizeof(lv_style_property_t)], sizeof(lv_color_t));
2019-12-19 11:05:04 +01:00
lv_style_attr_t attr_act;
attr_act.full = style->map[id + 1];
lv_style_attr_t attr_goal;
attr_goal.full = (prop >> 8) & 0xFF;
2019-12-19 22:44:18 +01:00
return attr_act.bits.state & attr_goal.bits.state;
}
}
int16_t lv_style_get_ptr(const lv_style_t * style, lv_style_property_t prop, void ** res)
{
2020-01-06 22:14:04 +01:00
if(style == NULL) return -1;
2019-12-19 22:44:18 +01:00
int32_t id = get_property_index(style, prop);
if(id < 0) {
return -1;
} else {
memcpy(res, &style->map[id + sizeof(lv_style_property_t)], sizeof(void*));
lv_style_attr_t attr_act;
attr_act.full = style->map[id + 1];
lv_style_attr_t attr_goal;
attr_goal.full = (prop >> 8) & 0xFF;
2019-12-19 11:05:04 +01:00
return attr_act.bits.state & attr_goal.bits.state;
2019-12-14 23:39:26 +01:00
}
}
2020-01-08 21:31:05 +01:00
uint32_t prop_fooled[256];
lv_res_t lv_style_dsc_get_int(lv_style_dsc_t * dsc, lv_style_property_t prop, lv_style_int_t * value)
{
if(dsc == NULL) return LV_RES_INV;
lv_res_t res = LV_RES_OK;
if(dsc->cache.enabled) {
switch(prop & (~LV_STYLE_STATE_MASK)) {
case LV_STYLE_BG_BLEND_MODE:
res = dsc->cache.bg_blend_mode;
break;
case LV_STYLE_BORDER_BLEND_MODE:
res = dsc->cache.border_blend_mode;
break;
case LV_STYLE_IMAGE_BLEND_MODE:
res = dsc->cache.image_blend_mode;
break;
case LV_STYLE_TEXT_BLEND_MODE:
res = dsc->cache.text_blend_mode;
break;
case LV_STYLE_LINE_BLEND_MODE:
res = dsc->cache.line_blend_mode;
break;
case LV_STYLE_SHADOW_BLEND_MODE:
res = dsc->cache.shadow_blend_mode;
break;
case LV_STYLE_PATTERN_BLEND_MODE:
res = dsc->cache.pattern_blend_mode;
break;
case LV_STYLE_CLIP_CORNER:
res = dsc->cache.clip_corner;
break;
case LV_STYLE_LETTER_SPACE:
res = dsc->cache.letter_space;
break;
case LV_STYLE_LINE_SPACE:
res = dsc->cache.line_space;
break;
case LV_STYLE_BORDER_PART:
res = dsc->cache.border_part;
break;
case LV_STYLE_BORDER_WIDTH:
res = dsc->cache.border_width;
break;
case LV_STYLE_SHADOW_WIDTH:
res = dsc->cache.shadow_width;
break;
}
}
if(res == LV_RES_INV) return LV_RES_INV;
lv_style_attr_t attr;
attr.full = prop >> 8;
int16_t weight_goal = attr.full;
int16_t weight_act;
int16_t weight = -1;
lv_style_int_t value_act;
weight_act = lv_style_get_int(&dsc->local, prop, &value_act);
/*On perfect match return the value immediately*/
if(weight_act == weight_goal) {
*value = value_act;
return LV_RES_OK;
}
/*If the found ID is better the current candidate then use it*/
else if(weight_act > weight) {
weight = weight_act;
*value = value_act;
}
int16_t ci;
for(ci = dsc->class_cnt - 1; ci >= 0; ci--) {
lv_style_t * class = lv_style_dsc_get_class(dsc, ci);
weight_act = lv_style_get_int(class, prop, &value_act);
/*On perfect match return the value immediately*/
if(weight_act == weight_goal) {
*value = value_act;
return LV_RES_OK;
}
/*If the found ID is better the current candidate then use it*/
else if(weight_act > weight) {
weight = weight_act;
*value = value_act;
}
}
if(weight >= 0) {
prop_fooled[prop&0xFF]++;
return LV_RES_OK;
}
else return LV_RES_INV;
}
lv_res_t lv_style_dsc_get_color(lv_style_dsc_t * dsc, lv_style_property_t prop, lv_color_t * value)
{
if(dsc == NULL) return LV_RES_INV;
lv_res_t res = LV_RES_OK;
if(dsc->cache.enabled) {
switch(prop & (~LV_STYLE_STATE_MASK)) {
case LV_STYLE_TEXT_COLOR:
res = dsc->cache.text_color;
break;
}
}
if(res == LV_RES_INV) return LV_RES_INV;
lv_style_attr_t attr;
attr.full = prop >> 8;
int16_t weight_goal = attr.full;
int16_t weight_act;
int16_t weight = -1;
lv_color_t value_act;
weight_act = lv_style_get_color(&dsc->local, prop, &value_act);
/*On perfect match return the value immediately*/
if(weight_act == weight_goal) {
*value = value_act;
return LV_RES_OK;
}
/*If the found ID is better the current candidate then use it*/
else if(weight_act > weight) {
weight = weight_act;
*value = value_act;
}
int16_t ci;
for(ci = dsc->class_cnt - 1; ci >= 0; ci--) {
lv_style_t * class = lv_style_dsc_get_class(dsc, ci);
weight_act = lv_style_get_color(class, prop, &value_act);
/*On perfect match return the value immediately*/
if(weight_act == weight_goal) {
*value = value_act;
return LV_RES_OK;
}
/*If the found ID is better the current candidate then use it*/
else if(weight_act > weight) {
weight = weight_act;
*value = value_act;
}
}
if(weight >= 0) {
prop_fooled[prop&0xFF]++;
return LV_RES_OK;
}
else return LV_RES_INV;
}
lv_res_t lv_style_dsc_get_opa(lv_style_dsc_t * dsc, lv_style_property_t prop, lv_opa_t * value)
{
if(dsc == NULL) return LV_RES_INV;
lv_res_t res = LV_RES_OK;
if(dsc->cache.enabled) {
switch(prop & (~LV_STYLE_STATE_MASK)) {
case LV_STYLE_OPA_SCALE:
res = dsc->cache.opa_scale;
break;
case LV_STYLE_BG_OPA:
res = dsc->cache.bg_opa;
break;
case LV_STYLE_BORDER_OPA:
res = dsc->cache.border_opa;
break;
case LV_STYLE_IMAGE_OPA:
res = dsc->cache.image_opa;
break;
case LV_STYLE_IMAGE_RECOLOR:
res = dsc->cache.image_recolor_opa;
break;
case LV_STYLE_TEXT_OPA:
res = dsc->cache.text_opa;
break;
case LV_STYLE_LINE_OPA:
res = dsc->cache.line_opa;
break;
case LV_STYLE_SHADOW_OPA:
res = dsc->cache.shadow_opa;
break;
case LV_STYLE_OVERLAY_OPA:
res = dsc->cache.overlay_opa;
break;
case LV_STYLE_PATTERN_OPA:
res = dsc->cache.pattern_opa;
break;
}
}
if(res == LV_RES_INV) return LV_RES_INV;
lv_style_attr_t attr;
attr.full = prop >> 8;
int16_t weight_goal = attr.full;
int16_t weight_act;
int16_t weight = -1;
lv_opa_t value_act;
weight_act = lv_style_get_opa(&dsc->local, prop, &value_act);
/*On perfect match return the value immediately*/
if(weight_act == weight_goal) {
*value = value_act;
return LV_RES_OK;
}
/*If the found ID is better the current candidate then use it*/
else if(weight_act > weight) {
weight = weight_act;
*value = value_act;
}
int16_t ci;
for(ci = dsc->class_cnt - 1; ci >= 0; ci--) {
lv_style_t * class = lv_style_dsc_get_class(dsc, ci);
weight_act = lv_style_get_opa(class, prop, &value_act);
/*On perfect match return the value immediately*/
if(weight_act == weight_goal) {
*value = value_act;
return LV_RES_OK;
}
/*If the found ID is better the current candidate then use it*/
else if(weight_act > weight) {
weight = weight_act;
*value = value_act;
}
}
if(weight >= 0) {
prop_fooled[prop&0xFF]++;
return LV_RES_OK;
}
else return LV_RES_INV;
}
lv_res_t lv_style_dsc_get_ptr(lv_style_dsc_t * dsc, lv_style_property_t prop, void ** value)
{
if(dsc == NULL) return LV_RES_INV;
lv_res_t res = LV_RES_OK;
if(dsc->cache.enabled) {
switch(prop & (~LV_STYLE_STATE_MASK)) {
case LV_STYLE_PATTERN_IMAGE:
res = dsc->cache.pattern_image;
break;
case LV_STYLE_FONT:
res = dsc->cache.font;
break;
}
}
if(res == LV_RES_INV) return LV_RES_INV;
lv_style_attr_t attr;
attr.full = prop >> 8;
int16_t weight_goal = attr.full;
int16_t weight_act;
int16_t weight = -1;
void * value_act;
weight_act = lv_style_get_ptr(&dsc->local, prop, &value_act);
/*On perfect match return the value immediately*/
if(weight_act == weight_goal) {
*value = value_act;
return LV_RES_OK;
}
/*If the found ID is better the current candidate then use it*/
else if(weight_act > weight) {
weight = weight_act;
*value = value_act;
}
int16_t ci;
for(ci = dsc->class_cnt - 1; ci >= 0; ci--) {
lv_style_t * class = lv_style_dsc_get_class(dsc, ci);
weight_act = lv_style_get_ptr(class, prop, &value_act);
/*On perfect match return the value immediately*/
if(weight_act == weight_goal) {
*value = value_act;
return LV_RES_OK;
}
/*If the found ID is better the current candidate then use it*/
else if(weight_act > weight) {
weight = weight_act;
*value = value_act;
}
}
if(weight >= 0) {
prop_fooled[prop&0xFF]++;
return LV_RES_OK;
}
else return LV_RES_INV;
}
lv_res_t lv_style_cache_update(lv_style_dsc_t * dsc)
{
if(dsc == NULL) return LV_RES_INV;
if(!dsc->cache.enabled) return LV_RES_OK;
dsc->cache.enabled = 0;
lv_style_int_t value;
lv_opa_t opa;
void * ptr;
lv_color_t color;
dsc->cache.bg_blend_mode = lv_style_dsc_get_int(dsc, LV_STYLE_BG_BLEND_MODE | LV_STYLE_STATE_ALL, &value) & 0x1U;
dsc->cache.border_blend_mode = lv_style_dsc_get_int(dsc, LV_STYLE_BORDER_BLEND_MODE | LV_STYLE_STATE_ALL, &value) & 0x1U;
dsc->cache.image_blend_mode = lv_style_dsc_get_int(dsc, LV_STYLE_IMAGE_BLEND_MODE | LV_STYLE_STATE_ALL, &value) & 0x1U;
dsc->cache.text_blend_mode = lv_style_dsc_get_int(dsc, LV_STYLE_TEXT_BLEND_MODE | LV_STYLE_STATE_ALL, &value) & 0x1U;
dsc->cache.line_blend_mode = lv_style_dsc_get_int(dsc, LV_STYLE_LINE_BLEND_MODE | LV_STYLE_STATE_ALL, &value) & 0x1U;
dsc->cache.shadow_blend_mode = lv_style_dsc_get_int(dsc, LV_STYLE_SHADOW_BLEND_MODE | LV_STYLE_STATE_ALL, &value) & 0x1U;
dsc->cache.pattern_blend_mode = lv_style_dsc_get_int(dsc, LV_STYLE_PATTERN_BLEND_MODE | LV_STYLE_STATE_ALL, &value) & 0x1U;
dsc->cache.clip_corner = lv_style_dsc_get_int(dsc, LV_STYLE_PATTERN_BLEND_MODE | LV_STYLE_STATE_ALL, &value) & 0x1U;
dsc->cache.letter_space = lv_style_dsc_get_int(dsc, LV_STYLE_LETTER_SPACE | LV_STYLE_STATE_ALL, &value) & 0x1U;
dsc->cache.line_space = lv_style_dsc_get_int(dsc, LV_STYLE_LINE_SPACE | LV_STYLE_STATE_ALL, &value) & 0x1U;
dsc->cache.border_part = lv_style_dsc_get_int(dsc, LV_STYLE_BORDER_PART | LV_STYLE_STATE_ALL, &value) & 0x1U;
dsc->cache.border_width = lv_style_dsc_get_int(dsc, LV_STYLE_BORDER_WIDTH | LV_STYLE_STATE_ALL, &value) & 0x1U;
dsc->cache.shadow_width = lv_style_dsc_get_int(dsc, LV_STYLE_SHADOW_WIDTH | LV_STYLE_STATE_ALL, &value) & 0x1U;
dsc->cache.opa_scale = lv_style_dsc_get_opa(dsc, LV_STYLE_OPA_SCALE | LV_STYLE_STATE_ALL, &opa) & 0x1U;
dsc->cache.bg_opa = lv_style_dsc_get_opa(dsc, LV_STYLE_BG_OPA | LV_STYLE_STATE_ALL, &opa) & 0x1U;
dsc->cache.border_opa = lv_style_dsc_get_opa(dsc, LV_STYLE_BORDER_OPA | LV_STYLE_STATE_ALL, &opa) & 0x1U;
dsc->cache.image_opa = lv_style_dsc_get_opa(dsc, LV_STYLE_IMAGE_OPA | LV_STYLE_STATE_ALL, &opa) & 0x1U;
dsc->cache.image_recolor_opa = lv_style_dsc_get_opa(dsc, LV_STYLE_IMAGE_RECOLOR_OPA | LV_STYLE_STATE_ALL, &opa) & 0x1U;
dsc->cache.text_opa = lv_style_dsc_get_opa(dsc, LV_STYLE_TEXT_OPA | LV_STYLE_STATE_ALL, &opa) & 0x1U;
dsc->cache.line_opa = lv_style_dsc_get_opa(dsc, LV_STYLE_LINE_OPA | LV_STYLE_STATE_ALL, &opa) & 0x1U;
dsc->cache.shadow_opa = lv_style_dsc_get_opa(dsc, LV_STYLE_SHADOW_OPA | LV_STYLE_STATE_ALL, &opa) & 0x1U;
dsc->cache.overlay_opa = lv_style_dsc_get_opa(dsc, LV_STYLE_OVERLAY_OPA | LV_STYLE_STATE_ALL, &opa) & 0x1U;
dsc->cache.pattern_opa = lv_style_dsc_get_opa(dsc, LV_STYLE_PATTERN_OPA | LV_STYLE_STATE_ALL, &opa) & 0x1U;
dsc->cache.text_color = lv_style_dsc_get_color(dsc, LV_STYLE_TEXT_COLOR | LV_STYLE_STATE_ALL, &color) & 0x1U;
dsc->cache.font = lv_style_dsc_get_ptr(dsc, LV_STYLE_FONT | LV_STYLE_STATE_ALL, &ptr) & 0x1U;
dsc->cache.pattern_image = lv_style_dsc_get_ptr(dsc, LV_STYLE_PATTERN_IMAGE | LV_STYLE_STATE_ALL, &ptr) & 0x1U;
dsc->cache.enabled = 1;
return LV_RES_OK;
}
2019-12-14 23:39:26 +01:00
#if LV_USE_ANIMATION
void lv_style_anim_init(lv_anim_t * a)
2017-07-28 13:57:56 +02:00
{
lv_anim_init(a);
2019-06-06 06:05:40 +02:00
a->start = 0;
a->end = STYLE_MIX_MAX;
2019-06-12 23:10:54 +02:00
a->exec_cb = (lv_anim_exec_xcb_t)style_animator;
2019-06-06 06:05:40 +02:00
a->path_cb = lv_anim_path_linear;
a->ready_cb = style_animation_common_end_cb;
lv_style_anim_dsc_t * dsc;
2019-12-31 22:13:32 +01:00
2019-06-06 06:05:40 +02:00
dsc = lv_mem_alloc(sizeof(lv_style_anim_dsc_t));
2019-09-24 23:14:17 +02:00
LV_ASSERT_MEM(dsc);
2019-06-06 06:05:40 +02:00
if(dsc == NULL) return;
dsc->ready_cb = NULL;
dsc->style_anim = NULL;
2019-12-31 22:13:32 +01:00
lv_style_init(&dsc->style_start);
lv_style_init(&dsc->style_end);
2019-06-06 06:05:40 +02:00
a->var = (void *)dsc;
2017-07-28 13:57:56 +02:00
}
void lv_style_anim_set_styles(lv_anim_t * a, lv_style_t * to_anim, const lv_style_t * start, const lv_style_t * end)
{
lv_style_anim_dsc_t * dsc = a->var;
2019-06-06 06:05:40 +02:00
dsc->style_anim = to_anim;
2019-12-31 22:13:32 +01:00
lv_style_copy(&dsc->style_start, start);
lv_style_copy(&dsc->style_end, end);
}
2017-11-27 17:48:54 +01:00
#endif
/**********************
* STATIC FUNCTIONS
**********************/
2019-12-14 23:39:26 +01:00
2020-01-08 21:31:05 +01:00
static uint32_t cnt = 0;
static uint32_t stat[256];
2019-12-14 23:39:26 +01:00
static inline int32_t get_property_index(const lv_style_t * style, lv_style_property_t prop)
{
uint8_t id_to_find = prop & 0xFF;
2019-12-19 11:05:04 +01:00
lv_style_attr_t attr;
attr.full = (prop >> 8) & 0xFF;
int16_t weight = -1;
int16_t id_guess = -1;
2019-12-14 23:39:26 +01:00
2020-01-08 21:31:05 +01:00
if(id_to_find == (LV_STYLE_RADIUS & 0xFF)) {
volatile uint8_t i = 0;
}
cnt++;
if(cnt > 100000) {
cnt = 0;
uint32_t i;
printf("\nQuerry:\n");
for(i = 0; i < 256; i++) {
if(stat[i]) printf("%02x: %d\n", i, stat[i]);
}
memset(stat, 0x00, sizeof(stat));
printf("\nFooled:\n");
for(i = 0; i < 256; i++) {
if(prop_fooled[i]) printf("%02x: %d\n", i, prop_fooled[i]);
}
memset(prop_fooled, 0x00, sizeof(stat));
printf("\n");
}
2019-12-14 23:39:26 +01:00
size_t i = 0;
while(i < style->size) {
2020-01-08 21:31:05 +01:00
stat[id_to_find]++;
2019-12-19 11:05:04 +01:00
lv_style_attr_t attr_act;
attr_act.full = style->map[i + 1];
2019-12-14 23:39:26 +01:00
if(style->map[i] == id_to_find) {
/*If the state perfectly matches return this property*/
2019-12-19 11:05:04 +01:00
if(attr_act.bits.state == attr.bits.state) {
return i;
}
/* Be sure the property not specifies other state than the requested.
2019-12-19 11:05:04 +01:00
* E.g. For HOVER+PRESS, HOVER only is OK, but HOVER+FOCUS not*/
else if((attr_act.bits.state & (~attr.bits.state)) == 0) {
/* Use this property if it describes better the requested state than the current candidate.
* E.g. for HOVER+FOCUS+PRESS prefer HOVER+FOCUS over FOCUS*/
if(attr_act.bits.state > weight) {
weight = attr_act.bits.state;
id_guess = i;
}
2019-12-14 23:39:26 +01:00
}
}
2019-12-19 11:05:04 +01:00
/*Go to the next property*/
2020-01-06 22:14:04 +01:00
if((style->map[i] & 0xF) < LV_STYLE_ID_COLOR) i+= sizeof(lv_style_int_t);
2019-12-19 11:05:04 +01:00
else if((style->map[i] & 0xF) < LV_STYLE_ID_OPA) i+= sizeof(lv_color_t);
else if((style->map[i] & 0xF) < LV_STYLE_ID_PTR) i+= sizeof(lv_opa_t);
else i+= sizeof(void*);
i += sizeof(lv_style_property_t);
2019-12-14 23:39:26 +01:00
}
2019-12-19 11:05:04 +01:00
return id_guess;
2019-12-14 23:39:26 +01:00
}
#if LV_USE_ANIMATION
2019-12-31 22:13:32 +01:00
2017-07-28 13:57:56 +02:00
/**
* 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_ANIM_RESOLUTION
2017-07-28 13:57:56 +02:00
*/
static void style_animator(lv_style_anim_dsc_t * dsc, lv_anim_value_t val)
2017-07-28 13:57:56 +02:00
{
2017-10-30 17:11:56 +01:00
const lv_style_t * start = &dsc->style_start;
2019-04-04 07:15:40 +02:00
const lv_style_t * end = &dsc->style_end;
lv_style_t * act = dsc->style_anim;
2017-07-28 13:57:56 +02:00
2019-12-31 22:13:32 +01:00
size_t i = 0;
lv_style_property_t prop_act;
while(i < start->size) {
prop_act = start->map[i] + (start->map[i + 1] << 8);
/*Value*/
if((start->map[i] & 0xF) < LV_STYLE_ID_COLOR) {
2020-01-06 22:14:04 +01:00
lv_style_int_t v1;
memcpy(&v1, &start->map[i + sizeof(lv_style_property_t)], sizeof(lv_style_int_t));
2019-12-31 22:13:32 +01:00
int16_t res2;
2020-01-06 22:14:04 +01:00
lv_style_int_t v2;
2020-01-07 00:49:47 +01:00
res2 = lv_style_get_int(end, prop_act, &v2);
2019-12-31 22:13:32 +01:00
if(res2 >= 0) {
2020-01-06 22:14:04 +01:00
lv_style_int_t vres = v1 + ((int32_t)((int32_t)(v2-v1) * val) >> 8);
2020-01-07 00:49:47 +01:00
lv_style_set_int(act, prop_act, vres);
2019-12-31 22:13:32 +01:00
}
2020-01-06 22:14:04 +01:00
i+= sizeof(lv_style_int_t);
2019-12-31 22:13:32 +01:00
}
/*Color*/
else if((start->map[i] & 0xF) < LV_STYLE_ID_OPA) {
lv_color_t color1;
memcpy(&color1, &start->map[i + sizeof(lv_style_property_t)], sizeof(lv_color_t));
int16_t res2;
lv_color_t color2;
res2 = lv_style_get_color(end, prop_act, &color2);
if(res2 >= 0) {
lv_color_t color_res = val == 256 ? color2 : lv_color_mix(color2, color1, (lv_opa_t)val);
lv_style_set_color(act, prop_act, color_res);
}
i+= sizeof(lv_color_t);
}
/*Opa*/
else if((start->map[i] & 0xF) < LV_STYLE_ID_PTR) {
lv_opa_t opa1;
memcpy(&opa1, &start->map[i + sizeof(lv_style_property_t)], sizeof(lv_opa_t));
int16_t res2;
lv_opa_t opa2;
res2 = lv_style_get_opa(end, prop_act, &opa2);
if(res2 >= 0) {
lv_opa_t opa_res = opa1 + ((uint16_t)((uint16_t)(opa2 - opa1) * val) >> 8);
lv_style_set_opa(act, prop_act, opa_res);
}
i+= sizeof(lv_opa_t);
}
else {
void * p1;
memcpy(p1, &start->map[i + sizeof(lv_style_property_t)], sizeof(void *));
int16_t res2;
void * p2;
res2 = lv_style_get_ptr(end, prop_act, &p2);
if(res2 >= 0) {
if(val > 128) lv_style_set_ptr(act, prop_act, p2);
else if(val > 128) lv_style_set_ptr(act, prop_act, p1);
}
i+= sizeof(void*);
}
i += sizeof(lv_style_property_t);
}
2017-07-28 13:57:56 +02:00
2017-11-19 20:45:40 +01:00
lv_obj_report_style_mod(dsc->style_anim);
}
2017-10-30 17:11:56 +01:00
/**
* Called when a style animation is ready
* It called the user defined call back and free the allocated memories
2019-04-22 08:45:07 +02:00
* @param a pointer to the animation
*/
2019-04-22 08:45:07 +02:00
static void style_animation_common_end_cb(lv_anim_t * a)
{
2019-06-06 06:05:40 +02:00
(void)a; /*Unused*/
2019-04-22 08:45:07 +02:00
lv_style_anim_dsc_t * dsc = a->var; /*To avoid casting*/
if(dsc->ready_cb) dsc->ready_cb(a);
lv_mem_free(dsc);
2017-07-28 13:57:56 +02:00
}
2017-11-27 17:48:54 +01:00
#endif