feat(obj) add lv_obj_move_to_index(obj, index), renamed lv_obj_get_child_id(obj) to lv_obj_get_index(obj) (#2514)

* - renamed lv_obj_get_id(obj) to lv_obj_get_index(obj).

- added lv_obj_move_to_index(obj, index).

* automatic review comment fixed

* removed unused variable

* review issue

* restored deprecated function in header, otherwise Build Micropython with LVGL submodule / build (pull_request)  failes

* moved deprecated lv_obj_get_child_id() back to lv_obj_tree.h, otherwise Micropython will not build

* inline function did not work

* made deprecated function 'static inline'

* and now also inline

* move static inline function to lv_api_map.h again

* removed lv_obj_move_up/down

* changed log to warning for deprecated function

* redefined lv_obj_move_foreground(obj) and lv_obj_move_background(obj) as inline functions now calling lv_obj_move_to_index(obj, index).

- lv_obj_swap(obj1, obj2) added. (#2461)
This commit is contained in:
Karijn Wessing
2021-09-05 20:47:55 +02:00
committed by GitHub
parent 54338f6e57
commit 780e0efe2c
11 changed files with 150 additions and 152 deletions

View File

@@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include "../../lv_examples.h"
#if LV_USE_LIST && LV_BUILD_EXAMPLES
@@ -58,25 +59,45 @@ static void event_handler_up(lv_event_t* e)
if ((code == LV_EVENT_CLICKED) || (code == LV_EVENT_LONG_PRESSED_REPEAT))
{
if (currentButton == NULL) return;
lv_obj_move_up(currentButton);
uint32_t index = lv_obj_get_index(currentButton);
if (index <= 0) return;
lv_obj_move_to_index(currentButton, index - 1);
lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
}
}
static void event_handler_center(lv_event_t* e)
{
const lv_event_code_t code = lv_event_get_code(e);
if ((code == LV_EVENT_CLICKED) || (code == LV_EVENT_LONG_PRESSED_REPEAT))
{
if (currentButton == NULL) return;
lv_obj_t* parent = lv_obj_get_parent(currentButton);
const uint32_t pos = lv_obj_get_child_cnt(parent) / 2;
lv_obj_move_to_index(currentButton, pos);
lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
}
}
static void event_handler_dn(lv_event_t* e)
{
lv_event_code_t code = lv_event_get_code(e);
const lv_event_code_t code = lv_event_get_code(e);
if ((code == LV_EVENT_CLICKED) || (code == LV_EVENT_LONG_PRESSED_REPEAT))
{
if (currentButton == NULL) return;
lv_obj_move_down(currentButton);
const uint32_t index = lv_obj_get_index(currentButton);
lv_obj_move_to_index(currentButton, index + 1);
lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
}
}
static void event_handler_bottom(lv_event_t* e)
{
lv_event_code_t code = lv_event_get_code(e);
const lv_event_code_t code = lv_event_get_code(e);
if (code == LV_EVENT_CLICKED)
{
if (currentButton == NULL) return;
@@ -87,28 +108,21 @@ static void event_handler_bottom(lv_event_t* e)
static void event_handler_swap(lv_event_t* e)
{
lv_event_code_t code = lv_event_get_code(e);
const lv_event_code_t code = lv_event_get_code(e);
// lv_obj_t* obj = lv_event_get_target(e);
if ((code == LV_EVENT_CLICKED) || (code == LV_EVENT_LONG_PRESSED_REPEAT))
{
uint32_t first = 0;
uint32_t last = lv_obj_get_child_cnt(list1);
if (last > 1)
{
last--;
while (first < last)
uint32_t cnt = lv_obj_get_child_cnt(list1);
for (int i = 0; i < 100; i++)
if (cnt > 1)
{
lv_obj_t* obj1 = lv_obj_get_child(list1, first);
lv_obj_t* obj2 = lv_obj_get_child(list1, last);
lv_obj_swap(obj1, obj2);
first++;
last--;
lv_obj_t* obj = lv_obj_get_child(list1, rand() % cnt);
lv_obj_move_to_index(obj, rand() % cnt);
if (currentButton != NULL)
{
lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
}
}
if (currentButton != NULL)
{
lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
}
}
}
}
@@ -122,7 +136,7 @@ void lv_example_list_2(void)
/*Add buttons to the list*/
lv_obj_t* btn;
int i;
for (i = 0; i < 30; i++) {
for (i = 0; i < 15; i++) {
btn = lv_btn_create(list1);
lv_obj_set_width(btn, lv_pct(50));
lv_obj_add_event_cb(btn, event_handler, LV_EVENT_CLICKED, NULL);
@@ -149,6 +163,10 @@ void lv_example_list_2(void)
lv_obj_add_event_cb(btn, event_handler_up, LV_EVENT_ALL, NULL);
lv_group_remove_obj(btn);
btn = lv_list_add_btn(list2, LV_SYMBOL_LEFT, "Center");
lv_obj_add_event_cb(btn, event_handler_center, LV_EVENT_ALL, NULL);
lv_group_remove_obj(btn);
btn = lv_list_add_btn(list2, LV_SYMBOL_DOWN, "Down");
lv_obj_add_event_cb(btn, event_handler_dn, LV_EVENT_ALL, NULL);
lv_group_remove_obj(btn);

View File

@@ -5,7 +5,7 @@
static void event_handler(lv_event_t * e)
{
lv_obj_t * obj = lv_event_get_target(e);
LV_LOG_USER("Button %d clicked", lv_obj_get_child_id(obj));
LV_LOG_USER("Button %d clicked", lv_obj_get_index(obj));
}
void lv_example_win_1(void)