Skip to content

Commit

Permalink
Merge pull request #89502 from kleonc/control-set-global-position-fix
Browse files Browse the repository at this point in the history
Fix `Control::set_global_position` for rotated/scaled transforms
  • Loading branch information
akien-mga committed Mar 15, 2024
2 parents 9c2db0c + f08e782 commit c7061d5
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions scene/gui/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1406,15 +1406,11 @@ void Control::_set_global_position(const Point2 &p_point) {

void Control::set_global_position(const Point2 &p_point, bool p_keep_offsets) {
ERR_MAIN_THREAD_GUARD;

Transform2D global_transform_cache = get_global_transform();
if (p_point == global_transform_cache.get_origin()) {
return; // Edge case, but avoids calculation.
}

Point2 internal_position = global_transform_cache.affine_inverse().xform(p_point);

set_position(internal_position + data.pos_cache, p_keep_offsets);
// (parent_global_transform * T(new_position) * internal_transform).origin == new_global_position
// (T(new_position) * internal_transform).origin == new_position_in_parent_space
// new_position == new_position_in_parent_space - internal_transform.origin
Point2 position_in_parent_space = data.parent_canvas_item ? data.parent_canvas_item->get_global_transform().affine_inverse().xform(p_point) : p_point;
set_position(position_in_parent_space - _get_internal_transform().get_origin(), p_keep_offsets);
}

Point2 Control::get_global_position() const {
Expand Down

0 comments on commit c7061d5

Please sign in to comment.