@playcanvas/web-components 0.19.0 → 0.21.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 (104) hide show
  1. package/dist/app.d.cts +5 -99
  2. package/dist/app.d.ts +5 -99
  3. package/dist/asset-binding.d.cts +1 -0
  4. package/dist/asset-binding.d.ts +1 -0
  5. package/dist/components/anim-clip.d.cts +8 -10
  6. package/dist/components/anim-clip.d.ts +8 -10
  7. package/dist/components/anim-component.d.cts +5 -4
  8. package/dist/components/anim-component.d.ts +5 -4
  9. package/dist/components/audio-listener-component.d.cts +5 -4
  10. package/dist/components/audio-listener-component.d.ts +5 -4
  11. package/dist/components/button-component.d.cts +14 -9
  12. package/dist/components/button-component.d.ts +14 -9
  13. package/dist/components/camera-component.d.cts +5 -4
  14. package/dist/components/camera-component.d.ts +5 -4
  15. package/dist/components/collision-component.d.cts +5 -4
  16. package/dist/components/collision-component.d.ts +5 -4
  17. package/dist/components/component.d.cts +2 -2
  18. package/dist/components/component.d.ts +2 -2
  19. package/dist/components/element-component.d.cts +5 -4
  20. package/dist/components/element-component.d.ts +5 -4
  21. package/dist/components/gsplat-component.d.cts +5 -4
  22. package/dist/components/gsplat-component.d.ts +5 -4
  23. package/dist/components/joint-component.d.cts +29 -14
  24. package/dist/components/joint-component.d.ts +29 -14
  25. package/dist/components/layout-child-component.d.cts +5 -4
  26. package/dist/components/layout-child-component.d.ts +5 -4
  27. package/dist/components/layout-group-component.d.cts +5 -4
  28. package/dist/components/layout-group-component.d.ts +5 -4
  29. package/dist/components/light-component.d.cts +5 -4
  30. package/dist/components/light-component.d.ts +5 -4
  31. package/dist/components/particle-system-component.d.cts +22 -5
  32. package/dist/components/particle-system-component.d.ts +22 -5
  33. package/dist/components/render-component.d.cts +5 -4
  34. package/dist/components/render-component.d.ts +5 -4
  35. package/dist/components/rigid-body-component.d.cts +5 -4
  36. package/dist/components/rigid-body-component.d.ts +5 -4
  37. package/dist/components/screen-component.d.cts +5 -4
  38. package/dist/components/screen-component.d.ts +5 -4
  39. package/dist/components/script-component.d.cts +9 -6
  40. package/dist/components/script-component.d.ts +9 -6
  41. package/dist/components/script-instance.d.cts +14 -6
  42. package/dist/components/script-instance.d.ts +14 -6
  43. package/dist/components/scroll-view-component.d.cts +29 -16
  44. package/dist/components/scroll-view-component.d.ts +29 -16
  45. package/dist/components/scrollbar-component.d.cts +11 -7
  46. package/dist/components/scrollbar-component.d.ts +11 -7
  47. package/dist/components/sound-component.d.cts +5 -4
  48. package/dist/components/sound-component.d.ts +5 -4
  49. package/dist/custom-elements.json +59 -59
  50. package/dist/entity-reference.d.cts +13 -0
  51. package/dist/entity-reference.d.ts +13 -0
  52. package/dist/material.d.cts +8 -6
  53. package/dist/material.d.ts +8 -6
  54. package/dist/model.d.cts +8 -9
  55. package/dist/model.d.ts +8 -9
  56. package/dist/parse.d.cts +0 -3
  57. package/dist/parse.d.ts +0 -3
  58. package/dist/pointer-controller.d.cts +1 -0
  59. package/dist/pointer-controller.d.ts +1 -0
  60. package/dist/pwc.cjs +1085 -633
  61. package/dist/pwc.cjs.map +1 -1
  62. package/dist/pwc.js +1085 -633
  63. package/dist/pwc.js.map +1 -1
  64. package/dist/pwc.min.js +1 -1
  65. package/dist/pwc.min.js.map +1 -1
  66. package/dist/pwc.min.mjs +1 -1
  67. package/dist/pwc.min.mjs.map +1 -1
  68. package/dist/pwc.mjs +1086 -634
  69. package/dist/pwc.mjs.map +1 -1
  70. package/dist/sky.d.cts +6 -7
  71. package/dist/sky.d.ts +6 -7
  72. package/dist/vscode.html-custom-data.json +10 -10
  73. package/dist/web-types.json +58 -58
  74. package/package.json +3 -3
  75. package/src/app.ts +16 -416
  76. package/src/asset-binding.ts +141 -0
  77. package/src/components/anim-clip.ts +22 -45
  78. package/src/components/anim-component.ts +10 -6
  79. package/src/components/audio-listener-component.ts +6 -5
  80. package/src/components/button-component.ts +25 -15
  81. package/src/components/camera-component.ts +6 -5
  82. package/src/components/collision-component.ts +6 -5
  83. package/src/components/component.ts +6 -4
  84. package/src/components/element-component.ts +6 -5
  85. package/src/components/gsplat-component.ts +6 -5
  86. package/src/components/joint-component.ts +36 -20
  87. package/src/components/layout-child-component.ts +6 -5
  88. package/src/components/layout-group-component.ts +6 -5
  89. package/src/components/light-component.ts +6 -5
  90. package/src/components/particle-system-component.ts +56 -30
  91. package/src/components/render-component.ts +6 -5
  92. package/src/components/rigid-body-component.ts +6 -5
  93. package/src/components/screen-component.ts +6 -5
  94. package/src/components/script-component.ts +28 -17
  95. package/src/components/script-instance.ts +14 -6
  96. package/src/components/scroll-view-component.ts +56 -34
  97. package/src/components/scrollbar-component.ts +20 -13
  98. package/src/components/sound-component.ts +6 -5
  99. package/src/entity-reference.ts +237 -0
  100. package/src/material.ts +25 -29
  101. package/src/model.ts +24 -48
  102. package/src/parse.ts +0 -35
  103. package/src/pointer-controller.ts +550 -0
  104. package/src/sky.ts +19 -30
