@playcanvas/web-components 0.8.2 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/app.d.ts +10 -2
- package/dist/asset.d.ts +18 -3
- package/dist/async-element.d.ts +51 -4
- package/dist/components/button-component.d.ts +10 -4
- package/dist/components/camera-component.d.ts +9 -4
- package/dist/components/collision-component.d.ts +9 -4
- package/dist/components/component.d.ts +7 -1
- package/dist/components/element-component.d.ts +17 -12
- package/dist/components/gsplat-component.d.ts +6 -1
- package/dist/components/layoutchild-component.d.ts +6 -1
- package/dist/components/layoutgroup-component.d.ts +21 -15
- package/dist/components/light-component.d.ts +12 -6
- package/dist/components/listener-component.d.ts +6 -1
- package/dist/components/particlesystem-component.d.ts +6 -1
- package/dist/components/render-component.d.ts +13 -4
- package/dist/components/rigidbody-component.d.ts +9 -4
- package/dist/components/screen-component.d.ts +6 -1
- package/dist/components/script-component.d.ts +116 -16
- package/dist/components/script.d.ts +72 -8
- package/dist/components/scrollbar-component.d.ts +10 -4
- package/dist/components/scrollview-component.d.ts +17 -10
- package/dist/components/sound-component.d.ts +6 -1
- package/dist/components/sound-slot.d.ts +8 -3
- package/dist/entity.d.ts +26 -2
- package/dist/index.d.ts +3 -2
- package/dist/material.d.ts +10 -0
- package/dist/model.d.ts +5 -0
- package/dist/module.d.ts +5 -0
- package/dist/pwc.cjs +1574 -853
- package/dist/pwc.cjs.map +1 -1
- package/dist/pwc.js +1574 -853
- package/dist/pwc.js.map +1 -1
- package/dist/pwc.min.js +1 -1
- package/dist/pwc.min.js.map +1 -1
- package/dist/pwc.mjs +1575 -855
- package/dist/pwc.mjs.map +1 -1
- package/dist/scene.d.ts +14 -5
- package/dist/sky.d.ts +6 -0
- package/dist/utils.d.ts +81 -23
- package/package.json +7 -7
- package/src/app.ts +38 -12
- package/src/asset.ts +61 -8
- package/src/async-element.ts +86 -5
- package/src/components/button-component.ts +26 -19
- package/src/components/camera-component.ts +29 -23
- package/src/components/collision-component.ts +19 -13
- package/src/components/component.ts +32 -13
- package/src/components/element-component.ts +58 -52
- package/src/components/gsplat-component.ts +14 -7
- package/src/components/layoutchild-component.ts +16 -9
- package/src/components/layoutgroup-component.ts +38 -31
- package/src/components/light-component.ts +33 -31
- package/src/components/listener-component.ts +8 -2
- package/src/components/particlesystem-component.ts +8 -2
- package/src/components/render-component.ts +19 -8
- package/src/components/rigidbody-component.ts +21 -15
- package/src/components/screen-component.ts +15 -9
- package/src/components/script-component.ts +512 -125
- package/src/components/script.ts +116 -16
- package/src/components/scrollbar-component.ts +19 -12
- package/src/components/scrollview-component.ts +37 -29
- package/src/components/sound-component.ts +16 -9
- package/src/components/sound-slot.ts +23 -14
- package/src/entity.ts +61 -71
- package/src/index.ts +5 -2
- package/src/material.ts +34 -1
- package/src/model.ts +6 -0
- package/src/module.ts +6 -0
- package/src/scene.ts +25 -12
- package/src/sky.ts +34 -16
- package/src/utils.ts +180 -40
package/src/utils.ts
CHANGED
|
@@ -3,89 +3,229 @@ import { Color, Entity, Quat, Vec2, Vec3, Vec4 } from 'playcanvas';
|
|
|
3
3
|
import { CSS_COLORS } from './colors';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* Parse a
|
|
7
|
-
* '#rrggbb', '#rrggbbaa', or a string of 3 or 4 comma-delimited numbers.
|
|
6
|
+
* Parse a boolean attribute value. The same rules apply to every boolean attribute:
|
|
8
7
|
*
|
|
9
|
-
*
|
|
8
|
+
* - Attribute absent (or removed): the supplied default is used.
|
|
9
|
+
* - Attribute set to the string 'false': `false`.
|
|
10
|
+
* - Attribute present with any other value, including the empty string of a bare boolean
|
|
11
|
+
* attribute (e.g. `<pc-light cast-shadows>`): `true`.
|
|
12
|
+
*
|
|
13
|
+
* @param value - The attribute value to parse (`null` when the attribute is absent).
|
|
14
|
+
* @param defaultValue - The value to use when the attribute is absent or removed.
|
|
15
|
+
* @returns The parsed boolean.
|
|
16
|
+
*/
|
|
17
|
+
export const parseBool = (value: string | null, defaultValue: boolean): boolean => {
|
|
18
|
+
return value === null ? defaultValue : value !== 'false';
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Splits an attribute value into exactly `count` numeric components. Returns `null` when the
|
|
23
|
+
* value does not consist of exactly `count` whitespace-separated finite numbers.
|
|
24
|
+
*
|
|
25
|
+
* @param value - The value to split.
|
|
26
|
+
* @param count - The required number of components.
|
|
27
|
+
* @returns The parsed components, or `null`.
|
|
28
|
+
* @ignore
|
|
29
|
+
*/
|
|
30
|
+
export const parseComponents = (value: string, count: number): number[] | null => {
|
|
31
|
+
const components = value.trim().split(/\s+/).map(Number);
|
|
32
|
+
if (components.length !== count || components.some(component => !Number.isFinite(component))) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
return components;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Clones a math-type default so parsed results never alias the caller's default instance. This
|
|
40
|
+
* is what makes it safe to pass the engine's shared frozen constants (e.g. `Vec3.ZERO`,
|
|
41
|
+
* `Color.WHITE`) as defaults.
|
|
42
|
+
*
|
|
43
|
+
* @param value - The default value to clone (`null` is passed through).
|
|
44
|
+
* @returns The cloned value.
|
|
45
|
+
*/
|
|
46
|
+
const cloneDefault = <T extends Color | Quat | Vec2 | Vec3 | Vec4 | null>(value: T): T => {
|
|
47
|
+
return (value === null ? null : value.clone()) as T;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Parse a color attribute value. The expected format is a CSS color name (e.g. 'rebeccapurple'),
|
|
52
|
+
* a hex color (e.g. '#ff0000' or '#f00'), or 3 or 4 space-separated numbers in the range 0 to 1
|
|
53
|
+
* (e.g. '1 0.5 0.5' or '1 0.5 0.5 0.5'). Returns `defaultValue` (cloned, when it is a color)
|
|
54
|
+
* when the attribute is absent (`null`), or when the value is malformed — the latter also logs
|
|
55
|
+
* a warning.
|
|
56
|
+
*
|
|
57
|
+
* @param value - The attribute value to parse (`null` when the attribute is absent).
|
|
58
|
+
* @param defaultValue - The value to use when the attribute is absent or invalid.
|
|
59
|
+
* @param attribute - The attribute name, used in the warning message.
|
|
10
60
|
* @returns The parsed Color object.
|
|
11
61
|
*/
|
|
12
|
-
export const parseColor = (value: string): Color => {
|
|
13
|
-
|
|
62
|
+
export const parseColor = <T extends Color | null>(value: string | null, defaultValue: T, attribute: string): Color | T => {
|
|
63
|
+
if (value === null) {
|
|
64
|
+
return cloneDefault(defaultValue);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// A CSS color name (e.g. 'rebeccapurple')
|
|
14
68
|
const hexColor = CSS_COLORS[value.toLowerCase()];
|
|
15
69
|
if (hexColor) {
|
|
16
70
|
return new Color().fromString(hexColor);
|
|
17
71
|
}
|
|
18
72
|
|
|
19
|
-
|
|
20
|
-
|
|
73
|
+
// A hex color (e.g. '#ff0000'), expanding short forms (e.g. '#f00') for Color.fromString
|
|
74
|
+
if (/^#(?:[0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(value)) {
|
|
75
|
+
let hex = value.slice(1);
|
|
76
|
+
if (hex.length === 3 || hex.length === 4) {
|
|
77
|
+
hex = hex.split('').map(char => char + char).join('');
|
|
78
|
+
}
|
|
79
|
+
return new Color().fromString(`#${hex}`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// 3 or 4 space-separated components (e.g. '1 0.5 0.5')
|
|
83
|
+
const components = parseComponents(value, 4) ?? parseComponents(value, 3);
|
|
84
|
+
if (components) {
|
|
85
|
+
return new Color(components);
|
|
21
86
|
}
|
|
22
87
|
|
|
23
|
-
|
|
24
|
-
return
|
|
88
|
+
console.warn(`Invalid value '${value}' for attribute '${attribute}'. Expected a CSS color name, a hex color or 3 or 4 space-separated numbers. Using '${defaultValue}'.`);
|
|
89
|
+
return cloneDefault(defaultValue);
|
|
25
90
|
};
|
|
26
91
|
|
|
27
92
|
/**
|
|
28
|
-
* Parse an Euler
|
|
93
|
+
* Parse an Euler-angles attribute value into a quaternion. The expected format is 3
|
|
94
|
+
* space-separated angles in degrees (e.g. '0 90 0'). Returns `defaultValue` (cloned, when it is
|
|
95
|
+
* a quaternion) when the attribute is absent (`null`), or when the value is malformed — the
|
|
96
|
+
* latter also logs a warning.
|
|
29
97
|
*
|
|
30
|
-
* @param value - The
|
|
98
|
+
* @param value - The attribute value to parse (`null` when the attribute is absent).
|
|
99
|
+
* @param defaultValue - The value to use when the attribute is absent or invalid.
|
|
100
|
+
* @param attribute - The attribute name, used in the warning message.
|
|
31
101
|
* @returns The parsed Quat object.
|
|
32
102
|
*/
|
|
33
|
-
export const parseQuat = (value: string): Quat => {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
103
|
+
export const parseQuat = <T extends Quat | null>(value: string | null, defaultValue: T, attribute: string): Quat | T => {
|
|
104
|
+
if (value === null) {
|
|
105
|
+
return cloneDefault(defaultValue);
|
|
106
|
+
}
|
|
107
|
+
const components = parseComponents(value, 3);
|
|
108
|
+
if (!components) {
|
|
109
|
+
console.warn(`Invalid value '${value}' for attribute '${attribute}'. Expected 3 space-separated numbers. Using '${defaultValue}'.`);
|
|
110
|
+
return cloneDefault(defaultValue);
|
|
111
|
+
}
|
|
112
|
+
return new Quat().setFromEulerAngles(components[0], components[1], components[2]);
|
|
38
113
|
};
|
|
39
114
|
|
|
40
115
|
/**
|
|
41
|
-
* Parse a Vec2
|
|
116
|
+
* Parse a Vec2 attribute value. The expected format is 2 space-separated numbers (e.g. '1 2').
|
|
117
|
+
* Returns `defaultValue` (cloned, when it is a vector) when the attribute is absent (`null`),
|
|
118
|
+
* or when the value is malformed — the latter also logs a warning.
|
|
42
119
|
*
|
|
43
|
-
* @param value - The
|
|
120
|
+
* @param value - The attribute value to parse (`null` when the attribute is absent).
|
|
121
|
+
* @param defaultValue - The value to use when the attribute is absent or invalid.
|
|
122
|
+
* @param attribute - The attribute name, used in the warning message.
|
|
44
123
|
* @returns The parsed Vec2 object.
|
|
45
124
|
*/
|
|
46
|
-
export const parseVec2 = (value: string): Vec2 => {
|
|
47
|
-
|
|
125
|
+
export const parseVec2 = <T extends Vec2 | null>(value: string | null, defaultValue: T, attribute: string): Vec2 | T => {
|
|
126
|
+
if (value === null) {
|
|
127
|
+
return cloneDefault(defaultValue);
|
|
128
|
+
}
|
|
129
|
+
const components = parseComponents(value, 2);
|
|
130
|
+
if (!components) {
|
|
131
|
+
console.warn(`Invalid value '${value}' for attribute '${attribute}'. Expected 2 space-separated numbers. Using '${defaultValue}'.`);
|
|
132
|
+
return cloneDefault(defaultValue);
|
|
133
|
+
}
|
|
48
134
|
return new Vec2(components);
|
|
49
135
|
};
|
|
50
136
|
|
|
51
137
|
/**
|
|
52
|
-
* Parse a Vec3
|
|
138
|
+
* Parse a Vec3 attribute value. The expected format is 3 space-separated numbers (e.g. '1 2 3').
|
|
139
|
+
* Returns `defaultValue` (cloned, when it is a vector) when the attribute is absent (`null`),
|
|
140
|
+
* or when the value is malformed — the latter also logs a warning.
|
|
53
141
|
*
|
|
54
|
-
* @param value - The
|
|
142
|
+
* @param value - The attribute value to parse (`null` when the attribute is absent).
|
|
143
|
+
* @param defaultValue - The value to use when the attribute is absent or invalid.
|
|
144
|
+
* @param attribute - The attribute name, used in the warning message.
|
|
55
145
|
* @returns The parsed Vec3 object.
|
|
56
146
|
*/
|
|
57
|
-
export const parseVec3 = (value: string): Vec3 => {
|
|
58
|
-
|
|
147
|
+
export const parseVec3 = <T extends Vec3 | null>(value: string | null, defaultValue: T, attribute: string): Vec3 | T => {
|
|
148
|
+
if (value === null) {
|
|
149
|
+
return cloneDefault(defaultValue);
|
|
150
|
+
}
|
|
151
|
+
const components = parseComponents(value, 3);
|
|
152
|
+
if (!components) {
|
|
153
|
+
console.warn(`Invalid value '${value}' for attribute '${attribute}'. Expected 3 space-separated numbers. Using '${defaultValue}'.`);
|
|
154
|
+
return cloneDefault(defaultValue);
|
|
155
|
+
}
|
|
59
156
|
return new Vec3(components);
|
|
60
157
|
};
|
|
61
158
|
|
|
62
159
|
/**
|
|
63
|
-
* Parse a Vec4
|
|
160
|
+
* Parse a Vec4 attribute value. The expected format is 4 space-separated numbers
|
|
161
|
+
* (e.g. '1 2 3 4'). Returns `defaultValue` (cloned, when it is a vector) when the attribute is
|
|
162
|
+
* absent (`null`), or when the value is malformed — the latter also logs a warning.
|
|
64
163
|
*
|
|
65
|
-
* @param value - The
|
|
164
|
+
* @param value - The attribute value to parse (`null` when the attribute is absent).
|
|
165
|
+
* @param defaultValue - The value to use when the attribute is absent or invalid.
|
|
166
|
+
* @param attribute - The attribute name, used in the warning message.
|
|
66
167
|
* @returns The parsed Vec4 object.
|
|
67
168
|
*/
|
|
68
|
-
export const parseVec4 = (value: string): Vec4 => {
|
|
69
|
-
|
|
169
|
+
export const parseVec4 = <T extends Vec4 | null>(value: string | null, defaultValue: T, attribute: string): Vec4 | T => {
|
|
170
|
+
if (value === null) {
|
|
171
|
+
return cloneDefault(defaultValue);
|
|
172
|
+
}
|
|
173
|
+
const components = parseComponents(value, 4);
|
|
174
|
+
if (!components) {
|
|
175
|
+
console.warn(`Invalid value '${value}' for attribute '${attribute}'. Expected 4 space-separated numbers. Using '${defaultValue}'.`);
|
|
176
|
+
return cloneDefault(defaultValue);
|
|
177
|
+
}
|
|
70
178
|
return new Vec4(components);
|
|
71
179
|
};
|
|
72
180
|
|
|
73
181
|
/**
|
|
74
|
-
* Resolves an enum value
|
|
75
|
-
*
|
|
182
|
+
* Resolves an enum attribute value against its set of valid names. Returns the value when it is
|
|
183
|
+
* one of the valid names. Returns `defaultValue` when the attribute is absent (`null`), or when
|
|
184
|
+
* the value is invalid — the latter also logs a warning listing the valid names.
|
|
76
185
|
*
|
|
77
|
-
* @param value - The attribute value to parse.
|
|
78
|
-
* @param
|
|
79
|
-
* @param defaultValue - The value to
|
|
80
|
-
* @
|
|
186
|
+
* @param value - The attribute value to parse (`null` when the attribute is absent).
|
|
187
|
+
* @param valid - The valid names: an array, or a map whose keys are the valid names.
|
|
188
|
+
* @param defaultValue - The value to use when the attribute is absent or invalid.
|
|
189
|
+
* @param attribute - The attribute name, used in the warning message.
|
|
190
|
+
* @returns The resolved enum name.
|
|
81
191
|
*/
|
|
82
|
-
export const parseEnum =
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
192
|
+
export const parseEnum = <T extends string>(
|
|
193
|
+
value: string | null,
|
|
194
|
+
valid: readonly T[] | ReadonlyMap<T, number>,
|
|
195
|
+
defaultValue: T,
|
|
196
|
+
attribute: string
|
|
197
|
+
): T => {
|
|
198
|
+
if (value === null) {
|
|
199
|
+
return defaultValue;
|
|
200
|
+
}
|
|
201
|
+
const names = Array.isArray(valid) ? valid : [...(valid as ReadonlyMap<T, number>).keys()];
|
|
202
|
+
if (names.includes(value as T)) {
|
|
203
|
+
return value as T;
|
|
204
|
+
}
|
|
205
|
+
console.warn(`Invalid value '${value}' for attribute '${attribute}'. Valid values: ${names.join(', ')}. Using '${defaultValue}'.`);
|
|
206
|
+
return defaultValue;
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Parses a number attribute value. Returns the parsed number when the value is a finite number.
|
|
211
|
+
* Returns `defaultValue` when the attribute is absent (`null`), or when the value is not a
|
|
212
|
+
* finite number — the latter also logs a warning.
|
|
213
|
+
*
|
|
214
|
+
* @param value - The attribute value to parse (`null` when the attribute is absent).
|
|
215
|
+
* @param defaultValue - The value to use when the attribute is absent or invalid.
|
|
216
|
+
* @param attribute - The attribute name, used in the warning message.
|
|
217
|
+
* @returns The parsed number.
|
|
218
|
+
*/
|
|
219
|
+
export const parseNumber = <T extends number | null>(value: string | null, defaultValue: T, attribute: string): number | T => {
|
|
220
|
+
if (value === null) {
|
|
221
|
+
return defaultValue;
|
|
222
|
+
}
|
|
223
|
+
const number = value.trim() === '' ? NaN : Number(value);
|
|
224
|
+
if (!Number.isFinite(number)) {
|
|
225
|
+
console.warn(`Invalid value '${value}' for attribute '${attribute}'. Expected a finite number. Using '${defaultValue}'.`);
|
|
226
|
+
return defaultValue;
|
|
86
227
|
}
|
|
87
|
-
|
|
88
|
-
return Number.isFinite(numeric) ? numeric : defaultValue;
|
|
228
|
+
return number;
|
|
89
229
|
};
|
|
90
230
|
|
|
91
231
|
/**
|