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