remote_transform_2d.cpp 7.4 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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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_global_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_configuration_warning();
  108. }
  109. NodePath RemoteTransform2D::get_remote_node() const {
  110. return remote_node;
  111. }
  112. void RemoteTransform2D::set_use_global_coordinates(const bool p_enable) {
  113. use_global_coordinates = p_enable;
  114. }
  115. bool RemoteTransform2D::get_use_global_coordinates() const {
  116. return use_global_coordinates;
  117. }
  118. void RemoteTransform2D::set_update_position(const bool p_update) {
  119. update_remote_position = p_update;
  120. _update_remote();
  121. }
  122. bool RemoteTransform2D::get_update_position() const {
  123. return update_remote_position;
  124. }
  125. void RemoteTransform2D::set_update_rotation(const bool p_update) {
  126. update_remote_rotation = p_update;
  127. _update_remote();
  128. }
  129. bool RemoteTransform2D::get_update_rotation() const {
  130. return update_remote_rotation;
  131. }
  132. void RemoteTransform2D::set_update_scale(const bool p_update) {
  133. update_remote_scale = p_update;
  134. _update_remote();
  135. }
  136. bool RemoteTransform2D::get_update_scale() const {
  137. return update_remote_scale;
  138. }
  139. String RemoteTransform2D::get_configuration_warning() const {
  140. if (!has_node(remote_node) || !Object::cast_to<Node2D>(get_node(remote_node))) {
  141. return TTR("Path property must point to a valid Node2D node to work.");
  142. }
  143. return String();
  144. }
  145. void RemoteTransform2D::_bind_methods() {
  146. ClassDB::bind_method(D_METHOD("set_remote_node", "path"), &RemoteTransform2D::set_remote_node);
  147. ClassDB::bind_method(D_METHOD("get_remote_node"), &RemoteTransform2D::get_remote_node);
  148. ClassDB::bind_method(D_METHOD("set_use_global_coordinates", "use_global_coordinates"), &RemoteTransform2D::set_use_global_coordinates);
  149. ClassDB::bind_method(D_METHOD("get_use_global_coordinates"), &RemoteTransform2D::get_use_global_coordinates);
  150. ClassDB::bind_method(D_METHOD("set_update_position", "update_remote_position"), &RemoteTransform2D::set_update_position);
  151. ClassDB::bind_method(D_METHOD("get_update_position"), &RemoteTransform2D::get_update_position);
  152. ClassDB::bind_method(D_METHOD("set_update_rotation", "update_remote_rotation"), &RemoteTransform2D::set_update_rotation);
  153. ClassDB::bind_method(D_METHOD("get_update_rotation"), &RemoteTransform2D::get_update_rotation);
  154. ClassDB::bind_method(D_METHOD("set_update_scale", "update_remote_scale"), &RemoteTransform2D::set_update_scale);
  155. ClassDB::bind_method(D_METHOD("get_update_scale"), &RemoteTransform2D::get_update_scale);
  156. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "remote_path"), "set_remote_node", "get_remote_node");
  157. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_global_coordinates"), "set_use_global_coordinates", "get_use_global_coordinates");
  158. ADD_GROUP("Update", "update_");
  159. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_position"), "set_update_position", "get_update_position");
  160. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_rotation"), "set_update_rotation", "get_update_rotation");
  161. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_scale"), "set_update_scale", "get_update_scale");
  162. }
  163. RemoteTransform2D::RemoteTransform2D() {
  164. use_global_coordinates = true;
  165. update_remote_position = true;
  166. update_remote_rotation = true;
  167. update_remote_scale = true;
  168. cache = 0;
  169. set_notify_transform(true);
  170. }