remote_transform_3d.cpp 8.4 KB

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