1 #include "../lv_examples.h"
2 #if LV_USE_BTN && LV_BUILD_EXAMPLES
3
4 static lv_style_t style_btn;
5 static lv_style_t style_btn_pressed;
6 static lv_style_t style_btn_red;
7
darken(const lv_color_filter_dsc_t * dsc,lv_color_t color,lv_opa_t opa)8 static lv_color_t darken(const lv_color_filter_dsc_t * dsc, lv_color_t color, lv_opa_t opa)
9 {
10 LV_UNUSED(dsc);
11 return lv_color_darken(color, opa);
12 }
13
style_init(void)14 static void style_init(void)
15 {
16 /*Create a simple button style*/
17 lv_style_init(&style_btn);
18 lv_style_set_radius(&style_btn, 10);
19 lv_style_set_bg_opa(&style_btn, LV_OPA_COVER);
20 lv_style_set_bg_color(&style_btn, lv_palette_lighten(LV_PALETTE_GREY, 3));
21 lv_style_set_bg_grad_color(&style_btn, lv_palette_main(LV_PALETTE_GREY));
22 lv_style_set_bg_grad_dir(&style_btn, LV_GRAD_DIR_VER);
23
24 lv_style_set_border_color(&style_btn, lv_color_black());
25 lv_style_set_border_opa(&style_btn, LV_OPA_20);
26 lv_style_set_border_width(&style_btn, 2);
27
28 lv_style_set_text_color(&style_btn, lv_color_black());
29
30 /*Create a style for the pressed state.
31 *Use a color filter to simply modify all colors in this state*/
32 static lv_color_filter_dsc_t color_filter;
33 lv_color_filter_dsc_init(&color_filter, darken);
34 lv_style_init(&style_btn_pressed);
35 lv_style_set_color_filter_dsc(&style_btn_pressed, &color_filter);
36 lv_style_set_color_filter_opa(&style_btn_pressed, LV_OPA_20);
37
38 /*Create a red style. Change only some colors.*/
39 lv_style_init(&style_btn_red);
40 lv_style_set_bg_color(&style_btn_red, lv_palette_main(LV_PALETTE_RED));
41 lv_style_set_bg_grad_color(&style_btn_red, lv_palette_lighten(LV_PALETTE_RED, 3));
42 }
43
44 /**
45 * Create styles from scratch for buttons.
46 */
lv_example_get_started_2(void)47 void lv_example_get_started_2(void)
48 {
49 /*Initialize the style*/
50 style_init();
51
52 /*Create a button and use the new styles*/
53 lv_obj_t * btn = lv_btn_create(lv_scr_act());
54 /* Remove the styles coming from the theme
55 * Note that size and position are also stored as style properties
56 * so lv_obj_remove_style_all will remove the set size and position too */
57 lv_obj_remove_style_all(btn);
58 lv_obj_set_pos(btn, 10, 10);
59 lv_obj_set_size(btn, 120, 50);
60 lv_obj_add_style(btn, &style_btn, 0);
61 lv_obj_add_style(btn, &style_btn_pressed, LV_STATE_PRESSED);
62
63 /*Add a label to the button*/
64 lv_obj_t * label = lv_label_create(btn);
65 lv_label_set_text(label, "Button");
66 lv_obj_center(label);
67
68 /*Create another button and use the red style too*/
69 lv_obj_t * btn2 = lv_btn_create(lv_scr_act());
70 lv_obj_remove_style_all(btn2); /*Remove the styles coming from the theme*/
71 lv_obj_set_pos(btn2, 10, 80);
72 lv_obj_set_size(btn2, 120, 50);
73 lv_obj_add_style(btn2, &style_btn, 0);
74 lv_obj_add_style(btn2, &style_btn_red, 0);
75 lv_obj_add_style(btn2, &style_btn_pressed, LV_STATE_PRESSED);
76 lv_obj_set_style_radius(btn2, LV_RADIUS_CIRCLE, 0); /*Add a local style too*/
77
78 label = lv_label_create(btn2);
79 lv_label_set_text(label, "Button 2");
80 lv_obj_center(label);
81 }
82
83 #endif
84