remote_transform_2d.cpp 8.8 KB

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