Prechádzať zdrojové kódy

Rework mousewheel code to have more reasonable values and fix wheelDeltaPercentage code. (#9814)

Dave Solares 4 rokov pred
rodič
commit
a6d5c2eb85

+ 11 - 35
src/Cameras/Inputs/arcRotateCameraMouseWheelInput.ts

@@ -4,7 +4,6 @@ import { EventState, Observer } from "../../Misc/observable";
 import { ArcRotateCamera } from "../../Cameras/arcRotateCamera";
 import { ICameraInput, CameraInputTypes } from "../../Cameras/cameraInputsManager";
 import { PointerInfo, PointerEventTypes } from "../../Events/pointerEvents";
-import { Scalar } from '../../Maths/math.scalar';
 import { Tools } from '../../Misc/tools';
 import { IWheelEvent } from "../../Events/deviceInputEvents";
 
@@ -34,16 +33,6 @@ export class ArcRotateCameraMouseWheelInput implements ICameraInput<ArcRotateCam
     private _wheel: Nullable<(p: PointerInfo, s: EventState) => void>;
     private _observer: Nullable<Observer<PointerInfo>>;
 
-    private computeDeltaFromMouseWheelLegacyEvent(mouseWheelDelta: number, radius: number) {
-        var delta = 0;
-        var wheelDelta = (mouseWheelDelta * 0.01 * this.wheelDeltaPercentage) * radius;
-        if (mouseWheelDelta > 0) {
-            delta = wheelDelta / (1.0 + this.wheelDeltaPercentage);
-        } else {
-            delta = wheelDelta * (1.0 + this.wheelDeltaPercentage);
-        }
-        return delta;
-    }
     /**
      * Attach the input controls to a specific dom element to get the input from.
      * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
@@ -54,35 +43,22 @@ export class ArcRotateCameraMouseWheelInput implements ICameraInput<ArcRotateCam
         this._wheel = (p, s) => {
             //sanity check - this should be a PointerWheel event.
             if (p.type !== PointerEventTypes.POINTERWHEEL) { return; }
-            var event = <IWheelEvent>p.event;
-            var delta = 0;
-
-            let mouseWheelLegacyEvent = event as any;
+            const event = <IWheelEvent>p.event;
+            let delta = 0;
             let wheelDelta = 0;
 
-            if (mouseWheelLegacyEvent.wheelDelta) {
-                wheelDelta = mouseWheelLegacyEvent.wheelDelta;
-            } else {
-                wheelDelta = -(event.deltaY || event.detail) * 60;
+            // Using the inertia percentage, calculate the value needed to move based on set wheel delta percentage
+            if (this.wheelDeltaPercentage !== 0) {
+                wheelDelta = (event.deltaY > 0 ? -1 : 1) * this.camera.radius * this.wheelDeltaPercentage;
+                delta = wheelDelta * (this.camera.inertia === 1 ? 1 : (1 - this.camera.inertia));
             }
+            // If there's no percentage set, just handle using a value that emulates Chrome
+            else {
+                wheelDelta = (event.deltaY > 0 ? -100 : 100);
 
-            if (this.wheelDeltaPercentage) {
-                delta = this.computeDeltaFromMouseWheelLegacyEvent(wheelDelta, this.camera.radius);
-
-                // If zooming in, estimate the target radius and use that to compute the delta for inertia
-                // this will stop multiple scroll events zooming in from adding too much inertia
-                if (delta > 0) {
-                    var estimatedTargetRadius = this.camera.radius;
-                    var targetInertia = this.camera.inertialRadiusOffset + delta;
-                    for (var i = 0; i < 20 && Math.abs(targetInertia) > 0.001; i++) {
-                        estimatedTargetRadius -= targetInertia;
-                        targetInertia *= this.camera.inertia;
-                    }
-                    estimatedTargetRadius = Scalar.Clamp(estimatedTargetRadius, 0, Number.MAX_VALUE);
-                    delta = this.computeDeltaFromMouseWheelLegacyEvent(wheelDelta, estimatedTargetRadius);
+                if (this.wheelPrecision !== 0) {
+                    delta = wheelDelta / (this.wheelPrecision * 40);
                 }
-            } else {
-                delta = wheelDelta / (this.wheelPrecision * 40);
             }
 
             if (delta) {