From 97d976f9c2027142817505d9ee2ba053cb035059 Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Wed, 9 Sep 2020 10:46:49 +0200 Subject: [PATCH] minor fixes --- src/lv_core/lv_grid.c | 147 ++++++++++++++++++++++------------ src/lv_core/lv_grid.h | 7 +- src/lv_core/lv_indev.c | 2 +- src/lv_core/lv_indev_scroll.c | 84 ++++++++++--------- src/lv_core/lv_obj.c | 2 +- src/lv_core/lv_obj.h | 20 ++--- 6 files changed, 158 insertions(+), 104 deletions(-) diff --git a/src/lv_core/lv_grid.c b/src/lv_core/lv_grid.c index 07df28622..845054be5 100644 --- a/src/lv_core/lv_grid.c +++ b/src/lv_core/lv_grid.c @@ -31,6 +31,7 @@ static void calc_explicit_rows(lv_obj_t * cont, _lv_grid_calc_t * calc); static void calc_implicit_cols(lv_obj_t * cont, _lv_grid_calc_t * calc); static void calc_implicit_rows(lv_obj_t * cont, _lv_grid_calc_t * calc); static void item_repos(lv_obj_t * cont, lv_obj_t * item, _lv_grid_calc_t * calc, item_repos_hint_t * hint); +static lv_coord_t grid_place(lv_coord_t cont_size, bool auto_size, uint8_t place, lv_coord_t gap, uint32_t track_num, lv_coord_t * size_array, lv_coord_t * pos_array); static void report_grid_change_core(const lv_grid_t * grid, lv_obj_t * obj); /********************** @@ -91,31 +92,37 @@ void lv_obj_report_grid_change(const lv_grid_t * grid) /** * Calculate the grid cells coordinates - * @param obj an object that has a grid + * @param cont an object that has a grid * @param calc store the calculated cells sizes here * @note `_lv_grid_calc_free(calc_out)` needs to be called when `calc_out` is not needed anymore */ -void _lv_grid_calc(struct _lv_obj_t * obj, _lv_grid_calc_t * calc_out) +void _lv_grid_calc(struct _lv_obj_t * cont, _lv_grid_calc_t * calc_out) { - if(obj->grid == NULL) return; + if(cont->grid == NULL) return; // printf("calc: %d, %d\n", obj->grid->col_dsc_len, obj->grid->row_dsc_len); - if(obj->grid->col_dsc && obj->grid->row_dsc) { - calc_explicit_rows(obj, calc_out); - calc_explicit_cols(obj, calc_out); + if(cont->grid->col_dsc && cont->grid->row_dsc) { + calc_explicit_rows(cont, calc_out); + calc_explicit_cols(cont, calc_out); } - else if(obj->grid->col_dsc && !obj->grid->row_dsc) { - calc_explicit_cols(obj, calc_out); - calc_implicit_rows(obj, calc_out); + else if(cont->grid->col_dsc && !cont->grid->row_dsc) { + calc_explicit_cols(cont, calc_out); + calc_implicit_rows(cont, calc_out); } - else if(!obj->grid->col_dsc && obj->grid->row_dsc) { - calc_implicit_cols(obj, calc_out); - calc_explicit_rows(obj, calc_out); + else if(!cont->grid->col_dsc && cont->grid->row_dsc) { + calc_implicit_cols(cont, calc_out); + calc_explicit_rows(cont, calc_out); } - calc_out->grid_w = calc_out->x[calc_out->col_num - 1] + calc_out->w[calc_out->col_num - 1] - calc_out->x[0]; - calc_out->grid_h = calc_out->y[calc_out->row_num - 1] + calc_out->h[calc_out->row_num - 1] - calc_out->y[0]; + bool auto_w = cont->w_set == LV_SIZE_AUTO ? true : false; + lv_coord_t cont_w = lv_obj_get_width_fit(cont); + calc_out->grid_w = grid_place(cont_w, auto_w, cont->grid->col_place, cont->grid->col_gap, calc_out->col_num, calc_out->w, calc_out->x); + bool auto_h = cont->h_set == LV_SIZE_AUTO ? true : false; + lv_coord_t cont_h = lv_obj_get_height_fit(cont); + calc_out->grid_h = grid_place(cont_h, auto_h, cont->grid->row_place, cont->grid->row_gap, calc_out->row_num, calc_out->h, calc_out->y); + + LV_ASSERT_MEM_INTEGRITY(); } /** @@ -246,8 +253,8 @@ static void calc_explicit_cols(lv_obj_t * cont, _lv_grid_calc_t * calc) } } - lv_coord_t free_w = lv_obj_get_width_fit(cont) - grid_w; - + lv_coord_t cont_w = lv_obj_get_width_fit(cont) - grid->col_gap * (grid->col_dsc_len - 1); + lv_coord_t free_w = cont_w - grid_w; for(i = 0; i < grid->col_dsc_len; i++) { lv_coord_t x = grid->col_dsc[i]; @@ -259,11 +266,6 @@ static void calc_explicit_cols(lv_obj_t * cont, _lv_grid_calc_t * calc) } } } - - calc->x[0] = 0; - for(i = 0; i < grid->col_dsc_len - 1; i++) { - calc->x[i + 1] = calc->x[i] + calc->w[i]; - } } static void calc_explicit_rows(lv_obj_t * cont, _lv_grid_calc_t * calc) @@ -290,7 +292,8 @@ static void calc_explicit_rows(lv_obj_t * cont, _lv_grid_calc_t * calc) } } - lv_coord_t free_h = lv_obj_get_height_fit(cont) - grid_h; + lv_coord_t cont_h = lv_obj_get_height_fit(cont) - grid->row_gap * (grid->row_dsc_len - 1); + lv_coord_t free_h = cont_h - grid_h; for(i = 0; i < grid->row_dsc_len; i++) { lv_coord_t x = grid->row_dsc[i]; @@ -302,12 +305,6 @@ static void calc_explicit_rows(lv_obj_t * cont, _lv_grid_calc_t * calc) } } } - - calc->y[0] = 0; - - for(i = 0; i < calc->row_num - 1; i++) { - calc->y[i + 1] = calc->y[i] + calc->h[i]; - } } @@ -317,12 +314,10 @@ static void calc_implicit_cols(lv_obj_t * cont, _lv_grid_calc_t * calc) uint32_t child_cnt = lv_obj_count_children(cont); - calc->col_num = ((child_cnt + grid->row_dsc_len - 1) / grid->row_dsc_len) + 1; /*+ grid->row_dsc_len - 1 to round up*/ - calc->w = _lv_mem_buf_get(sizeof(lv_coord_t) * calc->col_num); + calc->col_num = ((child_cnt + grid->row_dsc_len - 1) / grid->row_dsc_len); /*+ grid->row_dsc_len - 1 to round up*/ + calc->w = _lv_mem_buf_get(sizeof(lv_coord_t) * (calc->col_num + 1)); /*+1 to allow some limit checks later*/ calc->x = _lv_mem_buf_get(sizeof(lv_coord_t) * calc->col_num); - uint32_t i; - lv_obj_t * child = lv_obj_get_child_back(cont, NULL); uint32_t row_i = 0; uint32_t col_i = 0; @@ -342,13 +337,6 @@ static void calc_implicit_cols(lv_obj_t * cont, _lv_grid_calc_t * calc) } child = lv_obj_get_child_back(cont, child); } - - - calc->x[0] = 0; - - for(i = 0; i < calc->col_num - 1; i++) { - calc->x[i + 1] = calc->x[i] + calc->w[i]; - } } @@ -357,12 +345,10 @@ static void calc_implicit_rows(lv_obj_t * cont, _lv_grid_calc_t * calc) const lv_grid_t * grid = cont->grid; uint32_t child_cnt = lv_obj_count_children(cont); - calc->row_num = ((child_cnt + grid->col_dsc_len - 1) / grid->col_dsc_len) + 1; /*+ grid->col_dsc_len - 1 to round up*/ - calc->h = _lv_mem_buf_get(sizeof(lv_coord_t) * calc->row_num); + calc->row_num = ((child_cnt + grid->col_dsc_len - 1) / grid->col_dsc_len); /*+ grid->col_dsc_len - 1 to round up*/ + calc->h = _lv_mem_buf_get(sizeof(lv_coord_t) * (calc->row_num + 1)); /*+1 to allow some limit checks later*/ calc->y = _lv_mem_buf_get(sizeof(lv_coord_t) * calc->row_num); - uint32_t i; - lv_obj_t * child = lv_obj_get_child_back(cont, NULL); uint32_t col_i = 0; uint32_t row_i = 0; @@ -382,13 +368,6 @@ static void calc_implicit_rows(lv_obj_t * cont, _lv_grid_calc_t * calc) } child = lv_obj_get_child_back(cont, child); } - - - calc->y[0] = 0; - - for(i = 0; i < calc->row_num; i++) { - calc->y[i + 1] = calc->y[i] + calc->h[i]; - } } @@ -536,6 +515,74 @@ static void item_repos(lv_obj_t * cont, lv_obj_t * item, _lv_grid_calc_t * calc, if(moved) _lv_obj_move_to(item, x, y, false); } +/** + * Place the grid track according to place methods. It keeps the track sizes but sets their position. + * It can process both columns or rows according to the passed parameters. + * @param cont_size size of the containers content area (width/height) + * @param auto_size true: the container has auto size in the current direction + * @param place placeing method + * @param gap grid gap + * @param track_num number of tracks + * @param size_array array with the track sizes + * @param pos_array write the positions of the tracks here + * @return the total size of the grid + */ +static lv_coord_t grid_place(lv_coord_t cont_size, bool auto_size, uint8_t place, lv_coord_t gap, uint32_t track_num, lv_coord_t * size_array, lv_coord_t * pos_array) +{ + lv_coord_t grid_size = 0; + uint32_t i; + + if(auto_size) { + pos_array[0] = 0; + } else { + + /*With spaced placements gap will be calculated from the remaining space*/ + if(place == LV_GRID_SPACE_AROUND || place == LV_GRID_SPACE_BETWEEN || place == LV_GRID_SPACE_EVENLY) { + gap = 0; + } + + /*Get the full grid size with gap*/ + for(i = 0; i < track_num; i++) { + grid_size += size_array[i] + gap; + } + grid_size -= gap; + + /*Calculate the position of the first item and set gap is necessary*/ + switch(place) { + case LV_GRID_START: + pos_array[0] = 0; + break; + case LV_GRID_CENTER: + pos_array[0] = (cont_size - grid_size) / 2; + break; + case LV_GRID_END: + pos_array[0] = cont_size - grid_size; + break; + case LV_GRID_SPACE_BETWEEN: + pos_array[0] = 0; + gap = (lv_coord_t)(cont_size - grid_size) / (lv_coord_t)(track_num - 1); + break; + case LV_GRID_SPACE_AROUND: + gap = (lv_coord_t)(cont_size - grid_size) / (lv_coord_t)(track_num); + pos_array[0] = gap / 2; + break; + case LV_GRID_SPACE_EVENLY: + gap = (lv_coord_t)(cont_size - grid_size) / (lv_coord_t)(track_num + 1); + pos_array[0] = gap; + break; + + } + } + + /*Set the position of all tracks from the start position, gaps and track sizes*/ + for(i = 0; i < track_num - 1; i++) { + pos_array[i + 1] = pos_array[i] + size_array[i] + gap; + } + + /*Return the full size of the grid*/ + return pos_array[track_num - 1] + size_array[track_num - 1] - pos_array[0]; +} + /** * Refresh the grid of all children of an object. (Called recursively) diff --git a/src/lv_core/lv_grid.h b/src/lv_core/lv_grid.h index be4440f1c..14ac908ba 100644 --- a/src/lv_core/lv_grid.h +++ b/src/lv_core/lv_grid.h @@ -19,8 +19,8 @@ extern "C" { * DEFINES *********************/ -#define LV_GRID_CENTER 0 -#define LV_GRID_START 1 +#define LV_GRID_START 0 +#define LV_GRID_CENTER 1 #define LV_GRID_END 2 #define LV_GRID_STRETCH 3 #define LV_GRID_SPACE_EVENLY 4 @@ -93,7 +93,8 @@ typedef struct { lv_coord_t col_gap; lv_coord_t row_gap; lv_grid_flow_t flow; - uint8_t place_content; + uint8_t col_place; + uint8_t row_place; }lv_grid_t; typedef struct { diff --git a/src/lv_core/lv_indev.c b/src/lv_core/lv_indev.c index 565eb020f..24c5c0d94 100644 --- a/src/lv_core/lv_indev.c +++ b/src/lv_core/lv_indev.c @@ -1089,7 +1089,7 @@ static void indev_click_focus(lv_indev_proc_t * proc) { /*Handle click focus*/ lv_obj_t * obj_to_focus = _lv_obj_get_focused_obj(indev_obj_act); - if(lv_obj_has_flag(indev_obj_act, LV_OBJ_FLAG_PRESS_LOCK) == false && + if(lv_obj_has_flag(indev_obj_act, LV_OBJ_FLAG_CLICK_FOCUSABLE) && proc->types.pointer.last_pressed != obj_to_focus) { #if LV_USE_GROUP lv_group_t * g_act = lv_obj_get_group(indev_obj_act); diff --git a/src/lv_core/lv_indev_scroll.c b/src/lv_core/lv_indev_scroll.c index d8f859203..9c34798d3 100644 --- a/src/lv_core/lv_indev_scroll.c +++ b/src/lv_core/lv_indev_scroll.c @@ -356,30 +356,33 @@ static lv_coord_t find_snap_point_x(const lv_obj_t * obj, lv_coord_t min, lv_coo lv_obj_t * child = lv_obj_get_child_back(obj, NULL); while(child) { - lv_coord_t x_child = 0; - lv_coord_t x_parent = 0; - switch(obj->snap_align_x) { - case LV_SCROLL_SNAP_ALIGN_START: - x_child = child->coords.x1; - x_parent = obj->coords.x1; - break; - case LV_SCROLL_SNAP_ALIGN_END: - x_child = child->coords.x2; - x_parent = obj->coords.x2; - break; - case LV_SCROLL_SNAP_ALIGN_CENTER: - x_child = child->coords.x1 + lv_area_get_width(&child->coords) / 2; - x_parent = obj->coords.x1 + lv_area_get_width(&obj->coords) / 2; - } + if(lv_obj_has_flag(child, LV_OBJ_FLAG_SNAPABLE)) { + lv_coord_t x_child = 0; + lv_coord_t x_parent = 0; + switch(obj->snap_align_x) { + case LV_SCROLL_SNAP_ALIGN_START: + x_child = child->coords.x1; + x_parent = obj->coords.x1; + break; + case LV_SCROLL_SNAP_ALIGN_END: + x_child = child->coords.x2; + x_parent = obj->coords.x2; + break; + case LV_SCROLL_SNAP_ALIGN_CENTER: + x_child = child->coords.x1 + lv_area_get_width(&child->coords) / 2; + x_parent = obj->coords.x1 + lv_area_get_width(&obj->coords) / 2; + } - x_child += ofs; - if(x_child >= min && x_child <= max) { - lv_coord_t x = x_child - x_parent; - if(LV_MATH_ABS(x) < LV_MATH_ABS(dist)) dist = x; + x_child += ofs; + if(x_child >= min && x_child <= max) { + lv_coord_t x = x_child - x_parent; + if(LV_MATH_ABS(x) < LV_MATH_ABS(dist)) dist = x; + } } child = lv_obj_get_child_back(obj, child); } + LV_ASSERT_MEM_INTEGRITY(); return dist == LV_COORD_MAX ? 0: -dist; } @@ -399,35 +402,36 @@ static lv_coord_t find_snap_point_y(const lv_obj_t * obj, lv_coord_t min, lv_coo lv_coord_t dist = LV_COORD_MAX; - uint32_t cc = 0; lv_obj_t * child = lv_obj_get_child_back(obj, NULL); while(child) { - cc++; - lv_coord_t y_child = 0; - lv_coord_t y_parent = 0; - switch(obj->snap_align_y) { - case LV_SCROLL_SNAP_ALIGN_START: - y_child = child->coords.y1; - y_parent = obj->coords.y1; - break; - case LV_SCROLL_SNAP_ALIGN_END: - y_child = child->coords.y2; - y_parent = obj->coords.y2; - break; - case LV_SCROLL_SNAP_ALIGN_CENTER: - y_child = child->coords.y1 + lv_area_get_height(&child->coords) / 2; - y_parent = obj->coords.y1 + lv_area_get_height(&obj->coords) / 2; - } + if(lv_obj_has_flag(child, LV_OBJ_FLAG_SNAPABLE)) { + lv_coord_t y_child = 0; + lv_coord_t y_parent = 0; + switch(obj->snap_align_y) { + case LV_SCROLL_SNAP_ALIGN_START: + y_child = child->coords.y1; + y_parent = obj->coords.y1; + break; + case LV_SCROLL_SNAP_ALIGN_END: + y_child = child->coords.y2; + y_parent = obj->coords.y2; + break; + case LV_SCROLL_SNAP_ALIGN_CENTER: + y_child = child->coords.y1 + lv_area_get_height(&child->coords) / 2; + y_parent = obj->coords.y1 + lv_area_get_height(&obj->coords) / 2; + } - y_child += ofs; - if(y_child >= min && y_child <= max) { - lv_coord_t y = y_child - y_parent; - if(LV_MATH_ABS(y) < LV_MATH_ABS(dist)) dist = y; + y_child += ofs; + if(y_child >= min && y_child <= max) { + lv_coord_t y = y_child - y_parent; + if(LV_MATH_ABS(y) < LV_MATH_ABS(dist)) dist = y; + } } child = lv_obj_get_child_back(obj, child); } + LV_ASSERT_MEM_INTEGRITY(); return dist == LV_COORD_MAX ? 0 : -dist; } diff --git a/src/lv_core/lv_obj.c b/src/lv_core/lv_obj.c index bf4392e1f..78f400ed3 100644 --- a/src/lv_core/lv_obj.c +++ b/src/lv_core/lv_obj.c @@ -284,7 +284,7 @@ lv_obj_t * lv_obj_create(lv_obj_t * parent, const lv_obj_t * copy) /*Set attributes*/ new_obj->scroll_mode = LV_SCROLL_MODE_AUTO; new_obj->scroll_dir = LV_DIR_ALL; - new_obj->flags = LV_OBJ_FLAG_CLICKABLE; + new_obj->flags = LV_OBJ_FLAG_CLICKABLE | LV_OBJ_FLAG_SNAPABLE | LV_OBJ_FLAG_PRESS_LOCK | LV_OBJ_FLAG_CLICK_FOCUSABLE; if(parent) new_obj->flags |= LV_OBJ_FLAG_GESTURE_BUBBLE; new_obj->state = LV_STATE_DEFAULT; new_obj->scroll.x = 0; diff --git a/src/lv_core/lv_obj.h b/src/lv_core/lv_obj.h index 00a8712e1..fb655aaa1 100644 --- a/src/lv_core/lv_obj.h +++ b/src/lv_core/lv_obj.h @@ -187,15 +187,17 @@ typedef uint8_t lv_dir_t; enum { LV_OBJ_FLAG_HIDDEN = (1 << 0), LV_OBJ_FLAG_CLICKABLE = (1 << 1), - LV_OBJ_FLAG_SCROLLABLE = (1 << 2), - LV_OBJ_FLAG_SCROLL_ELASTIC = (1 << 3), - LV_OBJ_FLAG_SCROLL_MOMENTUM = (1 << 4), - LV_OBJ_FLAG_SCROLL_STOP = (1 << 5), - LV_OBJ_FLAG_PRESS_LOCK = (1 << 6), - LV_OBJ_FLAG_EVENT_BUBBLE = (1 << 7), - LV_OBJ_FLAG_GESTURE_BUBBLE = (1 << 8), - LV_OBJ_FLAG_FOCUS_BUBBLE = (1 << 9), - LV_OBJ_FLAG_ADV_HITTEST = (1 << 10), + LV_OBJ_FLAG_CLICK_FOCUSABLE = (1 << 2), + LV_OBJ_FLAG_SCROLLABLE = (1 << 3), + LV_OBJ_FLAG_SCROLL_ELASTIC = (1 << 4), + LV_OBJ_FLAG_SCROLL_MOMENTUM = (1 << 5), + LV_OBJ_FLAG_SCROLL_STOP = (1 << 6), + LV_OBJ_FLAG_SNAPABLE = (1 << 7), + LV_OBJ_FLAG_PRESS_LOCK = (1 << 8), + LV_OBJ_FLAG_EVENT_BUBBLE = (1 << 9), + LV_OBJ_FLAG_GESTURE_BUBBLE = (1 << 10), + LV_OBJ_FLAG_FOCUS_BUBBLE = (1 << 11), + LV_OBJ_FLAG_ADV_HITTEST = (1 << 12), }; typedef uint16_t lv_obj_flag_t;