From 0c6b6fe74959bf812de8d5f7b019c621bf62df9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E9=9D=92=E5=B1=B1?= Date: Sun, 21 Jan 2024 12:42:21 +0800 Subject: [PATCH] Fix issue where `set_global_position(global_position)` in `Control` resulted in a different result than `global_position` Previously, the case where the pivot point was not at the origin was ignored. --- scene/gui/control.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/scene/gui/control.cpp b/scene/gui/control.cpp index 349102fafbe..a7be5c9af0c 100644 --- a/scene/gui/control.cpp +++ b/scene/gui/control.cpp @@ -1419,13 +1419,15 @@ 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 inv; - if (data.parent_canvas_item) { - inv = data.parent_canvas_item->get_global_transform().affine_inverse(); + Transform2D global_transform_cache = get_global_transform(); + if (p_point == global_transform_cache.get_origin()) { + return; // Edge case, but avoids calculation. } - set_position(inv.xform(p_point), p_keep_offsets); + Point2 internal_position = global_transform_cache.affine_inverse().xform(p_point); + + set_position(internal_position + data.pos_cache, p_keep_offsets); } Point2 Control::get_global_position() const {