Skip to content

Commit 6b9acd7

Browse files
committed
Merge pull request godotengine#110055 from dagarsar/tree-rtl
Add helper methods to convert right-to-left `Rect2i` and `Point2i` in `Tree`'s `draw_item`
2 parents 7eaf1ee + bc9c14d commit 6b9acd7

File tree

2 files changed

+31
-44
lines changed

2 files changed

+31
-44
lines changed

scene/gui/tree.cpp

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,6 +2038,20 @@ int Tree::get_item_height(TreeItem *p_item) const {
20382038
return height;
20392039
}
20402040

2041+
Point2i Tree::convert_rtl_position(const Point2i &pos, int width) const {
2042+
if (cache.rtl) {
2043+
return Point2i(get_size().width - pos.x - width, pos.y);
2044+
}
2045+
return pos;
2046+
}
2047+
2048+
Rect2i Tree::convert_rtl_rect(const Rect2i &rect) const {
2049+
if (cache.rtl) {
2050+
return Rect2i(Point2i(get_size().width - rect.position.x - rect.size.x, rect.position.y), rect.size);
2051+
}
2052+
return rect;
2053+
}
2054+
20412055
void Tree::draw_item_rect(const TreeItem::Cell &p_cell, const Rect2i &p_rect, const Color &p_color, const Color &p_icon_color, int p_ol_size, const Color &p_ol_color) const {
20422056
ERR_FAIL_COND(theme_cache.font.is_null());
20432057

@@ -2332,9 +2346,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
23322346
if (p_item->cells[0].selected || is_row_hovered) {
23332347
const Rect2 content_rect = _get_content_rect();
23342348
Rect2i row_rect = Rect2i(Point2i(content_rect.position.x, item_rect.position.y), Size2i(content_rect.size.x, item_rect.size.y));
2335-
if (rtl) {
2336-
row_rect.position.x = get_size().width - row_rect.position.x - row_rect.size.x;
2337-
}
2349+
row_rect = convert_rtl_rect(row_rect);
23382350

23392351
if (p_item->cells[0].selected) {
23402352
if (is_row_hovered) {
@@ -2361,10 +2373,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
23612373
}
23622374

23632375
if (select_mode != SELECT_ROW) {
2364-
Rect2i r = cell_rect;
2365-
if (rtl) {
2366-
r.position.x = get_size().width - r.position.x - r.size.x;
2367-
}
2376+
Rect2i r = convert_rtl_rect(cell_rect);
23682377

23692378
// Cell hover.
23702379
if (is_cell_hovered && !p_item->cells[i].selected && !drop_mode_flags) {
@@ -2381,9 +2390,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
23812390

23822391
if (select_mode != SELECT_ROW) {
23832392
p_item->set_meta("__focus_rect", Rect2(r.position, r.size));
2384-
if (rtl) {
2385-
r.position.x = get_size().width - r.position.x - r.size.x;
2386-
}
2393+
r = convert_rtl_rect(r);
23872394
if (p_item->cells[i].selected) {
23882395
if (is_cell_hovered) {
23892396
if (has_focus(true)) {
@@ -2405,10 +2412,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
24052412
}
24062413

24072414
if (theme_cache.draw_guides) {
2408-
Rect2 r = cell_rect;
2409-
if (rtl) {
2410-
r.position.x = get_size().width - r.position.x - r.size.x;
2411-
}
2415+
Rect2 r = convert_rtl_rect(cell_rect);
24122416
RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2i(r.position.x, r.position.y + r.size.height), r.position + r.size, theme_cache.guide_color, 1);
24132417
}
24142418

@@ -2418,9 +2422,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
24182422
r.position.x = p_draw_ofs.x;
24192423
r.size.x = item_width + ofs;
24202424
}
2421-
if (rtl) {
2422-
r.position.x = get_size().width - r.position.x - r.size.x;
2423-
}
2425+
r = convert_rtl_rect(r);
24242426
if (p_item->cells[i].custom_bg_outline) {
24252427
RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, r.size.x, 1), p_item->cells[i].bg_color);
24262428
RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y + r.size.y - 1, r.size.x, 1), p_item->cells[i].bg_color);
@@ -2432,10 +2434,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
24322434
}
24332435

24342436
if (drop_mode_flags && drop_mode_over) {
2435-
Rect2 r = cell_rect;
2436-
if (rtl) {
2437-
r.position.x = get_size().width - r.position.x - r.size.x;
2438-
}
2437+
Rect2 r = convert_rtl_rect(cell_rect);
24392438
if (drop_mode_over == p_item) {
24402439
if (drop_mode_section == 0 || drop_mode_section == -1) {
24412440
// Line above.
@@ -2475,9 +2474,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
24752474
update_item_cell(p_item, i);
24762475
}
24772476

2478-
if (rtl) {
2479-
item_rect.position.x = get_size().width - item_rect.position.x - item_rect.size.x;
2480-
}
2477+
item_rect = convert_rtl_rect(item_rect);
24812478

24822479
Point2i text_pos = item_rect.position;
24832480
text_pos.y += Math::floor((item_rect.size.y - p_item->cells[i].text_buf->get_size().y) * 0.5);
@@ -2648,30 +2645,22 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
26482645
bool should_draw_hovered = !drop_mode_flags && cache.hover_item == p_item && cache.hover_column == i && cache.hover_button_index_in_column == j && !p_item->cells[i].buttons[j].disabled;
26492646

26502647
if (should_draw_pressed || should_draw_hovered) {
2651-
Point2 od = button_ofs;
2652-
if (rtl) {
2653-
od.x = get_size().width - od.x - button_size.x;
2654-
}
2648+
Point2 od = convert_rtl_position(button_ofs, button_size.x);
26552649
if (should_draw_pressed && should_draw_hovered) {
26562650
theme_cache.button_pressed->draw(get_canvas_item(), Rect2(od.x, od.y, button_size.width, MAX(button_size.height, label_h)));
26572651
} else {
26582652
theme_cache.button_hover->draw(get_canvas_item(), Rect2(od.x, od.y, button_size.width, MAX(button_size.height, label_h)));
26592653
}
26602654
}
26612655
if (selected_item == p_item && selected_col == i && selected_button == j) {
2662-
Point2 od = button_ofs;
2663-
if (rtl) {
2664-
od.x = get_size().width - od.x - button_size.x;
2665-
}
2656+
Point2 od = convert_rtl_position(button_ofs, button_size.x);
26662657
theme_cache.button_hover->draw(get_canvas_item(), Rect2(od.x, od.y, button_size.width, MAX(button_size.height, label_h)));
26672658
}
26682659

26692660
button_ofs.y += (label_h - button_size.height) / 2;
26702661
button_ofs += theme_cache.button_pressed->get_offset();
26712662

2672-
if (rtl) {
2673-
button_ofs.x = get_size().width - button_ofs.x - button_texture->get_width();
2674-
}
2663+
button_ofs = convert_rtl_position(button_ofs, button_texture->get_width());
26752664
button_texture->draw(ci, button_ofs, p_item->cells[i].buttons[j].disabled ? Color(1, 1, 1, 0.5) : p_item->cells[i].buttons[j].color);
26762665
item_width_with_buttons -= button_size.width + theme_cache.button_margin;
26772666
}
@@ -2683,9 +2672,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
26832672
}
26842673

26852674
if (select_mode == SELECT_MULTI && selected_item == p_item && selected_col == i) {
2686-
if (is_layout_rtl()) {
2687-
cell_rect.position.x = get_size().width - cell_rect.position.x - cell_rect.size.x;
2688-
}
2675+
cell_rect = convert_rtl_rect(cell_rect);
26892676
if (has_focus(true)) {
26902677
theme_cache.cursor->draw(ci, cell_rect);
26912678
} else {
@@ -2700,7 +2687,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
27002687
Ref<Texture2D> arrow;
27012688

27022689
if (p_item->collapsed) {
2703-
if (is_layout_rtl()) {
2690+
if (rtl) {
27042691
arrow = theme_cache.arrow_collapsed_mirrored;
27052692
} else {
27062693
arrow = theme_cache.arrow_collapsed;
@@ -2721,9 +2708,9 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
27212708
}
27222709

27232710
if (arrow_draw_size.width > 0) {
2711+
apos = convert_rtl_position(apos, arrow_draw_size.width);
27242712
Point2 src_pos = Point2();
27252713
if (rtl) {
2726-
apos.x = get_size().width - apos.x - arrow_draw_size.width;
27272714
src_pos = Point2(arrow_full_size.width - arrow_draw_size.width, 0);
27282715
}
27292716
Rect2 arrow_rect = Rect2(apos, arrow_draw_size);
@@ -2779,10 +2766,8 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
27792766
int more_prev_ofs = 0;
27802767

27812768
if (root_pos.y + line_width >= 0) {
2782-
if (rtl) {
2783-
root_pos.x = get_size().width - root_pos.x;
2784-
parent_pos.x = get_size().width - parent_pos.x;
2785-
}
2769+
root_pos = convert_rtl_position(root_pos);
2770+
parent_pos = convert_rtl_position(parent_pos);
27862771
float parent_bottom_y = root_pos.y + parent_line_width * 0.5 + parent_line_pixel_shift;
27872772

27882773
// Order of parts on this bend: the horizontal line first, then the vertical line.

scene/gui/tree.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,8 @@ class Tree : public Control {
549549

550550
int compute_item_height(TreeItem *p_item) const;
551551
int get_item_height(TreeItem *p_item) const;
552+
Point2i convert_rtl_position(const Point2i &pos, int width = 0) const;
553+
Rect2i convert_rtl_rect(const Rect2i &Rect2) const;
552554
void _update_all();
553555
void update_column(int p_col);
554556
void update_item_cell(TreeItem *p_item, int p_col) const;

0 commit comments

Comments
 (0)