Skip to content

Commit

Permalink
Merge pull request #80105 from Sauermann/fix-global-transform
Browse files Browse the repository at this point in the history
Fix global transform validity for `Node2D` and `Control`
  • Loading branch information
akien-mga committed Aug 28, 2023
2 parents b00796e + 152572a commit 2c0a74a
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 14 deletions.
8 changes: 0 additions & 8 deletions scene/2d/node_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down
4 changes: 4 additions & 0 deletions scene/gui/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

Expand Down
7 changes: 5 additions & 2 deletions scene/main/canvas_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}

Expand Down
5 changes: 1 addition & 4 deletions scene/main/canvas_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,8 @@ class CanvasItem : public Node {

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);
}
}
Expand Down
66 changes: 66 additions & 0 deletions tests/scene/test_control.h
Original file line number Diff line number Diff line change
@@ -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
63 changes: 63 additions & 0 deletions tests/scene/test_node_2d.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions tests/test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down

0 comments on commit 2c0a74a

Please sign in to comment.