From f8cc7893d0821c1de8031e64c5eb73bdb5d176dc Mon Sep 17 00:00:00 2001 From: John Pennycook Date: Sun, 27 Sep 2020 10:58:54 -0700 Subject: [PATCH] Make YSort stable Keeps track of the order in which items are collected by _collect_ysort_children, and uses that order to break ties between items with similar Y positions. (cherry picked from commit 8d3afa985b479417b46811050e9b8bd9f7861f06) --- servers/visual/visual_server_canvas.cpp | 1 + servers/visual/visual_server_canvas.h | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/servers/visual/visual_server_canvas.cpp b/servers/visual/visual_server_canvas.cpp index f386ca66421..4fe1e62c07c 100644 --- a/servers/visual/visual_server_canvas.cpp +++ b/servers/visual/visual_server_canvas.cpp @@ -62,6 +62,7 @@ void _collect_ysort_children(VisualServerCanvas::Item *p_canvas_item, Transform2 child_items[i]->ysort_xform = p_transform; child_items[i]->ysort_pos = p_transform.xform(child_items[i]->xform.elements[2]); child_items[i]->material_owner = child_items[i]->use_parent_material ? p_material_owner : NULL; + child_items[i]->ysort_index = r_index; } r_index++; diff --git a/servers/visual/visual_server_canvas.h b/servers/visual/visual_server_canvas.h index 2303ec03db0..abdb9a8e19e 100644 --- a/servers/visual/visual_server_canvas.h +++ b/servers/visual/visual_server_canvas.h @@ -52,6 +52,7 @@ public: Color ysort_modulate; Transform2D ysort_xform; Vector2 ysort_pos; + int ysort_index; Vector child_items; @@ -68,6 +69,7 @@ public: ysort_children_count = -1; ysort_xform = Transform2D(); ysort_pos = Vector2(); + ysort_index = 0; } }; @@ -83,8 +85,9 @@ public: _FORCE_INLINE_ bool operator()(const Item *p_left, const Item *p_right) const { - if (Math::is_equal_approx(p_left->ysort_pos.y, p_right->ysort_pos.y)) - return p_left->ysort_pos.x < p_right->ysort_pos.x; + if (Math::is_equal_approx(p_left->ysort_pos.y, p_right->ysort_pos.y)) { + return p_left->ysort_index < p_right->ysort_index; + } return p_left->ysort_pos.y < p_right->ysort_pos.y; }