Skip to content

Commit

Permalink
Fix Tree relationship line 1px width draw bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
Koyper committed Oct 26, 2024
1 parent ee363af commit 5332c0a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
35 changes: 20 additions & 15 deletions scene/gui/tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2532,7 +2532,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
if (theme_cache.draw_relationship_lines > 0 && (!hide_root || c->parent != root) && c->is_visible_in_tree()) {
int root_ofs = children_pos.x + ((p_item->disable_folding || hide_folding) ? theme_cache.h_separation : theme_cache.item_margin);
int parent_ofs = p_pos.x + theme_cache.item_margin;
Point2i root_pos = Point2i(root_ofs, children_pos.y + child_self_height / 2) - theme_cache.offset + p_draw_ofs;
Point2i root_pos = Point2i(root_ofs, children_pos.y + child_self_height * 0.5) - theme_cache.offset + p_draw_ofs;

if (c->get_visible_child_count() > 0) {
root_pos -= Point2i(theme_cache.arrow->get_width(), 0);
Expand All @@ -2542,7 +2542,11 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
float parent_line_width = theme_cache.parent_hl_line_width * Math::round(theme_cache.base_scale);
float children_line_width = theme_cache.children_hl_line_width * Math::round(theme_cache.base_scale);

Point2i parent_pos = Point2i(parent_ofs - theme_cache.arrow->get_width() / 2, p_pos.y + label_h / 2 + theme_cache.arrow->get_height() / 2) - theme_cache.offset + p_draw_ofs;
float line_pixel_shift = int(Math::round(line_width * content_scale_factor)) % 2 == 0 ? 0.0 : 0.5;
float parent_line_pixel_shift = int(Math::round(parent_line_width * content_scale_factor)) % 2 == 0 ? 0.0 : 0.5;
float children_line_pixel_shift = int(Math::round(children_line_width * content_scale_factor)) % 2 == 0 ? 0.0 : 0.5;

Point2i parent_pos = Point2i(parent_ofs - theme_cache.arrow->get_width() * 0.5, p_pos.y + label_h * 0.5 + theme_cache.arrow->get_height() * 0.5) - theme_cache.offset + p_draw_ofs;

int more_prev_ofs = 0;

Expand All @@ -2556,43 +2560,43 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
if (_is_branch_selected(c)) {
// If this item or one of its children is selected, we draw the line using parent highlight style.
if (htotal >= 0) {
RenderingServer::get_singleton()->canvas_item_add_line(ci, root_pos, Point2i(parent_pos.x + Math::floor(parent_line_width / 2), root_pos.y), theme_cache.parent_hl_line_color, parent_line_width);
RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(root_pos.x, root_pos.y + parent_line_pixel_shift), Point2(parent_pos.x + Math::floor(parent_line_width * 0.5), root_pos.y + parent_line_pixel_shift), theme_cache.parent_hl_line_color, parent_line_width);
}
RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2i(parent_pos.x, root_pos.y + Math::floor(parent_line_width / 2)), Point2i(parent_pos.x, prev_hl_ofs), theme_cache.parent_hl_line_color, parent_line_width);
RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(parent_pos.x + parent_line_pixel_shift, root_pos.y + Math::floor(parent_line_width * 0.5) + parent_line_pixel_shift), Point2(parent_pos.x + parent_line_pixel_shift, prev_hl_ofs), theme_cache.parent_hl_line_color, parent_line_width);

more_prev_ofs = theme_cache.parent_hl_line_margin;
prev_hl_ofs = root_pos.y + Math::floor(parent_line_width / 2);
prev_hl_ofs = root_pos.y + Math::floor(parent_line_width * 0.5);
} else if (p_item->is_selected(0)) {
// If parent item is selected (but this item is not), we draw the line using children highlight style.
// Siblings of the selected branch can be drawn with a slight offset and their vertical line must appear as highlighted.
if (_is_sibling_branch_selected(c)) {
if (htotal >= 0) {
RenderingServer::get_singleton()->canvas_item_add_line(ci, root_pos, Point2i(parent_pos.x + Math::floor(parent_line_width / 2), root_pos.y), theme_cache.children_hl_line_color, children_line_width);
RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(root_pos.x, root_pos.y + children_line_pixel_shift), Point2i(parent_pos.x + Math::floor(parent_line_width * 0.5), root_pos.y + children_line_pixel_shift), theme_cache.children_hl_line_color, children_line_width);
}
RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2i(parent_pos.x, root_pos.y + Math::floor(parent_line_width / 2)), Point2i(parent_pos.x, prev_hl_ofs), theme_cache.parent_hl_line_color, parent_line_width);
RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(parent_pos.x + parent_line_pixel_shift, root_pos.y + Math::floor(parent_line_width * 0.5) + parent_line_pixel_shift), Point2(parent_pos.x + parent_line_pixel_shift, prev_hl_ofs), theme_cache.parent_hl_line_color, parent_line_width);

