From 97282e67bdb9265a6d85076f7a842554d79fb683 Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Wed, 19 May 2021 21:54:32 +0200 Subject: [PATCH] feat(example) add obj drag example --- examples/widgets/lv_example_widgets.h | 1 + examples/widgets/obj/index.rst | 8 +++++- examples/widgets/obj/lv_example_obj_2.c | 33 +++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 examples/widgets/obj/lv_example_obj_2.c diff --git a/examples/widgets/lv_example_widgets.h b/examples/widgets/lv_example_widgets.h index b67890d68..08fb72195 100644 --- a/examples/widgets/lv_example_widgets.h +++ b/examples/widgets/lv_example_widgets.h @@ -91,6 +91,7 @@ void lv_example_meter_4(void); void lv_example_msgbox_1(void); void lv_example_obj_1(void); +void lv_example_obj_2(void); void lv_example_roller_1(void); void lv_example_roller_2(void); diff --git a/examples/widgets/obj/index.rst b/examples/widgets/obj/index.rst index 3b098c490..737e9abd2 100644 --- a/examples/widgets/obj/index.rst +++ b/examples/widgets/obj/index.rst @@ -4,7 +4,13 @@ C Base objects with custom styles """""""""""""""""""""""""""""""" -.. lv_example:: lv_ex_widgets/lv_ex_obj/lv_ex_obj_1 +.. lv_example:: widgets/obj/lv_example_obj_1 + :language: c + +Make an object draggable +"""""""""""""""""""""""""""" + +.. lv_example:: widgets/obj/lv_example_obj_2 :language: c MicroPython diff --git a/examples/widgets/obj/lv_example_obj_2.c b/examples/widgets/obj/lv_example_obj_2.c new file mode 100644 index 000000000..631e9e611 --- /dev/null +++ b/examples/widgets/obj/lv_example_obj_2.c @@ -0,0 +1,33 @@ +#include "../../lv_examples.h" +#if LV_BUILD_EXAMPLES + +static void drag_event_handler(lv_event_t * e) +{ + lv_obj_t * obj = lv_event_get_target(e); + + lv_indev_t * indev = lv_indev_get_act(); + lv_point_t vect; + lv_indev_get_vect(indev, &vect); + + lv_coord_t x = lv_obj_get_x(obj) + vect.x; + lv_coord_t y = lv_obj_get_y(obj) + vect.y; + lv_obj_set_pos(obj, x, y); +} + + +/** + * Make an object dragable. + */ +void lv_example_obj_2(void) +{ + lv_obj_t * obj; + obj = lv_obj_create(lv_scr_act()); + lv_obj_set_size(obj, 150, 100); + lv_obj_add_event_cb(obj, drag_event_handler, LV_EVENT_PRESSING, NULL); + + lv_obj_t * label = lv_label_create(obj); + lv_label_set_text(label, "Drag me"); + lv_obj_center(label); + +} +#endif