1 #include "../../lv_examples.h"
2 #if LV_USE_BTNMATRIX && LV_BUILD_EXAMPLES
3 
event_cb(lv_event_t * e)4 static void event_cb(lv_event_t * e)
5 {
6     lv_event_code_t code = lv_event_get_code(e);
7     lv_obj_t * obj = lv_event_get_target(e);
8     if(code == LV_EVENT_DRAW_PART_BEGIN) {
9         lv_obj_draw_part_dsc_t * dsc = lv_event_get_draw_part_dsc(e);
10 
11         /*When the button matrix draws the buttons...*/
12         if(dsc->class_p == &lv_btnmatrix_class && dsc->type == LV_BTNMATRIX_DRAW_PART_BTN) {
13             /*Change the draw descriptor of the 2nd button*/
14             if(dsc->id == 1) {
15                 dsc->rect_dsc->radius = 0;
16                 if(lv_btnmatrix_get_selected_btn(obj) == dsc->id)  dsc->rect_dsc->bg_color = lv_palette_darken(LV_PALETTE_BLUE, 3);
17                 else dsc->rect_dsc->bg_color = lv_palette_main(LV_PALETTE_BLUE);
18 
19                 dsc->rect_dsc->shadow_width = 6;
20                 dsc->rect_dsc->shadow_ofs_x = 3;
21                 dsc->rect_dsc->shadow_ofs_y = 3;
22                 dsc->label_dsc->color = lv_color_white();
23             }
24             /*Change the draw descriptor of the 3rd button*/
25             else if(dsc->id == 2) {
26                 dsc->rect_dsc->radius = LV_RADIUS_CIRCLE;
27                 if(lv_btnmatrix_get_selected_btn(obj) == dsc->id)  dsc->rect_dsc->bg_color = lv_palette_darken(LV_PALETTE_RED, 3);
28                 else dsc->rect_dsc->bg_color = lv_palette_main(LV_PALETTE_RED);
29 
30                 dsc->label_dsc->color = lv_color_white();
31             }
32             else if(dsc->id == 3) {
33                 dsc->label_dsc->opa = LV_OPA_TRANSP; /*Hide the text if any*/
34 
35             }
36         }
37     }
38     if(code == LV_EVENT_DRAW_PART_END) {
39         lv_obj_draw_part_dsc_t * dsc = lv_event_get_draw_part_dsc(e);
40 
41         /*When the button matrix draws the buttons...*/
42         if(dsc->class_p == &lv_btnmatrix_class && dsc->type == LV_BTNMATRIX_DRAW_PART_BTN) {
43             /*Add custom content to the 4th button when the button itself was drawn*/
44             if(dsc->id == 3) {
45                 LV_IMG_DECLARE(img_star);
46                 lv_img_header_t header;
47                 lv_res_t res = lv_img_decoder_get_info(&img_star, &header);
48                 if(res != LV_RES_OK) return;
49 
50                 lv_area_t a;
51                 a.x1 = dsc->draw_area->x1 + (lv_area_get_width(dsc->draw_area) - header.w) / 2;
52                 a.x2 = a.x1 + header.w - 1;
53                 a.y1 = dsc->draw_area->y1 + (lv_area_get_height(dsc->draw_area) - header.h) / 2;
54                 a.y2 = a.y1 + header.h - 1;
55 
56                 lv_draw_img_dsc_t img_draw_dsc;
57                 lv_draw_img_dsc_init(&img_draw_dsc);
58                 img_draw_dsc.recolor = lv_color_black();
59                 if(lv_btnmatrix_get_selected_btn(obj) == dsc->id)  img_draw_dsc.recolor_opa = LV_OPA_30;
60 
61                 lv_draw_img(dsc->draw_ctx, &img_draw_dsc, &a, &img_star);
62             }
63         }
64     }
65 }
66 
67 /**
68  * Add custom drawer to the button matrix to customize buttons one by one
69  */
lv_example_btnmatrix_2(void)70 void lv_example_btnmatrix_2(void)
71 {
72     lv_obj_t * btnm = lv_btnmatrix_create(lv_scr_act());
73     lv_obj_add_event_cb(btnm, event_cb, LV_EVENT_ALL, NULL);
74     lv_obj_center(btnm);
75 }
76 
77 #endif
78