Files
lvgl/examples/event/lv_example_event_draw.c
Mutahhar Mustafa Khan d76a346376 docs(examples): introduce summary and description to examples (#9968)
Co-authored-by: Gabor Kiss-Vamosi <kisvegabor@gmail.com>
2026-04-20 14:05:57 +02:00

69 lines
2.2 KiB
C

#include "../lv_examples.h"
#if LV_BUILD_EXAMPLES
static int32_t size = 0;
static bool size_dec = false;
static void timer_cb(lv_timer_t * timer)
{
lv_obj_invalidate((lv_obj_t *) lv_timer_get_user_data(timer));
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)
{
lv_obj_t * obj = lv_event_get_target_obj(e);
lv_draw_task_t * draw_task = lv_event_get_draw_task(e);
lv_draw_dsc_base_t * base_dsc = (lv_draw_dsc_base_t *) lv_draw_task_get_draw_dsc(draw_task);
if(base_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_t obj_coords;
lv_obj_get_coords(obj, &obj_coords);
lv_area_align(&obj_coords, &a, LV_ALIGN_CENTER, 0, 0);
lv_draw_rect(base_dsc->layer, &draw_dsc, &a);
}
}
/**
* @title Custom drawing on draw task events
* @brief Draw a pulsing red circle on top of a container with a draw task callback.
*
* A 200x200 container is centered and flagged with
* `LV_OBJ_FLAG_SEND_DRAW_TASK_EVENTS`. An `LV_EVENT_DRAW_TASK_ADDED`
* callback inspects the draw task and, when `base_dsc->part` is
* `LV_PART_MAIN`, calls `lv_draw_rect` to paint a pink circle with a red
* border and outline. A 30 ms `lv_timer_t` bounces a `size` counter between
* 0 and 50 and invalidates the container each tick to animate the circle.
*/
void lv_example_event_draw(void)
{
lv_obj_t * cont = lv_obj_create(lv_screen_active());
lv_obj_set_size(cont, 200, 200);
lv_obj_center(cont);
lv_obj_add_event_cb(cont, event_cb, LV_EVENT_DRAW_TASK_ADDED, NULL);
lv_obj_add_flag(cont, LV_OBJ_FLAG_SEND_DRAW_TASK_EVENTS);
lv_timer_create(timer_cb, 30, cont);
}
#endif