From 451eddb1237a7b90c5d6c483132e1878ea0c2db0 Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Thu, 8 Sep 2022 23:55:00 +0200 Subject: [PATCH] example(event): simplify lv_example_event_4 --- examples/event/index.rst | 5 ++ examples/event/lv_example_event_4.c | 76 +++++++++++++---------------- 2 files changed, 38 insertions(+), 43 deletions(-) diff --git a/examples/event/index.rst b/examples/event/index.rst index a556ac8ce..b47635315 100644 --- a/examples/event/index.rst +++ b/examples/event/index.rst @@ -16,4 +16,9 @@ Event bubbling .. lv_example:: event/lv_example_event_3 :language: c +Draw event +"""""""""""""""""""""""" +.. lv_example:: event/lv_example_event_4 + :language: c + diff --git a/examples/event/lv_example_event_4.c b/examples/event/lv_example_event_4.c index 7d459b81d..21a6ba746 100644 --- a/examples/event/lv_example_event_4.c +++ b/examples/event/lv_example_event_4.c @@ -2,65 +2,55 @@ #if LV_BUILD_EXAMPLES -static int n = 3; -static lv_obj_t * label = NULL; +static uint32_t size = 0; +static bool size_dec = false; static void timer_cb(lv_timer_t * timer) { - if(n < 3 || n > 32) { - n = 3; - } - else { - static uint32_t old_tick = 0; - uint32_t tick = lv_tick_get(); - if(!old_tick) { - old_tick = tick; - } - if(tick - old_tick > 3000) { - n++; - lv_label_set_text_fmt(label, "%d sides", n); - old_tick = tick; - } - } lv_obj_invalidate(timer->user_data); + if(size_dec) size--; + else size++; + + if(size == 50) size_dec = true; + else if(size == 0) size_dec = false; } static void event_cb(lv_event_t * e) { - /*The original target of the event. Can be the buttons or the container*/ - lv_draw_ctx_t * draw_ctx = lv_event_get_draw_ctx(e); - lv_draw_rect_dsc_t draw_dsc; - lv_draw_rect_dsc_init(&draw_dsc); - draw_dsc.bg_color = lv_palette_main(LV_PALETTE_LIGHT_GREEN); - draw_dsc.bg_opa = LV_OPA_COVER; - lv_point_t points[32]; - int i, r = 150; - uint32_t tick = lv_tick_get(); - for(i = 0; i < n; i++) { - int angle = i * 360 / n + ((tick % 36000) / 100); - lv_coord_t x = 150 + (r * lv_trigo_cos(angle) >> LV_TRIGO_SHIFT), y = - 150 + (r * lv_trigo_sin(angle) >> LV_TRIGO_SHIFT); - points[i].x = x; - points[i].y = y; + lv_obj_t * obj = lv_event_get_target(e); + lv_obj_draw_part_dsc_t * dsc = lv_event_get_draw_part_dsc(e); + if(dsc->class_p == &lv_obj_class && dsc->part == LV_PART_MAIN) { + lv_draw_rect_dsc_t draw_dsc; + lv_draw_rect_dsc_init(&draw_dsc); + draw_dsc.bg_color = lv_color_hex(0xffaaaa); + draw_dsc.radius = LV_RADIUS_CIRCLE; + draw_dsc.border_color = lv_color_hex(0xff5555); + draw_dsc.border_width = 2; + draw_dsc.outline_color = lv_color_hex(0xff0000); + draw_dsc.outline_pad = 3; + draw_dsc.outline_width = 2; + + lv_area_t a; + a.x1 = 0; + a.y1 = 0; + a.x2 = size; + a.y2 = size; + lv_area_align(&obj->coords, &a, LV_ALIGN_CENTER, 0, 0); + + lv_draw_rect(dsc->draw_ctx, &draw_dsc, &a); } - lv_draw_polygon(draw_ctx, &draw_dsc, points, n); } /** - * Demonstrate event bubbling + * Demonstrate the usage of draw event */ void lv_example_event_4(void) { - lv_obj_t * cont = lv_obj_create(lv_scr_act()); - lv_obj_remove_style_all(cont); - lv_obj_set_size(cont, 300, 300); - label = lv_label_create(cont); - lv_label_set_text_fmt(label, "%d sides", n); - lv_obj_center(label); - - lv_obj_add_event_cb(cont, event_cb, LV_EVENT_DRAW_MAIN, NULL); - lv_timer_create(timer_cb, 17, cont); + lv_obj_set_size(cont, 200, 200); + lv_obj_center(cont); + lv_obj_add_event_cb(cont, event_cb, LV_EVENT_DRAW_PART_END, NULL); + lv_timer_create(timer_cb, 30, cont); } #endif