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
_update_cache()33 void RemoteTransform2D::_update_cache() {
34
35 cache = 0;
36 if (has_node(remote_node)) {
37 Node *node = get_node(remote_node);
38 if (!node || this == node || node->is_a_parent_of(this) || this->is_a_parent_of(node)) {
39 return;
40 }
41
42 cache = node->get_instance_ID();
43 }
44 }
45
_update_remote()46 void RemoteTransform2D::_update_remote() {
47
48 if (!is_inside_tree())
49 return;
50
51 if (!cache)
52 return;
53
54 Object *obj = ObjectDB::get_instance(cache);
55 if (!obj)
56 return;
57
58 Node2D *n = obj->cast_to<Node2D>();
59 if (!n)
60 return;
61
62 if (!n->is_inside_tree())
63 return;
64
65 //todo make faster
66 n->set_global_transform(get_global_transform());
67 }
68
_notification(int p_what)69 void RemoteTransform2D::_notification(int p_what) {
70
71 switch (p_what) {
72
73 case NOTIFICATION_READY: {
74
75 _update_cache();
76
77 } break;
78 case NOTIFICATION_TRANSFORM_CHANGED: {
79 if (!is_inside_tree())
80 break;
81
82 if (cache) {
83
84 _update_remote();
85 }
86
87 } break;
88 }
89 }
90
set_remote_node(const NodePath & p_remote_node)91 void RemoteTransform2D::set_remote_node(const NodePath &p_remote_node) {
92
93 remote_node = p_remote_node;
94 if (is_inside_tree())
95 _update_cache();
96
97 update_configuration_warning();
98 }
99
get_remote_node() const100 NodePath RemoteTransform2D::get_remote_node() const {
101
102 return remote_node;
103 }
104
get_configuration_warning() const105 String RemoteTransform2D::get_configuration_warning() const {
106
107 if (!has_node(remote_node) || !get_node(remote_node) || !get_node(remote_node)->cast_to<Node2D>()) {
108 return TTR("Path property must point to a valid Node2D node to work.");
109 }
110
111 return String();
112 }
113
_bind_methods()114 void RemoteTransform2D::_bind_methods() {
115
116 ObjectTypeDB::bind_method(_MD("set_remote_node", "path"), &RemoteTransform2D::set_remote_node);
117 ObjectTypeDB::bind_method(_MD("get_remote_node"), &RemoteTransform2D::get_remote_node);
118
119 ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "remote_path"), _SCS("set_remote_node"), _SCS("get_remote_node"));
120 }
121
RemoteTransform2D()122 RemoteTransform2D::RemoteTransform2D() {
123
124 cache = 0;
125 }
126