Skip to content

Commit

Permalink
DeviceInputSystem: Pull browser specific code into WebDeviceInputSyst…
Browse files Browse the repository at this point in the history
…em (#12865)

* WIP

* Fixed ref to srcElement

* Remove commented code

* Prettier

* Changed wheel event check to inputIndex check

* PR feedback

* formatting

* PR Feedback

* prettier
  • Loading branch information
PolygonalSun authored Sep 6, 2022
1 parent a76bcd1 commit 505c0b2
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 58 deletions.
23 changes: 3 additions & 20 deletions packages/dev/core/src/Cameras/Inputs/BaseCameraMouseWheelInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,9 @@ export abstract class BaseCameraMouseWheelInput implements ICameraInput<Camera>

const platformScale = event.deltaMode === EventConstants.DOM_DELTA_LINE ? this._ffMultiplier : 1; // If this happens to be set to DOM_DELTA_LINE, adjust accordingly

if (event.deltaY !== undefined) {
// Most recent browsers versions have delta properties.
// Firefox >= v17 (Has WebGL >= v4)
// Chrome >= v31 (Has WebGL >= v8)
// Edge >= v12 (Has WebGl >= v12)
// https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent
this._wheelDeltaX += (this.wheelPrecisionX * platformScale * event.deltaX) / this._normalize;
this._wheelDeltaY -= (this.wheelPrecisionY * platformScale * event.deltaY) / this._normalize;
this._wheelDeltaZ += (this.wheelPrecisionZ * platformScale * event.deltaZ) / this._normalize;
} else if ((<any>event).wheelDeltaY !== undefined) {
// Unsure whether these catch anything more. Documentation
// online is contradictory.
this._wheelDeltaX += (this.wheelPrecisionX * platformScale * (<any>event).wheelDeltaX) / this._normalize;
this._wheelDeltaY -= (this.wheelPrecisionY * platformScale * (<any>event).wheelDeltaY) / this._normalize;
this._wheelDeltaZ += (this.wheelPrecisionZ * platformScale * (<any>event).wheelDeltaZ) / this._normalize;
} else if ((<any>event).wheelDelta) {
// IE >= v9 (Has WebGL >= v11)
// Maybe others?
this._wheelDeltaY -= (this.wheelPrecisionY * (<any>event).wheelDelta) / this._normalize;
}
this._wheelDeltaX += (this.wheelPrecisionX * platformScale * event.deltaX) / this._normalize;
this._wheelDeltaY -= (this.wheelPrecisionY * platformScale * event.deltaY) / this._normalize;
this._wheelDeltaZ += (this.wheelPrecisionZ * platformScale * event.deltaZ) / this._normalize;

if (event.preventDefault) {
if (!noPreventDefault) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export abstract class BaseCameraPointersInput implements ICameraInput<Camera> {
return;
}

const srcElement = <HTMLElement>(evt.srcElement || evt.target);
const srcElement = <HTMLElement>evt.target;

this._altKey = evt.altKey;
this._ctrlKey = evt.ctrlKey;
Expand All @@ -85,8 +85,8 @@ export abstract class BaseCameraPointersInput implements ICameraInput<Camera> {
this._buttonsPressed = evt.buttons;

if (engine.isPointerLock) {
const offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
const offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
const offsetX = evt.movementX;
const offsetY = evt.movementY;

this.onTouch(null, offsetX, offsetY);
this._pointA = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,9 @@ export class ArcRotateCameraMouseWheelInput implements ICameraInput<ArcRotateCam
}
const event = <IWheelEvent>p.event;
let delta = 0;

const mouseWheelLegacyEvent = event as any;
let wheelDelta = 0;

const platformScale = event.deltaMode === EventConstants.DOM_DELTA_LINE ? ffMultiplier : 1; // If this happens to be set to DOM_DELTA_LINE, adjust accordingly
if (event.deltaY !== undefined) {
wheelDelta = -(event.deltaY * platformScale);
} else if ((<any>event).wheelDeltaY !== undefined) {
wheelDelta = -((<any>event).wheelDeltaY * platformScale);
} else {
wheelDelta = mouseWheelLegacyEvent.wheelDelta;
}

const wheelDelta = -(event.deltaY * platformScale);

if (this.customComputeDeltaFromMouseWheel) {
delta = this.customComputeDeltaFromMouseWheel(wheelDelta, this, event);
Expand Down
6 changes: 3 additions & 3 deletions packages/dev/core/src/Cameras/Inputs/flyCameraMouseInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class FlyCameraMouseInput implements ICameraInput<FlyCamera> {
return;
}

const srcElement = <HTMLElement>(e.srcElement || e.target);
const srcElement = <HTMLElement>e.target;

// Mouse down.
if (p.type === PointerEventTypes.POINTERDOWN) {
Expand Down Expand Up @@ -225,8 +225,8 @@ export class FlyCameraMouseInput implements ICameraInput<FlyCamera> {
return;
}

const offsetX = e.movementX || e.mozMovementX || e.webkitMovementX || e.msMovementX || 0;
const offsetY = e.movementY || e.mozMovementY || e.webkitMovementY || e.msMovementY || 0;
const offsetX = e.movementX;
const offsetY = e.movementY;

this._rotateCamera(offsetX, offsetY);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ export class FollowCameraMouseWheelInput implements ICameraInput<FollowCamera> {
const event = <IWheelEvent>p.event;
let delta = 0;

// Chrome, Safari: event.deltaY
// IE: event.wheelDelta
// Firefox: event.detail (inverted)
const wheelDelta = Math.max(-1, Math.min(1, event.deltaY || (<any>event).wheelDelta || -(<any>event).detail));
const wheelDelta = Math.max(-1, Math.min(1, event.deltaY));
if (this.wheelDeltaPercentage) {
console.assert(
<number>(<unknown>this.axisControlRadius) + <number>(<unknown>this.axisControlHeight) + <number>(<unknown>this.axisControlRotation) <= 1,
Expand Down
6 changes: 3 additions & 3 deletions packages/dev/core/src/Cameras/Inputs/freeCameraMouseInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class FreeCameraMouseInput implements ICameraInput<FreeCamera> {
return;
}

const srcElement = <HTMLElement>(evt.srcElement || evt.target);
const srcElement = <HTMLElement>evt.target;

if (p.type === PointerEventTypes.POINTERDOWN && (this._currentActiveButton === -1 || isTouch)) {
try {
Expand Down Expand Up @@ -169,7 +169,7 @@ export class FreeCameraMouseInput implements ICameraInput<FreeCamera> {
return;
}

let offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
let offsetX = evt.movementX;
if (this.camera.getScene().useRightHandedSystem) {
offsetX *= -1;
}
Expand All @@ -178,7 +178,7 @@ export class FreeCameraMouseInput implements ICameraInput<FreeCamera> {
}
this.camera.cameraRotation.y += offsetX / this.angularSensibility;

const offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
const offsetY = evt.movementY;
this.camera.cameraRotation.x += offsetY / this.angularSensibility;

this._previousPosition = null;
Expand Down
27 changes: 23 additions & 4 deletions packages/dev/core/src/DeviceInput/webDeviceInputSystem.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Engine } from "../Engines/engine";
import type { IUIEvent } from "../Events/deviceInputEvents";
import type { IPointerEvent, IUIEvent } from "../Events/deviceInputEvents";
import { DomManagement } from "../Misc/domManagement";
import type { Observer } from "../Misc/observable";
import { Tools } from "../Misc/tools";
Expand Down Expand Up @@ -420,12 +420,19 @@ export class WebDeviceInputSystem implements IDeviceInputSystem {

const pointer = this._inputs[deviceType][deviceSlot];
if (pointer) {
const deviceEvent = evt as IPointerEvent;
deviceEvent.inputIndex = PointerInput.Move;

if (evt.movementX === undefined) {
evt.movementX = evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || evt.clientX - pointer[PointerInput.Horizontal];
}
if (evt.movementY === undefined) {
evt.movementY = evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || evt.clientY - pointer[PointerInput.Vertical];
}

pointer[PointerInput.Horizontal] = evt.clientX;
pointer[PointerInput.Vertical] = evt.clientY;

const deviceEvent = evt as IUIEvent;
deviceEvent.inputIndex = PointerInput.Move;

this._onInputChanged(deviceType, deviceSlot, deviceEvent);

// Lets Propagate the event for move with same position.
Expand Down Expand Up @@ -513,6 +520,12 @@ export class WebDeviceInputSystem implements IDeviceInputSystem {

if (previousHorizontal !== evt.clientX || previousVertical !== evt.clientY) {
deviceEvent.inputIndex = PointerInput.Move;
if (evt.movementX === undefined) {
evt.movementX = evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || evt.clientX - pointer[PointerInput.Horizontal];
}
if (evt.movementY === undefined) {
evt.movementY = evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || evt.clientY - pointer[PointerInput.Vertical];
}
this._onInputChanged(deviceType, deviceSlot, deviceEvent);
}
}
Expand Down Expand Up @@ -543,6 +556,12 @@ export class WebDeviceInputSystem implements IDeviceInputSystem {

if (previousHorizontal !== evt.clientX || previousVertical !== evt.clientY) {
deviceEvent.inputIndex = PointerInput.Move;
if (evt.movementX === undefined) {
evt.movementX = evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || evt.clientX - pointer[PointerInput.Horizontal];
}
if (evt.movementY === undefined) {
evt.movementY = evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || evt.clientY - pointer[PointerInput.Vertical];
}
this._onInputChanged(deviceType, deviceSlot, deviceEvent);
}

Expand Down
15 changes: 11 additions & 4 deletions packages/dev/core/src/Events/deviceInputEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,23 +156,27 @@ export interface IMouseEvent extends IUIEvent {

/**
* Delta of movement on X axis
* @deprecated Use 'movementX' instead
*/
mozMovementX?: number;

/**
* Delta of movement on Y axis
* @deprecated Use 'movementY' instead
*/
mozMovementY?: number;

/**
* Delta of movement on X axis
* @deprecated Use 'movementX' instead
*/
msMovementX?: any;
msMovementX?: number;

/**
* Delta of movement on Y axis
* @deprecated Use 'movementY' instead
*/
msMovementY?: any;
msMovementY?: number;

/**
* Current coordinate of X within container
Expand Down Expand Up @@ -201,13 +205,15 @@ export interface IMouseEvent extends IUIEvent {

/**
* Delta of movement on X axis
* @deprecated Use 'movementX' instead
*/
webkitMovementX?: any;
webkitMovementX?: number;

/**
* Delta of movement on Y axis
* @deprecated Use 'movementY' instead
*/
webkitMovementY?: any;
webkitMovementY?: number;

/**
* Alias of clientX
Expand Down Expand Up @@ -271,6 +277,7 @@ export interface IWheelEvent extends IMouseEvent {

/**
* WheelDelta (From MouseWheel Event)
* @deprecated
*/
wheelDelta?: number;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/dev/core/src/Inputs/scene.inputManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export class InputManager {
}

if (pickResult) {
const type = evt.type === "wheel" || evt.type === "mousewheel" || evt.type === "DOMMouseScroll" ? PointerEventTypes.POINTERWHEEL : PointerEventTypes.POINTERMOVE;
const type = evt.inputIndex >= PointerInput.MouseWheelX && evt.inputIndex <= PointerInput.MouseWheelZ ? PointerEventTypes.POINTERWHEEL : PointerEventTypes.POINTERMOVE;

if (scene.onPointerMove) {
scene.onPointerMove(evt, pickResult, type);
Expand Down Expand Up @@ -660,7 +660,7 @@ export class InputManager {
this._checkPrePointerObservable(
null,
evt as IPointerEvent,
evt.type === "wheel" || evt.type === "mousewheel" || evt.type === "DOMMouseScroll" ? PointerEventTypes.POINTERWHEEL : PointerEventTypes.POINTERMOVE
evt.inputIndex >= PointerInput.MouseWheelX && evt.inputIndex <= PointerInput.MouseWheelZ ? PointerEventTypes.POINTERWHEEL : PointerEventTypes.POINTERMOVE
)
) {
return;
Expand Down
6 changes: 3 additions & 3 deletions packages/dev/core/src/Offline/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ export class Database implements IOfflineProvider {
transaction.onabort = (event) => {
try {
//backwards compatibility with ts 1.0, srcElement doesn't have an "error" according to ts 1.3
const srcElement = <any>(event.srcElement || event.target);
const srcElement = <any>event.target;
const error = srcElement.error;
if (error && error.name === "QuotaExceededError") {
this._hasReachedQuota = true;
Expand Down Expand Up @@ -477,7 +477,7 @@ export class Database implements IOfflineProvider {
transaction.onabort = (event) => {
try {
//backwards compatibility with ts 1.0, srcElement doesn't have an "error" according to ts 1.3
const error = (<any>event.srcElement)["error"];
const error = (<any>event.target)["error"];
if (error && error.name === "QuotaExceededError") {
this._hasReachedQuota = true;
}
Expand Down Expand Up @@ -621,7 +621,7 @@ export class Database implements IOfflineProvider {
transaction.onabort = (event) => {
try {
//backwards compatibility with ts 1.0, srcElement doesn't have an "error" according to ts 1.3
const error = (<any>event.srcElement)["error"];
const error = (<any>event.target)["error"];
if (error && error.name === "QuotaExceededError") {
this._hasReachedQuota = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ export class GraphNode {

private _onDown(evt: PointerEvent) {
// Check if this is coming from the port
if (evt.srcElement && (evt.srcElement as HTMLElement).nodeName === "IMG") {
if (evt.target && (evt.target as HTMLElement).nodeName === "IMG") {
return;
}

Expand Down

0 comments on commit 505c0b2

Please sign in to comment.