remote_transform_2d.cpp 7.8 KB

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