remote_transform_2d.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*************************************************************************/
  2. /* remote_transform_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_2d.h"
  31. void RemoteTransform2D::_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) || this->is_ancestor_of(node)) {
  36. return;
  37. }
  38. cache = node->get_instance_id();
  39. }
  40. }
  41. void RemoteTransform2D::_update_remote() {
  42. if (!is_inside_tree()) {
  43. return;
  44. }
  45. if (cache.is_null()) {
  46. return;
  47. }
  48. Node2D *n = Object::cast_to<Node2D>(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. Transform2D n_trans = n->get_global_transform();
  61. Transform2D our_trans = get_global_transform();
  62. Vector2 n_scale = n->get_scale();
  63. if (!update_remote_position) {
  64. our_trans.set_origin(n_trans.get_origin());
  65. }
  66. if (!update_remote_rotation) {
  67. our_trans.set_rotation(n_trans.get_rotation());
  68. }
  69. n->set_global_transform(our_trans);
  70. if (update_remote_scale) {
  71. n->set_scale(get_global_scale());
  72. } else {
  73. n->set_scale(n_scale);
  74. }
  75. }
  76. } else {
  77. if (update_remote_position && update_remote_rotation && update_remote_scale) {
  78. n->set_transform(get_transform());
  79. } else {
  80. Transform2D n_trans = n->get_transform();
  81. Transform2D our_trans = get_transform();
  82. Vector2 n_scale = n->get_scale();
  83. if (!update_remote_position) {
  84. our_trans.set_origin(n_trans.get_origin());
  85. }
  86. if (!update_remote_rotation) {
  87. our_trans.set_rotation(n_trans.get_rotation());
  88. }
  89. n->set_transform(our_trans);
  90. if (update_remote_scale) {
  91. n->set_scale(get_scale());
  92. } else {
  93. n->set_scale(n_scale);
  94. }
  95. }
  96. }
  97. }
  98. void RemoteTransform2D::_notification(int p_what) {
  99. switch (p_what) {
  100. case NOTIFICATION_ENTER_TREE: {
  101. _update_cache();
  102. } break;
  103. case NOTIFICATION_TRANSFORM_CHANGED: {
  104. if (!is_inside_tree()) {
  105. break;
  106. }
  107. if (cache.is_valid()) {
  108. _update_remote();
  109. }
  110. } break;
  111. }
  112. }
  113. void RemoteTransform2D::set_remote_node(const NodePath &p_remote_node) {
  114. remote_node = p_remote_node;
  115. if (is_inside_tree()) {
  116. _update_cache();
  117. _update_remote();
  118. }
  119. update_configuration_warnings();
  120. }
  121. NodePath RemoteTransform2D::get_remote_node() const {
  122. return remote_node;
  123. }
  124. void RemoteTransform2D::set_use_global_coordinates(const bool p_enable) {
  125. use_global_coordinates = p_enable;
  126. _update_remote();
  127. }
  128. bool RemoteTransform2D::get_use_global_coordinates() const {
  129. return use_global_coordinates;
  130. }
  131. void RemoteTransform2D::set_update_position(const bool p_update) {
  132. update_remote_position = p_update;
  133. _update_remote();
  134. }
  135. bool RemoteTransform2D::get_update_position() const {
  136. return update_remote_position;
  137. }
  138. void RemoteTransform2D::set_update_rotation(const bool p_update) {
  139. update_remote_rotation = p_update;
  140. _update_remote();
  141. }
  142. bool RemoteTransform2D::get_update_rotation() const {
  143. return update_remote_rotation;
  144. }
  145. void RemoteTransform2D::set_update_scale(const bool p_update) {
  146. update_remote_scale = p_update;
  147. _update_remote();
  148. }
  149. bool RemoteTransform2D::get_update_scale() const {
  150. return update_remote_scale;
  151. }
  152. void RemoteTransform2D::force_update_cache() {
  153. _update_cache();
  154. }
  155. TypedArray<String> RemoteTransform2D::get_configuration_warnings() const {
  156. TypedArray<String> warnings = Node::get_configuration_warnings();
  157. if (!has_node(remote_node) || !Object::cast_to<Node2D>(get_node(remote_node))) {
  158. warnings.push_back(RTR("Path property must point to a valid Node2D node to work."));
  159. }
  160. return warnings;
  161. }
  162. void RemoteTransform2D::_bind_methods() {
  163. ClassDB::bind_method(D_METHOD("set_remote_node", "path"), &RemoteTransform2D::set_remote_node);
  164. ClassDB::bind_method(D_METHOD("get_remote_node"), &RemoteTransform2D::get_remote_node);
  165. ClassDB::bind_method(D_METHOD("force_update_cache"), &RemoteTransform2D::force_update_cache);
  166. ClassDB::bind_method(D_METHOD("set_use_global_coordinates", "use_global_coordinates"), &RemoteTransform2D::set_use_global_coordinates);
  167. ClassDB::bind_method(D_METHOD("get_use_global_coordinates"), &RemoteTransform2D::get_use_global_coordinates);
  168. ClassDB::bind_method(D_METHOD("set_update_position", "update_remote_position"), &RemoteTransform2D::set_update_position);
  169. ClassDB::bind_method(D_METHOD("get_update_position"), &RemoteTransform2D::get_update_position);
  170. ClassDB::bind_method(D_METHOD("set_update_rotation", "update_remote_rotation"), &RemoteTransform2D::set_update_rotation);
  171. ClassDB::bind_method(D_METHOD("get_update_rotation"), &RemoteTransform2D::get_update_rotation);
  172. ClassDB::bind_method(D_METHOD("set_update_scale", "update_remote_scale"), &RemoteTransform2D::set_update_scale);
  173. ClassDB::bind_method(D_METHOD("get_update_scale"), &RemoteTransform2D::get_update_scale);
  174. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "remote_path", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node2D"), "set_remote_node", "get_remote_node");
  175. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_global_coordinates"), "set_use_global_coordinates", "get_use_global_coordinates");
  176. ADD_GROUP("Update", "update_");
  177. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_position"), "set_update_position", "get_update_position");
  178. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_rotation"), "set_update_rotation", "get_update_rotation");
  179. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_scale"), "set_update_scale", "get_update_scale");
  180. }
  181. RemoteTransform2D::RemoteTransform2D() {
  182. set_notify_transform(true);
  183. }