1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-14 06:42:58 +08:00

fix(align) fix lv_obj_align_to

This commit is contained in:
Gabor Kiss-Vamosi 2021-06-16 14:14:29 +02:00
parent 8017f47227
commit 93b38e92be

View File

@ -341,123 +341,127 @@ void lv_obj_align_to(lv_obj_t * obj, const lv_obj_t * base, lv_align_t align, lv
lv_coord_t x = 0; lv_coord_t x = 0;
lv_coord_t y = 0; lv_coord_t y = 0;
lv_obj_t * parent = lv_obj_get_parent(obj);
lv_coord_t border_width = lv_obj_get_style_border_width(parent, LV_PART_MAIN); lv_obj_t * parent = lv_obj_get_parent(obj);
lv_coord_t pleft = lv_obj_get_style_pad_left(parent, LV_PART_MAIN) + border_width; lv_coord_t pborder = lv_obj_get_style_border_width(parent, LV_PART_MAIN);
lv_coord_t ptop = lv_obj_get_style_pad_top(parent, LV_PART_MAIN) + border_width; lv_coord_t pleft = lv_obj_get_style_pad_left(parent, LV_PART_MAIN) + pborder;
lv_coord_t ptop = lv_obj_get_style_pad_top(parent, LV_PART_MAIN) + pborder;
lv_coord_t bborder = lv_obj_get_style_border_width(base, LV_PART_MAIN);
lv_coord_t bleft = lv_obj_get_style_pad_left(base, LV_PART_MAIN) + bborder;
lv_coord_t btop = lv_obj_get_style_pad_top(base, LV_PART_MAIN) + bborder;
if(align == LV_ALIGN_DEFAULT) { if(align == LV_ALIGN_DEFAULT) {
if(lv_obj_get_style_base_dir(parent, LV_PART_MAIN) == LV_BASE_DIR_RTL) align = LV_ALIGN_TOP_RIGHT; if(lv_obj_get_style_base_dir(base, LV_PART_MAIN) == LV_BASE_DIR_RTL) align = LV_ALIGN_TOP_RIGHT;
else align = LV_ALIGN_TOP_LEFT; else align = LV_ALIGN_TOP_LEFT;
} }
switch(align) { switch(align) {
case LV_ALIGN_CENTER: case LV_ALIGN_CENTER:
x = lv_obj_get_content_width(base) / 2 - lv_obj_get_width(obj) / 2; x = lv_obj_get_content_width(base) / 2 - lv_obj_get_width(obj) / 2 + bleft;
y = lv_obj_get_content_height(base) / 2 - lv_obj_get_height(obj) / 2; y = lv_obj_get_content_height(base) / 2 - lv_obj_get_height(obj) / 2 + btop;
break; break;
case LV_ALIGN_TOP_LEFT: case LV_ALIGN_TOP_LEFT:
x = 0; x = bleft;
y = 0; y = btop;
break; break;
case LV_ALIGN_TOP_MID: case LV_ALIGN_TOP_MID:
x = lv_obj_get_content_width(base) / 2 - lv_obj_get_width(obj) / 2; x = lv_obj_get_content_width(base) / 2 - lv_obj_get_width(obj) / 2 + bleft;
y = 0; y = btop;
break; break;
case LV_ALIGN_TOP_RIGHT: case LV_ALIGN_TOP_RIGHT:
x = lv_obj_get_content_width(base) - lv_obj_get_width(obj); x = lv_obj_get_content_width(base) - lv_obj_get_width(obj) + bleft;
y = 0; y = btop;
break; break;
case LV_ALIGN_BOTTOM_LEFT: case LV_ALIGN_BOTTOM_LEFT:
x = 0; x = bleft;
y = lv_obj_get_content_height(base) - lv_obj_get_height(obj); y = lv_obj_get_content_height(base) - lv_obj_get_height(obj) + btop;
break; break;
case LV_ALIGN_BOTTOM_MID: case LV_ALIGN_BOTTOM_MID:
x = lv_obj_get_content_width(base) / 2 - lv_obj_get_width(obj) / 2; x = lv_obj_get_content_width(base) / 2 - lv_obj_get_width(obj) / 2 + bleft;
y = lv_obj_get_content_height(base) - lv_obj_get_height(obj); y = lv_obj_get_content_height(base) - lv_obj_get_height(obj) + btop;
break; break;
case LV_ALIGN_BOTTOM_RIGHT: case LV_ALIGN_BOTTOM_RIGHT:
x = lv_obj_get_content_width(base) - lv_obj_get_width(obj); x = lv_obj_get_content_width(base) - lv_obj_get_width(obj) + bleft;
y = lv_obj_get_content_height(base) - lv_obj_get_height(obj); y = lv_obj_get_content_height(base) - lv_obj_get_height(obj) + btop;
break; break;
case LV_ALIGN_LEFT_MID: case LV_ALIGN_LEFT_MID:
x = 0; x = bleft;
y = lv_obj_get_content_height(base) / 2 - lv_obj_get_height(obj) / 2; y = lv_obj_get_content_height(base) / 2 - lv_obj_get_height(obj) / 2 + btop;
break; break;
case LV_ALIGN_RIGHT_MID: case LV_ALIGN_RIGHT_MID:
x = lv_obj_get_content_width(base) - lv_obj_get_width(obj); x = lv_obj_get_content_width(base) - lv_obj_get_width(obj) + bleft;
y = lv_obj_get_content_height(base) / 2 - lv_obj_get_height(obj) / 2; y = lv_obj_get_content_height(base) / 2 - lv_obj_get_height(obj) / 2 + btop;
break; break;
case LV_ALIGN_OUT_TOP_LEFT: case LV_ALIGN_OUT_TOP_LEFT:
x = -pleft; x = 0;
y = -lv_obj_get_height(obj) - ptop; y = -lv_obj_get_height(obj);
break; break;
case LV_ALIGN_OUT_TOP_MID: case LV_ALIGN_OUT_TOP_MID:
x = lv_obj_get_width(base) / 2 - lv_obj_get_width(obj) / 2 - pleft; x = lv_obj_get_width(base) / 2 - lv_obj_get_width(obj) / 2;
y = -lv_obj_get_height(obj) - ptop; y = -lv_obj_get_height(obj);
break; break;
case LV_ALIGN_OUT_TOP_RIGHT: case LV_ALIGN_OUT_TOP_RIGHT:
x = lv_obj_get_width(base) - lv_obj_get_width(obj) - pleft; x = lv_obj_get_width(base) - lv_obj_get_width(obj);
y = -lv_obj_get_height(obj) - ptop; y = -lv_obj_get_height(obj);
break; break;
case LV_ALIGN_OUT_BOTTOM_LEFT: case LV_ALIGN_OUT_BOTTOM_LEFT:
x = - pleft; x = 0;
y = lv_obj_get_height(base) - ptop; y = lv_obj_get_height(base);
break; break;
case LV_ALIGN_OUT_BOTTOM_MID: case LV_ALIGN_OUT_BOTTOM_MID:
x = lv_obj_get_width(base) / 2 - lv_obj_get_width(obj) / 2 - pleft; x = lv_obj_get_width(base) / 2 - lv_obj_get_width(obj) / 2;
y = lv_obj_get_height(base) - ptop; y = lv_obj_get_height(base);
break; break;
case LV_ALIGN_OUT_BOTTOM_RIGHT: case LV_ALIGN_OUT_BOTTOM_RIGHT:
x = lv_obj_get_width(base) - lv_obj_get_width(obj) - pleft; x = lv_obj_get_width(base) - lv_obj_get_width(obj);
y = lv_obj_get_height(base) - ptop; y = lv_obj_get_height(base);
break; break;
case LV_ALIGN_OUT_LEFT_TOP: case LV_ALIGN_OUT_LEFT_TOP:
x = -lv_obj_get_width(obj) - pleft; x = -lv_obj_get_width(obj);
y = - ptop; y = 0;
break; break;
case LV_ALIGN_OUT_LEFT_MID: case LV_ALIGN_OUT_LEFT_MID:
x = -lv_obj_get_width(obj) - pleft; x = -lv_obj_get_width(obj);
y = lv_obj_get_height(base) / 2 - lv_obj_get_height(obj) / 2 - ptop; y = lv_obj_get_height(base) / 2 - lv_obj_get_height(obj) / 2;
break; break;
case LV_ALIGN_OUT_LEFT_BOTTOM: case LV_ALIGN_OUT_LEFT_BOTTOM:
x = -lv_obj_get_width(obj) - pleft; x = -lv_obj_get_width(obj);
y = lv_obj_get_height(base) - lv_obj_get_height(obj) - ptop; y = lv_obj_get_height(base) - lv_obj_get_height(obj);
break; break;
case LV_ALIGN_OUT_RIGHT_TOP: case LV_ALIGN_OUT_RIGHT_TOP:
x = lv_obj_get_width(base) - pleft; x = lv_obj_get_width(base);
y = - ptop; y = 0;
break; break;
case LV_ALIGN_OUT_RIGHT_MID: case LV_ALIGN_OUT_RIGHT_MID:
x = lv_obj_get_width(base) - pleft; x = lv_obj_get_width(base);
y = lv_obj_get_height(base) / 2 - lv_obj_get_height(obj) / 2 - ptop; y = lv_obj_get_height(base) / 2 - lv_obj_get_height(obj) / 2;
break; break;
case LV_ALIGN_OUT_RIGHT_BOTTOM: case LV_ALIGN_OUT_RIGHT_BOTTOM:
x = lv_obj_get_width(base) - pleft; x = lv_obj_get_width(base);
y = lv_obj_get_height(base) - lv_obj_get_height(obj) - ptop; y = lv_obj_get_height(base) - lv_obj_get_height(obj);
break; break;
} }
x += x_ofs + base->coords.x1 - parent->coords.x1 + lv_obj_get_scroll_left(parent); x += x_ofs + base->coords.x1 - parent->coords.x1 + lv_obj_get_scroll_left(parent) - pleft;
y += y_ofs + base->coords.y1 - parent->coords.y1 + lv_obj_get_scroll_top(parent); y += y_ofs + base->coords.y1 - parent->coords.y1 + lv_obj_get_scroll_top(parent) - ptop;
lv_obj_set_style_align(obj, LV_ALIGN_TOP_LEFT, 0); lv_obj_set_style_align(obj, LV_ALIGN_TOP_LEFT, 0);
lv_obj_set_pos(obj, x, y); lv_obj_set_pos(obj, x, y);