remote_transform_3d.cpp 8.4 KB

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