Debian Codegraph @

scene/2d/remote_transform_2d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* remote_transform_2d.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "remote_transform_2d.h"
32
33 void RemoteTransform2D::_update_cache() {
34 cache = ObjectID();
35 if (has_node(remote_node)) {
36 Node *node = get_node(remote_node);
37 if (!node || this == node || node->is_ancestor_of(this) || is_ancestor_of(node)) {
38 return;
39 }
40
41 cache = node->get_instance_id();
42 }
43 }
44
45 void RemoteTransform2D::_update_remote() {
46 if (!is_inside_tree()) {
47 return;
48 }
49
50 if (cache.is_null()) {
51 return;
52 }
53
54 Node2D *n = ObjectDB::get_instance<Node2D>(cache);
55 if (!n) {
56 return;
57 }
58
59 if (!n->is_inside_tree()) {
60 return;
61 }
62
63 if (!(update_remote_position || update_remote_rotation || update_remote_scale)) {
64 return; // The transform data of the RemoteTransform2D is not used at all.
65 }
66
67 //todo make faster
68 if (use_global_coordinates) {
69 if (update_remote_position && update_remote_rotation && update_remote_scale) {
70 n->set_global_transform(get_global_transform());
71 return;
72 }
73
74 Transform2D n_trans = n->get_global_transform();
75 Transform2D our_trans = get_global_transform();
76
77 // There are more steps in the operation of set_rotation, so avoid calling it.
78 Transform2D trans = update_remote_rotation ? our_trans : n_trans;
79
80 if (update_remote_rotation ^ update_remote_position) {
81 trans.set_origin(update_remote_position ? our_trans.get_origin() : n_trans.get_origin());
82 }
83 if (update_remote_rotation ^ update_remote_scale) {
84 trans.set_scale(update_remote_scale ? our_trans.get_scale() : n_trans.get_scale());
85 }
86
87 n->set_global_transform(trans);
88 } else {
89 if (update_remote_position && update_remote_rotation && update_remote_scale) {
90 n->set_transform(get_transform());
91 return;
92 }
93
94 Transform2D n_trans = n->get_transform();
95 Transform2D our_trans = get_transform();
96
97 // There are more steps in the operation of set_rotation, so avoid calling it.
98 Transform2D trans = update_remote_rotation ? our_trans : n_trans;
99
100 if (update_remote_rotation ^ update_remote_position) {
101 trans.set_origin(update_remote_position ? our_trans.get_origin() : n_trans.get_origin());
102 }
103 if (update_remote_rotation ^ update_remote_scale) {
104 trans.set_scale(update_remote_scale ? our_trans.get_scale() : n_trans.get_scale());
105 }
106
107 n->set_transform(trans);
108 }
109 }
110
111 void RemoteTransform2D::_notification(int p_what) {
112 switch (p_what) {
113 case NOTIFICATION_ENTER_TREE: {
114 _update_cache();
115 } break;
116
117 case NOTIFICATION_RESET_PHYSICS_INTERPOLATION: {
118 if (cache.is_valid()) {
119 _update_remote();
120 Node2D *n = ObjectDB::get_instance<Node2D>(cache);
121 if (n) {
122 n->reset_physics_interpolation();
123 }
124 }
125 } break;
126
127 case NOTIFICATION_LOCAL_TRANSFORM_CHANGED:
128 case NOTIFICATION_TRANSFORM_CHANGED: {
129 if (!is_inside_tree()) {
130 break;
131 }
132
133 if (cache.is_valid()) {
134 _update_remote();
135 }
136 } break;
137 }
138 }
139
140 void RemoteTransform2D::set_remote_node(const NodePath &p_remote_node) {
141 if (remote_node == p_remote_node) {
142 return;
143 }
144
145 remote_node = p_remote_node;
146 if (is_inside_tree()) {
147 _update_cache();
148 _update_remote();
149 }
150
151 update_configuration_warnings();
152 }
153
154 NodePath RemoteTransform2D::get_remote_node() const {
155 return remote_node;
156 }
157
158 void RemoteTransform2D::set_use_global_coordinates(const bool p_enable) {
159 if (use_global_coordinates == p_enable) {
160 return;
161 }
162
163 use_global_coordinates = p_enable;
164 set_notify_transform(use_global_coordinates);
165 set_notify_local_transform(!use_global_coordinates);
166 _update_remote();
167 }
168
169 bool RemoteTransform2D::get_use_global_coordinates() const {
170 return use_global_coordinates;
171 }
172
173 void RemoteTransform2D::set_update_position(const bool p_update) {
174 if (update_remote_position == p_update) {
175 return;
176 }
177 update_remote_position = p_update;
178 _update_remote();
179 }
180
181 bool RemoteTransform2D::get_update_position() const {
182 return update_remote_position;
183 }
184
185 void RemoteTransform2D::set_update_rotation(const bool p_update) {
186 if (update_remote_rotation == p_update) {
187 return;
188 }
189 update_remote_rotation = p_update;
190 _update_remote();
191 }
192
193 bool RemoteTransform2D::get_update_rotation() const {
194 return update_remote_rotation;
195 }
196
197 void RemoteTransform2D::set_update_scale(const bool p_update) {
198 if (update_remote_scale == p_update) {
199 return;
200 }
201 update_remote_scale = p_update;
202 _update_remote();
203 }
204
205 bool RemoteTransform2D::get_update_scale() const {
206 return update_remote_scale;
207 }
208
209 void RemoteTransform2D::force_update_cache() {
210 _update_cache();
211 }
212
213 PackedStringArray RemoteTransform2D::get_configuration_warnings() const {
214 PackedStringArray warnings = Node2D::get_configuration_warnings();
215
216 if (!has_node(remote_node) || !Object::cast_to<Node2D>(get_node(remote_node))) {
217 warnings.push_back(RTR("Path property must point to a valid Node2D node to work."));
218 }
219
220 return warnings;
221 }
222
223 void RemoteTransform2D::_bind_methods() {
224 ClassDB::bind_method(D_METHOD("set_remote_node", "path"), &RemoteTransform2D::set_remote_node);
225 ClassDB::bind_method(D_METHOD("get_remote_node"), &RemoteTransform2D::get_remote_node);
226 ClassDB::bind_method(D_METHOD("force_update_cache"), &RemoteTransform2D::force_update_cache);
227
228 ClassDB::bind_method(D_METHOD("set_use_global_coordinates", "use_global_coordinates"), &RemoteTransform2D::set_use_global_coordinates);
229 ClassDB::bind_method(D_METHOD("get_use_global_coordinates"), &RemoteTransform2D::get_use_global_coordinates);
230
231 ClassDB::bind_method(D_METHOD("set_update_position", "update_remote_position"), &RemoteTransform2D::set_update_position);
232 ClassDB::bind_method(D_METHOD("get_update_position"), &RemoteTransform2D::get_update_position);
233 ClassDB::bind_method(D_METHOD("set_update_rotation", "update_remote_rotation"), &RemoteTransform2D::set_update_rotation);
234 ClassDB::bind_method(D_METHOD("get_update_rotation"), &RemoteTransform2D::get_update_rotation);
235 ClassDB::bind_method(D_METHOD("set_update_scale", "update_remote_scale"), &RemoteTransform2D::set_update_scale);
236 ClassDB::bind_method(D_METHOD("get_update_scale"), &RemoteTransform2D::get_update_scale);
237
238 ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "remote_path", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node2D"), "set_remote_node", "get_remote_node");
239 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_global_coordinates"), "set_use_global_coordinates", "get_use_global_coordinates");
240
241 ADD_GROUP("Update", "update_");
242 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_position"), "set_update_position", "get_update_position");
243 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_rotation"), "set_update_rotation", "get_update_rotation");
244 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_scale"), "set_update_scale", "get_update_scale");
245 }
246
247 RemoteTransform2D::RemoteTransform2D() {
248 set_notify_transform(use_global_coordinates);
249 set_notify_local_transform(!use_global_coordinates);
250 set_hide_clip_children(true);
251 }