package/src/app.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { CameraComponent, GraphicsDevice, GraphNode, GSplatComponent, Entity } from 'playcanvas';
1
+ import type { GraphicsDevice, GraphNode, Entity } from 'playcanvas';
2
2
  import {
3
3
  AppBase,
4
4
  AppOptions,
@@ -7,7 +7,6 @@ import {
7
7
  FILLMODE_NONE,
8
8
  Keyboard,
9
9
  Mouse,
10
- Picker,
11
10
  RESOLUTION_AUTO,
12
11
  AnimComponentSystem,
13
12
  AnimationComponentSystem,
@@ -60,8 +59,7 @@ import {
60
59
  BatchManager,
61
60
  SoundManager,
62
61
  Lightmapper,
63
- XrManager,
64
- MeshInstance
62
+ XrManager
65
63
  } from 'playcanvas';
66
64
 
67
65
  import type { AssetElement } from './asset';
@@ -72,57 +70,9 @@ import type { EntityOwnerElement } from './entity-owner';
72
70
  import { LoadingBar } from './loading-bar';
73
71
  import type { MaterialElement } from './material';
74
72
  import { parseBool, parseEnum, parseNumber } from './parse';
73
+ import { PointerController } from './pointer-controller';
75
74
  import type { WasmElement } from './wasm';
76
75
 
77
- /**
78
- * The event types whose listeners make an element a hover target. Hover resolution walks past
79
- * elements listening for none of them, so a silent element never swallows an ancestor's
80
- * enter/leave pair.
81
- */
82
- const hoverEventTypes = ['pointerenter', 'pointerleave', 'pointermove'] as const;
83
-
84
- /**
85
- * The canvas listeners each synthesized event type is driven by. Enter and leave are derived
86
- * from move picks. A click is concluded from the down/up pair, with pointercancel discarding a
87
- * press the browser takes back (for example a touch that becomes a scroll).
88
- */
89
- const canvasEventsFor: Record<(typeof SYNTHESIZED_EVENTS)[number], readonly string[]> = {
90
- pointermove: ['pointermove'],
91
- pointerenter: ['pointermove'],
92
- pointerleave: ['pointermove'],
93
- pointerdown: ['pointerdown'],
94
- pointerup: ['pointerup'],
95
- click: ['pointerdown', 'pointerup', 'pointercancel']
96
- };
97
-
98
- /**
99
- * How long after a click a further click on the same target still raises the click count that
100
- * `detail` carries, approximating the platform's double-click time.
101
- */
102
- const CLICK_CHAIN_MS = 500;
103
-
104
- /**
105
- * Finds the nearest common inclusive ancestor of two picked nodes - the node a click belongs to
106
- * when the press and the release picked different geometry, exactly as the DOM assigns a click
107
- * whose down and up have different targets.
108
- *
109
- * @param a - The node the press picked, or `null`.
110
- * @param b - The node the release picked, or `null`.
111
- * @returns The nearest common inclusive ancestor, or `null` when there is none.
112
- */
113
- const commonAncestor = (a: GraphNode | null, b: GraphNode | null): GraphNode | null => {
114
- const ancestors = new Set<GraphNode>();
115
- for (let node = a; node !== null; node = node.parent) {
116
- ancestors.add(node);
117
- }
118
- for (let node = b; node !== null; node = node.parent) {
119
- if (ancestors.has(node)) {
120
- return node;
121
- }
122
- }
123
- return null;
124
- };
125
-
126
76
  /**
127
77
  * Gives `pc-app` the sizing contract of a replaced element (`<video>`, `<img>`): a block-level
128
78
  * box that the page's CSS sizes, defaulting to the canvas's own 300x150 intrinsic size, with the
@@ -220,37 +170,16 @@ class AppElement extends AsyncElement {
220
170
  */
221
171
  private _entityElements = new Map<GraphNode, EntityBaseElement>();
222
172
 
223
- private _picker: Picker | null = null;
224
-
225
- private _hoveredEntity: EntityBaseElement | null = null;
226
-
227
- // Identifies the newest in-flight hover pick, so out-of-order results can be discarded
228
- private _pickToken = 0;
229
-
230
- private _pointerHandlers: Record<string, EventListener | null> = {
231
- pointermove: null,
232
- pointerdown: null,
233
- pointerup: null,
234
- pointercancel: null
235
- };
236
-
237
173
  /**
238
- * The pick of each pointer's primary-button press, keyed by pointerId and kept while a click
239
- * may still conclude it. The promise is stored rather than its result, so a release can
240
- * await a press pick that has not resolved yet. Entries are removed by the matching
241
- * pointerup or pointercancel, and only ever stored while some element listens for click -
242
- * which is also what keeps those two canvas listeners attached.
174
+ * The pointer-input subsystem: the picker, the canvas handlers, and the synthesized-event
175
+ * dispatch. The element drives its lifecycle (connect on boot, resize with the drawing
176
+ * buffer, listener syncs, disconnect on teardown) and hands it the two lookups it needs -
177
+ * everything else about pointer input lives in the controller.
243
178
  */
244
- private _downPicks = new Map<number, Promise<GraphNode | null>>();
245
-
246
- /** Whether any element in the tree listens for click. Maintained by _syncCanvasListeners. */
247
- private _clickListened = false;
248
-
249
- /**
250
- * The previous click's target, time and count, for chaining successive clicks into the
251
- * click count that `detail` carries. `null` until a click has fired.
252
- */
253
- private _lastClick: { element: EntityBaseElement; time: number; count: number } | null = null;
179
+ private _pointer = new PointerController({
180
+ elementFromNode: (node) => this._entityElements.get(node) ?? null,
181
+ pointerTargets: () => Array.from(this.querySelectorAll<EntityBaseElement>('pc-entity, pc-model, pc-node'))
182
+ });
254
183
 
255
184
  private _app: AppBase | null = null;
256
185
 
@@ -296,8 +225,8 @@ class AppElement extends AsyncElement {
296
225
  // entities. Registered once here rather than on every boot - the sync no-ops while there
297
226
  // is no canvas, and a re-booted element must not stack a second set.
298
227
  SYNTHESIZED_EVENTS.forEach((type) => {
299
- this.addEventListener(`${type}:connect`, () => this._syncCanvasListeners());
300
- this.addEventListener(`${type}:disconnect`, () => this._syncCanvasListeners());
228
+ this.addEventListener(`${type}:connect`, () => this._pointer.syncListeners());
229
+ this.addEventListener(`${type}:disconnect`, () => this._pointer.syncListeners());
301
230
  });
302
231
  }
303
232
 
@@ -490,7 +419,7 @@ class AppElement extends AsyncElement {
490
419
  app.setCanvasFillMode(FILLMODE_NONE);
491
420
  app.setCanvasResolution(RESOLUTION_AUTO);
492
421
 
493
- this._pickerCreate();
422
+ this._pointer.connect(app, this._canvas!);
494
423
 
495
424
  // Track the element's box rather than the window: containers resize without any window
496
425
  // event (splitter drags, flex reflow, animations). Guarded because jsdom has no
@@ -600,7 +529,7 @@ class AppElement extends AsyncElement {
600
529
  this._bootGeneration++;
601
530
 
602
531
  this._optionsLocked = false;
603
- this._pickerDestroy();
532
+ this._pointer.disconnect();
604
533
 
605
534
  // Clean up the application. Destroying it destroys every entity, whose destroy hooks
606
535
  // unregister them - clear() covers any entity the engine no longer reached.
@@ -641,55 +570,7 @@ class AppElement extends AsyncElement {
641
570
  }
642
571
  this.app.updateCanvasSize();
643
572
  const { width, height } = this.app.graphicsDevice;
644
- this._picker?.resize(width, height);
645
- }
646
-
647
- private _pickerCreate() {
648
- const { width, height } = this.app!.graphicsDevice;
649
- this._picker = new Picker(this.app!, width, height);
650
-
651
- // Create bound handlers but don't attach them yet. The handlers pick asynchronously, so
652
- // each is wrapped to discard the promise - a listener must not return one, and nothing
653
- // awaits the result.
654
- const listener = (handler: (event: PointerEvent) => Promise<void>): EventListener => {
655
- return (event: Event) => {
656
- handler.call(this, event as PointerEvent);
657
- };
658
- };
659
-
660
- this._pointerHandlers.pointermove = listener(this._onPointerMove);
661
- this._pointerHandlers.pointerdown = listener(this._onPointerDown);
662
- this._pointerHandlers.pointerup = listener(this._onPointerUp);
663
- this._pointerHandlers.pointercancel = (event: Event) => {
664
- this._downPicks.delete((event as PointerEvent).pointerId);
665
- };
666
-
667
- // Attach canvas listeners for element listeners registered before this boot (e.g.
668
- // handlers created from inline attributes when their elements were first upgraded, or
669
- // listeners carried over from before a re-boot)
670
- this._syncCanvasListeners();
671
- }
672
-
673
- private _pickerDestroy() {
674
- if (this._canvas) {
675
- Object.entries(this._pointerHandlers).forEach(([type, handler]) => {
676
- if (handler) {
677
- this._canvas!.removeEventListener(type, handler);
678
- }
679
- });
680
- }
681
-
682
- this._picker = null;
683
- this._hoveredEntity = null;
684
- this._pointerHandlers = {
685
- pointermove: null,
686
- pointerdown: null,
687
- pointerup: null,
688
- pointercancel: null
689
- };
690
- this._downPicks.clear();
691
- this._clickListened = false;
692
- this._lastClick = null;
573
+ this._pointer.resize(width, height);
693
574
  }
694
575
 
695
576
  /**
@@ -727,287 +608,6 @@ class AppElement extends AsyncElement {
727
608
  return this._entityElements.get(entity) ?? null;
728
609
  }
729
610
 
730
- /**
731
- * Resolves the element that owns hover for a picked node: the nearest node up the parent
732
- * chain - starting with the node itself - whose element listens for any of the hover event
733
- * types. Skipping silent elements matches {@link _elementWithListener}, so a registered
734
- * element with no hover listeners (a `<pc-model>` host, a plain child entity) is transparent
735
- * to hover rather than swallowing a listening ancestor's enter/leave pair.
736
- *
737
- * @param node - The picked node, or `null`.
738
- * @returns The hover-owning element, or `null`.
739
- */
740
- private _hoverTarget(node: GraphNode | null): EntityBaseElement | null {
741
- while (node !== null) {
742
- const element = this._entityElements.get(node);
743
- if (element && hoverEventTypes.some((type) => element._hasListeners(type))) {
744
- return element;
745
- }
746
- node = node.parent;
747
- }
748
- return null;
749
- }
750
-
751
- /**
752
- * Like {@link _elementFromNode}, but skips elements without a listener for `type`, so a hit
753
- * on an unlistened child still reaches a listening ancestor.
754
- *
755
- * @param node - The picked node, or `null`.
756
- * @param type - The pointer event type a listener is required for.
757
- * @returns The nearest listening element, or `null`.
758
- */
759
- private _elementWithListener(node: GraphNode | null, type: string): EntityBaseElement | null {
760
- while (node !== null) {
761
- const element = this._entityElements.get(node);
762
- if (element?._hasListeners(type)) {
763
- return element;
764
- }
765
- node = node.parent;
766
- }
767
- return null;
768
- }
769
-
770
- /**
771
- * Converts a pointer event's client coordinates into drawing-buffer coordinates - the space
772
- * the pick buffer and the camera viewports are laid out in. When the canvas has no CSS box
773
- * to map through (jsdom; a hidden canvas receives no pointer events in a browser), the
774
- * client coordinates are passed through unmapped and `mapped` is false, so callers know the
775
- * coordinates correspond to no real geometry.
776
- *
777
- * @param event - The pointer event to convert.
778
- * @param canvas - The canvas the event was dispatched on.
779
- * @returns The buffer-space coordinates, and whether they were actually mapped.
780
- */
781
- private _getPickerCoordinates(
782
- event: PointerEvent,
783
- canvas: HTMLCanvasElement
784
- ): { x: number; y: number; mapped: boolean } {
785
- const canvasRect = canvas.getBoundingClientRect();
786
- if (canvasRect.width === 0 || canvasRect.height === 0) {
787
- return { x: event.clientX, y: event.clientY, mapped: false };
788
- }
789
- const scaleX = canvas.width / canvasRect.width;
790
- const scaleY = canvas.height / canvasRect.height;
791
- return {
792
- x: (event.clientX - canvasRect.left) * scaleX,
793
- y: (event.clientY - canvasRect.top) * scaleY,
794
- mapped: true
795
- };
796
- }
797
-
798
- /**
799
- * Whether a camera's viewport contains the point. A camera renders into its normalized
800
- * `rect`, whose origin is the bottom-left of the canvas while buffer coordinates run from
801
- * the top-left - so the vertical test flips, as the engine's ElementInput flips it for UI
802
- * input. The right and bottom edges are exclusive: a viewport rasterizes the half-open
803
- * pixel range [left, right) x [top, bottom), so a coordinate on a shared edge belongs to
804
- * the viewport whose first pixel it is - never to the one it just left, whose pick buffer
805
- * holds nothing there.
806
- *
807
- * @param camera - The camera to test.
808
- * @param x - The x coordinate, in buffer space.
809
- * @param y - The y coordinate, in buffer space.
810
- * @param canvas - The canvas the coordinates are relative to.
811
- * @returns Whether the camera's viewport contains the point.
812
- */
813
- private _cameraContains(camera: CameraComponent, x: number, y: number, canvas: HTMLCanvasElement): boolean {
814
- const rect = camera.rect;
815
- const left = rect.x * canvas.width;
816
- const bottom = (1 - rect.y) * canvas.height;
817
- const top = bottom - rect.w * canvas.height;
818
- return x >= left && x < left + rect.z * canvas.width && y >= top && y < bottom;
819
- }
820
-
821
- /**
822
- * Picks the scene under the pointer and returns the graph node that was hit, or `null`.
823
- *
824
- * The camera is resolved the way the engine's ElementInput resolves it for UI input:
825
- * enabled cameras are tried topmost-first (they render in ascending `priority` order),
826
- * skipping cameras that render to a texture and cameras whose viewport `rect` does not
827
- * contain the pointer. A camera that picks nothing ends the search if it clears the color
828
- * buffer - its background visually owns the pixel - and otherwise cedes to the cameras
829
- * beneath it, so an overlay camera only intercepts picks where it actually drew something.
830
- * The pick buffer is prepared per camera, so each camera picks from its own layers.
831
- *
832
- * The read back is asynchronous because the synchronous {@link Picker.getSelection} is not
833
- * supported on WebGPU, where it returns an empty selection rather than failing - which
834
- * silently disabled every `onpointer*` handler once WebGPU became the resolved backend. The
835
- * async variant works on both backends and does not block the main thread on a GPU read.
836
- *
837
- * @param event - The pointer event to pick under.
838
- * @returns The graph node under the pointer, or `null` if nothing was hit.
839
- */
840
- private async _pickNode(event: PointerEvent): Promise<GraphNode | null> {
841
- const app = this.app;
842
- const picker = this._picker;
843
- const canvas = this._canvas;
844
- if (!app || !picker || !canvas) return null;
845
-
846
- const { x, y, mapped } = this._getPickerCoordinates(event, canvas);
847
-
848
- // Walked from the end: the array is sorted by ascending priority, so the last camera
849
- // renders last and sits on top. Read through .at() because a pick handler may remove
850
- // cameras while an earlier iteration's read back is in flight.
851
- const cameras = app.systems.camera?.cameras ?? [];
852
- for (let i = cameras.length - 1; i >= 0; i--) {
853
- const camera = cameras.at(i);
854
-
855
- // A camera rendering to a texture is not on the canvas.
856
- if (!camera || camera.renderTarget) continue;
857
-
858
- // Coordinates that could not be mapped cannot be tested for containment.
859
- if (mapped && !this._cameraContains(camera, x, y, canvas)) continue;
860
-
861
- picker.prepare(camera, app.scene);
862
- const selection = await picker.getSelectionAsync(x, y);
863
-
864
- // The element may have disconnected while the read back was in flight.
865
- if (!this._picker || !this.app) return null;
866
-
867
- if (selection.length > 0) {
868
- const item = selection[0];
869
- return item instanceof MeshInstance ? item.node : (item as GSplatComponent).entity;
870
- }
871
-
872
- // Nothing hit. A camera that clears the color buffer paints its background over
873
- // everything beneath it, so the miss is final; one that does not is an overlay
874
- // that the cameras beneath show through, so they get their turn.
875
- if (camera.clearColorBuffer) return null;
876
- }
877
-
878
- return null;
879
- }
880
-
881
- private async _onPointerMove(event: PointerEvent) {
882
- if (!this._picker || !this.app) return;
883
-
884
- // Moves arrive faster than a pick resolves, so results can land out of order. Only the
885
- // newest pick may update the hover state - an older one describes a pointer position the
886
- // user has already left.
887
- const token = ++this._pickToken;
888
- const node = await this._pickNode(event);
889
- if (token !== this._pickToken || !this._picker) return;
890
-
891
- // The hovered element is the nearest one up the node's parent chain with a hover
892
- // listener - the nearest-listener rule down/up use. Dispatch is still gated per event
893
- // type below: having any hover listener selects the target, each event needs its own.
894
- const newHoverEntity = this._hoverTarget(node);
895
-
896
- // Handle enter/leave events
897
- if (this._hoveredEntity !== newHoverEntity) {
898
- if (this._hoveredEntity && this._hoveredEntity._hasListeners('pointerleave')) {
899
- this._hoveredEntity.dispatchEvent(new PointerEvent('pointerleave', event));
900
- }
901
- if (newHoverEntity && newHoverEntity._hasListeners('pointerenter')) {
902
- newHoverEntity.dispatchEvent(new PointerEvent('pointerenter', event));
903
- }
904
- }
905
-
906
- // Update hover state
907
- this._hoveredEntity = newHoverEntity;
908
-
909
- // Handle pointermove event
910
- if (newHoverEntity && newHoverEntity._hasListeners('pointermove')) {
911
- newHoverEntity.dispatchEvent(new PointerEvent('pointermove', event));
912
- }
913
- }
914
-
915
- private async _onPointerDown(event: PointerEvent) {
916
- if (!this._picker || !this.app) return;
917
-
918
- const pick = this._pickNode(event);
919
-
920
- // A click concludes on the matching pointerup, which needs to know what the press
921
- // picked. Primary button only - the only button a click can conclude from - and only
922
- // while click is listened for, since it is the click mapping that keeps the pointerup
923
- // and pointercancel listeners attached to clean the entry up again.
924
- if (this._clickListened && event.button === 0) {
925
- this._downPicks.set(event.pointerId, pick);
926
- }
927
-
928
- const node = await pick;
929
- if (!this._picker) return; // the element disconnected while the pick was in flight
930
-
931
- const entityElement = this._elementWithListener(node, 'pointerdown');
932
- if (entityElement) {
933
- entityElement.dispatchEvent(new PointerEvent('pointerdown', event));
934
- }
935
- }
936
-
937
- private async _onPointerUp(event: PointerEvent) {
938
- if (!this._picker || !this.app) return;
939
-
940
- // The press pick this release may conclude as a click. Claimed synchronously, so the
941
- // entry is gone before any other event for this pointer can be handled.
942
- const downPick = this._downPicks.get(event.pointerId);
943
- this._downPicks.delete(event.pointerId);
944
-
945
- const node = await this._pickNode(event);
946
- if (!this._picker) return; // the element disconnected while the pick was in flight
947
-
948
- const entityElement = this._elementWithListener(node, 'pointerup');
949
- if (entityElement) {
950
- entityElement.dispatchEvent(new PointerEvent('pointerup', event));
951
- }
952
-
953
- // A click fires where the DOM fires it: at the nearest common inclusive ancestor of
954
- // what the press and the release picked, for the primary button only. The press pick
955
- // may still be in flight - a quick tap resolves in pick order, not event order.
956
- if (!downPick || event.button !== 0) return;
957
- const downNode = await downPick;
958
- if (!this._picker) return;
959
-
960
- const clickElement = this._elementWithListener(commonAncestor(downNode, node), 'click');
961
- if (clickElement) {
962
- const click = new PointerEvent('click', event);
963
-
964
- // The init above copied pointerup's `detail`, which the Pointer Events spec fixes
965
- // at 0 - but click is exempt: its detail is the click count, chained here as the
966
- // platform chains it (same target, within the double-click window). Overridden
967
- // with defineProperty because an event instance used as an init dict cannot have
968
- // single fields replaced.
969
- const time = performance.now();
970
- const last = this._lastClick;
971
- const count =
972
- last && last.element === clickElement && time - last.time <= CLICK_CHAIN_MS ? last.count + 1 : 1;
973
- this._lastClick = { element: clickElement, time, count };
974
- Object.defineProperty(click, 'detail', { value: count });
975
-
976
- clickElement.dispatchEvent(click);
977
- }
978
- }
979
-
980
- /**
981
- * Attaches exactly the canvas listeners the tree's current element listeners need, and
982
- * detaches the rest. Recomputed whenever a listener connects or disconnects anywhere under
983
- * this element: several synthesized types can need the same canvas listener (enter, leave
984
- * and move all ride the move pick; click rides the down/up pair), so one type's removal
985
- * must not detach a listener another type still uses. Re-attaching an attached listener is
986
- * a no-op by EventTarget semantics, so no attach state is kept.
987
- */
988
- private _syncCanvasListeners() {
989
- const canvas = this._canvas;
990
- if (!canvas) return; // not booted yet: _pickerCreate syncs once the handlers exist
991
-
992
- const elements = Array.from(this.querySelectorAll<EntityBaseElement>('pc-entity, pc-model, pc-node'));
993
- const needed = new Set<string>();
994
- for (const type of SYNTHESIZED_EVENTS) {
995
- if (elements.some((element) => element._hasListeners(type))) {
996
- canvasEventsFor[type].forEach((canvasType) => needed.add(canvasType));
997
- }
998
- }
999
- this._clickListened = elements.some((element) => element._hasListeners('click'));
1000
-
1001
- Object.entries(this._pointerHandlers).forEach(([canvasType, handler]) => {
1002
- if (!handler) return;
1003
- if (needed.has(canvasType)) {
1004
- canvas.addEventListener(canvasType, handler);
1005
- } else {
1006
- canvas.removeEventListener(canvasType, handler);
1007
- }
1008
- });
1009
- }
1010
-
1011
611
  /**
1012
612
  * Warns that a graphics option was written too late to have any effect. These options are read
1013
613
  * once, when the element connects and creates its graphics device, so a later write updates
@@ -0,0 +1,141 @@
1
+ import type { Asset, EventHandle } from 'playcanvas';
2
+
3
+ import { useAsset } from './asset';
4
+
5
+ // Keep `export` on these declarations. TypeScript removes the declaration and its inline export
6
+ // when `stripInternal` is enabled. A separate `export { ... }` statement would remain in the
7
+ // generated .d.ts file and refer to a declaration that had been removed.
8
+
9
+ /**
10
+ * Functions called when an {@link AssetBinding} finishes loading an asset or encounters an
11
+ * error.
12
+ *
13
+ * @internal
14
+ */
15
+ export type AssetBindingCallbacks = {
16
+ /**
17
+ * Called when the asset has a usable resource. If it was already loaded, this can run before
18
+ * {@link AssetBinding.bind} returns. Otherwise it runs when the asset fires `load`.
19
+ */
20
+ load: (asset: Asset) => void;
21
+
22
+ /**
23
+ * Called when the asset fails to load, including when it had already failed before
24
+ * {@link AssetBinding.bind} was called. If this is omitted, the binding keeps waiting so a
25
+ * later successful reload can still call `load`.
26
+ */
27
+ error?: (err: string | Error) => void;
28
+ };
29
+
30
+ /**
31
+ * Watches the asset currently selected by an element.
32
+ *
33
+ * An element can change its asset while the old one is still loading, or disconnect before the
34
+ * load finishes. This class makes sure callbacks from those older loads do nothing. Calling
35
+ * {@link bind} stops watching the previous asset and starts watching the new one. Calling
36
+ * {@link cancel} stops watching altogether.
37
+ *
38
+ * Asset lookup goes through {@link useAsset}, so selecting a lazy asset starts its load. The
39
+ * caller still decides what to do with the result, such as creating scene content, reporting an
40
+ * error, or marking an element ready.
41
+ *
42
+ * Reuse one `AssetBinding` for each asset-valued property throughout the element's lifetime. It
43
+ * is safe for a callback to call `bind` again: the new asset remains active after the callback
44
+ * returns.
45
+ *
46
+ * @internal
47
+ */
48
+ export class AssetBinding {
49
+ /**
50
+ * Each bind or cancel gets a new number. Event handlers remember the number they were created
51
+ * with and return if it is no longer current. Old listeners are normally removed as well, but
52
+ * this check also protects against an event that was already in progress when removal
53
+ * happened.
54
+ */
55
+ private _generation = 0;
56
+
57
+ private _loadHandle: EventHandle | null = null;
58
+
59
+ private _errorHandle: EventHandle | null = null;
60
+
61
+ private _detach() {
62
+ this._loadHandle?.off();
63
+ this._loadHandle = null;
64
+ this._errorHandle?.off();
65
+ this._errorHandle = null;
66
+ }
67
+
68
+ /**
69
+ * Stops watching the current asset and prevents its callbacks from running. The binding can
70
+ * be used again by calling {@link bind}.
71
+ */
72
+ cancel() {
73
+ this._generation++;
74
+ this._detach();
75
+ }
76
+
77
+ /**
78
+ * Starts watching the asset registered under `id` and stops watching the previous one. A
79
+ * missing `id` still clears the previous binding. Looking up a lazy asset starts its load.
80
+ *
81
+ * If the asset has already loaded successfully, `load` runs before this method returns. An
82
+ * earlier failure calls `error` immediately when that callback is provided; without one, the
83
+ * binding waits for a later successful reload. Assets still loading are watched for the same
84
+ * two outcomes. Once an event is handled, both listeners are removed.
85
+ *
86
+ * @param id - The `id` of the `<pc-asset>` element to bind to.
87
+ * @param callbacks - Functions to call when loading succeeds or fails.
88
+ * @returns The selected asset, or `undefined` if no asset has this `id`. The caller decides
89
+ * how to handle a missing asset.
90
+ */
91
+ bind(id: string, callbacks: AssetBindingCallbacks): Asset | undefined {
92
+ const generation = ++this._generation;
93
+ this._detach();
94
+
95
+ const asset = useAsset(id);
96
+ if (!asset) {
97
+ return undefined;
98
+ }
99
+
100
+ const { error } = callbacks;
101
+
102
+ if (asset.loaded) {
103
+ // PlayCanvas sets `loaded` after both success and failure, so a resource must also be
104
+ // present before this counts as success. Use `!= null` deliberately: `undefined` and
105
+ // `null` both mean there is no resource, while a valid resource can still be falsy
106
+ // (for example, an empty text file produces '').
107
+ if (asset.resource != null) {
108
+ callbacks.load(asset);
109
+ return asset;
110
+ }
111
+ if (error) {
112
+ error(`asset '${id}' failed to load`);
113
+ return asset;
114
+ }
115
+ }
116
+
117
+ // Old listeners are normally removed by bind or cancel. The number check is a second
118
+ // safeguard for a late event. Check it before _detach so an old callback cannot remove
119
+ // the listeners for the current asset. Whichever current event runs first removes both
120
+ // listeners.
121
+ this._loadHandle = asset.once('load', () => {
122
+ if (generation !== this._generation) {
123
+ return;
124
+ }
125
+ this._detach();
126
+ callbacks.load(asset);
127
+ });
128
+
129
+ if (error) {
130
+ this._errorHandle = asset.once('error', (err: string | Error) => {
131
+ if (generation !== this._generation) {
132
+ return;
133
+ }
134
+ this._detach();
135
+ error(err);
136
+ });
137
+ }
138
+
139
+ return asset;
140
+ }
141
+ }