Skip to content

Commit

Permalink
fix(type): refine PointerEvents types
Browse files Browse the repository at this point in the history
1. add new PickGUIEvent3D for GUI picks events
2. add types for pick result
  • Loading branch information
lslzl3000 committed Nov 27, 2024
1 parent 1e79847 commit 3029b5c
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 173 deletions.
10 changes: 5 additions & 5 deletions samples/gui/Sample_UIButton.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GUIHelp } from "@orillusion/debug/GUIHelp";
import { createExampleScene } from "@samples/utils/ExampleScene";
import { Engine3D, Object3DUtil, Object3D, GUISpace, WorldPanel, ViewPanel, UIButton, UITextField, Color, TextAnchor, PointerEvent3D, UIImage, ImageType, ComponentBase, View3D, UITransform, UIPanel, UIInteractiveStyle, UIButtonTransition } from "@orillusion/core";
import { Engine3D, Object3DUtil, Object3D, GUISpace, WorldPanel, ViewPanel, UIButton, UITextField, Color, TextAnchor, UIImage, ImageType, ComponentBase, View3D, UITransform, UIPanel, UIInteractiveStyle, UIButtonTransition, PickGUIEvent3D } from "@orillusion/core";

export class Sample_UIButton {
button: UIButton;
Expand Down Expand Up @@ -64,10 +64,10 @@ export class Sample_UIButton {
buttonLabel.color = new Color(1, 0.8, 0.4);
buttonLabel.alignment = TextAnchor.MiddleCenter;

quad.addEventListener(PointerEvent3D.PICK_CLICK_GUI, this.onUIClick, this);
quad.addEventListener(PointerEvent3D.PICK_OUT_GUI, this.onOut, this);
quad.addEventListener(PointerEvent3D.PICK_OVER_GUI, this.onOver, this);
quad.addEventListener(PointerEvent3D.PICK_DOWN_GUI, this.onDown, this);
quad.addEventListener(PickGUIEvent3D.PICK_CLICK_GUI, this.onUIClick, this);
quad.addEventListener(PickGUIEvent3D.PICK_OUT_GUI, this.onOut, this);
quad.addEventListener(PickGUIEvent3D.PICK_OVER_GUI, this.onOver, this);
quad.addEventListener(PickGUIEvent3D.PICK_DOWN_GUI, this.onDown, this);
}

{
Expand Down
8 changes: 4 additions & 4 deletions samples/gui/panel/GUIPanelPOI.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Color, Engine3D, ImageType, Object3D, PointerEvent3D, TextAnchor, UIImage, UIInteractive, UITextField, clamp } from "@orillusion/core";
import { Color, Engine3D, ImageType, Object3D, PickGUIEvent3D, TextAnchor, UIImage, UIInteractive, UITextField, clamp } from "@orillusion/core";
import { sampleUIPanelClick, sampleUIPanelDispatcher } from "./GUIBinder";

export class GUIPanelPOI {
Expand Down Expand Up @@ -61,7 +61,7 @@ export class GUIPanelPOI {
this._backImage.uiTransform.y = -60;

uiChild.addEventListener(
PointerEvent3D.PICK_CLICK_GUI,
PickGUIEvent3D.PICK_CLICK_GUI,
() => {
this._remainTime = 500;
sampleUIPanelClick.data = this.objUI;
Expand All @@ -71,15 +71,15 @@ export class GUIPanelPOI {
);

uiChild.addEventListener(
PointerEvent3D.PICK_OVER_GUI,
PickGUIEvent3D.PICK_OVER_GUI,
() => {
this._backImage.color = this._outColor;
},
this,
);

uiChild.addEventListener(
PointerEvent3D.PICK_OUT_GUI,
PickGUIEvent3D.PICK_OUT_GUI,
() => {
this._backImage.color = this._originColor;
},
Expand Down
36 changes: 18 additions & 18 deletions src/components/gui/GUIPick.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Engine3D } from '../../Engine3D';
import { PointerEvent3D } from '../../event/eventConst/PointerEvent3D';
import { PickGUIEvent3D, PointerEvent3D } from '../../event/eventConst/PointerEvent3D';
import { MouseCode } from '../../event/MouseCode';
import { webGPUContext } from '../../gfx/graphics/webGpu/Context3D';
import { Ray } from '../../math/Ray';
Expand All @@ -19,14 +19,14 @@ export class GUIPick {
private _ray: Ray;

private _mouseCode: MouseCode;
private _clickEvent: PointerEvent3D;
private _outEvent: PointerEvent3D;
private _overEvent: PointerEvent3D;
private _upEvent: PointerEvent3D;
private _downEvent: PointerEvent3D;
private _clickEvent: PickGUIEvent3D;
private _outEvent: PickGUIEvent3D;
private _overEvent: PickGUIEvent3D;
private _upEvent: PickGUIEvent3D;
private _downEvent: PickGUIEvent3D;
private _view: View3D;

// private mouseMove: PointerEvent3D;
// private mouseMove: PickGUIEvent3D;

/**
* Initialize the pickup and call it internally during engine initialization
Expand All @@ -35,12 +35,12 @@ export class GUIPick {
this._view = view;
this._ray = new Ray();

this._clickEvent = new PointerEvent3D(PointerEvent3D.PICK_CLICK_GUI);
this._outEvent = new PointerEvent3D(PointerEvent3D.PICK_OUT_GUI);
this._overEvent = new PointerEvent3D(PointerEvent3D.PICK_OVER_GUI);
// this.mouseMove = new PointerEvent3D(PointerEvent3D.PICK_MOVE);
this._upEvent = new PointerEvent3D(PointerEvent3D.PICK_UP_GUI);
this._downEvent = new PointerEvent3D(PointerEvent3D.PICK_DOWN_GUI);
this._clickEvent = new PickGUIEvent3D(PickGUIEvent3D.PICK_CLICK_GUI);
this._outEvent = new PickGUIEvent3D(PickGUIEvent3D.PICK_OUT_GUI);
this._overEvent = new PickGUIEvent3D(PickGUIEvent3D.PICK_OVER_GUI);
// this.mouseMove = new PointerEvent3D(PickGUIEvent3D.PICK_MOVE);
this._upEvent = new PickGUIEvent3D(PickGUIEvent3D.PICK_UP_GUI);
this._downEvent = new PickGUIEvent3D(PickGUIEvent3D.PICK_DOWN_GUI);

Engine3D.inputSystem.addEventListener(PointerEvent3D.POINTER_DOWN, this.onTouchDown, this, null, 1);
Engine3D.inputSystem.addEventListener(PointerEvent3D.POINTER_UP, this.onTouchUp, this, null, 1);
Expand All @@ -67,15 +67,15 @@ export class GUIPick {
if(_target != this._lastOverTarget){
_target.mouseStyle = UIInteractiveStyle.OVER;
Object.assign(this._overEvent, e);
this._overEvent.type = PointerEvent3D.PICK_OVER_GUI;
this._overEvent.type = PickGUIEvent3D.PICK_OVER_GUI;
this._overEvent.target = _target.object3D;
this._overEvent.data = ret;
_target.object3D.dispatchEvent(this._overEvent);

if (this._lastOverTarget) {
this._lastOverTarget.mouseStyle = UIInteractiveStyle.NORMAL;
Object.assign(this._outEvent, e);
this._outEvent.type = PointerEvent3D.PICK_OUT_GUI;
this._outEvent.type = PickGUIEvent3D.PICK_OUT_GUI;
this._outEvent.target = _target.object3D;
this._outEvent.data = ret;
this._lastOverTarget.object3D.dispatchEvent(this._outEvent);
Expand All @@ -86,7 +86,7 @@ export class GUIPick {
if (this._lastOverTarget) {
this._lastOverTarget.mouseStyle = UIInteractiveStyle.NORMAL;
Object.assign(this._outEvent, e);
this._outEvent.type = PointerEvent3D.PICK_OUT_GUI;
this._outEvent.type = PickGUIEvent3D.PICK_OUT_GUI;
this._outEvent.target = this._lastOverTarget.object3D;
this._outEvent.data = ret;
this._lastOverTarget.object3D.dispatchEvent(this._outEvent);
Expand All @@ -113,7 +113,7 @@ export class GUIPick {
if (collider) {
collider.mouseStyle = UIInteractiveStyle.DOWN;
Object.assign(this._downEvent, e);
this._downEvent.type = PointerEvent3D.PICK_DOWN_GUI;
this._downEvent.type = PickGUIEvent3D.PICK_DOWN_GUI;
this._downEvent.target = collider.object3D;
this._downEvent.data = ret;
collider.object3D.dispatchEvent(this._downEvent);
Expand All @@ -139,7 +139,7 @@ export class GUIPick {
this._calcDistanceVec2.set(e.mouseX, e.mouseY);
if (this._calcDistanceVec2.distance(this._lastDownPosition) <= this._clickDistanceSpan) {
Object.assign(this._clickEvent, e);
this._clickEvent.type = PointerEvent3D.PICK_CLICK_GUI;
this._clickEvent.type = PickGUIEvent3D.PICK_CLICK_GUI;
this._clickEvent.target = collider.object3D;
this._clickEvent.data = ret;
collider.object3D.dispatchEvent(this._clickEvent);
Expand Down
2 changes: 1 addition & 1 deletion src/event/CEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class CEvent {
/**
* extra data.Used for the transmission process of events, carrying data
*/
public data: any;
public data?: any;

/**
*
Expand Down
143 changes: 115 additions & 28 deletions src/event/eventConst/PointerEvent3D.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
import { GUIHitInfo } from '../../components/gui/uiComponents/IUIInteractive';
import { Vector2 } from '../../math/Vector2';
import { Vector3 } from '../../math/Vector3';
import { CEvent } from '../CEvent';

type pickResult = {
meshID: number,
worldPos: Vector3,
worldNormal: Vector3,
screenUv?: Vector2,
distance?: number
}

/**
* enum event type of pointer.
* {@link InputSystem}
* @group Events
*/
export class PointerEvent3D extends CEvent {

/**
* Triggered when the touch point enters the collision
*/
public static PICK_OVER = 'onPickOver';

/**
* Triggered when the touch point enters the interactive GUI
*/
public static PICK_OVER_GUI = 'onPickOverGUI';

/**
* Triggered when the touch point clicked the collision
*/
public static PICK_CLICK = 'onPickClick';

/**
* Triggered when the touch point clicked the interactive GUI
*/
public static PICK_CLICK_GUI = 'onPickClickGUI';

/**
* Triggered when the touch point leave the collision
*/
public static PICK_OUT = 'onPickOut';

/**
* Triggered when the touch point leave the interactive GUI
*/
public static PICK_OUT_GUI = 'onPickOutGUI';

/**
* Triggered when the touch point move on the collision
*/
Expand All @@ -47,21 +42,11 @@ export class PointerEvent3D extends CEvent {
*/
public static PICK_UP = 'onPickUp';

/**
* Triggered when the touch point release from the interactive GUI
*/
public static PICK_UP_GUI = 'onPickUpGUI';

/**
* Triggered when the touch point pressed the collision
*/
public static PICK_DOWN = 'onPickDown';

/**
* Triggered when the touch point pressed the interactive GUI
*/
public static PICK_DOWN_GUI = 'onPickDownGUI';

/**
*
* Triggered when the right pointer clicked
Expand Down Expand Up @@ -127,7 +112,7 @@ export class PointerEvent3D extends CEvent {
/**
* event type
*/
public pointerType: string;
public pointerType: string = 'onPointer';

/**
* whether it's the preferred pointer in this type of pointer.
Expand Down Expand Up @@ -167,10 +152,112 @@ export class PointerEvent3D extends CEvent {

/**
* Returns a positive value when scrolling down,
* a negative value when scrolling up, otherwise 0.
* a negative value when scrolling up, otherwise 0.
*/
public deltaY: number;

declare public data: pickResult

/**
* @internal
*/
public deltaZ: number;

/**
* @internal
*/
public reset() {
super.reset();
this.mouseX = 0;
this.mouseY = 0;
this.movementX = 0;
this.movementY = 0;
this.deltaX = 0;
this.deltaY = 0;
this.deltaZ = 0;
}
}

export class PickGUIEvent3D extends CEvent {
/**
* Triggered when the touch point enters the interactive GUI
*/
public static PICK_OVER_GUI = 'onPickOverGUI';

/**
* Triggered when the touch point clicked the interactive GUI
*/
public static PICK_CLICK_GUI = 'onPickClickGUI';

/**
* Triggered when the touch point leave the interactive GUI
*/
public static PICK_OUT_GUI = 'onPickOutGUI';

/**
* Triggered when the touch point release from the interactive GUI
*/
public static PICK_UP_GUI = 'onPickUpGUI';

/**
* Triggered when the touch point pressed the interactive GUI
*/
public static PICK_DOWN_GUI = 'onPickDownGUI';

/**
* A unique identifier for an event caused by a pointer.
*/
public pointerId: number;

/**
* event type
*/
public pointerType: string = 'onPickGUI';

/**
* whether it's the preferred pointer in this type of pointer.
*/
public isPrimary: boolean;

/**
* Normalize values
*/
public pressure: number;

/**
* coord x of mouse
*/
public mouseX: number;

/**
* coord y of mouse
*/
public mouseY: number;

/**
* delta of coord x of mouse
*/
public movementX: number;

/**
* delta of coord y of mouse
*/
public movementY: number;

/**
* Returns a negative value when scrolling left,
* a positive value when scrolling right, otherwise 0.
*/
public deltaX: number;

/**
* Returns a positive value when scrolling down,
* a negative value when scrolling up, otherwise 0.
*/
public deltaY: number;

declare public data: GUIHitInfo

/**
* @internal
*/
Expand Down
Loading

0 comments on commit 3029b5c

Please sign in to comment.