Repository navigation
Expand file tree
/
Copy pathbinding_script.js
More file actions
192 lines (170 loc) · 6.24 KB
/
Copy pathbinding_script.js
File metadata and controls
192 lines (170 loc) · 6.24 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
(function start_script_binding() {
const GodotAnonymousConnectionManager = new(class GodotAnonymousConnectionManager extends godot.Reference {
anonymous_connection_index = 0;
get_slot_id(emitter, signal, target) {
return `__${emitter.get_instance_id()}:${signal}:${target.get_instance_id()}__`;
}
get_anonymous_method_name() {
return `__anonymous_slot:${this.anonymous_connection_index++}`;
}
})();
const godot_object_connect = godot.Object.prototype.connect;
Object.defineProperty(godot.Object.prototype, 'connect', {
// Object.prototype.connect(signal_name, target, method, params, flags)
value: function connect(...args) {
if (args.length < 2) throw new Error('2 or more arguments expected');
let signal = args[0];
if (typeof signal !== 'string' || !signal) throw new Error('signal name expected for argument #0');
let offset = 0;
let target = null;
let method = null;
let method_name = null;
let target_caller = null;
let params = [];
let flags = 0;
if (typeof args[1] === 'function') {
offset = -1;
target = GodotAnonymousConnectionManager;
method = args[1];
} else if (args.length >= 3) {
target_caller = args[1];
target = target_caller || GodotAnonymousConnectionManager;
if (!(target instanceof godot.Object)) {
new Error('godot.Object expected for signal target');
}
method = args[2];
} else {
throw new Error('method name or function expected for signal method');
}
if (args.length > 3 + offset) {
let p = args[3 + offset];
if (p && p.length)
params = p;
}
if (args.length > 4 + offset) {
let p = args[4 + offset];
if (typeof p === 'number')
flags = p;
}
if (typeof method === 'string' && method.length) {
method_name = method;
} else if (typeof method === 'function') {
const slot_id = GodotAnonymousConnectionManager.get_slot_id(this, signal, target);
if (target[method.name] === method) {
method_name = method.name;
} else {
method_name = GodotAnonymousConnectionManager.get_anonymous_method_name();
function callback(...args) { method.apply(target_caller, args); };
target[method_name] = callback;
callback[slot_id] = method_name;
}
method[slot_id] = method_name;
} else {
throw new Error('method name or function expected for signal method');
}
// console.log("connect", this, signal, target, method_name, params, flags);
return godot_object_connect.apply(this, [signal, target, method_name, params, flags]);
},
writable: false,
configurable: true
});
const godot_object_disconnect = godot.Object.prototype.disconnect;
Object.defineProperty(godot.Object.prototype, 'disconnect', {
// Object.prototype.disconnect(signal_name, target, method)
value: function disconnect(...args) {
if (args.length < 2) throw new Error('2 or more arguments expected');
let signal = args[0];
if (typeof signal !== 'string' || !signal) throw new Error('signal name expected for argument #0');
let target = null;
let method_name = null;
let method = null;
let slot_id = undefined;
if (typeof args[1] === 'function') {
target = GodotAnonymousConnectionManager;
method = args[1];
} else if (args.length >= 3) {
target = args[1] || GodotAnonymousConnectionManager;
if (!(target instanceof godot.Object)) {
new Error('godot.Object expected for signal target');
}
method = args[2];
} else {
throw new Error('method name or function expected');
}
if (typeof method === 'string' && method) {
method_name = method;
} else if (typeof method === 'function') {
slot_id = GodotAnonymousConnectionManager.get_slot_id(this, signal, target);
method_name = method[slot_id];
method[slot_id] = undefined;
} else {
throw new Error('method name or function expected');
}
if (slot_id) {
if (typeof target[method_name] !== 'undefined') {
target[method_name] = undefined;
delete target[method_name];
}
delete method[slot_id];
}
// console.log("disconnect", this, signal, target, method_name);
godot_object_disconnect.apply(this, [signal, target, method_name]);
},
writable: false,
configurable: true
});
const godot_object_is_connected = godot.Object.prototype.is_connected;
Object.defineProperty(godot.Object.prototype, 'is_connected', {
// Object.prototype.is_connected(signal_name, target, method)
value: function godot_is_connected_override(...args) {
if (args.length < 3) throw new Error('3 arguments expected');
let signal = args[0];
if (typeof signal !== 'string' || !signal) throw new Error('signal name expected for argument #1');
let target = null;
let method = null;
let method_name = null;
let slot_id = undefined;
if (typeof args[1] === 'function') {
target = GodotAnonymousConnectionManager;
method = args[1];
} else if (args.length >= 3) {
target = args[1] || GodotAnonymousConnectionManager;
if (!(target instanceof godot.Object)) {
new Error('godot.Object expected for signal target');
}
method = args[2];
} else {
throw new Error('method name or function expected for argument #2');
}
if (typeof method === 'string') {
method_name = method;
} else if (typeof method === 'function') {
slot_id = GodotAnonymousConnectionManager.get_slot_id(this, signal, target);
method_name = method[slot_id];
} else {
throw new Error('method name or function expected');
}
return godot_object_is_connected.apply(this, [signal, target, method_name]);
},
writable: false,
configurable: true
});
function godot_yield(target, signal) {
return new Promise(function(resolve, reject) {
function callback(...args) {
const slot_id = GodotAnonymousConnectionManager.get_slot_id(target, signal, GodotAnonymousConnectionManager);
const method_name = callback[slot_id];
GodotAnonymousConnectionManager[method_name] = undefined;
delete GodotAnonymousConnectionManager[method_name];
delete callback[slot_id];
resolve(args);
};
target.connect(signal, GodotAnonymousConnectionManager, callback, [], godot.Object.CONNECT_ONESHOT);
});
}
Object.defineProperty(godot, 'yield', {
value: godot_yield,
writable: false,
configurable: true
});
})();