remote_transform_2d.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_TRANSFORM_CHANGED: {
  99. if (!is_inside_tree()) {
  100. break;
  101. }
  102. if (cache.is_valid()) {
  103. _update_remote();
  104. }
  105. } break;
  106. }
  107. }
  108. void RemoteTransform2D::set_remote_node(const NodePath &p_remote_node) {
  109. remote_node = p_remote_node;
  110. if (is_inside_tree()) {
  111. _update_cache();
  112. _update_remote();
  113. }
  114. update_configuration_warnings();
  115. }
  116. NodePath RemoteTransform2D::get_remote_node() const {
  117. return remote_node;
  118. }
  119. void RemoteTransform2D::set_use_global_coordinates(const bool p_enable) {
  120. use_global_coordinates = p_enable;
  121. _update_remote();
  122. }
  123. bool RemoteTransform2D::get_use_global_coordinates() const {
  124. return use_global_coordinates;
  125. }
  126. void RemoteTransform2D::set_update_position(const bool p_update) {
  127. update_remote_position = p_update;
  128. _update_remote();
  129. }
  130. bool RemoteTransform2D::get_update_position() const {
  131. return update_remote_position;
  132. }
  133. void RemoteTransform2D::set_update_rotation(const bool p_update) {
  134. update_remote_rotation = p_update;
  135. _update_remote();
  136. }
  137. bool RemoteTransform2D::get_update_rotation() const {
  138. return update_remote_rotation;
  139. }
  140. void RemoteTransform2D::set_update_scale(const bool p_update) {
  141. update_remote_scale = p_update;
  142. _update_remote();
  143. }
  144. bool RemoteTransform2D::get_update_scale() const {
  145. return update_remote_scale;
  146. }
  147. void RemoteTransform2D::force_update_cache() {
  148. _update_cache();
  149. }
  150. PackedStringArray RemoteTransform2D::get_configuration_warnings() const {
  151. PackedStringArray warnings = Node::get_configuration_warnings();
  152. if (!has_node(remote_node) || !Object::cast_to<Node2D>(get_node(remote_node))) {
  153. warnings.push_back(RTR("Path property must point to a valid Node2D node to work."));
  154. }
  155. return warnings;
  156. }
  157. void RemoteTransform2D::_bind_methods() {
  158. ClassDB::bind_method(D_METHOD("set_remote_node", "path"), &RemoteTransform2D::set_remote_node);
  159. ClassDB::bind_method(D_METHOD("get_remote_node"), &RemoteTransform2D::get_remote_node);
  160. ClassDB::bind_method(D_METHOD("force_update_cache"), &RemoteTransform2D::force_update_cache);
  161. ClassDB::bind_method(D_METHOD("set_use_global_coordinates", "use_global_coordinates"), &RemoteTransform2D::set_use_global_coordinates);
  162. ClassDB::bind_method(D_METHOD("get_use_global_coordinates"), &RemoteTransform2D::get_use_global_coordinates);
  163. ClassDB::bind_method(D_METHOD("set_update_position", "update_remote_position"), &RemoteTransform2D::set_update_position);
  164. ClassDB::bind_method(D_METHOD("get_update_position"), &RemoteTransform2D::get_update_position);
  165. ClassDB::bind_method(D_METHOD("set_update_rotation", "update_remote_rotation"), &RemoteTransform2D::set_update_rotation);
  166. ClassDB::bind_method(D_METHOD("get_update_rotation"), &RemoteTransform2D::get_update_rotation);
  167. ClassDB::bind_method(D_METHOD("set_update_scale", "update_remote_scale"), &RemoteTransform2D::set_update_scale);
  168. ClassDB::bind_method(D_METHOD("get_update_scale"), &RemoteTransform2D::get_update_scale);
  169. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "remote_path", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node2D"), "set_remote_node", "get_remote_node");
  170. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_global_coordinates"), "set_use_global_coordinates", "get_use_global_coordinates");
  171. ADD_GROUP("Update", "update_");
  172. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_position"), "set_update_position", "get_update_position");
  173. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_rotation"), "set_update_rotation", "get_update_rotation");
  174. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_scale"), "set_update_scale", "get_update_scale");
  175. }
  176. RemoteTransform2D::RemoteTransform2D() {
  177. set_notify_transform(true);
  178. set_hide_clip_children(true);
  179. }