@playcanvas/web-components 0.8.1 → 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.
Files changed (71) hide show
  1. package/dist/app.d.ts +10 -2
  2. package/dist/asset.d.ts +18 -3
  3. package/dist/async-element.d.ts +51 -4
  4. package/dist/components/button-component.d.ts +10 -4
  5. package/dist/components/camera-component.d.ts +9 -4
  6. package/dist/components/collision-component.d.ts +9 -4
  7. package/dist/components/component.d.ts +7 -1
  8. package/dist/components/element-component.d.ts +17 -12
  9. package/dist/components/gsplat-component.d.ts +6 -1
  10. package/dist/components/layoutchild-component.d.ts +6 -1
  11. package/dist/components/layoutgroup-component.d.ts +21 -15
  12. package/dist/components/light-component.d.ts +12 -6
  13. package/dist/components/listener-component.d.ts +6 -1
  14. package/dist/components/particlesystem-component.d.ts +6 -1
  15. package/dist/components/render-component.d.ts +13 -4
  16. package/dist/components/rigidbody-component.d.ts +9 -4
  17. package/dist/components/screen-component.d.ts +6 -1
  18. package/dist/components/script-component.d.ts +116 -16
  19. package/dist/components/script.d.ts +72 -8
  20. package/dist/components/scrollbar-component.d.ts +10 -4
  21. package/dist/components/scrollview-component.d.ts +17 -10
  22. package/dist/components/sound-component.d.ts +6 -1
  23. package/dist/components/sound-slot.d.ts +8 -3
  24. package/dist/entity.d.ts +26 -2
  25. package/dist/index.d.ts +3 -2
  26. package/dist/material.d.ts +10 -0
  27. package/dist/model.d.ts +5 -0
  28. package/dist/module.d.ts +5 -0
  29. package/dist/pwc.cjs +1574 -853
  30. package/dist/pwc.cjs.map +1 -1
  31. package/dist/pwc.js +1574 -853
  32. package/dist/pwc.js.map +1 -1
  33. package/dist/pwc.min.js +1 -1
  34. package/dist/pwc.min.js.map +1 -1
  35. package/dist/pwc.mjs +1575 -855
  36. package/dist/pwc.mjs.map +1 -1
  37. package/dist/scene.d.ts +14 -5
  38. package/dist/sky.d.ts +6 -0
  39. package/dist/utils.d.ts +81 -23
  40. package/package.json +23 -13
  41. package/src/app.ts +38 -12
  42. package/src/asset.ts +61 -8
  43. package/src/async-element.ts +86 -5
  44. package/src/components/button-component.ts +26 -19
  45. package/src/components/camera-component.ts +29 -23
  46. package/src/components/collision-component.ts +19 -13
  47. package/src/components/component.ts +32 -13
  48. package/src/components/element-component.ts +58 -52
  49. package/src/components/gsplat-component.ts +14 -7
  50. package/src/components/layoutchild-component.ts +16 -9
  51. package/src/components/layoutgroup-component.ts +38 -31
  52. package/src/components/light-component.ts +33 -31
  53. package/src/components/listener-component.ts +8 -2
  54. package/src/components/particlesystem-component.ts +8 -2
  55. package/src/components/render-component.ts +19 -8
  56. package/src/components/rigidbody-component.ts +21 -15
  57. package/src/components/screen-component.ts +15 -9
  58. package/src/components/script-component.ts +512 -125
  59. package/src/components/script.ts +116 -16
  60. package/src/components/scrollbar-component.ts +19 -12
  61. package/src/components/scrollview-component.ts +37 -29
  62. package/src/components/sound-component.ts +16 -9
  63. package/src/components/sound-slot.ts +23 -14
  64. package/src/entity.ts +61 -71
  65. package/src/index.ts +5 -2
  66. package/src/material.ts +34 -1
  67. package/src/model.ts +6 -0
  68. package/src/module.ts +6 -0
  69. package/src/scene.ts +25 -12
  70. package/src/sky.ts +34 -16
  71. 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 color string into a Color object. String can be in the format of '#rgb', '#rgba',
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
- * @param value - The color string to parse.
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
- // Check if it's a CSS color name first
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
- if (value.startsWith('#')) {
20
- return new Color().fromString(value);
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
- const components = value.split(' ').map(Number);
24
- return new Color(components);
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 angles string into a Quat object. String can be in the format of 'x,y,z'.
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 Euler angles string to parse.
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
- const [x, y, z] = value.split(' ').map(Number);
35
- const q = new Quat();
36
- q.setFromEulerAngles(x, y, z);
37
- return q;
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 string into a Vec2 object. String can be in the format of 'x,y'.
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 Vec2 string to parse.
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
- const components = value.split(' ').map(Number);
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 string into a Vec3 object. String can be in the format of 'x,y,z'.
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 Vec3 string to parse.
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
- const components = value.split(' ').map(Number);
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 string into a Vec4 object. String can be in the format of 'x,y,z,w'.
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 Vec4 string to parse.
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
- const components = value.split(' ').map(Number);
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 supplied as either a named string (looked up in `map`) or a numeric
75
- * string. Falls back to `defaultValue` when the value is neither a known name nor a finite number.
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 map - A map of named values to their numeric enum equivalents.
79
- * @param defaultValue - The value to return when parsing fails.
80
- * @returns The resolved numeric enum value.
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 = (value: string, map: Map<string, number>, defaultValue: number): number => {
83
- const named = map.get(value);
84
- if (named !== undefined) {
85
- return named;
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
- const numeric = Number(value);
88
- return Number.isFinite(numeric) ? numeric : defaultValue;
228
+ return number;
89
229
  };
90
230
 
91
231
  /**