remote_transform.cpp 7.2 KB

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