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

354 lines
11 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
/**********************
* STATIC VARIABLES
**********************/
2019-12-14 23:39:26 +01:00
//lv_style_t lv_style_scr;
//lv_style_t lv_style_transp;
//lv_style_t lv_style_transp_fit;
//lv_style_t lv_style_transp_tight;
//lv_style_t lv_style_plain;
//lv_style_t lv_style_plain_color;
//lv_style_t lv_style_pretty;
//lv_style_t lv_style_pretty_color;
//lv_style_t lv_style_btn_rel;
//lv_style_t lv_style_btn_pr;
//lv_style_t lv_style_btn_tgl_rel;
//lv_style_t lv_style_btn_tgl_pr;
//lv_style_t lv_style_btn_ina;
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/**
2019-12-14 23:39:26 +01:00
* Init. the built-in styles
*/
2019-12-14 23:39:26 +01:00
void lv_style_built_in_init(void)
{
2019-09-27 03:28:44 +02:00
2019-12-14 23:39:26 +01:00
}
void lv_style_init(lv_style_t * style)
{
style->map = NULL;
style->size = 0;
style->used_groups = 0;
}
void lv_style_dsc_init(lv_style_dsc_t * style_dsc)
{
lv_style_init(&style_dsc->local);
style_dsc->classes = NULL;
}
2017-05-08 10:09:41 +02:00
/**
* Copy a style to an other
* @param dest pointer to the destination style
* @param src pointer to the source style
*/
void lv_style_copy(lv_style_t * dest, const lv_style_t * src)
2017-05-08 10:09:41 +02:00
{
memcpy(dest, src, sizeof(lv_style_t));
}
2019-12-14 23:39:26 +01:00
void lv_style_set_value(lv_style_t * style, lv_style_property_t prop, lv_style_value_t value)
{
int32_t id = get_property_index(style, prop);
/*If exists update the property*/
if(id >= 0) {
memcpy(style->map + id + sizeof(lv_style_property_t), &value, sizeof(lv_style_value_t));
}
/*Add new property if not exists yet*/
else {
style->size += sizeof(lv_style_property_t) + sizeof(lv_style_value_t);
style->map = lv_mem_realloc(style->map, style->size);
LV_ASSERT_MEM(style->map);
if(style->map == NULL) return;
memcpy(style->map + style->size - (sizeof(lv_style_property_t) + sizeof(lv_style_value_t)), &prop, sizeof(lv_style_property_t));
memcpy(style->map + style->size - sizeof(lv_style_value_t), &value, sizeof(lv_style_value_t));
}
}
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);
/*If exists update the property*/
if(id >= 0) {
memcpy(style->map + id + sizeof(lv_style_property_t), &color, sizeof(lv_color_t));
}
/*Add new property if not exists yet*/
else {
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));
}
}
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);
/*If exists update the property*/
if(id >= 0) {
memcpy(style->map + id + sizeof(lv_style_property_t), &opa, sizeof(lv_opa_t));
}
/*Add new property if not exists yet*/
else {
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;
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));
}
}
lv_res_t lv_style_get_value(const lv_style_t * style, lv_style_property_t prop, lv_style_value_t * res)
{
int32_t id = get_property_index(style, prop);
if(id < 0) {
res = 0;
return LV_RES_INV;
} else {
memcpy(res, &style->map[id + sizeof(lv_style_property_t)], sizeof(lv_style_value_t));
return LV_RES_OK;
}
}
lv_res_t lv_style_get_opa(const lv_style_t * style, lv_style_property_t prop, lv_opa_t * res)
{
int32_t id = get_property_index(style, prop);
if(id < 0) {
res = 0;
return LV_RES_INV;
} else {
memcpy(res, &style->map[id + sizeof(lv_style_property_t)], sizeof(lv_opa_t));
return LV_RES_OK;
}
}
lv_res_t lv_style_get_color(const lv_style_t * style, lv_style_property_t prop, lv_color_t * res)
{
int32_t id = get_property_index(style, prop);
if(id < 0) {
res = 0;
return LV_RES_INV;
} else {
memcpy(res, &style->map[id + sizeof(lv_style_property_t)], sizeof(lv_color_t));
return LV_RES_OK;
}
}
2018-08-26 13:49:23 +02:00
/**
* Mix two styles according to a given ratio
2018-10-05 17:22:49 +02:00
* @param start start style
2018-08-26 13:49:23 +02:00
* @param end end style
* @param res store the result style here
* @param ratio the ratio of mix [0..256]; 0: `start` style; 256: `end` style
*/
2019-06-06 06:05:40 +02:00
void lv_style_mix(const lv_style_t * start, const lv_style_t * end, lv_style_t * res, uint16_t ratio)
2018-08-26 13:49:23 +02:00
{
2019-12-14 23:39:26 +01:00
// STYLE_ATTR_MIX(body.opa, ratio);
// STYLE_ATTR_MIX(body.radius, ratio);
// STYLE_ATTR_MIX(body.border.width, ratio);
// STYLE_ATTR_MIX(body.border.opa, ratio);
// STYLE_ATTR_MIX(body.shadow.width, ratio);
// STYLE_ATTR_MIX(body.shadow.offset.x, ratio);
// STYLE_ATTR_MIX(body.shadow.offset.y, ratio);
// STYLE_ATTR_MIX(body.shadow.spread, ratio);
// STYLE_ATTR_MIX(body.padding.left, ratio);
// STYLE_ATTR_MIX(body.padding.right, ratio);
// STYLE_ATTR_MIX(body.padding.top, ratio);
// STYLE_ATTR_MIX(body.padding.bottom, ratio);
// STYLE_ATTR_MIX(body.padding.inner, ratio);
// STYLE_ATTR_MIX(text.line_space, ratio);
// STYLE_ATTR_MIX(text.letter_space, ratio);
// STYLE_ATTR_MIX(text.opa, ratio);
// STYLE_ATTR_MIX(line.width, ratio);
// STYLE_ATTR_MIX(line.opa, ratio);
// STYLE_ATTR_MIX(image.intense, ratio);
// STYLE_ATTR_MIX(image.opa, ratio);
//
// lv_opa_t opa = ratio == STYLE_MIX_MAX ? LV_OPA_COVER : ratio;
//
// res->body.main_color = lv_color_mix(end->body.main_color, start->body.main_color, opa);
// res->body.grad_color = lv_color_mix(end->body.grad_color, start->body.grad_color, opa);
// res->body.border.color = lv_color_mix(end->body.border.color, start->body.border.color, opa);
// res->body.shadow.color = lv_color_mix(end->body.shadow.color, start->body.shadow.color, opa);
// res->text.color = lv_color_mix(end->text.color, start->text.color, opa);
// res->image.color = lv_color_mix(end->image.color, start->image.color, opa);
// res->line.color = lv_color_mix(end->line.color, start->line.color, opa);
//
// if(ratio < (STYLE_MIX_MAX >> 1)) {
// res->body.border.part = start->body.border.part;
// res->glass = start->glass;
// res->text.font = start->text.font;
// res->line.rounded = start->line.rounded;
// } else {
// res->body.border.part = end->body.border.part;
// res->glass = end->glass;
// res->text.font = end->text.font;
// res->line.rounded = end->line.rounded;
// }
2018-08-26 13:49:23 +02: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;
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;
lv_style_copy(&dsc->style_start, &lv_style_plain);
lv_style_copy(&dsc->style_end, &lv_style_plain);
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;
memcpy(&dsc->style_start, start, sizeof(lv_style_t));
memcpy(&dsc->style_end, end, sizeof(lv_style_t));
memcpy(dsc->style_anim, start, sizeof(lv_style_t));
}
2017-11-27 17:48:54 +01:00
#endif
/**********************
* STATIC FUNCTIONS
**********************/
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;
size_t i = 0;
while(i < style->size) {
if(style->map[i] == id_to_find) {
return i;
} else {
lv_style_attr_t attr;
attr.full = style->map[i + 1];
switch(attr.bits.type) {
case LV_STYLE_ATTR_TYPE_VALUE:
i+= sizeof(lv_style_value_t);
break;
case LV_STYLE_ATTR_TYPE_OPA:
i+= sizeof(lv_opa_t);
break;
case LV_STYLE_ATTR_TYPE_COLOR:
i+= sizeof(lv_color_t);
break;
case LV_STYLE_ATTR_TYPE_PTR:
i+= sizeof(void*);
break;
}
i += sizeof(lv_style_property_t);
}
}
return -1;
}
#if LV_USE_ANIMATION
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
2018-08-26 13:49:23 +02:00
lv_style_mix(start, end, act, val);
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