Debian Codegraph @

scene/3d/remote_transform_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* remote_transform_3d.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_3d.h"
32
33 void RemoteTransform3D::_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 RemoteTransform3D::_update_remote() {
46 if (!is_inside_tree()) {
47 return;
48 }
49
50 if (cache.is_null()) {
51 return;
52 }
53
54 Node3D *target_node = ObjectDB::get_instance<Node3D>(cache);
55 if (!target_node) {
56 return;
57 }
58
59 if (!target_node->is_inside_tree()) {
60 return;
61 }
62
63 Transform3D our_trans = use_global_coordinates ? get_global_transform() : get_transform();
64
65 if (update_remote_position && update_remote_rotation && update_remote_scale) {
66 if (use_global_coordinates) {
67 target_node->set_global_transform(our_trans);
68 } else {
69 target_node->set_transform(our_trans);
70 }
71 } else {
72 Transform3D target_trans = use_global_coordinates ? target_node->get_global_transform() : target_node->get_transform();
73
74 if (update_remote_rotation && update_remote_scale) {
75 target_trans.basis = our_trans.basis;
76 } else if (update_remote_rotation) {
77 for (int i = 0; i < 3; i++) {
78 Vector3 our_col = our_trans.basis.get_column(i);
79 Vector3 target_col = target_trans.basis.get_column(i);
80 target_trans.basis.set_column(i, our_col.normalized() * target_col.length());
81 }
82 } else if (update_remote_scale) {
83 for (int i = 0; i < 3; i++) {
84 Vector3 our_col = our_trans.basis.get_column(i);
85 Vector3 target_col = target_trans.basis.get_column(i);
86 target_trans.basis.set_column(i, target_col.normalized() * our_col.length());
87 }
88 }
89
90 if (update_remote_position) {
91 target_trans.origin = our_trans.origin;
92 }
93
94 if (use_global_coordinates) {
95 target_node->set_global_transform(target_trans);
96 } else {
97 target_node->set_transform(target_trans);
98 }
99 }
100 }
101
102 void RemoteTransform3D::_notification(int p_what) {
103 switch (p_what) {
104 case NOTIFICATION_ENTER_TREE: {
105 _update_cache();
106 } break;
107
108 case NOTIFICATION_RESET_PHYSICS_INTERPOLATION: {
109 if (cache.is_valid()) {
110 _update_remote();
111 Node3D *n = ObjectDB::get_instance<Node3D>(cache);
112 if (n) {
113 n->reset_physics_interpolation();
114 }
115 }
116 } break;
117
118 case NOTIFICATION_LOCAL_TRANSFORM_CHANGED:
119 case NOTIFICATION_TRANSFORM_CHANGED: {
120 if (!is_inside_tree()) {
121 break;
122 }
123
124 if (cache.is_valid()) {
125 _update_remote();
126 }
127 } break;
128 }
129 }
130
131 void RemoteTransform3D::set_remote_node(const NodePath &p_remote_node) {
132 if (remote_node == p_remote_node) {
133 return;
134 }
135
136 remote_node = p_remote_node;
137 if (is_inside_tree()) {
138 _update_cache();
139 _update_remote();
140 }
141
142 update_configuration_warnings();
143 }
144
145 NodePath RemoteTransform3D::get_remote_node() const {
146 return remote_node;
147 }
148
149 void RemoteTransform3D::set_use_global_coordinates(const bool p_enable) {
150 if (use_global_coordinates == p_enable) {
151 return;
152 }
153
154 use_global_coordinates = p_enable;
155
156 set_notify_transform(use_global_coordinates);
157 set_notify_local_transform(!use_global_coordinates);
158 _update_remote();
159 }
160
161 bool RemoteTransform3D::get_use_global_coordinates() const {
162 return use_global_coordinates;
163 }
164
165 void RemoteTransform3D::set_update_position(const bool p_update) {
166 if (update_remote_position == p_update) {
167 return;
168 }
169 update_remote_position = p_update;
170 _update_remote();
171 }
172
173 bool RemoteTransform3D::get_update_position() const {
174 return update_remote_position;
175 }
176
177 void RemoteTransform3D::set_update_rotation(const bool p_update) {
178 if (update_remote_rotation == p_update) {
179 return;
180 }
181 update_remote_rotation = p_update;
182 _update_remote();
183 }
184
185 bool RemoteTransform3D::get_update_rotation() const {
186 return update_remote_rotation;
187 }
188
189 void RemoteTransform3D::set_update_scale(const bool p_update) {
190 if (update_remote_scale == p_update) {
191 return;
192 }
193 update_remote_scale = p_update;
194 _update_remote();
195 }
196
197 bool RemoteTransform3D::get_update_scale() const {
198 return update_remote_scale;
199 }
200
201 void RemoteTransform3D::force_update_cache() {
202 _update_cache();
203 }
204
205 PackedStringArray RemoteTransform3D::get_configuration_warnings() const {
206 PackedStringArray warnings = Node3D::get_configuration_warnings();
207
208 if (!has_node(remote_node) || !Object::cast_to<Node3D>(get_node(remote_node))) {
209 warnings.push_back(RTR("The \"Remote Path\" property must point to a valid Node3D or Node3D-derived node to work."));
210 }
211
212 return warnings;
213 }
214
215 void RemoteTransform3D::_bind_methods() {
216 ClassDB::bind_method(D_METHOD("set_remote_node", "path"), &RemoteTransform3D::set_remote_node);
217 ClassDB::bind_method(D_METHOD("get_remote_node"), &RemoteTransform3D::get_remote_node);
218 ClassDB::bind_method(D_METHOD("force_update_cache"), &RemoteTransform3D::force_update_cache);
219
220 ClassDB::bind_method(D_METHOD("set_use_global_coordinates", "use_global_coordinates"), &RemoteTransform3D::set_use_global_coordinates);
221 ClassDB::bind_method(D_METHOD("get_use_global_coordinates"), &RemoteTransform3D::get_use_global_coordinates);
222
223 ClassDB::bind_method(D_METHOD("set_update_position", "update_remote_position"), &RemoteTransform3D::set_update_position);
224 ClassDB::bind_method(D_METHOD("get_update_position"), &RemoteTransform3D::get_update_position);
225 ClassDB::bind_method(D_METHOD("set_update_rotation", "update_remote_rotation"), &RemoteTransform3D::set_update_rotation);
226 ClassDB::bind_method(D_METHOD("get_update_rotation"), &RemoteTransform3D::get_update_rotation);
227 ClassDB::bind_method(D_METHOD("set_update_scale", "update_remote_scale"), &RemoteTransform3D::set_update_scale);
228 ClassDB::bind_method(D_METHOD("get_update_scale"), &RemoteTransform3D::get_update_scale);
229
230 ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "remote_path", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node3D"), "set_remote_node", "get_remote_node");
231 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_global_coordinates"), "set_use_global_coordinates", "get_use_global_coordinates");
232
233 ADD_GROUP("Update", "update_");
234 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_position"), "set_update_position", "get_update_position");
235 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_rotation"), "set_update_rotation", "get_update_rotation");
236 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_scale"), "set_update_scale", "get_update_scale");
237 }
238
239 RemoteTransform3D::RemoteTransform3D() {
240 set_notify_transform(use_global_coordinates);
241 set_notify_local_transform(!use_global_coordinates);
242 }