forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript-attributes.js
More file actions
271 lines (250 loc) · 10.5 KB
/
script-attributes.js
File metadata and controls
271 lines (250 loc) · 10.5 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
Object.assign(pc, function () {
var components = ['x', 'y', 'z', 'w'];
var rawToValue = function (app, args, value, old) {
var i;
switch (args.type) {
case 'boolean':
return !!value;
case 'number':
if (typeof value === 'number') {
return value;
} else if (typeof value === 'string') {
var v = parseInt(value, 10);
if (isNaN(v)) return null;
return v;
} else if (typeof value === 'boolean') {
return 0 + value;
}
return null;
case 'json':
if (typeof value === 'object') {
return value;
}
try {
return JSON.parse(value);
} catch (ex) {
return null;
}
case 'asset':
if (value instanceof pc.Asset) {
return value;
} else if (typeof value === 'number') {
return app.assets.get(value) || null;
} else if (typeof value === 'string') {
return app.assets.get(parseInt(value, 10)) || null;
}
return null;
case 'entity':
if (value instanceof pc.GraphNode) {
return value;
} else if (typeof value === 'string') {
return app.getEntityFromIndex(value);
}
return null;
case 'rgb':
case 'rgba':
if (value instanceof pc.Color) {
if (old instanceof pc.Color) {
old.copy(value);
return old;
}
return value.clone();
} else if (value instanceof Array && value.length >= 3 && value.length <= 4) {
for (i = 0; i < value.length; i++) {
if (typeof value[i] !== 'number')
return null;
}
if (!old) old = new pc.Color();
old.r = value[0];
old.g = value[1];
old.b = value[2];
old.a = (value.length === 3) ? 1 : value[3];
return old;
} else if (typeof value === 'string' && /#([0-9abcdef]{2}){3,4}/i.test(value)) {
if (!old)
old = new pc.Color();
old.fromString(value);
return old;
}
return null;
case 'vec2':
case 'vec3':
case 'vec4':
var len = parseInt(args.type.slice(3), 10);
if (value instanceof pc['Vec' + len]) {
if (old instanceof pc['Vec' + len]) {
old.copy(value);
return old;
}
return value.clone();
} else if (value instanceof Array && value.length === len) {
for (i = 0; i < value.length; i++) {
if (typeof value[i] !== 'number')
return null;
}
if (!old) old = new pc['Vec' + len]();
for (i = 0; i < len; i++)
old[components[i]] = value[i];
return old;
}
return null;
case 'curve':
if (value) {
var curve;
if (value instanceof pc.Curve || value instanceof pc.CurveSet) {
curve = value.clone();
} else {
var CurveType = value.keys[0] instanceof Array ? pc.CurveSet : pc.Curve;
curve = new CurveType(value.keys);
curve.type = value.type;
}
return curve;
}
break;
}
return value;
};
/* eslint-disable jsdoc/no-undefined-types */
/**
* @class
* @name pc.ScriptAttributes
* @classdesc Container of Script Attribute definitions. Implements an interface to add/remove attributes and store their definition for a {@link pc.ScriptType}.
* Note: An instance of pc.ScriptAttributes is created automatically by each {@link pc.ScriptType}.
* @param {Class<pc.ScriptType>} scriptType - Script Type that attributes relate to.
*/
/* eslint-enable jsdoc/no-undefined-types */
var ScriptAttributes = function (scriptType) {
this.scriptType = scriptType;
this.index = { };
};
/**
* @function
* @name pc.ScriptAttributes#add
* @description Add Attribute.
* @param {string} name - Name of an attribute.
* @param {object} args - Object with Arguments for an attribute.
* @param {("boolean"|"number"|"string"|"json"|"asset"|"entity"|"rgb"|"rgba"|"vec2"|"vec3"|"vec4"|"curve")} args.type - Type of an attribute value.
* @param {*} [args.default] - Default attribute value.
* @param {string} [args.title] - Title for Editor's for field UI.
* @param {string} [args.description] - Description for Editor's for field UI.
* @param {string|string[]} [args.placeholder] - Placeholder for Editor's for field UI.
* For multi-field types, such as vec2, vec3, and others use array of strings.
* @param {boolean} [args.array] - If attribute can hold single or multiple values.
* @param {number} [args.size] - If attribute is array, maximum number of values can be set.
* @param {number} [args.min] - Minimum value for type 'number', if max and min defined, slider will be rendered in Editor's UI.
* @param {number} [args.max] - Maximum value for type 'number', if max and min defined, slider will be rendered in Editor's UI.
* @param {number} [args.precision] - Level of precision for field type 'number' with floating values.
* @param {number} [args.step] - Step value for type 'number'. The amount used to increment the value when using the arrow keys in the Editor's UI.
* @param {string} [args.assetType] - Name of asset type to be used in 'asset' type attribute picker in Editor's UI, defaults to '*' (all).
* @param {string[]} [args.curves] - List of names for Curves for field type 'curve'.
* @param {string} [args.color] - String of color channels for Curves for field type 'curve', can be any combination of `rgba` characters.
* Defining this property will render Gradient in Editor's field UI.
* @param {object[]} [args.enum] - List of fixed choices for field, defined as array of objects, where key in object is a title of an option.
* @example
* PlayerController.attributes.add('fullName', {
* type: 'string'
* });
* @example
* PlayerController.attributes.add('speed', {
* type: 'number',
* title: 'Speed',
* placeholder: 'km/h',
* default: 22.2
* });
* @example
* PlayerController.attributes.add('resolution', {
* type: 'number',
* default: 32,
* enum: [
* { '32x32': 32 },
* { '64x64': 64 },
* { '128x128': 128 }
* ]
* });
*/
ScriptAttributes.prototype.add = function (name, args) {
if (this.index[name]) {
// #ifdef DEBUG
console.warn('attribute \'' + name + '\' is already defined for script type \'' + this.scriptType.name + '\'');
// #endif
return;
} else if (pc.createScript.reservedAttributes[name]) {
// #ifdef DEBUG
console.warn('attribute \'' + name + '\' is a reserved attribute name');
// #endif
return;
}
this.index[name] = args;
Object.defineProperty(this.scriptType.prototype, name, {
get: function () {
return this.__attributes[name];
},
set: function (raw) {
var old = this.__attributes[name];
// convert to appropriate type
if (args.array) {
this.__attributes[name] = [];
if (raw) {
var i;
var len;
for (i = 0, len = raw.length; i < len; i++) {
this.__attributes[name].push(rawToValue(this.app, args, raw[i], old ? old[i] : null));
}
}
} else {
this.__attributes[name] = rawToValue(this.app, args, raw, old);
}
this.fire('attr', name, this.__attributes[name], old);
this.fire('attr:' + name, this.__attributes[name], old);
}
});
};
/**
* @function
* @name pc.ScriptAttributes#remove
* @description Remove Attribute.
* @param {string} name - Name of an attribute.
* @returns {boolean} True if removed or false if not defined.
* @example
* PlayerController.attributes.remove('fullName');
*/
ScriptAttributes.prototype.remove = function (name) {
if (!this.index[name])
return false;
delete this.index[name];
delete this.scriptType.prototype[name];
return true;
};
/**
* @function
* @name pc.ScriptAttributes#has
* @description Detect if Attribute is added.
* @param {string} name - Name of an attribute.
* @returns {boolean} True if Attribute is defined.
* @example
* if (PlayerController.attributes.has('fullName')) {
* // attribute fullName is defined
* }
*/
ScriptAttributes.prototype.has = function (name) {
return !!this.index[name];
};
/**
* @function
* @name pc.ScriptAttributes#get
* @description Get object with attribute arguments.
* Note: Changing argument properties will not affect existing Script Instances.
* @param {string} name - Name of an attribute.
* @returns {?object} Arguments with attribute properties.
* @example
* // changing default value for an attribute 'fullName'
* var attr = PlayerController.attributes.get('fullName');
* if (attr) attr.default = 'Unknown';
*/
ScriptAttributes.prototype.get = function (name) {
return this.index[name] || null;
};
return {
ScriptAttributes: ScriptAttributes
};
}());