@playcanvas/web-components 0.1.1 → 0.1.2
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/README.md +236 -1
- package/dist/components/script-component.d.ts +25 -0
- package/dist/components/script.d.ts +12 -6
- package/dist/pwc.cjs +115 -54
- package/dist/pwc.cjs.map +1 -1
- package/dist/pwc.js +115 -54
- 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 +115 -54
- package/dist/pwc.mjs.map +1 -1
- package/package.json +1 -1
- package/src/components/script-component.ts +120 -1
- package/src/components/script.ts +23 -65
- package/src/components/sound-component.ts +1 -1
- package/src/components/sound-slot.ts +1 -1
package/src/components/script.ts
CHANGED
|
@@ -1,85 +1,43 @@
|
|
|
1
|
-
import { ScriptType } from 'playcanvas';
|
|
2
|
-
|
|
3
|
-
import { AppElement } from '../app';
|
|
4
|
-
import { ScriptComponentElement } from './script-component';
|
|
5
|
-
|
|
6
1
|
/**
|
|
7
2
|
* Represents a script in the PlayCanvas engine.
|
|
8
3
|
*/
|
|
9
4
|
class ScriptElement extends HTMLElement {
|
|
10
|
-
private _attributes:
|
|
5
|
+
private _attributes: string = '{}';
|
|
11
6
|
|
|
12
7
|
private _enabled: boolean = true;
|
|
13
8
|
|
|
14
9
|
private _name: string = '';
|
|
15
10
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
if (!appElement) {
|
|
21
|
-
console.error(`${this.tagName.toLowerCase()} should be a descendant of pc-app`);
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
await appElement.getApplication();
|
|
26
|
-
|
|
27
|
-
const scriptAttributes = this.getAttribute('attributes');
|
|
28
|
-
if (scriptAttributes) {
|
|
29
|
-
try {
|
|
30
|
-
this._attributes = JSON.parse(scriptAttributes);
|
|
31
|
-
} catch (e) {
|
|
32
|
-
console.error('Failed to parse script attributes:', e);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// When the script is created, initialize it with the necessary attributes
|
|
37
|
-
this.scriptsElement?.component!.on(`create:${this._name}`, (scriptInstance) => {
|
|
38
|
-
Object.assign(scriptInstance, this._attributes);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
this._script = this.scriptsElement?.component!.create(this._name, { preloading: false }) ?? null;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
disconnectedCallback() {
|
|
45
|
-
this.scriptsElement?.component!.destroy(this._name);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
refreshAttributes() {
|
|
49
|
-
if (this._script) {
|
|
50
|
-
Object.entries(this._attributes).forEach(([name, value]) => {
|
|
51
|
-
if (this._script && name in this._script) {
|
|
52
|
-
(this._script as any)[name] = value;
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
protected get scriptsElement(): ScriptComponentElement | null {
|
|
59
|
-
const scriptsElement = this.parentElement as ScriptComponentElement;
|
|
60
|
-
|
|
61
|
-
if (!(scriptsElement instanceof ScriptComponentElement)) {
|
|
62
|
-
console.warn('pc-script must be a direct child of a pc-scripts element');
|
|
63
|
-
return null;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return scriptsElement;
|
|
67
|
-
}
|
|
68
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Sets the attributes of the script.
|
|
13
|
+
* @param value - The attributes of the script.
|
|
14
|
+
*/
|
|
69
15
|
set scriptAttributes(value: string) {
|
|
70
|
-
this._attributes =
|
|
71
|
-
this.
|
|
16
|
+
this._attributes = value;
|
|
17
|
+
this.dispatchEvent(new CustomEvent('scriptattributeschange', {
|
|
18
|
+
detail: { attributes: value },
|
|
19
|
+
bubbles: true
|
|
20
|
+
}));
|
|
72
21
|
}
|
|
73
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Gets the attributes of the script.
|
|
25
|
+
* @returns The attributes of the script.
|
|
26
|
+
*/
|
|
74
27
|
get scriptAttributes() {
|
|
75
|
-
return
|
|
28
|
+
return this._attributes;
|
|
76
29
|
}
|
|
77
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Sets the enabled state of the script.
|
|
33
|
+
* @param value - The enabled state of the script.
|
|
34
|
+
*/
|
|
78
35
|
set enabled(value: boolean) {
|
|
79
36
|
this._enabled = value;
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
37
|
+
this.dispatchEvent(new CustomEvent('scriptenablechange', {
|
|
38
|
+
detail: { enabled: value },
|
|
39
|
+
bubbles: true
|
|
40
|
+
}));
|
|
83
41
|
}
|
|
84
42
|
|
|
85
43
|
/**
|