remote_transform_2d.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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-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_2d.h"
  31. #include "scene/scene_string_names.h"
  32. void RemoteTransform2D::_update_cache() {
  33. cache = 0;
  34. if (has_node(remote_node)) {
  35. Node *node = get_node(remote_node);
  36. if (!node || this == node || node->is_a_parent_of(this) || this->is_a_parent_of(node)) {
  37. return;
  38. }
  39. cache = node->get_instance_id();
  40. }
  41. }
  42. void RemoteTransform2D::_update_remote() {
  43. if (!is_inside_tree())
  44. return;
  45. if (!cache)
  46. return;
  47. Node2D *n = Object::cast_to<Node2D>(ObjectDB::get_instance(cache));
  48. if (!n)
  49. return;
  50. if (!n->is_inside_tree())
  51. return;
  52. //todo make faster
  53. if (use_global_coordinates) {
  54. if (update_remote_position && update_remote_rotation && update_remote_scale) {
  55. n->set_global_transform(get_global_transform());
  56. } else {
  57. Transform2D n_trans = n->get_global_transform();
  58. Transform2D our_trans = get_global_transform();
  59. Vector2 n_scale = n->get_scale();
  60. if (!update_remote_position)
  61. our_trans.set_origin(n_trans.get_origin());
  62. if (!update_remote_rotation)
  63. our_trans.set_rotation(n_trans.get_rotation());
  64. n->set_global_transform(our_trans);
  65. if (update_remote_scale)
  66. n->set_scale(get_global_scale());
  67. else
  68. n->set_scale(n_scale);
  69. }
  70. } else {
  71. if (update_remote_position && update_remote_rotation && update_remote_scale) {
  72. n->set_transform(get_transform());
  73. } else {
  74. Transform2D n_trans = n->get_transform();
  75. Transform2D our_trans = get_transform();
  76. Vector2 n_scale = n->get_scale();
  77. if (!update_remote_position)
  78. our_trans.set_origin(n_trans.get_origin());
  79. if (!update_remote_rotation)
  80. our_trans.set_rotation(n_trans.get_rotation());
  81. n->set_transform(our_trans);
  82. if (update_remote_scale)
  83. n->set_scale(get_scale());
  84. else
  85. n->set_scale(n_scale);
  86. }
  87. }
  88. }
  89. void RemoteTransform2D::_notification(int p_what) {
  90. switch (p_what) {
  91. case NOTIFICATION_READY: {
  92. _update_cache();
  93. } break;
  94. case NOTIFICATION_TRANSFORM_CHANGED: {
  95. if (!is_inside_tree())
  96. break;
  97. if (cache) {
  98. _update_remote();
  99. }
  100. } break;
  101. }
  102. }
  103. void RemoteTransform2D::set_remote_node(const NodePath &p_remote_node) {
  104. remote_node = p_remote_node;
  105. if (is_inside_tree()) {
  106. _update_cache();
  107. _update_remote();
  108. }
  109. update_configuration_warning();
  110. }
  111. NodePath RemoteTransform2D::get_remote_node() const {
  112. return remote_node;
  113. }
  114. void RemoteTransform2D::set_use_global_coordinates(const bool p_enable) {
  115. use_global_coordinates = p_enable;
  116. _update_remote();
  117. }
  118. bool RemoteTransform2D::get_use_global_coordinates() const {
  119. return use_global_coordinates;
  120. }
  121. void RemoteTransform2D::set_update_position(const bool p_update) {
  122. update_remote_position = p_update;
  123. _update_remote();
  124. }
  125. bool RemoteTransform2D::get_update_position() const {
  126. return update_remote_position;
  127. }
  128. void RemoteTransform2D::set_update_rotation(const bool p_update) {
  129. update_remote_rotation = p_update;
  130. _update_remote();
  131. }
  132. bool RemoteTransform2D::get_update_rotation() const {
  133. return update_remote_rotation;
  134. }
  135. void RemoteTransform2D::set_update_scale(const bool p_update) {
  136. update_remote_scale = p_update;
  137. _update_remote();
  138. }
  139. bool RemoteTransform2D::get_update_scale() const {
  140. return update_remote_scale;
  141. }
  142. String RemoteTransform2D::get_configuration_warning() const {
  143. if (!has_node(remote_node) || !Object::cast_to<Node2D>(get_node(remote_node))) {
  144. return TTR("Path property must point to a valid Node2D node to work.");
  145. }
  146. return String();
  147. }
  148. void RemoteTransform2D::_bind_methods() {
  149. ClassDB::bind_method(D_METHOD("set_remote_node", "path"), &RemoteTransform2D::set_remote_node);
  150. ClassDB::bind_method(D_METHOD("get_remote_node"), &RemoteTransform2D::get_remote_node);
  151. ClassDB::bind_method(D_METHOD("set_use_global_coordinates", "use_global_coordinates"), &RemoteTransform2D::set_use_global_coordinates);
  152. ClassDB::bind_method(D_METHOD("get_use_global_coordinates"), &RemoteTransform2D::get_use_global_coordinates);
  153. ClassDB::bind_method(D_METHOD("set_update_position", "update_remote_position"), &RemoteTransform2D::set_update_position);
  154. ClassDB::bind_method(D_METHOD("get_update_position"), &RemoteTransform2D::get_update_position);
  155. ClassDB::bind_method(D_METHOD("set_update_rotation", "update_remote_rotation"), &RemoteTransform2D::set_update_rotation);
  156. ClassDB::bind_method(D_METHOD("get_update_rotation"), &RemoteTransform2D::get_update_rotation);
  157. ClassDB::bind_method(D_METHOD("set_update_scale", "update_remote_scale"), &RemoteTransform2D::set_update_scale);
  158. ClassDB::bind_method(D_METHOD("get_update_scale"), &RemoteTransform2D::get_update_scale);
  159. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "remote_path", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node2D"), "set_remote_node", "get_remote_node");
  160. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_global_coordinates"), "set_use_global_coordinates", "get_use_global_coordinates");
  161. ADD_GROUP("Update", "update_");
  162. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_position"), "set_update_position", "get_update_position");
  163. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_rotation"), "set_update_rotation", "get_update_rotation");
  164. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_scale"), "set_update_scale", "get_update_scale");
  165. }
  166. RemoteTransform2D::RemoteTransform2D() {
  167. use_global_coordinates = true;
  168. update_remote_position = true;
  169. update_remote_rotation = true;
  170. update_remote_scale = true;
  171. cache = 0;
  172. set_notify_transform(true);
  173. }