-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecorators.js
106 lines (106 loc) · 3.52 KB
/
decorators.js
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
/**
* Expose as an ECMAClass.
* An ECMAScript object is created and attached automaticly when construct an instance from this class
*/
export function gdclass(target) {
const id = gdclass['internal_class_id'] = gdclass['internal_class_id'] ? gdclass['internal_class_id'] + 1 : 1;
const class_name = `AnonymousECMAClass${id}`;
godot.register_class(target, class_name);
}
/** Set the script is runable in editor */
export function tool(target) {
godot.set_script_tooled(target, true);
}
/** Set the script icon */
export function icon(icon) {
return function (target) {
godot.set_script_icon(target, icon);
};
}
/** Register signal to godot script */
export function signal(target, property, descriptor) {
var constructor = typeof (target) === 'function' ? target : target.constructor;
var prototype = constructor.prototype;
godot.register_signal(target, property);
descriptor = descriptor || {};
descriptor.value = property;
descriptor.writable = false;
Object.defineProperty(constructor, property, descriptor);
Object.defineProperty(prototype, property, descriptor);
}
/**
* Register property to godot class
* @param value The default value of the property
*/
export function property(info) {
return function (target, property, descriptor) {
info = info || {};
godot.register_property(target, property, info);
return descriptor;
};
}
/**
* Return the node with `path` if the `_onready` is called
* @param path The path or the type to get the node
*/
export function onready(path) {
return function (target, property, descriptor) {
const key = `$onready:${property}`;
descriptor = descriptor || {};
descriptor.set = function (v) { this[key] = v; };
descriptor.get = function () {
let v = this[key];
if (!v) {
v = this.get_node(path);
this[key] = v;
}
return v;
};
return descriptor;
};
}
/**
* Register the member as a node property
* **Note: The value is null before current node is ready**
* @param path The default path name of the node
*/
export function node(target, property, descriptor) {
const key = `$onready:${property}`;
const path_key = `${property} `; // <-- a space at the end
descriptor = descriptor || {};
descriptor.set = function (v) { this[key] = v; };
descriptor.get = function () {
let v = this[key];
if (!v) {
v = this.get_node(this[path_key]);
this[key] = v;
}
return v;
};
godot.register_property(target, path_key, { type: godot.TYPE_NODE_PATH });
return descriptor;
}
/**
* Register an enumeration property
* @param enumeration Enumeration name list
* @param default_value The default value of the property
*/
export function enumeration(enumeration, default_value) {
return function (target, property, descriptor) {
const pi = {
hint: godot.PropertyHint.PROPERTY_HINT_ENUM,
type: typeof (default_value) === 'string' ? godot.TYPE_STRING : godot.TYPE_INT,
hint_string: '',
default: typeof (default_value) === 'string' ? default_value : 0
};
for (let i = 0; i < enumeration.length; i++) {
pi.hint_string += enumeration[i];
if (i < enumeration.length - 1) {
pi.hint_string += ',';
}
}
godot.register_property(target, property, pi);
return descriptor;
};
}
//# sourceMappingURL=decorators.js.map