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
14 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
}
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
#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
**********************/
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;
2017-11-20 14:26:18 +01:00
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
**********************/
/**
* Init the basic styles
*/
2018-06-19 09:49:58 +02:00
void lv_style_init(void)
{
2017-04-21 09:15:39 +02:00
/* Not White/Black/Gray colors are created by HSV model with
* HUE = 210*/
/*Screen style*/
2019-04-04 07:15:40 +02:00
lv_style_scr.glass = 0;
lv_style_scr.body.opa = LV_OPA_COVER;
lv_style_scr.body.main_color = LV_COLOR_WHITE;
lv_style_scr.body.grad_color = LV_COLOR_WHITE;
lv_style_scr.body.radius = 0;
lv_style_scr.body.padding.left = 0;
lv_style_scr.body.padding.right = 0;
lv_style_scr.body.padding.top = 0;
2019-03-14 19:37:09 +01:00
lv_style_scr.body.padding.bottom = 0;
2019-06-26 15:45:01 +02:00
lv_style_scr.body.padding.inner = LV_DPI / 20;
2017-10-18 16:07:19 +02:00
2017-11-23 21:28:36 +01:00
lv_style_scr.body.border.color = LV_COLOR_BLACK;
2019-04-04 07:15:40 +02:00
lv_style_scr.body.border.opa = LV_OPA_COVER;
lv_style_scr.body.border.width = 0;
2019-04-04 07:15:40 +02:00
lv_style_scr.body.border.part = LV_BORDER_FULL;
2017-10-18 16:07:19 +02:00
2017-11-23 21:28:36 +01:00
lv_style_scr.body.shadow.color = LV_COLOR_GRAY;
2019-04-04 07:15:40 +02:00
lv_style_scr.body.shadow.type = LV_SHADOW_FULL;
lv_style_scr.body.shadow.width = 0;
LV_FONT_DECLARE(lv_font_heb_16);
2019-04-04 07:15:40 +02:00
lv_style_scr.text.opa = LV_OPA_COVER;
lv_style_scr.text.color = lv_color_make(0x30, 0x30, 0x30);
lv_style_scr.text.sel_color = lv_color_make(0x55, 0x96, 0xd8);
lv_style_scr.text.font = &lv_font_heb_16;//LV_FONT_DEFAULT;
2019-06-05 12:25:49 +02:00
lv_style_scr.text.letter_space = 0;
2019-04-04 07:15:40 +02:00
lv_style_scr.text.line_space = 2;
2017-10-18 16:07:19 +02:00
2019-04-04 07:15:40 +02:00
lv_style_scr.image.opa = LV_OPA_COVER;
lv_style_scr.image.color = lv_color_make(0x20, 0x20, 0x20);
2017-11-23 21:28:36 +01:00
lv_style_scr.image.intense = LV_OPA_TRANSP;
2017-10-18 16:07:19 +02:00
2019-04-04 07:15:40 +02:00
lv_style_scr.line.opa = LV_OPA_COVER;
lv_style_scr.line.color = lv_color_make(0x20, 0x20, 0x20);
lv_style_scr.line.width = 2;
2018-09-13 00:57:20 +02:00
lv_style_scr.line.rounded = 0;
2019-09-27 03:28:44 +02:00
#if LV_USE_DEBUG
#if LV_USE_ASSERT_STYLE
lv_style_scr.debug_sentinel = LV_STYLE_DEGUG_SENTINEL_VALUE;
#endif
#endif
2017-04-24 16:16:36 +02:00
/*Plain style (by default near the same as the screen style)*/
2019-04-12 07:27:53 +02:00
lv_style_copy(&lv_style_plain, &lv_style_scr);
2019-06-26 15:45:01 +02:00
lv_style_plain.body.padding.left = LV_DPI / 20;
lv_style_plain.body.padding.right = LV_DPI / 20;
lv_style_plain.body.padding.top = LV_DPI / 20;
lv_style_plain.body.padding.bottom = LV_DPI / 20;
/*Plain color style*/
2019-04-12 07:27:53 +02:00
lv_style_copy(&lv_style_plain_color, &lv_style_plain);
2019-04-04 07:15:40 +02:00
lv_style_plain_color.text.color = lv_color_make(0xf0, 0xf0, 0xf0);
lv_style_plain_color.image.color = lv_color_make(0xf0, 0xf0, 0xf0);
lv_style_plain_color.line.color = lv_color_make(0xf0, 0xf0, 0xf0);
2019-03-17 07:44:48 +01:00
lv_style_plain_color.body.main_color = lv_color_make(0x55, 0x96, 0xd8);
2017-11-20 14:26:18 +01:00
lv_style_plain_color.body.grad_color = lv_style_plain_color.body.main_color;
/*Pretty style */
2019-04-12 07:27:53 +02:00
lv_style_copy(&lv_style_pretty, &lv_style_plain);
2019-04-04 07:15:40 +02:00
lv_style_pretty.text.color = lv_color_make(0x20, 0x20, 0x20);
lv_style_pretty.image.color = lv_color_make(0x20, 0x20, 0x20);
lv_style_pretty.line.color = lv_color_make(0x20, 0x20, 0x20);
lv_style_pretty.body.main_color = LV_COLOR_WHITE;
lv_style_pretty.body.grad_color = LV_COLOR_SILVER;
lv_style_pretty.body.radius = LV_DPI / 15;
2019-03-17 07:44:48 +01:00
lv_style_pretty.body.border.color = lv_color_make(0x40, 0x40, 0x40);
2019-04-04 07:15:40 +02:00
lv_style_pretty.body.border.width = LV_DPI / 50 >= 1 ? LV_DPI / 50 : 1;
lv_style_pretty.body.border.opa = LV_OPA_30;
/*Pretty color style*/
2019-04-12 07:27:53 +02:00
lv_style_copy(&lv_style_pretty_color, &lv_style_pretty);
2019-04-04 07:15:40 +02:00
lv_style_pretty_color.text.color = lv_color_make(0xe0, 0xe0, 0xe0);
lv_style_pretty_color.image.color = lv_color_make(0xe0, 0xe0, 0xe0);
lv_style_pretty_color.line.color = lv_color_make(0xc0, 0xc0, 0xc0);
lv_style_pretty_color.body.main_color = lv_color_make(0x6b, 0x9a, 0xc7);
lv_style_pretty_color.body.grad_color = lv_color_make(0x2b, 0x59, 0x8b);
2019-03-17 07:44:48 +01:00
lv_style_pretty_color.body.border.color = lv_color_make(0x15, 0x2c, 0x42);
/*Transparent style*/
2019-04-12 07:27:53 +02:00
lv_style_copy(&lv_style_transp, &lv_style_plain);
2019-04-04 07:15:40 +02:00
lv_style_transp.glass = 1;
lv_style_transp.body.border.width = 0;
2019-04-04 07:15:40 +02:00
lv_style_transp.body.opa = LV_OPA_TRANSP;
2018-07-22 11:12:47 +08:00
/*Transparent fitting size*/
2019-04-12 07:27:53 +02:00
lv_style_copy(&lv_style_transp_fit, &lv_style_transp);
2019-04-04 07:15:40 +02:00
lv_style_transp_fit.body.padding.left = 0;
lv_style_transp_fit.body.padding.right = 0;
lv_style_transp_fit.body.padding.top = 0;
2019-03-14 19:37:09 +01:00
lv_style_transp_fit.body.padding.bottom = 0;
2018-07-22 11:12:47 +08:00
/*Transparent tight style*/
2019-04-12 07:27:53 +02:00
lv_style_copy(&lv_style_transp_tight, &lv_style_transp_fit);
lv_style_transp_tight.body.padding.inner = 0;
/*Button released style*/
2019-04-12 07:27:53 +02:00
lv_style_copy(&lv_style_btn_rel, &lv_style_plain);
2019-04-04 07:15:40 +02:00
lv_style_btn_rel.body.main_color = lv_color_make(0x76, 0xa2, 0xd0);
lv_style_btn_rel.body.grad_color = lv_color_make(0x19, 0x3a, 0x5d);
lv_style_btn_rel.body.radius = LV_DPI / 15;
lv_style_btn_rel.body.padding.left = LV_DPI / 4;
lv_style_btn_rel.body.padding.right = LV_DPI / 4;
lv_style_btn_rel.body.padding.top = LV_DPI / 6;
2019-03-14 19:37:09 +01:00
lv_style_btn_rel.body.padding.bottom = LV_DPI / 6;
2019-04-04 07:15:40 +02:00
lv_style_btn_rel.body.padding.inner = LV_DPI / 10;
lv_style_btn_rel.body.border.color = lv_color_make(0x0b, 0x19, 0x28);
lv_style_btn_rel.body.border.width = LV_DPI / 50 >= 1 ? LV_DPI / 50 : 1;
lv_style_btn_rel.body.border.opa = LV_OPA_70;
lv_style_btn_rel.body.shadow.color = LV_COLOR_GRAY;
lv_style_btn_rel.body.shadow.width = 0;
lv_style_btn_rel.text.color = lv_color_make(0xff, 0xff, 0xff);
lv_style_btn_rel.image.color = lv_color_make(0xff, 0xff, 0xff);
/*Button pressed style*/
2019-04-12 07:27:53 +02:00
lv_style_copy(&lv_style_btn_pr, &lv_style_btn_rel);
2019-03-17 07:44:48 +01:00
lv_style_btn_pr.body.main_color = lv_color_make(0x33, 0x62, 0x94);
lv_style_btn_pr.body.grad_color = lv_color_make(0x10, 0x26, 0x3c);
2019-04-04 07:15:40 +02:00
lv_style_btn_pr.text.color = lv_color_make(0xa4, 0xb5, 0xc6);
lv_style_btn_pr.image.color = lv_color_make(0xa4, 0xb5, 0xc6);
lv_style_btn_pr.line.color = lv_color_make(0xa4, 0xb5, 0xc6);
/*Button toggle released style*/
2019-04-12 07:27:53 +02:00
lv_style_copy(&lv_style_btn_tgl_rel, &lv_style_btn_rel);
2019-04-04 07:15:40 +02:00
lv_style_btn_tgl_rel.body.main_color = lv_color_make(0x0a, 0x11, 0x22);
lv_style_btn_tgl_rel.body.grad_color = lv_color_make(0x37, 0x62, 0x90);
2019-03-17 07:44:48 +01:00
lv_style_btn_tgl_rel.body.border.color = lv_color_make(0x01, 0x07, 0x0d);
2019-04-04 07:15:40 +02:00
lv_style_btn_tgl_rel.text.color = lv_color_make(0xc8, 0xdd, 0xf4);
lv_style_btn_tgl_rel.image.color = lv_color_make(0xc8, 0xdd, 0xf4);
lv_style_btn_tgl_rel.line.color = lv_color_make(0xc8, 0xdd, 0xf4);
/*Button toggle pressed style*/
2019-04-12 07:27:53 +02:00
lv_style_copy(&lv_style_btn_tgl_pr, &lv_style_btn_tgl_rel);
2019-03-17 07:44:48 +01:00
lv_style_btn_tgl_pr.body.main_color = lv_color_make(0x02, 0x14, 0x27);
lv_style_btn_tgl_pr.body.grad_color = lv_color_make(0x2b, 0x4c, 0x70);
2019-04-04 07:15:40 +02:00
lv_style_btn_tgl_pr.text.color = lv_color_make(0xa4, 0xb5, 0xc6);
lv_style_btn_tgl_pr.image.color = lv_color_make(0xa4, 0xb5, 0xc6);
lv_style_btn_tgl_pr.line.color = lv_color_make(0xa4, 0xb5, 0xc6);
/*Button inactive style*/
2019-04-12 07:27:53 +02:00
lv_style_copy(&lv_style_btn_ina, &lv_style_btn_rel);
2019-04-04 07:15:40 +02:00
lv_style_btn_ina.body.main_color = lv_color_make(0xd8, 0xd8, 0xd8);
lv_style_btn_ina.body.grad_color = lv_color_make(0xd8, 0xd8, 0xd8);
2019-03-17 07:44:48 +01:00
lv_style_btn_ina.body.border.color = lv_color_make(0x90, 0x90, 0x90);
2019-04-04 07:15:40 +02:00
lv_style_btn_ina.text.color = lv_color_make(0x70, 0x70, 0x70);
lv_style_btn_ina.image.color = lv_color_make(0x70, 0x70, 0x70);
lv_style_btn_ina.line.color = lv_color_make(0x70, 0x70, 0x70);
}
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));
}
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
{
2018-10-05 17:22:49 +02: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.padding.left, ratio);
STYLE_ATTR_MIX(body.padding.right, ratio);
STYLE_ATTR_MIX(body.padding.top, ratio);
STYLE_ATTR_MIX(body.padding.bottom, ratio);
2018-10-05 17:22:49 +02:00
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;
2019-04-04 07:15:40 +02:00
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);
2018-10-05 17:22:49 +02:00
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);
2019-04-04 07:15:40 +02:00
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);
2018-10-05 17:22:49 +02:00
if(ratio < (STYLE_MIX_MAX >> 1)) {
res->body.border.part = start->body.border.part;
2019-04-04 07:15:40 +02:00
res->glass = start->glass;
res->text.font = start->text.font;
2018-10-05 17:22:49 +02:00
res->body.shadow.type = start->body.shadow.type;
2019-04-04 07:15:40 +02:00
res->line.rounded = start->line.rounded;
2018-10-05 17:22:49 +02:00
} else {
res->body.border.part = end->body.border.part;
2019-04-04 07:15:40 +02:00
res->glass = end->glass;
res->text.font = end->text.font;
2018-10-05 17:22:49 +02:00
res->body.shadow.type = end->body.shadow.type;
2019-04-04 07:15:40 +02:00
res->line.rounded = end->line.rounded;
2018-10-05 17:22:49 +02:00
}
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
**********************/
#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