Fix global transform validity for `Node2D` and `Control`

Set global transform to invalid when changing transform
This commit is contained in:
Markus Sauermann 2023-08-01 01:55:31 +02:00
parent eca6f0eb54
commit 152572ac38
7 changed files with 141 additions and 14 deletions

View File

@ -138,10 +138,6 @@ void Node2D::_update_transform() {
RenderingServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), transform); RenderingServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), transform);
if (!is_inside_tree()) {
return;
}
_notify_transform(); _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); RenderingServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), transform);
if (!is_inside_tree()) {
return;
}
_notify_transform(); _notify_transform();
} }

View File

@ -1734,6 +1734,10 @@ void Control::_size_changed() {
if (pos_changed && !size_changed) { if (pos_changed && !size_changed) {
_update_canvas_item_transform(); //move because it won't be updated _update_canvas_item_transform(); //move because it won't be updated
} }
} else {
if (pos_changed) {
_notify_transform();
}
} }
} }

View File

@ -320,7 +320,6 @@ void CanvasItem::_notification(int p_what) {
} }
} }
_set_global_invalid(true);
_enter_canvas(); _enter_canvas();
RenderingServer::get_singleton()->canvas_item_set_visible(canvas_item, is_visible_in_tree()); // The visibility of the parent may change. 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: { case NOTIFICATION_WORLD_2D_CHANGED: {
_exit_canvas(); _exit_canvas();
_enter_canvas(); _enter_canvas();
} } break;
case NOTIFICATION_PARENTED: {
// The node is not inside the tree during this notification.
_notify_transform();
} break;
} }
} }

View File

@ -152,11 +152,8 @@ private:
protected: protected:
_FORCE_INLINE_ void _notify_transform() { _FORCE_INLINE_ void _notify_transform() {
if (!is_inside_tree()) {
return;
}
_notify_transform(this); _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); notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
} }
} }

View File

@ -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

View File

@ -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

View File

@ -95,6 +95,7 @@
#include "tests/scene/test_bit_map.h" #include "tests/scene/test_bit_map.h"
#include "tests/scene/test_code_edit.h" #include "tests/scene/test_code_edit.h"
#include "tests/scene/test_color_picker.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.h"
#include "tests/scene/test_curve_2d.h" #include "tests/scene/test_curve_2d.h"
#include "tests/scene/test_curve_3d.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_2d.h"
#include "tests/scene/test_navigation_region_3d.h" #include "tests/scene/test_navigation_region_3d.h"
#include "tests/scene/test_node.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_packed_scene.h"
#include "tests/scene/test_path_2d.h" #include "tests/scene/test_path_2d.h"
#include "tests/scene/test_path_3d.h" #include "tests/scene/test_path_3d.h"