Преглед изворни кода

Mouse wheel for FreeCamera: Add Babylon Observables.

duncan law пре 4 година
родитељ
комит
aa414cfe75

+ 26 - 1
src/Cameras/Inputs/BaseCameraMouseWheelInput.ts

@@ -1,6 +1,6 @@
 import { Nullable } from "../../types";
 import { serialize } from "../../Misc/decorators";
-import { EventState, Observer } from "../../Misc/observable";
+import { EventState, Observable, Observer } from "../../Misc/observable";
 import { Camera } from "../../Cameras/camera";
 import { ICameraInput } from "../../Cameras/cameraInputsManager";
 import { PointerInfo, PointerEventTypes } from "../../Events/pointerEvents";
@@ -37,6 +37,12 @@ export abstract class BaseCameraMouseWheelInput implements ICameraInput<Camera>
     @serialize()
     public wheelPrecisionZ = 3.0;
 
+    /**
+     * Observable for when a mouse wheel move event occurs.
+     */
+    public onChangedObservable = new Observable<
+        {wheelDeltaX: number, wheelDeltaY: number, wheelDeltaZ: number}>(); 
+
     private _wheel: Nullable<(pointer: PointerInfo, _: EventState) => void>;
     private _observer: Nullable<Observer<PointerInfo>>;
 
@@ -123,6 +129,25 @@ export abstract class BaseCameraMouseWheelInput implements ICameraInput<Camera>
             this._observer = null;
             this._wheel = null;
         }
+        if (this.onChangedObservable) {
+            this.onChangedObservable.clear();
+        }
+    }
+
+    /**
+     * Called for each rendered frame.
+     */
+    public checkInputs(): void {
+        this.onChangedObservable.notifyObservers({
+            wheelDeltaX: this._wheelDeltaX,
+            wheelDeltaY: this._wheelDeltaY,
+            wheelDeltaZ: this._wheelDeltaZ
+        });
+
+        // Clear deltas.
+        this._wheelDeltaX = 0;
+        this._wheelDeltaY = 0;
+        this._wheelDeltaZ = 0;
     }
 
     /**

+ 2 - 31
src/Cameras/Inputs/freeCameraMouseWheelInput.ts

@@ -24,19 +24,6 @@ export enum AXIS {
 }
 
 /**
- * An interface for user definable callback to be called on mouse wheel movement.
- */
-export interface IFreeCameraMouseWheelCustomCallback {
-    /**
-     * @param camera The camera instance the mouse wheel is attached to.
-     * @param wheelDeltaX The change in value of the mouse wheel's X axis since last called.
-     * @param wheelDeltaY The change in value of the mouse wheel's X axis since last called.
-     * @param wheelDeltaZ The change in value of the mouse wheel's X axis since last called.
-     */
-    (camera: FreeCamera, wheelDeltaX: number, wheelDeltaY: number, wheelDeltaZ: number): void;
-}
-
-/**
  * Manage the mouse wheel inputs to control a free camera.
  * @see https://doc.babylonjs.com/how_to/customizing_camera_inputs
  */
@@ -299,14 +286,6 @@ export class FreeCameraMouseWheelInput extends BaseCameraMouseWheelInput {
     }
 
     /**
-     * A user configurable callback to be called on mouse wheel movement.
-     * To be used whenever the default functionality of this class does not
-     * change the required camera parameter by default.
-     */
-    @serialize()
-    public customCallback: Nullable<IFreeCameraMouseWheelCustomCallback> = null;
-
-    /**
      * Called for each rendered frame.
      */
     public checkInputs(): void {
@@ -343,16 +322,8 @@ export class FreeCameraMouseWheelInput extends BaseCameraMouseWheelInput {
         this.camera.cameraDirection.addInPlace(transformedDirection);
         this.camera.cameraDirection.addInPlace(this._moveScene);
 
-        // Do the user defined customCallback if set.
-        if (this.customCallback !== null) {
-            this.customCallback(
-                this.camera, this._wheelDeltaX, this._wheelDeltaY, this._wheelDeltaZ);
-        }
-
-        // Clear deltas.
-        this._wheelDeltaX = 0;
-        this._wheelDeltaY = 0;
-        this._wheelDeltaZ = 0;
+        // Call the base class implementation to handle observers and do cleanup.
+        super.checkInputs();
     }
 
     private _moveRelative = Vector3.Zero();