1from imagetools import get_png_info, open_png
2
3# Register PNG image decoder
4decoder = lv.img.decoder_create()
5decoder.info_cb = get_png_info
6decoder.open_cb = open_png
7
8# Create an image from the png file
9try:
10    with open('../../assets/img_star.png','rb') as f:
11        png_data = f.read()
12except:
13    print("Could not find star.png")
14    sys.exit()
15
16img_star_argb = lv.img_dsc_t({
17  'data_size': len(png_data),
18  'data': png_data
19})
20
21def event_cb(e):
22    code = e.get_code()
23    obj = e.get_target()
24    dsc = lv.obj_draw_part_dsc_t.__cast__(e.get_param())
25    if code == lv.EVENT.DRAW_PART_BEGIN:
26        # Change the draw descriptor the 2nd button
27        if dsc.id == 1:
28            dsc.rect_dsc.radius = 0
29            if obj.get_selected_btn() == dsc.id:
30                dsc.rect_dsc.bg_color = lv.palette_darken(lv.PALETTE.GREY, 3)
31            else:
32                dsc.rect_dsc.bg_color = lv.palette_main(lv.PALETTE.BLUE)
33
34            dsc.rect_dsc.shadow_width = 6
35            dsc.rect_dsc.shadow_ofs_x = 3
36            dsc.rect_dsc.shadow_ofs_y = 3
37            dsc.label_dsc.color = lv.color_white()
38
39        # Change the draw descriptor the 3rd button
40
41        elif dsc.id == 2:
42            dsc.rect_dsc.radius = lv.RADIUS.CIRCLE
43            if obj.get_selected_btn() == dsc.id:
44                dsc.rect_dsc.bg_color = lv.palette_darken(lv.PALETTE.RED, 3)
45            else:
46                dsc.rect_dsc.bg_color = lv.palette_main(lv.PALETTE.RED)
47
48                dsc.label_dsc.color = lv.color_white()
49        elif dsc.id == 3:
50            dsc.label_dsc.opa = lv.OPA.TRANSP  # Hide the text if any
51
52    if code == lv.EVENT.DRAW_PART_END:
53        # Add custom content to the 4th button when the button itself was drawn
54        if dsc.id == 3:
55            # LV_IMG_DECLARE(img_star)
56            header = lv.img_header_t()
57            res = lv.img.decoder_get_info(img_star_argb, header)
58            if res != lv.RES.OK:
59                print("error when getting image header")
60                return
61            else:
62                a = lv.area_t()
63                a.x1 = dsc.draw_area.x1 + (dsc.draw_area.get_width() - header.w) // 2
64                a.x2 = a.x1 + header.w - 1
65                a.y1 = dsc.draw_area.y1 + (dsc.draw_area.get_height() - header.h) // 2
66                a.y2 = a.y1 + header.h - 1
67                img_draw_dsc = lv.draw_img_dsc_t()
68                img_draw_dsc.init()
69                img_draw_dsc.recolor = lv.color_black()
70                if obj.get_selected_btn() == dsc.id:
71                    img_draw_dsc.recolor_opa = lv.OPA._30
72
73                dsc.draw_ctx.img(img_draw_dsc, a, img_star_argb)
74
75#
76# Add custom drawer to the button matrix to c
77#
78btnm = lv.btnmatrix(lv.scr_act())
79btnm.add_event_cb(event_cb, lv.EVENT.ALL, None)
80btnm.center()
81
82