Skip to content

Commit

Permalink
Make YSort stable
Browse files Browse the repository at this point in the history
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 8d3afa9)
  • Loading branch information
Pennycook authored and akien-mga committed Sep 29, 2020
1 parent fbafd5c commit f8cc789
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions servers/visual/visual_server_canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down
7 changes: 5 additions & 2 deletions servers/visual/visual_server_canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class VisualServerCanvas {
Color ysort_modulate;
Transform2D ysort_xform;
Vector2 ysort_pos;
int ysort_index;

Vector<Item *> child_items;

Expand All @@ -68,6 +69,7 @@ class VisualServerCanvas {
ysort_children_count = -1;
ysort_xform = Transform2D();
ysort_pos = Vector2();
ysort_index = 0;
}
};

Expand All @@ -83,8 +85,9 @@ class VisualServerCanvas {

_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;
}
Expand Down

0 comments on commit f8cc789

Please sign in to comment.