laymur 0.3.30 → 0.4.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.
@@ -2,6 +2,7 @@ import type { WebGLRenderer } from "three";
2
2
  import type { UILayerInputListener } from "../layers/UILayer";
3
3
  import { type UILayer } from "../layers/UILayer";
4
4
  import type { UILayerElement, UIPlaneElement } from "../miscellaneous/asserts";
5
+ import { UIInputEvent } from "../miscellaneous/UIInputEvent";
5
6
  import { UIMicro } from "../miscellaneous/UIMicro";
6
7
  import { UIMode } from "../miscellaneous/UIMode";
7
8
  import type { UISceneWrapperClientAPI } from "../miscellaneous/UISceneWrapperClientAPI";
@@ -41,6 +42,7 @@ export declare abstract class UIElement extends UIAnchor implements UIPlaneEleme
41
42
  protected zIndexInternal: number;
42
43
  /** Optional input listener for handling user interactions when in interactive mode. */
43
44
  protected listener?: UILayerInputListener;
45
+ protected isPointerInside: boolean;
44
46
  /** Client API for interacting with the scene wrapper (rendering system). */
45
47
  protected readonly sceneWrapper: UISceneWrapperClientAPI;
46
48
  /** Handle to the plane object in the rendering system. */
@@ -160,9 +162,10 @@ export declare abstract class UIElement extends UIAnchor implements UIPlaneEleme
160
162
  * the element should not be used anymore.
161
163
  */
162
164
  destroy(): void;
163
- protected readonly onDown: (x: number, y: number) => boolean;
164
- protected readonly onMove: (x: number, y: number) => boolean;
165
- protected readonly onUp: (x: number, y: number) => boolean;
165
+ protected readonly catchPointerDown: (x: number, y: number) => boolean;
166
+ protected readonly catchPointerMove: (x: number, y: number) => boolean;
167
+ protected readonly catchPointerUp: (x: number, y: number) => boolean;
168
+ protected handleInputEvent(x: number, y: number, inputEvent: UIInputEvent): boolean;
166
169
  /**
167
170
  * Called before each render frame to update the element's transform matrix.
168
171
  * Applies micro-transformations (position, rotation, scale, anchor) and updates
package/dist/index.js CHANGED
@@ -3001,18 +3001,6 @@ class UILayer extends i {
3001
3001
  get orientation() {
3002
3002
  return this.orientationInternal;
3003
3003
  }
3004
- // public set x(value: number) {
3005
- // throw new Error("You cannot set the x position of a UI layer.");
3006
- // }
3007
- // public set y(value: number) {
3008
- // throw new Error("You cannot set the y position of a UI layer.");
3009
- // }
3010
- // public set width(value: number) {
3011
- // throw new Error("You cannot set the width of a UI layer.");
3012
- // }
3013
- // public set height(value: number) {
3014
- // throw new Error("You cannot set the height of a UI layer.");
3015
- // }
3016
3004
  /**
3017
3005
  * Sets the visibility and interaction mode and emits change event.
3018
3006
  * @param value - The new UIMode setting
@@ -3062,35 +3050,14 @@ class UILayer extends i {
3062
3050
  this.sceneWrapper.render(renderer);
3063
3051
  }
3064
3052
  }
3065
- pointerDownInternal(x, y) {
3066
- if (this.mode === UIMode.INTERACTIVE) {
3067
- this._private_inputListeners.sort((a, b) => b.zIndex - a.zIndex);
3068
- for (const listener of this._private_inputListeners) {
3069
- if (listener.listener.catchDown(x, y)) {
3070
- return;
3071
- }
3072
- }
3073
- }
3053
+ onPointerDownInternal(x, y) {
3054
+ this._private_handleInputEvent(x, y, "catchPointerDown");
3074
3055
  }
3075
- pointerMoveInternal(x, y) {
3076
- if (this.mode === UIMode.INTERACTIVE) {
3077
- this._private_inputListeners.sort((a, b) => b.zIndex - a.zIndex);
3078
- for (const listener of this._private_inputListeners) {
3079
- if (listener.listener.catchMove(x, y)) {
3080
- return;
3081
- }
3082
- }
3083
- }
3056
+ onPointerMoveInternal(x, y) {
3057
+ this._private_handleInputEvent(x, y, "catchPointerMove");
3084
3058
  }
3085
- pointerUpInternal(x, y) {
3086
- if (this.mode === UIMode.INTERACTIVE) {
3087
- this._private_inputListeners.sort((a, b) => b.zIndex - a.zIndex);
3088
- for (const listener of this._private_inputListeners) {
3089
- if (listener.listener.catchUp(x, y)) {
3090
- return;
3091
- }
3092
- }
3093
- }
3059
+ onPointerUpInternal(x, y) {
3060
+ this._private_handleInputEvent(x, y, "catchPointerUp");
3094
3061
  }
3095
3062
  /**
3096
3063
  * Internal method for accessing the constraint solver wrapper.
@@ -3124,28 +3091,11 @@ class UILayer extends i {
3124
3091
  * @param zIndex - The z-index priority for event handling order
3125
3092
  * @throws Will throw an error if the listener is already registered
3126
3093
  */
