remote_transform.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**************************************************************************/
  2. /* remote_transform.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.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. }
  45. if (!cache) {
  46. return;
  47. }
  48. Spatial *n = Object::cast_to<Spatial>(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. Transform our_trans = get_global_transform();
  61. if (update_remote_rotation) {
  62. n->set_rotation(our_trans.basis.get_rotation());
  63. }
  64. if (update_remote_scale) {
  65. n->set_scale(our_trans.basis.get_scale());
  66. }
  67. if (update_remote_position) {
  68. Transform 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. Transform our_trans = get_transform();
  78. if (update_remote_rotation) {
  79. n->set_rotation(our_trans.basis.get_rotation());
  80. }
  81. if (update_remote_scale) {
  82. n->set_scale(our_trans.basis.get_scale());
  83. }
  84. if (update_remote_position) {
  85. Transform 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 RemoteTransform::_notification(int p_what) {
  93. switch (p_what) {
  94. case NOTIFICATION_ENTER_TREE: {
  95. _update_cache();
  96. } break;
  97. case NOTIFICATION_TRANSFORM_CHANGED: {
  98. if (!is_inside_tree()) {
  99. break;
  100. }
  101. if (cache) {
  102. _update_remote();
  103. }
  104. } break;
  105. }
  106. }
  107. void RemoteTransform::set_remote_node(const NodePath &p_remote_node) {
  108. remote_node = p_remote_node;
  109. if (is_inside_tree()) {
  110. _update_cache();
  111. _update_remote();
  112. }
  113. update_configuration_warning();
  114. }
  115. NodePath RemoteTransform::get_remote_node() const {
  116. return remote_node;
  117. }
  118. void RemoteTransform::set_use_global_coordinates(const bool p_enable) {
  119. use_global_coordinates = p_enable;
  120. }
  121. bool RemoteTransform::get_use_global_coordinates() const {
  122. return use_global_coordinates;
  123. }
  124. void RemoteTransform::set_update_position(const bool p_update) {
  125. update_remote_position = p_update;
  126. _update_remote();
  127. }
  128. bool RemoteTransform::get_update_position() const {
  129. return update_remote_position;
  130. }
  131. void RemoteTransform::set_update_rotation(const bool p_update) {
  132. update_remote_rotation = p_update;
  133. _update_remote();
  134. }
  135. bool RemoteTransform::get_update_rotation() const {
  136. return update_remote_rotation;
  137. }
  138. void RemoteTransform::set_update_scale(const bool p_update) {
  139. update_remote_scale = p_update;
  140. _update_remote();
  141. }
  142. bool RemoteTransform::get_update_scale() const {
  143. return update_remote_scale;
  144. }
  145. void RemoteTransform::force_update_cache() {
  146. _update_cache();
  147. }
  148. String RemoteTransform::get_configuration_warning() const {
  149. String warning = Spatial::get_configuration_warning();
  150. if (!has_node(remote_node) || !Object::cast_to<Spatial>(get_node(remote_node))) {
  151. if (warning != String()) {
  152. warning += "\n\n";
  153. }
  154. warning += TTR("The \"Remote Path\" property must point to a valid Spatial or Spatial-derived node to work.");
  155. }
  156. return warning;
  157. }
  158. void RemoteTransform::_bind_methods() {
  159. ClassDB::bind_method(D_METHOD("set_remote_node", "path"), &RemoteTransform::set_remote_node);
  160. ClassDB::bind_method(D_METHOD("get_remote_node"), &RemoteTransform::get_remote_node);
  161. ClassDB::bind_method(D_METHOD("force_update_cache"), &RemoteTransform::force_update_cache);
  162. ClassDB::bind_method(D_METHOD("set_use_global_coordinates", "use_global_coordinates"), &RemoteTransform::set_use_global_coordinates);
  163. ClassDB::bind_method(D_METHOD("get_use_global_coordinates"), &RemoteTransform::get_use_global_coordinates);
  164. ClassDB::bind_method(D_METHOD("set_update_position", "update_remote_position"), &RemoteTransform::set_update_position);
  165. ClassDB::bind_method(D_METHOD("get_update_position"), &RemoteTransform::get_update_position);
  166. ClassDB::bind_method(D_METHOD("set_update_rotation", "update_remote_rotation"), &RemoteTransform::set_update_rotation);
  167. ClassDB::bind_method(D_METHOD("get_update_rotation"), &RemoteTransform::get_update_rotation);
  168. ClassDB::bind_method(D_METHOD("set_update_scale", "update_remote_scale"), &RemoteTransform::set_update_scale);
  169. ClassDB::bind_method(D_METHOD("get_update_scale"), &RemoteTransform::get_update_scale);
  170. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "remote_path", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Spatial"), "set_remote_node", "get_remote_node");
  171. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_global_coordinates"), "set_use_global_coordinates", "get_use_global_coordinates");
  172. ADD_GROUP("Update", "update_");
  173. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_position"), "set_update_position", "get_update_position");
  174. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_rotation"), "set_update_rotation", "get_update_rotation");
  175. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_scale"), "set_update_scale", "get_update_scale");
  176. }
  177. RemoteTransform::RemoteTransform() {
  178. use_global_coordinates = true;
  179. update_remote_position = true;
  180. update_remote_rotation = true;
  181. update_remote_scale = true;
  182. cache = 0;
  183. set_notify_transform(true);
  184. }