remote_transform.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*************************************************************************/
  2. /* remote_transform.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  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.h"
  31. void RemoteTransform::_update_cache() {
  32. cache = 0;
  33. if (has_node(remote_node)) {
  34. Node *node = get_node(remote_node);
  35. if (!node || this == node || node->is_a_parent_of(this) || this->is_a_parent_of(node)) {
  36. return;
  37. }
  38. cache = node->get_instance_id();
  39. }
  40. }
  41. void RemoteTransform::_update_remote() {
  42. if (!is_inside_tree())
  43. return;
  44. if (!cache)
  45. return;
  46. Spatial *n = Object::cast_to<Spatial>(ObjectDB::get_instance(cache));
  47. if (!n)
  48. return;
  49. if (!n->is_inside_tree())
  50. return;
  51. //todo make faster
  52. if (use_global_coordinates) {
  53. if (update_remote_position && update_remote_rotation && update_remote_scale) {
  54. n->set_global_transform(get_global_transform());
  55. } else {
  56. Transform n_trans = n->get_global_transform();
  57. Transform our_trans = get_global_transform();
  58. if (!update_remote_position)
  59. our_trans.set_origin(n_trans.get_origin());
  60. n->set_global_transform(our_trans);
  61. if (!update_remote_rotation)
  62. n->set_rotation(n_trans.basis.get_rotation());
  63. if (!update_remote_scale)
  64. n->set_scale(n_trans.basis.get_scale());
  65. }
  66. } else {
  67. if (update_remote_position && update_remote_rotation && update_remote_scale) {
  68. n->set_global_transform(get_global_transform());
  69. } else {
  70. Transform n_trans = n->get_transform();
  71. Transform our_trans = get_transform();
  72. if (!update_remote_position)
  73. our_trans.set_origin(n_trans.get_origin());
  74. n->set_transform(our_trans);
  75. if (!update_remote_rotation)
  76. n->set_rotation(n_trans.basis.get_rotation());
  77. if (!update_remote_scale)
  78. n->set_scale(n_trans.basis.get_scale());
  79. }
  80. }
  81. }
  82. void RemoteTransform::_notification(int p_what) {
  83. switch (p_what) {
  84. case NOTIFICATION_READY: {
  85. _update_cache();
  86. } break;
  87. case NOTIFICATION_TRANSFORM_CHANGED: {
  88. if (!is_inside_tree())
  89. break;
  90. if (cache) {
  91. _update_remote();
  92. }
  93. } break;
  94. }
  95. }
  96. void RemoteTransform::set_remote_node(const NodePath &p_remote_node) {
  97. remote_node = p_remote_node;
  98. if (is_inside_tree())
  99. _update_cache();
  100. update_configuration_warning();
  101. }
  102. NodePath RemoteTransform::get_remote_node() const {
  103. return remote_node;
  104. }
  105. void RemoteTransform::set_use_global_coordinates(const bool p_enable) {
  106. use_global_coordinates = p_enable;
  107. }
  108. bool RemoteTransform::get_use_global_coordinates() const {
  109. return use_global_coordinates;
  110. }
  111. void RemoteTransform::set_update_position(const bool p_update) {
  112. update_remote_position = p_update;
  113. _update_remote();
  114. }
  115. bool RemoteTransform::get_update_position() const {
  116. return update_remote_position;
  117. }
  118. void RemoteTransform::set_update_rotation(const bool p_update) {
  119. update_remote_rotation = p_update;
  120. _update_remote();
  121. }
  122. bool RemoteTransform::get_update_rotation() const {
  123. return update_remote_rotation;
  124. }
  125. void RemoteTransform::set_update_scale(const bool p_update) {
  126. update_remote_scale = p_update;
  127. _update_remote();
  128. }
  129. bool RemoteTransform::get_update_scale() const {
  130. return update_remote_scale;
  131. }
  132. String RemoteTransform::get_configuration_warning() const {
  133. if (!has_node(remote_node) || !Object::cast_to<Spatial>(get_node(remote_node))) {
  134. return TTR("Path property must point to a valid Spatial node to work.");
  135. }
  136. return String();
  137. }
  138. void RemoteTransform::_bind_methods() {
  139. ClassDB::bind_method(D_METHOD("set_remote_node", "path"), &RemoteTransform::set_remote_node);
  140. ClassDB::bind_method(D_METHOD("get_remote_node"), &RemoteTransform::get_remote_node);
  141. ClassDB::bind_method(D_METHOD("set_use_global_coordinates", "use_global_coordinates"), &RemoteTransform::set_use_global_coordinates);
  142. ClassDB::bind_method(D_METHOD("get_use_global_coordinates"), &RemoteTransform::get_use_global_coordinates);
  143. ClassDB::bind_method(D_METHOD("set_update_position", "update_remote_position"), &RemoteTransform::set_update_position);
  144. ClassDB::bind_method(D_METHOD("get_update_position"), &RemoteTransform::get_update_position);
  145. ClassDB::bind_method(D_METHOD("set_update_rotation", "update_remote_rotation"), &RemoteTransform::set_update_rotation);
  146. ClassDB::bind_method(D_METHOD("get_update_rotation"), &RemoteTransform::get_update_rotation);
  147. ClassDB::bind_method(D_METHOD("set_update_scale", "update_remote_scale"), &RemoteTransform::set_update_scale);
  148. ClassDB::bind_method(D_METHOD("get_update_scale"), &RemoteTransform::get_update_scale);
  149. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "remote_path"), "set_remote_node", "get_remote_node");
  150. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_global_coordinates"), "set_use_global_coordinates", "get_use_global_coordinates");
  151. ADD_GROUP("Update", "update_");
  152. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_position"), "set_update_position", "get_update_position");
  153. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_rotation"), "set_update_rotation", "get_update_rotation");
  154. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_scale"), "set_update_scale", "get_update_scale");
  155. }
  156. RemoteTransform::RemoteTransform() {
  157. use_global_coordinates = true;
  158. update_remote_position = true;
  159. update_remote_rotation = true;
  160. update_remote_scale = true;
  161. cache = 0;
  162. set_notify_transform(true);
  163. }