3127
- ["listenPointerInput"](listener, zIndex) {
3128
- if (this._private_inputListeners.find((l) => l.listener === listener)) {
3094
+ ["subscribePointerInput"](listener) {
3095
+ if (this._private_inputListeners.find((l) => l === listener)) {
3129
3096
  throw new Error("Listener already exists");
3130
3097
  }
3131
- this._private_inputListeners.push({ zIndex, listener });
3132
- }
3133
- /**
3134
- * Internal method for updating the z-index of an existing input listener.
3135
- *
3136
- * Changes the priority order for event handling of an already registered listener.
3137
- * The listener must already exist in the system.
3138
- *
3139
- * @param listener - The input listener to update
3140
- * @param zIndex - The new z-index priority
3141
- * @throws Will throw an error if the listener is not found
3142
- */
3143
- ["setListenerZIndex"](listener, zIndex) {
3144
- const index = this._private_inputListeners.findIndex((l) => l.listener === listener);
3145
- if (index === -1) {
3146
- throw new Error("Listener not found");
3147
- }
3148
- this._private_inputListeners[index].zIndex = zIndex;
3098
+ this._private_inputListeners.push(listener);
3149
3099
  }
3150
3100
  /**
3151
3101
  * Internal method for unregistering an input listener.
@@ -3156,13 +3106,23 @@ class UILayer extends i {
3156
3106
  * @param listener - The input listener to remove
3157
3107
  * @throws Will throw an error if the listener is not found
3158
3108
  */
3159
- ["unlistenPointerInput"](listener) {
3160
- const index = this._private_inputListeners.findIndex((l) => l.listener === listener);
3109
+ ["unsubscribePointerInput"](listener) {
3110
+ const index = this._private_inputListeners.findIndex((l) => l === listener);
3161
3111
  if (index === -1) {
3162
3112
  throw new Error("Listener not found");
3163
3113
  }
3164
3114
  this._private_inputListeners.splice(index, 1);
3165
3115
  }
3116
+ _private_handleInputEvent(x, y, catchFunctionName) {
3117
+ if (this.mode === UIMode.INTERACTIVE) {
3118
+ this._private_inputListeners.sort((a, b) => b.zIndex - a.zIndex);
3119
+ for (const listener of this._private_inputListeners) {
3120
+ if (listener[catchFunctionName](x, y)) {
3121
+ return;
3122
+ }
3123
+ }
3124
+ }
3125
+ }
3166
3126
  }
3167
3127
 
3168
3128
  /**
@@ -4382,9 +4342,11 @@ var source$2 = "void main() {\n vec2 transformedUV = (uvTransform * vec3(io_U
4382
4342
  */
4383
4343
  var UIInputEvent;
4384
4344
  (function (UIInputEvent) {
4385
- UIInputEvent["DOWN"] = "click";
4386
- UIInputEvent["MOVE"] = "move";
4387
- UIInputEvent["UP"] = "up";
4345
+ UIInputEvent["DOWN"] = "pointerdown";
4346
+ UIInputEvent["MOVE"] = "pointermove";
4347
+ UIInputEvent["UP"] = "pointerup";
4348
+ UIInputEvent["ENTER"] = "pointerenter";
4349
+ UIInputEvent["LEAVE"] = "pointerleave";
4388
4350
  })(UIInputEvent || (UIInputEvent = {}));
4389
4351
 
4390
4352
  /**
@@ -4705,19 +4667,6 @@ const quaternion = new Quaternion();
4705
4667
  const scale = new Vector3();
4706
4668
  const matrix = new Matrix4();
4707
4669
  const axis = new Vector3(0, 0, 1);
4708
- /**
4709
- * Checks if a point (x, y) is within the bounds of a UI element.
4710
- * @param x - The x coordinate to test
4711
- * @param y - The y coordinate to test
4712
- * @param element - The UI element to test against
4713
- * @returns True if the point is within the element's bounds
4714
- */
4715
- function isInsideElement(x, y, element) {
4716
- return (x > element.x &&
4717
- x < element.x + element.width &&
4718
- y > element.y &&
4719
- y < element.y + element.height);
4720
- }
4721
4670
  /**
4722
4671
  * Abstract base class for all renderable UI elements with dimensions.
4723
4672
  *
@@ -4757,26 +4706,15 @@ class UIElement extends UIAnchor {
4757
4706
  this.transparencyInternal = UITransparencyMode.BLEND;
4758
4707
  /** Internal storage for the z-index (depth) value. */
4759
4708
  this.zIndexInternal = 0;
4760
- this.onDown = (x, y) => {
4761
- const clicked = isInsideElement(x, y, this);
4762
- if (clicked) {
4763
- this.emit(UIInputEvent.DOWN, x, y, this);
4764
- }
4765
- return clicked;
4709
+ this.isPointerInside = false;
4710
+ this.catchPointerDown = (x, y) => {
4711
+ return this.handleInputEvent(x, y, UIInputEvent.DOWN);
4766
4712
  };
4767
- this.onMove = (x, y) => {
4768
- const clicked = isInsideElement(x, y, this);
4769
- if (clicked) {
4770
- this.emit(UIInputEvent.MOVE, x, y, this);
4771
- }
4772
- return clicked;
4713
+ this.catchPointerMove = (x, y) => {
4714
+ return this.handleInputEvent(x, y, UIInputEvent.MOVE);
4773
4715
  };
4774
- this.onUp = (x, y) => {
4775
- const clicked = isInsideElement(x, y, this);
4776
- if (clicked) {
4777
- this.emit(UIInputEvent.UP, x, y, this);
4778
- }
4779
- return clicked;
4716
+ this.catchPointerUp = (x, y) => {
4717
+ return this.handleInputEvent(x, y, UIInputEvent.UP);
4780
4718
  };
4781
4719
  this.layer.on(UILayerEvent.WILL_RENDER, this.onWillRender, this);
4782
4720
  this.wVariable = this.solverWrapper.createVariable(width, UIPriority.P6);
@@ -4876,7 +4814,7 @@ class UIElement extends UIAnchor {
4876
4814
  assertValidNumber(value, "UIElement zIndex");
4877
4815
  this.zIndexInternal = value;
4878
4816
  if (this.listener) {
4879
- this.layer["setListenerZIndex"](this.listener, value);
4817
+ this.listener.zIndex = this.zIndexInternal;
4880
4818
  }
4881
4819
  }
4882
4820
  /**
@@ -4912,14 +4850,15 @@ class UIElement extends UIAnchor {
4912
4850
  if (this.modeInternal !== value) {
4913
4851
  if (value === UIMode.INTERACTIVE) {
4914
4852
  this.listener = {
4915
- catchDown: this.onDown,
4916
- catchMove: this.onMove,
4917
- catchUp: this.onUp,
4853
+ catchPointerDown: this.catchPointerDown,
4854
+ catchPointerMove: this.catchPointerMove,
4855
+ catchPointerUp: this.catchPointerUp,
4856
+ zIndex: this.zIndexInternal,
4918
4857
  };
4919
- this.layer["listenPointerInput"](this.listener, this.zIndexInternal);
4858
+ this.layer["subscribePointerInput"](this.listener);
4920
4859
  }
4921
4860
  else if (this.modeInternal === UIMode.INTERACTIVE && this.listener) {
4922
- this.layer["unlistenPointerInput"](this.listener);
4861
+ this.layer["unsubscribePointerInput"](this.listener);
4923
4862
  this.listener = undefined;
4924
4863
  }
4925
4864
  this.modeInternal = value;
@@ -4946,13 +4885,30 @@ class UIElement extends UIAnchor {
4946
4885
  destroy() {
4947
4886
  this.layer.off(UILayerEvent.WILL_RENDER, this.onWillRender, this);
4948
4887
  if (this.listener) {
4949
- this.layer["unlistenPointerInput"](this.listener);
4888
+ this.layer["unsubscribePointerInput"](this.listener);
4950
4889
  }
4951
4890
  this.solverWrapper.removeVariable(this.hVariable);
4952
4891
  this.solverWrapper.removeVariable(this.wVariable);
4953
4892
  this.sceneWrapper.destroyPlane(this.planeHandler);
4954
4893
  super.destroy();
4955
4894
  }
4895
+ handleInputEvent(x, y, inputEvent) {
4896
+ const isPointerInside = x > this.x &&
4897
+ x < this.x + this.width &&
4898
+ y > this.y &&
4899
+ y < this.y + this.height;
4900
+ if (isPointerInside) {
4901
+ this.emit(inputEvent, x, y, this);
4902
+ }
4903
+ if (this.isPointerInside && !isPointerInside) {
4904
+ this.emit(UIInputEvent.LEAVE, x, y, this);
4905
+ }
4906
+ else if (!this.isPointerInside && isPointerInside) {
4907
+ this.emit(UIInputEvent.ENTER, x, y, this);
4908
+ }
4909
+ this.isPointerInside = isPointerInside;
4910
+ return isPointerInside;
4911
+ }
4956
4912
  /**
4957
4913
  * Called before each render frame to update the element's transform matrix.
4958
4914
  * Applies micro-transformations (position, rotation, scale, anchor) and updates
@@ -6759,37 +6715,13 @@ class UIFullscreenLayer extends UILayer {
6759
6715
  this.resizeInternal(window.innerWidth * scale, window.innerHeight * scale);
6760
6716
  };
6761
6717
  this._private_onDown = (event) => {
6762
- const rect = event.target instanceof HTMLElement
6763
- ? event.target.getBoundingClientRect()
6764
- : null;
6765
- const x = rect ? event.clientX - rect.left : event.clientX;
6766
- const y = rect
6767
- ? rect.bottom - event.clientY
6768
- : window.innerHeight - event.clientY;
6769
- const scale = this._private_resizePolicyInternal["calculateScaleInternal"](window.innerWidth, window.innerHeight);
6770
- this.pointerDownInternal(x * scale, y * scale);
6718
+ this._private_handleWindowInput(event, "onPointerDownInternal");
6771
6719
  };
6772
6720
  this._private_onMove = (event) => {
6773
- const rect = event.target instanceof HTMLElement
6774
- ? event.target.getBoundingClientRect()
6775
- : null;
6776
- const x = rect ? event.clientX - rect.left : event.clientX;
6777
- const y = rect
6778
- ? rect.bottom - event.clientY
6779
- : window.innerHeight - event.clientY;
6780
- const scale = this._private_resizePolicyInternal["calculateScaleInternal"](window.innerWidth, window.innerHeight);
6781
- this.pointerMoveInternal(x * scale, y * scale);
6721
+ this._private_handleWindowInput(event, "onPointerMoveInternal");
6782
6722
  };
6783
6723
  this._private_onUp = (event) => {
6784
- const rect = event.target instanceof HTMLElement
6785
- ? event.target.getBoundingClientRect()
6786
- : null;
6787
- const x = rect ? event.clientX - rect.left : event.clientX;
6788
- const y = rect
6789
- ? rect.bottom - event.clientY
6790
- : window.innerHeight - event.clientY;
6791
- const scale = this._private_resizePolicyInternal["calculateScaleInternal"](window.innerWidth, window.innerHeight);
6792
- this.pointerUpInternal(x * scale, y * scale);
6724
+ this._private_handleWindowInput(event, "onPointerUpInternal");
6793
6725
  };
6794
6726
  window.addEventListener("resize", this._private_onResize);
6795
6727
  window.addEventListener("pointerdown", this._private_onDown);
@@ -6823,6 +6755,8 @@ class UIFullscreenLayer extends UILayer {
6823
6755
  destroy() {
6824
6756
  window.removeEventListener("resize", this._private_onResize);
6825
6757
  window.removeEventListener("pointerdown", this._private_onDown);
6758
+ window.removeEventListener("pointermove", this._private_onMove);
6759
+ window.removeEventListener("pointerup", this._private_onUp);
6826
6760
  this._private_resizePolicyInternal.off(UIResizePolicyEvent.CHANGE, this._private_onResize);
6827
6761
  }
6828
6762
  /**
@@ -6848,6 +6782,17 @@ class UIFullscreenLayer extends UILayer {
6848
6782
  projectedPosition.y = MathUtils.mapLinear(projectedPosition.y, -1, 1, this.y, this.height);
6849
6783
  return projectedPosition;
6850
6784
  }
6785
+ _private_handleWindowInput(event, pointerFunctionName) {
6786
+ const rect = event.target instanceof HTMLElement
6787
+ ? event.target.getBoundingClientRect()
6788
+ : null;
6789
+ const x = rect ? event.clientX - rect.left : event.clientX;
6790
+ const y = rect
6791
+ ? rect.bottom - event.clientY
6792
+ : window.innerHeight - event.clientY;
6793
+ const scale = this._private_resizePolicyInternal["calculateScaleInternal"](window.innerWidth, window.innerHeight);
6794
+ this[pointerFunctionName](x * scale, y * scale);
6795
+ }
6851
6796
  }
6852
6797
 
6853
6798
  const INSTANCED_GEOMETRY = (() => {