1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-14 06:42:58 +08:00
lvgl/lv_obj/lv_style.c

304 lines
11 KiB
C
Raw Normal View History

/**
* @file lv_style.c
*
*/
/*********************
* INCLUDES
*********************/
2017-11-26 23:57:39 +01:00
#include "../../lv_conf.h"
#include "lv_style.h"
2017-07-28 13:57:56 +02:00
#include "lv_obj.h"
2017-11-23 20:42:14 +01:00
#include "../lv_misc/lv_mem.h"
/*********************
* DEFINES
*********************/
2017-11-27 17:48:54 +01:00
#if LV_NO_ANIM == 0
2017-10-30 17:11:56 +01:00
#define LV_STYLE_ANIM_RES 256 /*Animation max in 1024 steps*/
2017-07-28 13:57:56 +02:00
#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)
2017-11-27 17:48:54 +01:00
#endif
/**********************
* TYPEDEFS
**********************/
2017-11-27 17:48:54 +01:00
#if LV_NO_ANIM == 0
2017-07-28 13:57:56 +02:00
typedef struct {
2017-10-31 16:47:44 +01:00
lv_style_t style_start; /*Save not only pointers because if same as 'style_anim' then it will be modified too*/
lv_style_t style_end;
2017-07-28 13:57:56 +02:00
lv_style_t * style_anim;
}lv_style_anim_dsc_t;
2017-11-27 17:48:54 +01:00
#endif
/**********************
* STATIC PROTOTYPES
**********************/
2017-11-27 17:48:54 +01:00
#if LV_NO_ANIM == 0
2017-07-28 13:57:56 +02:00
static void lv_style_aimator(lv_style_anim_dsc_t * dsc, int32_t val);
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
*/
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*/
lv_style_scr.glass = 0;
2017-11-23 21:28:36 +01:00
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;
2017-10-18 16:07:19 +02:00
lv_style_scr.body.radius = 0;
lv_style_scr.body.padding.ver = LV_DPI / 12;
lv_style_scr.body.padding.hor = LV_DPI / 12;
lv_style_scr.body.padding.inner = LV_DPI / 12;
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;
lv_style_scr.body.border.opa = LV_OPA_COVER;
lv_style_scr.body.border.width = 0;
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;
lv_style_scr.body.shadow.type = LV_SHADOW_FULL;
lv_style_scr.body.shadow.width = 0;
2017-10-18 16:07:19 +02:00
2017-11-23 21:28:36 +01:00
lv_style_scr.text.opa = LV_OPA_COVER;
lv_style_scr.text.color = LV_COLOR_MAKE(0x30, 0x30, 0x30);
2017-11-19 19:28:45 +01:00
lv_style_scr.text.font = LV_FONT_DEFAULT;
lv_style_scr.text.letter_space = 1 << LV_ANTIALIAS;
lv_style_scr.text.line_space = 2 << LV_ANTIALIAS;
2017-10-18 16:07:19 +02:00
2017-11-23 21:28:36 +01:00
lv_style_scr.image.opa = LV_OPA_COVER;
lv_style_scr.image.color = LV_COLOR_MAKE(0x20, 0x20, 0x20);
lv_style_scr.image.intense = LV_OPA_TRANSP;
2017-10-18 16:07:19 +02:00
2017-11-23 21:28:36 +01: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 = 1 << LV_ANTIALIAS;
2017-04-24 16:16:36 +02:00
/*Plain style (by default near the same as the screen style)*/
memcpy(&lv_style_plain, &lv_style_scr, sizeof(lv_style_t));
/*Plain color style*/
memcpy(&lv_style_plain_color, &lv_style_plain, sizeof(lv_style_t));
2017-11-23 21:28:36 +01: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);
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 */
memcpy(&lv_style_pretty, &lv_style_plain, sizeof(lv_style_t));
2017-11-23 21:28:36 +01: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;
2017-10-18 16:07:19 +02:00
lv_style_pretty.body.radius = LV_DPI / 15;
2017-11-23 21:28:36 +01:00
lv_style_pretty.body.border.color = LV_COLOR_MAKE(0x40, 0x40, 0x40);
lv_style_pretty.body.border.width = LV_DPI / 50 >= 1 ? LV_DPI / 50 : 1;
2017-11-23 21:28:36 +01:00
lv_style_pretty.body.border.opa = LV_OPA_30;
/*Pretty color style*/
memcpy(&lv_style_pretty_color, &lv_style_pretty, sizeof(lv_style_t));
2017-11-23 21:28:36 +01:00
lv_style_pretty_color.text.color = LV_COLOR_MAKE(0xc0, 0xc0, 0xc0);
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);
lv_style_pretty_color.body.border.color = LV_COLOR_MAKE(0x15, 0x2c, 0x42);
/*Transparent style*/
memcpy(&lv_style_transp, &lv_style_plain, sizeof(lv_style_t));
lv_style_transp.body.empty = 1;
lv_style_transp.glass = 1;
lv_style_transp.body.border.width = 0;
/*Transparent tight style*/
memcpy(&lv_style_transp_fit, &lv_style_transp, sizeof(lv_style_t));
lv_style_transp_fit.body.padding.hor = 0;
lv_style_transp_fit.body.padding.ver = 0;
/*Transparent fitting size*/
memcpy(&lv_style_transp_tight, &lv_style_transp_fit, sizeof(lv_style_t));
lv_style_transp_tight.body.padding.inner = 0;
/*Button released style*/
2017-11-20 14:26:18 +01:00
memcpy(&lv_style_btn_rel, &lv_style_plain, sizeof(lv_style_t));
2017-11-23 21:28:36 +01: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);
2017-11-20 14:26:18 +01:00
lv_style_btn_rel.body.radius = LV_DPI / 15;
lv_style_btn_rel.body.padding.hor = LV_DPI / 4;
lv_style_btn_rel.body.padding.ver = LV_DPI / 6;
lv_style_btn_rel.body.padding.inner = LV_DPI / 10;
2017-11-23 21:28:36 +01:00
lv_style_btn_rel.body.border.color = LV_COLOR_MAKE(0x0b, 0x19, 0x28);
2017-11-29 10:46:59 +01:00
lv_style_btn_rel.body.border.width = 1;//LV_DPI / 50 >= 1 ? LV_DPI / 50 : 1;
2017-11-23 21:28:36 +01:00
lv_style_btn_rel.body.border.opa = LV_OPA_70;
lv_style_btn_rel.text.color = LV_COLOR_MAKE(0xff, 0xff, 0xff);
lv_style_btn_rel.body.shadow.color = LV_COLOR_GRAY;
2017-11-20 14:26:18 +01:00
lv_style_btn_rel.body.shadow.width = 0;
/*Button pressed style*/
2017-11-20 14:26:18 +01:00
memcpy(&lv_style_btn_pr, &lv_style_btn_rel, sizeof(lv_style_t));
2017-11-23 21:28:36 +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);
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*/
2017-11-20 14:26:18 +01:00
memcpy(&lv_style_btn_tgl_rel, &lv_style_btn_rel, sizeof(lv_style_t));
2017-11-23 21:28:36 +01: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);
lv_style_btn_tgl_rel.body.border.color = LV_COLOR_MAKE(0x01, 0x07, 0x0d);
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*/
2017-11-20 14:26:18 +01:00
memcpy(&lv_style_btn_tgl_pr, &lv_style_btn_tgl_rel, sizeof(lv_style_t));
2017-11-23 21:28:36 +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);
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*/
2017-11-20 14:26:18 +01:00
memcpy(&lv_style_btn_ina, &lv_style_btn_rel, sizeof(lv_style_t));
2017-11-23 21:28:36 +01: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);
lv_style_btn_ina.body.border.color = LV_COLOR_MAKE(0x90, 0x90, 0x90);
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));
}
2017-11-27 17:48:54 +01:00
#if LV_NO_ANIM == 0
2017-07-28 13:57:56 +02:00
/**
* 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;
2017-11-23 21:28:36 +01:00
dsc = lv_mem_alloc(sizeof(lv_style_anim_dsc_t));
2017-07-28 13:57:56 +02:00
dsc->style_anim = anim->style_anim;
2017-10-30 17:11:56 +01:00
memcpy(&dsc->style_start, anim->style_start, sizeof(lv_style_t));
memcpy(&dsc->style_end, anim->style_end, sizeof(lv_style_t));
2017-07-28 13:57:56 +02:00
2017-11-23 21:28:36 +01:00
lv_anim_t a;
2017-07-28 13:57:56 +02:00
a.var = (void*)dsc;
a.start = 0;
a.end = LV_STYLE_ANIM_RES;
2017-11-23 21:28:36 +01:00
a.fp = (lv_anim_fp_t)lv_style_aimator;
a.path = lv_anim_get_path(LV_ANIM_PATH_LIN);
2017-07-28 13:57:56 +02:00
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;
2017-11-23 21:28:36 +01:00
lv_anim_create(&a);
2017-07-28 13:57:56 +02:00
}
2017-11-27 17:48:54 +01:00
#endif
/**********************
* STATIC FUNCTIONS
**********************/
2017-11-27 17:48:54 +01:00
#if LV_NO_ANIM == 0
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_STYLE_ANIM_RES
*/
static void lv_style_aimator(lv_style_anim_dsc_t * dsc, int32_t val)
{
2017-10-30 17:11:56 +01:00
const lv_style_t * start = &dsc->style_start;
const lv_style_t * end = &dsc->style_end;
2017-07-28 13:57:56 +02:00
lv_style_t * act = dsc->style_anim;
STYLE_ATTR_ANIM(body.opa, val);
2017-10-18 16:07:19 +02:00
STYLE_ATTR_ANIM(body.radius, val);
STYLE_ATTR_ANIM(body.border.width, val);
STYLE_ATTR_ANIM(body.shadow.width, val);
STYLE_ATTR_ANIM(body.padding.hor, val);
STYLE_ATTR_ANIM(body.padding.ver, val);
2017-10-19 12:46:49 +02:00
STYLE_ATTR_ANIM(body.padding.inner, val);
STYLE_ATTR_ANIM(text.line_space, val);
STYLE_ATTR_ANIM(text.letter_space, val);
2017-10-18 16:07:19 +02:00
STYLE_ATTR_ANIM(line.width, val);
2017-10-19 12:46:49 +02:00
STYLE_ATTR_ANIM(image.intense, val);
2017-10-18 16:07:19 +02:00
2017-11-23 21:28:36 +01:00
act->body.main_color = lv_color_mix(end->body.main_color, start->body.main_color, val);
act->body.grad_color = lv_color_mix(end->body.grad_color, start->body.grad_color, val);
act->body.border.color = lv_color_mix(end->body.border.color, start->body.border.color, val);
act->body.shadow.color = lv_color_mix(end->body.shadow.color, start->body.shadow.color, val);
act->text.color = lv_color_mix(end->text.color, start->text.color, val);
act->image.color = lv_color_mix(end->image.color, start->image.color, val);
act->line.color = lv_color_mix(end->line.color, start->line.color, val);
2017-07-28 13:57:56 +02:00
if(val == 0) {
2017-10-18 16:07:19 +02:00
act->body.empty = start->body.empty;
2017-10-19 12:46:49 +02:00
act->glass = start->glass;
act->text.font = start->text.font;
act->body.shadow.type = start->body.shadow.type;
2017-07-28 13:57:56 +02:00
}
if(val == LV_STYLE_ANIM_RES) {
2017-10-18 16:07:19 +02:00
act->body.empty = end->body.empty;
2017-10-19 12:46:49 +02:00
act->glass = end->glass;
act->text.font = end->text.font;
act->body.shadow.type = end->body.shadow.type;
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
if(val == LV_STYLE_ANIM_RES) {
2017-11-23 21:28:36 +01:00
lv_mem_free(dsc);
2017-10-30 17:11:56 +01:00
}
2017-07-28 13:57:56 +02:00
}
2017-11-27 17:48:54 +01:00
#endif