From 152572ac3862718e770b81c862970089810bcc34 Mon Sep 17 00:00:00 2001 From: Markus Sauermann <6299227+Sauermann@users.noreply.github.com> Date: Tue, 1 Aug 2023 01:55:31 +0200 Subject: [PATCH] Fix global transform validity for `Node2D` and `Control` Set global transform to invalid when changing transform --- scene/2d/node_2d.cpp | 8 ----- scene/gui/control.cpp | 4 +++ scene/main/canvas_item.cpp | 7 ++-- scene/main/canvas_item.h | 5 +-- tests/scene/test_control.h | 66 ++++++++++++++++++++++++++++++++++++++ tests/scene/test_node_2d.h | 63 ++++++++++++++++++++++++++++++++++++ tests/test_main.cpp | 2 ++ 7 files changed, 141 insertions(+), 14 deletions(-) create mode 100644 tests/scene/test_control.h create mode 100644 tests/scene/test_node_2d.h diff --git a/scene/2d/node_2d.cpp b/scene/2d/node_2d.cpp index 4b7fad5edf0..fb3b1996389 100644 --- a/scene/2d/node_2d.cpp +++ b/scene/2d/node_2d.cpp @@ -138,10 +138,6 @@ void Node2D::_update_transform() { RenderingServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), transform); - if (!is_inside_tree()) { - return; - } - _notify_transform(); } @@ -378,10 +374,6 @@ void Node2D::set_transform(const Transform2D &p_transform) { RenderingServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), transform); - if (!is_inside_tree()) { - return; - } - _notify_transform(); } diff --git a/scene/gui/control.cpp b/scene/gui/control.cpp index 0ca04faf9b1..63692dd0642 100644 --- a/scene/gui/control.cpp +++ b/scene/gui/control.cpp @@ -1734,6 +1734,10 @@ void Control::_size_changed() { if (pos_changed && !size_changed) { _update_canvas_item_transform(); //move because it won't be updated } + } else { + if (pos_changed) { + _notify_transform(); + } } } diff --git a/scene/main/canvas_item.cpp b/scene/main/canvas_item.cpp index 2947d73552f..855cad51e9c 100644 --- a/scene/main/canvas_item.cpp +++ b/scene/main/canvas_item.cpp @@ -320,7 +320,6 @@ void CanvasItem::_notification(int p_what) { } } - _set_global_invalid(true); _enter_canvas(); RenderingServer::get_singleton()->canvas_item_set_visible(canvas_item, is_visible_in_tree()); // The visibility of the parent may change. @@ -367,7 +366,11 @@ void CanvasItem::_notification(int p_what) { case NOTIFICATION_WORLD_2D_CHANGED: { _exit_canvas(); _enter_canvas(); - } + } break; + case NOTIFICATION_PARENTED: { + // The node is not inside the tree during this notification. + _notify_transform(); + } break; } } diff --git a/scene/main/canvas_item.h b/scene/main/canvas_item.h index 7f6bbb17c35..51591e1ac66 100644 --- a/scene/main/canvas_item.h +++ b/scene/main/canvas_item.h @@ -152,11 +152,8 @@ private: protected: _FORCE_INLINE_ void _notify_transform() { - if (!is_inside_tree()) { - return; - } _notify_transform(this); - if (!block_transform_notify && notify_local_transform) { + if (is_inside_tree() && !block_transform_notify && notify_local_transform) { notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED); } } diff --git a/tests/scene/test_control.h b/tests/scene/test_control.h new file mode 100644 index 00000000000..3d7e389e0a7 --- /dev/null +++ b/tests/scene/test_control.h @@ -0,0 +1,66 @@ +/**************************************************************************/ +/* test_control.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#ifndef TEST_CONTROL_H +#define TEST_CONTROL_H + +#include "scene/gui/control.h" + +#include "tests/test_macros.h" + +namespace TestControl { + +TEST_CASE("[SceneTree][Control]") { + SUBCASE("[Control][Global Transform] Global Transform should be accessible while not in SceneTree.") { // GH-79453 + Control *test_node = memnew(Control); + Control *test_child = memnew(Control); + test_node->add_child(test_child); + + test_node->set_global_position(Point2(1, 1)); + CHECK_EQ(test_node->get_global_position(), Point2(1, 1)); + CHECK_EQ(test_child->get_global_position(), Point2(1, 1)); + test_node->set_global_position(Point2(2, 2)); + CHECK_EQ(test_node->get_global_position(), Point2(2, 2)); + test_node->set_scale(Vector2(4, 4)); + CHECK_EQ(test_node->get_global_transform(), Transform2D(0, Size2(4, 4), 0, Vector2(2, 2))); + test_node->set_scale(Vector2(1, 1)); + test_node->set_rotation_degrees(90); + CHECK_EQ(test_node->get_global_transform(), Transform2D(Math_PI / 2, Vector2(2, 2))); + test_node->set_pivot_offset(Vector2(1, 0)); + CHECK_EQ(test_node->get_global_transform(), Transform2D(Math_PI / 2, Vector2(3, 1))); + + memdelete(test_child); + memdelete(test_node); + } +} + +} // namespace TestControl + +#endif // TEST_CONTROL_H diff --git a/tests/scene/test_node_2d.h b/tests/scene/test_node_2d.h new file mode 100644 index 00000000000..7e93c77e22f --- /dev/null +++ b/tests/scene/test_node_2d.h @@ -0,0 +1,63 @@ +/**************************************************************************/ +/* test_node_2d.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#ifndef TEST_NODE_2D_H +#define TEST_NODE_2D_H + +#include "scene/2d/node_2d.h" + +#include "tests/test_macros.h" + +namespace TestNode2D { + +TEST_CASE("[SceneTree][Node2D]") { + SUBCASE("[Node2D][Global Transform] Global Transform should be accessible while not in SceneTree.") { // GH-79453 + Node2D *test_node = memnew(Node2D); + test_node->set_name("node"); + Node2D *test_child = memnew(Node2D); + test_child->set_name("child"); + test_node->add_child(test_child); + + test_node->set_global_position(Point2(1, 1)); + CHECK_EQ(test_node->get_global_position(), Point2(1, 1)); + CHECK_EQ(test_child->get_global_position(), Point2(1, 1)); + test_node->set_global_position(Point2(2, 2)); + CHECK_EQ(test_node->get_global_position(), Point2(2, 2)); + test_node->set_global_transform(Transform2D(0, Point2(3, 3))); + CHECK_EQ(test_node->get_global_position(), Point2(3, 3)); + + memdelete(test_child); + memdelete(test_node); + } +} + +} // namespace TestNode2D + +#endif // TEST_NODE_2D_H diff --git a/tests/test_main.cpp b/tests/test_main.cpp index d775136e906..55d4f67b07e 100644 --- a/tests/test_main.cpp +++ b/tests/test_main.cpp @@ -95,6 +95,7 @@ #include "tests/scene/test_bit_map.h" #include "tests/scene/test_code_edit.h" #include "tests/scene/test_color_picker.h" +#include "tests/scene/test_control.h" #include "tests/scene/test_curve.h" #include "tests/scene/test_curve_2d.h" #include "tests/scene/test_curve_3d.h" @@ -106,6 +107,7 @@ #include "tests/scene/test_navigation_region_2d.h" #include "tests/scene/test_navigation_region_3d.h" #include "tests/scene/test_node.h" +#include "tests/scene/test_node_2d.h" #include "tests/scene/test_packed_scene.h" #include "tests/scene/test_path_2d.h" #include "tests/scene/test_path_3d.h"