1 /**
2  * @file lv_style.h
3  *
4  */
5 
6 #ifndef LV_STYLE_H
7 #define LV_STYLE_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /*********************
14  *      INCLUDES
15  *********************/
16 #include <stdbool.h>
17 #include "../lv_font/lv_font.h"
18 #include "../lv_misc/lv_color.h"
19 #include "../lv_misc/lv_area.h"
20 #include "../lv_misc/lv_anim.h"
21 
22 /*********************
23  *      DEFINES
24  *********************/
25 #define LV_RADIUS_CIRCLE (LV_COORD_MAX) /**< A very big radius to always draw as circle*/
26 
27 /**********************
28  *      TYPEDEFS
29  **********************/
30 
31 /*Border types (Use 'OR'ed values)*/
32 enum {
33     LV_BORDER_NONE     = 0x00,
34     LV_BORDER_BOTTOM   = 0x01,
35     LV_BORDER_TOP      = 0x02,
36     LV_BORDER_LEFT     = 0x04,
37     LV_BORDER_RIGHT    = 0x08,
38     LV_BORDER_FULL     = 0x0F,
39     LV_BORDER_INTERNAL = 0x10, /**< FOR matrix-like objects (e.g. Button matrix)*/
40 };
41 typedef uint8_t lv_border_part_t;
42 
43 /*Shadow types*/
44 enum {
45     LV_SHADOW_BOTTOM = 0, /**< Only draw bottom shadow */
46     LV_SHADOW_FULL,       /**< Draw shadow on all sides */
47 };
48 typedef uint8_t lv_shadow_type_t;
49 
50 /**
51  * Objects in LittlevGL can be assigned a style - which holds information about
52  * how the object should be drawn.
53  *
54  * This allows for easy customization without having to modify the object's design
55  * function.
56  */
57 typedef struct
58 {
59     uint8_t glass : 1; /**< 1: Do not inherit this style*/
60 
61     /** Object background. */
62     struct
63     {
64         lv_color_t main_color; /**< Object's main background color. */
65         lv_color_t grad_color; /**< Second color. If not equal to `main_color` a gradient will be drawn for the background. */
66         lv_coord_t radius; /**< Object's corner radius. You can use #LV_RADIUS_CIRCLE if you want to draw a circle. */
67         lv_opa_t opa; /**< Object's opacity (0-255). */
68 
69         struct
70         {
71             lv_color_t color; /**< Border color */
72             lv_coord_t width; /**< Border width */
73             lv_border_part_t part; /**< Which borders to draw */
74             lv_opa_t opa; /**< Border opacity. */
75         } border;
76 
77 
78         struct
79         {
80             lv_color_t color;
81             lv_coord_t width;
82             lv_shadow_type_t type; /**< Which parts of the shadow to draw */
83         } shadow;
84 
85         struct
86         {
87             lv_coord_t top;
88             lv_coord_t bottom;
89             lv_coord_t left;
90             lv_coord_t right;
91             lv_coord_t inner;
92         } padding;
93     } body;
94 
95     /** Style for text drawn by this object. */
96     struct
97     {
98         lv_color_t color; /**< Text color */
99         lv_color_t sel_color; /**< Text selection background color. */
100         const lv_font_t * font;
101         lv_coord_t letter_space; /**< Space between letters */
102         lv_coord_t line_space; /**< Space between lines (vertical) */
103         lv_opa_t opa; /**< Text opacity */
104     } text;
105 
106     /**< Style of images. */
107     struct
108     {
109         lv_color_t color; /**< Color to recolor the image with */
110         lv_opa_t intense; /**< Opacity of recoloring (0 means no recoloring) */
111         lv_opa_t opa; /**< Opacity of whole image */
112     } image;
113 
114     /**< Style of lines (not borders). */
115     struct
116     {
117         lv_color_t color;
118         lv_coord_t width;
119         lv_opa_t opa;
120         uint8_t rounded : 1; /**< 1: rounded line endings*/
121     } line;
122 } lv_style_t;
123 
124 #if LV_USE_ANIMATION
125 /** Data structure for style animations. */
126 typedef struct
127 {
128     lv_style_t style_start; /*Save not only pointers because can be same as 'style_anim' then it
129                                will be modified too*/
130     lv_style_t style_end;
131     lv_style_t * style_anim;
132     lv_anim_ready_cb_t ready_cb;
133 } lv_style_anim_dsc_t;
134 #endif
135 
136 /**********************
137  * GLOBAL PROTOTYPES
138  **********************/
139 
140 /**
141  *  Init the basic styles
142  */
143 void lv_style_init(void);
144 
145 /**
146  * Copy a style to an other
147  * @param dest pointer to the destination style
148  * @param src pointer to the source style
149  */
150 void lv_style_copy(lv_style_t * dest, const lv_style_t * src);
151 
152 /**
153  * Mix two styles according to a given ratio
154  * @param start start style
155  * @param end end style
156  * @param res store the result style here
157  * @param ratio the ratio of mix [0..256]; 0: `start` style; 256: `end` style
158  */
159 void lv_style_mix(const lv_style_t * start, const lv_style_t * end, lv_style_t * res, uint16_t ratio);
160 
161 #if LV_USE_ANIMATION
162 
163 /**
164  * Initialize an animation variable.
165  * E.g.:
166  * lv_anim_t a;
167  * lv_style_anim__init(&a);
168  * lv_style_anim_set_...(&a);
169  * lv_style_anim_create(&a);
170  * @param a pointer to an `lv_anim_t` variable to initialize
171  */
172 void lv_style_anim_init(lv_anim_t * a);
173 
174 /**
175  *
176  * @param a pointer to an initialized `lv_anim_t` variable
177  * @param to_anim pointer to the style to animate
178  * @param start pointer to a style to animate from (start value)
179  * @param end pointer to a style to animate to (end value)
180  */
181 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);
182 
183 /**
184  * Set the duration and delay of an animation
185  * @param a pointer to an initialized `lv_anim_t` variable
186  * @param duration duration of the animation in milliseconds
187  * @param delay delay before the animation in milliseconds
188  */
lv_style_anim_set_time(lv_anim_t * a,uint16_t duration,uint16_t delay)189 static inline void lv_style_anim_set_time(lv_anim_t * a, uint16_t duration, uint16_t delay)
190 {
191     lv_anim_set_time(a, duration, delay);
192 }
193 
194 /**
195  * Set a function call when the animation is ready
196  * @param a pointer to an initialized `lv_anim_t` variable
197  * @param ready_cb a function call when the animation is ready
198  */
lv_style_anim_set_ready_cb(lv_anim_t * a,lv_anim_ready_cb_t ready_cb)199 static inline void lv_style_anim_set_ready_cb(lv_anim_t * a, lv_anim_ready_cb_t ready_cb)
200 {
201     lv_style_anim_dsc_t * dsc = (lv_style_anim_dsc_t *)a->var;
202     dsc->ready_cb             = ready_cb;
203 }
204 
205 /**
206  * Make the animation to play back to when the forward direction is ready
207  * @param a pointer to an initialized `lv_anim_t` variable
208  * @param wait_time time in milliseconds to wait before starting the back direction
209  */
lv_style_anim_set_playback(lv_anim_t * a,uint16_t wait_time)210 static inline void lv_style_anim_set_playback(lv_anim_t * a, uint16_t wait_time)
211 {
212     lv_anim_set_playback(a, wait_time);
213 }
214 
215 /**
216  * Disable playback. (Disabled after `lv_anim_init()`)
217  * @param a pointer to an initialized `lv_anim_t` variable
218  */
lv_style_anim_clear_playback(lv_anim_t * a)219 static inline void lv_style_anim_clear_playback(lv_anim_t * a)
220 {
221     lv_anim_clear_playback(a);
222 }
223 
224 /**
225  * Make the animation to start again when ready.
226  * @param a pointer to an initialized `lv_anim_t` variable
227  * @param wait_time time in milliseconds to wait before starting the animation again
228  */
lv_style_anim_set_repeat(lv_anim_t * a,uint16_t wait_time)229 static inline void lv_style_anim_set_repeat(lv_anim_t * a, uint16_t wait_time)
230 {
231     lv_anim_set_repeat(a, wait_time);
232 }
233 
234 /**
235  * Disable repeat. (Disabled after `lv_anim_init()`)
236  * @param a pointer to an initialized `lv_anim_t` variable
237  */
lv_style_anim_clear_repeat(lv_anim_t * a)238 static inline void lv_style_anim_clear_repeat(lv_anim_t * a)
239 {
240     lv_anim_clear_repeat(a);
241 }
242 
243 /**
244  * Create an animation
245  * @param a an initialized 'anim_t' variable. Not required after call.
246  */
lv_style_anim_create(lv_anim_t * a)247 static inline void lv_style_anim_create(lv_anim_t * a)
248 {
249     lv_anim_create(a);
250 }
251 
252 #endif
253 
254 /*************************
255  *    GLOBAL VARIABLES
256  *************************/
257 extern lv_style_t lv_style_scr;
258 extern lv_style_t lv_style_transp;
259 extern lv_style_t lv_style_transp_fit;
260 extern lv_style_t lv_style_transp_tight;
261 extern lv_style_t lv_style_plain;
262 extern lv_style_t lv_style_plain_color;
263 extern lv_style_t lv_style_pretty;
264 extern lv_style_t lv_style_pretty_color;
265 extern lv_style_t lv_style_btn_rel;
266 extern lv_style_t lv_style_btn_pr;
267 extern lv_style_t lv_style_btn_tgl_rel;
268 extern lv_style_t lv_style_btn_tgl_pr;
269 extern lv_style_t lv_style_btn_ina;
270 
271 /**********************
272  *      MACROS
273  **********************/
274 
275 #ifdef __cplusplus
276 } /* extern "C" */
277 #endif
278 
279 #endif /*LV_STYLE_H*/
280