remote_transform_2d.cpp 8.6 KB

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