Skip to content

Commit bc9c14d

Browse files
committed
Add helper methods and simplify logic
1 parent b7c5fca commit bc9c14d

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
@@ -2027,6 +2027,20 @@ int Tree::get_item_height(TreeItem *p_item) const {
20272027
return height;
20282028
}
20292029

2030+
Point2i Tree::convert_rtl_position(const Point2i &pos, int width) const {
2031+
if (cache.rtl) {
2032+
return Point2i(get_size().width - pos.x - width, pos.y);
2033+
}
2034+
return pos;
2035+
}
2036+
2037+
Rect2i Tree::convert_rtl_rect(const Rect2i &rect) const {
2038+
if (cache.rtl) {
2039+
return Rect2i(Point2i(get_size().width - rect.position.x - rect.size.x, rect.position.y), rect.size);
2040+
}
2041+
return rect;
2042+
}
2043+
20302044
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 {
20312045
ERR_FAIL_COND(theme_cache.font.is_null());
20322046

@@ -2321,9 +2335,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
23212335
if (p_item->cells[0].selected || is_row_hovered) {
23222336
const Rect2 content_rect = _get_content_rect();
23232337
Rect2i row_rect = Rect2i(Point2i(content_rect.position.x, item_rect.position.y), Size2i(content_rect.size.x, item_rect.size.y));
2324-
if (rtl) {
2325-
row_rect.position.x = get_size().width - row_rect.position.x - row_rect.size.x;
2326-
}
2338+
row_rect = convert_rtl_rect(row_rect);
23272339

23282340
if (p_item->cells[0].selected) {
23292341
if (is_row_hovered) {
@@ -2350,10 +2362,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
23502362
}
23512363

23522364
if (select_mode != SELECT_ROW) {
2353-
Rect2i r = cell_rect;
2354-
if (rtl) {
2355-
r.position.x = get_size().width - r.position.x - r.size.x;
2356-
}
2365+
Rect2i r = convert_rtl_rect(cell_rect);
23572366

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

23712380
if (select_mode != SELECT_ROW) {
23722381
p_item->set_meta("__focus_rect", Rect2(r.position, r.size));
2373-
if (rtl) {
2374-
r.position.x = get_size().width - r.position.x - r.size.x;
2375-
}
2382+
r = convert_rtl_rect(r);
23762383
if (p_item->cells[i].selected) {
23772384
if (is_cell_hovered) {
23782385
if (has_focus(true)) {
@@ -2394,10 +2401,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
23942401
}
23952402

23962403
if (theme_cache.draw_guides) {
2397-
Rect2 r = cell_rect;
2398-
if (rtl) {
2399-
r.position.x = get_size().width - r.position.x - r.size.x;
2400-
}
2404+
Rect2 r = convert_rtl_rect(cell_rect);
24012405
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);
24022406
}
24032407

@@ -2407,9 +2411,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
24072411
r.position.x = p_draw_ofs.x;
24082412
r.size.x = item_width + ofs;
24092413
}
2410-
if (rtl) {
2411-
r.position.x = get_size().width - r.position.x - r.size.x;
2412-
}
2414+
r = convert_rtl_rect(r);
24132415
if (p_item->cells[i].custom_bg_outline) {
24142416
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);
24152417
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);
@@ -2421,10 +2423,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
24212423
}
24222424

24232425
if (drop_mode_flags && drop_mode_over) {
2424-
Rect2 r = cell_rect;
2425-
if (rtl) {
2426-
r.position.x = get_size().width - r.position.x - r.size.x;
2427-
}
2426+
Rect2 r = convert_rtl_rect(cell_rect);
24282427
if (drop_mode_over == p_item) {
24292428
if (drop_mode_section == 0 || drop_mode_section == -1) {
24302429
// Line above.
@@ -2464,9 +2463,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
24642463
update_item_cell(p_item, i);
24652464
}
24662465

2467-
if (rtl) {
2468-
item_rect.position.x = get_size().width - item_rect.position.x - item_rect.size.x;
2469-
}
2466+
item_rect = convert_rtl_rect(item_rect);
24702467

24712468
Point2i text_pos = item_rect.position;
24722469
text_pos.y += Math::floor((item_rect.size.y - p_item->cells[i].text_buf->get_size().y) * 0.5);
@@ -2637,30 +2634,22 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
26372634
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;
26382635

26392636
if (should_draw_pressed || should_draw_hovered) {
2640-
Point2 od = button_ofs;
2641-
if (rtl) {
2642-
od.x = get_size().width - od.x - button_size.x;
2643-
}
2637+
Point2 od = convert_rtl_position(button_ofs, button_size.x);
26442638
if (should_draw_pressed && should_draw_hovered) {
26452639
theme_cache.button_pressed->draw(get_canvas_item(), Rect2(od.x, od.y, button_size.width, MAX(button_size.height, label_h)));
26462640
} else {
26472641
theme_cache.button_hover->draw(get_canvas_item(), Rect2(od.x, od.y, button_size.width, MAX(button_size.height, label_h)));
26482642
}
26492643
}
26502644
if (selected_item == p_item && selected_col == i && selected_button == j) {
2651-
Point2 od = button_ofs;
2652-
if (rtl) {
2653-
od.x = get_size().width - od.x - button_size.x;
2654-
}
2645+
Point2 od = convert_rtl_position(button_ofs, button_size.x);
26552646
theme_cache.button_hover->draw(get_canvas_item(), Rect2(od.x, od.y, button_size.width, MAX(button_size.height, label_h)));
26562647
}
26572648

26582649
button_ofs.y += (label_h - button_size.height) / 2;
26592650
button_ofs += theme_cache.button_pressed->get_offset();
26602651

2661-
if (rtl) {
2662-
button_ofs.x = get_size().width - button_ofs.x - button_texture->get_width();
2663-
}
2652+
button_ofs = convert_rtl_position(button_ofs, button_texture->get_width());
26642653
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);
26652654
item_width_with_buttons -= button_size.width + theme_cache.button_margin;
26662655
}
@@ -2672,9 +2661,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
26722661
}
26732662

26742663
if (select_mode == SELECT_MULTI && selected_item == p_item && selected_col == i) {
2675-
if (is_layout_rtl()) {
2676-
cell_rect.position.x = get_size().width - cell_rect.position.x - cell_rect.size.x;
2677-
}
2664+
cell_rect = convert_rtl_rect(cell_rect);
26782665
if (has_focus(true)) {
26792666
theme_cache.cursor->draw(ci, cell_rect);
26802667
} else {
@@ -2689,7 +2676,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
26892676
Ref<Texture2D> arrow;
26902677

26912678
if (p_item->collapsed) {
2692-
if (is_layout_rtl()) {
2679+
if (rtl) {
26932680
arrow = theme_cache.arrow_collapsed_mirrored;
26942681
} else {
26952682
arrow = theme_cache.arrow_collapsed;
@@ -2710,9 +2697,9 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
27102697
}
27112698

27122699
if (arrow_draw_size.width > 0) {
2700+
apos = convert_rtl_position(apos, arrow_draw_size.width);
27132701
Point2 src_pos = Point2();
27142702
if (rtl) {
2715-
apos.x = get_size().width - apos.x - arrow_draw_size.width;
27162703
src_pos = Point2(arrow_full_size.width - arrow_draw_size.width, 0);
27172704
}
27182705
Rect2 arrow_rect = Rect2(apos, arrow_draw_size);
@@ -2768,10 +2755,8 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
27682755
int more_prev_ofs = 0;
27692756

27702757
if (root_pos.y + line_width >= 0) {
2771-
if (rtl) {
2772-
root_pos.x = get_size().width - root_pos.x;
2773-
parent_pos.x = get_size().width - parent_pos.x;
2774-
}
2758+
root_pos = convert_rtl_position(root_pos);
2759+
parent_pos = convert_rtl_position(parent_pos);
27752760
float parent_bottom_y = root_pos.y + parent_line_width * 0.5 + parent_line_pixel_shift;
27762761

27772762
// 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
@@ -548,6 +548,8 @@ class Tree : public Control {
548548

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

0 commit comments

Comments
 (0)