| 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) || 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 *n = Object::cast_to<Node3D>(ObjectDB::get_instance(cache)); |
| 55 | if (!n) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | if (!n->is_inside_tree()) { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | //todo make faster |
| 64 | if (use_global_coordinates) { |
| 65 | if (update_remote_position && update_remote_rotation && update_remote_scale) { |
| 66 | n->set_global_transform(get_global_transform()); |
| 67 | } else { |
| 68 | Transform3D our_trans = get_global_transform(); |
| 69 | |
| 70 | if (update_remote_rotation) { |
| 71 | n->set_rotation(our_trans.basis.get_euler_normalized(EulerOrder(n->get_rotation_order()))); |
| 72 | } |
| 73 | |
| 74 | if (update_remote_scale) { |
| 75 | n->set_scale(our_trans.basis.get_scale()); |
| 76 | } |
| 77 | |
| 78 | if (update_remote_position) { |
| 79 | Transform3D n_trans = n->get_global_transform(); |
| 80 | |
| 81 | n_trans.set_origin(our_trans.get_origin()); |
| 82 | n->set_global_transform(n_trans); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | } else { |
| 87 | if (update_remote_position && update_remote_rotation && update_remote_scale) { |
| 88 | n->set_transform(get_transform()); |
| 89 | } else { |
| 90 | Transform3D our_trans = get_transform(); |
| 91 | |
| 92 | if (update_remote_rotation) { |
| 93 | n->set_rotation(our_trans.basis.get_euler_normalized(EulerOrder(n->get_rotation_order()))); |
| 94 | } |
| 95 | |
| 96 | if (update_remote_scale) { |
| 97 | n->set_scale(our_trans.basis.get_scale()); |
| 98 | } |
| 99 | |
| 100 | if (update_remote_position) { |
| 101 | Transform3D n_trans = n->get_transform(); |
| 102 | |
| 103 | n_trans.set_origin(our_trans.get_origin()); |
| 104 | n->set_transform(n_trans); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void RemoteTransform3D::_notification(int p_what) { |
| 111 | switch (p_what) { |
| 112 | case NOTIFICATION_ENTER_TREE: { |
| 113 | _update_cache(); |
| 114 | } break; |
| 115 | |
| 116 | case NOTIFICATION_TRANSFORM_CHANGED: { |
| 117 | if (!is_inside_tree()) { |
| 118 | break; |
| 119 | } |
| 120 | |
| 121 | if (cache.is_valid()) { |
| 122 | _update_remote(); |
| 123 | } |
| 124 | } break; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void RemoteTransform3D::set_remote_node(const NodePath &p_remote_node) { |
| 129 | remote_node = p_remote_node; |
| 130 | if (is_inside_tree()) { |
| 131 | _update_cache(); |
| 132 | _update_remote(); |
| 133 | } |
| 134 | |
| 135 | update_configuration_warnings(); |
| 136 | } |
| 137 | |
| 138 | NodePath RemoteTransform3D::get_remote_node() const { |
| 139 | return remote_node; |
| 140 | } |
| 141 | |
| 142 | void RemoteTransform3D::set_use_global_coordinates(const bool p_enable) { |
| 143 | use_global_coordinates = p_enable; |
| 144 | } |
| 145 | |
| 146 | bool RemoteTransform3D::get_use_global_coordinates() const { |
| 147 | return use_global_coordinates; |
| 148 | } |
| 149 | |
| 150 | void RemoteTransform3D::set_update_position(const bool p_update) { |
| 151 | update_remote_position = p_update; |
| 152 | _update_remote(); |
| 153 | } |
| 154 | |
| 155 | bool RemoteTransform3D::get_update_position() const { |
| 156 | return update_remote_position; |
| 157 | } |
| 158 | |
| 159 | void RemoteTransform3D::set_update_rotation(const bool p_update) { |
| 160 | update_remote_rotation = p_update; |
| 161 | _update_remote(); |
| 162 | } |
| 163 | |
| 164 | bool RemoteTransform3D::get_update_rotation() const { |
| 165 | return update_remote_rotation; |
| 166 | } |
| 167 | |
| 168 | void RemoteTransform3D::set_update_scale(const bool p_update) { |
| 169 | update_remote_scale = p_update; |
| 170 | _update_remote(); |
| 171 | } |
| 172 | |
| 173 | bool RemoteTransform3D::get_update_scale() const { |
| 174 | return update_remote_scale; |
| 175 | } |
| 176 | |
| 177 | void RemoteTransform3D::force_update_cache() { |
| 178 | _update_cache(); |
| 179 | } |
| 180 | |
| 181 | PackedStringArray RemoteTransform3D::get_configuration_warnings() const { |
| 182 | PackedStringArray warnings = Node::get_configuration_warnings(); |
| 183 | |
| 184 | if (!has_node(remote_node) || !Object::cast_to<Node3D>(get_node(remote_node))) { |
| 185 | warnings.push_back(RTR("The \"Remote Path\" property must point to a valid Node3D or Node3D-derived node to work." )); |
| 186 | } |
| 187 | |
| 188 | return warnings; |
| 189 | } |
| 190 | |
| 191 | void RemoteTransform3D::_bind_methods() { |
| 192 | ClassDB::bind_method(D_METHOD("set_remote_node" , "path" ), &RemoteTransform3D::set_remote_node); |
| 193 | ClassDB::bind_method(D_METHOD("get_remote_node" ), &RemoteTransform3D::get_remote_node); |
| 194 | ClassDB::bind_method(D_METHOD("force_update_cache" ), &RemoteTransform3D::force_update_cache); |
| 195 | |
| 196 | ClassDB::bind_method(D_METHOD("set_use_global_coordinates" , "use_global_coordinates" ), &RemoteTransform3D::set_use_global_coordinates); |
| 197 | ClassDB::bind_method(D_METHOD("get_use_global_coordinates" ), &RemoteTransform3D::get_use_global_coordinates); |
| 198 | |
| 199 | ClassDB::bind_method(D_METHOD("set_update_position" , "update_remote_position" ), &RemoteTransform3D::set_update_position); |
| 200 | ClassDB::bind_method(D_METHOD("get_update_position" ), &RemoteTransform3D::get_update_position); |
| 201 | ClassDB::bind_method(D_METHOD("set_update_rotation" , "update_remote_rotation" ), &RemoteTransform3D::set_update_rotation); |
| 202 | ClassDB::bind_method(D_METHOD("get_update_rotation" ), &RemoteTransform3D::get_update_rotation); |
| 203 | ClassDB::bind_method(D_METHOD("set_update_scale" , "update_remote_scale" ), &RemoteTransform3D::set_update_scale); |
| 204 | ClassDB::bind_method(D_METHOD("get_update_scale" ), &RemoteTransform3D::get_update_scale); |
| 205 | |
| 206 | ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "remote_path" , PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node3D" ), "set_remote_node" , "get_remote_node" ); |
| 207 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_global_coordinates" ), "set_use_global_coordinates" , "get_use_global_coordinates" ); |
| 208 | |
| 209 | ADD_GROUP("Update" , "update_" ); |
| 210 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_position" ), "set_update_position" , "get_update_position" ); |
| 211 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_rotation" ), "set_update_rotation" , "get_update_rotation" ); |
| 212 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_scale" ), "set_update_scale" , "get_update_scale" ); |
| 213 | } |
| 214 | |
| 215 | RemoteTransform3D::RemoteTransform3D() { |
| 216 | set_notify_transform(true); |
| 217 | } |
| 218 | |