-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremote_transform_2d.gd
134 lines (97 loc) · 2.99 KB
/
remote_transform_2d.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class_name RemoteTransform2DScripted tool extends Node2D
# port author: Xrayez
# license: MIT
# source: https://github.com/godotengine/godot/blob/2b824b4e4/scene/2d/remote_transform_2d.cpp
# diffs:
# - no Transform2D.set_rotation/scale in GDScript yet, so those are replaced
# with a more low-level Transform2D API.
export var remote_path := NodePath()
var cache = 0
export var use_global_coordinates := true
export var update_position := true setget set_update_position
export var update_scale := true setget set_update_scale
export var update_rotation := true setget set_update_rotation
func _init():
set_notify_transform(true)
func _update_cache():
cache = 0
if has_node(remote_path):
var node = get_node(remote_path)
if not node or self == node or node.is_a_parent_of(self) or is_a_parent_of(node):
return
cache = node.get_instance_id()
func _update_remote():
if not is_inside_tree():
return
if not cache:
return
var node = instance_from_id(cache)
if not node:
return
if not node.is_inside_tree():
return
if use_global_coordinates:
if update_position and update_rotation and update_scale:
node.global_transform = global_transform
else:
var n_trans = node.global_transform
var our_trans = global_transform
var n_scale = node.scale
if not update_position:
our_trans = Transform2D(our_trans.x, our_trans.y, n_trans.origin)
if not update_rotation:
our_trans = Transform2D(n_trans.x, n_trans.y, our_trans.origin)
node.global_transform = our_trans
if update_scale:
node.scale = global_scale
else:
node.scale = n_scale
else:
if update_position and update_rotation and update_scale:
node.transform = transform
else:
var n_trans = node.transform
var our_trans = transform
var n_scale = node.scale
if not update_position:
our_trans = Transform2D(our_trans.x, our_trans.y, n_trans.origin)
if not update_rotation:
our_trans = Transform2D(n_trans.x, n_trans.y, our_trans.origin)
node.transform = our_trans
if update_scale:
node.scale = scale
else:
node.scale = n_scale
func _notification(p_what):
match p_what:
NOTIFICATION_ENTER_TREE:
_update_cache()
NOTIFICATION_TRANSFORM_CHANGED:
if not is_inside_tree():
return
if cache:
_update_remote()
func set_remote_path(p_remote_path):
remote_path = p_remote_path
if is_inside_tree():
_update_cache()
_update_remote()
update_configuration_warning()
func set_use_global_coordinates(p_enable):
use_global_coordinates = p_enable
_update_remote()
func set_update_position(p_update):
update_position = p_update
_update_remote()
func set_update_rotation(p_update):
update_rotation = p_update
_update_remote()
func set_update_scale(p_update):
update_scale = p_update
_update_remote()
func force_update_cache():
_update_cache()
func _get_configuration_warning():
if not has_node(remote_path) or not (get_node(remote_path) as Node2D):
return tr("Path property must point to a valid Node2D node to work.")
return String()