prev_hl_ofs = root_pos.y + Math::floor(parent_line_width / 2);
prev_hl_ofs = root_pos.y + Math::floor(parent_line_width * 0.5);
} else {
if (htotal >= 0) {
RenderingServer::get_singleton()->canvas_item_add_line(ci, root_pos, Point2i(parent_pos.x + Math::floor(children_line_width / 2), root_pos.y), theme_cache.children_hl_line_color, children_line_width);
RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(root_pos.x, root_pos.y + children_line_pixel_shift), Point2(parent_pos.x + Math::floor(children_line_width * 0.5), root_pos.y + children_line_pixel_shift), theme_cache.children_hl_line_color, children_line_width);
}
RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2i(parent_pos.x, root_pos.y + Math::floor(children_line_width / 2)), Point2i(parent_pos.x, prev_ofs + Math::floor(children_line_width / 2)), theme_cache.children_hl_line_color, children_line_width);
RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(parent_pos.x + children_line_pixel_shift, root_pos.y + Math::floor(children_line_width * 0.5) + children_line_pixel_shift), Point2(parent_pos.x + children_line_pixel_shift, prev_ofs + Math::floor(children_line_width * 0.5)), theme_cache.children_hl_line_color, children_line_width);
}
} else {
// If nothing of the above is true, we draw the line using normal style.
// Siblings of the selected branch can be drawn with a slight offset and their vertical line must appear as highlighted.
if (_is_sibling_branch_selected(c)) {
if (htotal >= 0) {
RenderingServer::get_singleton()->canvas_item_add_line(ci, root_pos, Point2i(parent_pos.x + theme_cache.parent_hl_line_margin, root_pos.y), theme_cache.relationship_line_color, line_width);
RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(root_pos.x, root_pos.y + line_pixel_shift), Point2(parent_pos.x + theme_cache.parent_hl_line_margin, root_pos.y + line_pixel_shift), theme_cache.relationship_line_color, line_width);
}
RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2i(parent_pos.x, root_pos.y + Math::floor(parent_line_width / 2)), Point2i(parent_pos.x, prev_hl_ofs), theme_cache.parent_hl_line_color, parent_line_width);
RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(parent_pos.x + parent_line_pixel_shift, root_pos.y + Math::floor(parent_line_width * 0.5) + parent_line_pixel_shift), Point2(parent_pos.x + parent_line_pixel_shift, prev_hl_ofs), theme_cache.parent_hl_line_color, parent_line_width);

prev_hl_ofs = root_pos.y + Math::floor(parent_line_width / 2);
prev_hl_ofs = root_pos.y + Math::floor(parent_line_width * 0.5);
} else {
if (htotal >= 0) {
RenderingServer::get_singleton()->canvas_item_add_line(ci, root_pos, Point2i(parent_pos.x + Math::floor(line_width / 2), root_pos.y), theme_cache.relationship_line_color, line_width);
RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(root_pos.x, root_pos.y + line_pixel_shift), Point2(parent_pos.x + Math::floor(line_width * 0.5), root_pos.y + line_pixel_shift), theme_cache.relationship_line_color, line_width);
}
RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2i(parent_pos.x, root_pos.y + Math::floor(line_width / 2)), Point2i(parent_pos.x, prev_ofs + Math::floor(line_width / 2)), theme_cache.relationship_line_color, line_width);
RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(parent_pos.x + line_pixel_shift, root_pos.y + Math::floor(line_width * 0.5) + line_pixel_shift), Point2(parent_pos.x + line_pixel_shift, prev_ofs + Math::floor(line_width * 0.5)), theme_cache.relationship_line_color, line_width);
}
}
}
Expand Down Expand Up @@ -4333,6 +4337,7 @@ void Tree::_notification(int p_what) {
draw_size.y -= tbh;

cache.rtl = is_layout_rtl();
content_scale_factor = popup_editor->is_embedded() ? 1.0 : popup_editor->get_parent_visible_window()->get_content_scale_factor();

if (root && get_size().x > 0 && get_size().y > 0) {
int self_height = 0; // Just to pass a reference, we don't need the root's `self_height`.
Expand Down
1 change: 1 addition & 0 deletions scene/gui/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ class Tree : public Control {
Vector2 range_drag_capture_pos;

bool propagate_mouse_activated = false;
float content_scale_factor = 0.0;

//TreeItem *cursor_item;
//int cursor_column;
Expand Down
6 changes: 3 additions & 3 deletions scene/theme/default_theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -844,9 +844,9 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const
theme->set_constant("inner_item_margin_top", "Tree", 0);
theme->set_constant("button_margin", "Tree", Math::round(4 * scale));
theme->set_constant("draw_relationship_lines", "Tree", 0);
theme->set_constant("relationship_line_width", "Tree", 1);
theme->set_constant("parent_hl_line_width", "Tree", 1);
theme->set_constant("children_hl_line_width", "Tree", 1);
theme->set_constant("relationship_line_width", "Tree", 2);
theme->set_constant("parent_hl_line_width", "Tree", 2);
theme->set_constant("children_hl_line_width", "Tree", 2);
theme->set_constant("parent_hl_line_margin", "Tree", 0);
theme->set_constant("draw_guides", "Tree", 1);
theme->set_constant("scroll_border", "Tree", Math::round(4 * scale));
Expand Down

0 comments on commit 5332c0a

Please sign in to comment.