浏览代码

Added observe event functions

Dave Solares 5 年之前
父节点
当前提交
f2961502f9
共有 2 个文件被更改,包括 73 次插入16 次删除
  1. 48 6
      src/DeviceInput/InputDevices/deviceSourceManager.ts
  2. 25 10
      src/DeviceInput/deviceInputSystem.ts

+ 48 - 6
src/DeviceInput/InputDevices/deviceSourceManager.ts

@@ -12,6 +12,7 @@ export class DeviceSource<T extends DeviceType> {
     /**
      * Observable to handle device input changes per device
      */
+    public onInputChangedObservable = new Observable<{ inputIndex: number, previousState: Nullable<number>, currentState: Nullable<number> }>();
     private _deviceInputSystem: DeviceInputSystem;
     private _touchPoints: Array<string>;
 
@@ -22,7 +23,13 @@ export class DeviceSource<T extends DeviceType> {
      * @param deviceType Type of device
      * @param deviceSlot "Slot" or index that device is referenced in
      */
-    constructor(deviceInputSystem: DeviceInputSystem, public deviceName: string, public deviceType: DeviceInputs<T>, public deviceSlot: number = 0) {
+    constructor(deviceInputSystem: DeviceInputSystem,
+        /** Name of device to be used by DeviceInputSystem */
+        public deviceName: string,
+        /** Type of device */
+        public deviceType: DeviceInputs<T>,
+        /** "Slot" or index that device is referenced in */
+        public deviceSlot: number = 0) {
         this._deviceInputSystem = deviceInputSystem;
 
         if (deviceType == DeviceType.Touch) {
@@ -32,7 +39,7 @@ export class DeviceSource<T extends DeviceType> {
 
     /**
      * Get input for specific input
-     * @param inputIndex
+     * @param inputIndex index of specific input on device
      * @returns Input value from DeviceInputSystem
      */
     public getInput(inputIndex: T): Nullable<number> {
@@ -49,7 +56,7 @@ export class DeviceSource<T extends DeviceType> {
      */
     public addTouchPoints(name: string) {
         if (this.deviceType == DeviceType.Touch) {
-            this._touchPoints.push(name)
+            this._touchPoints.push(name);
         }
     }
 
@@ -97,9 +104,10 @@ export class DeviceSourceManager implements IDisposable {
 
     /**
      * Default Constructor
-     * @param engine - engine to pull input element from
+     * @param engine engine to pull input element from
+     * @param enableObserveEvents boolean to enable use of observe events
      */
-    constructor(engine: Engine) {
+    constructor(engine: Engine, enableObserveEvents: boolean = false) {
         const numberOfDeviceTypes = Object.keys(DeviceType).length / 2;
         this._devices = new Array<Array<DeviceSource<DeviceType>>>(numberOfDeviceTypes);
         this._firstDevice = new Array<number>(numberOfDeviceTypes);
@@ -121,6 +129,17 @@ export class DeviceSourceManager implements IDisposable {
             this._removeDevice(deviceName);
             this.onAfterDeviceDisconnectedObservable.notifyObservers({ deviceType, deviceSlot });
         };
+
+        if (!this._deviceInputSystem.observeInput && enableObserveEvents) {
+            this._deviceInputSystem.observeInput = (deviceName, inputIndex, previousState, currentState) => {
+                const deviceType = this._getDeviceTypeFromName(deviceName);
+                const deviceSlot = this._getDeviceSlot(deviceName);
+
+                if (deviceType == DeviceType.Keyboard || deviceType == DeviceType.Mouse || deviceType == DeviceType.Touch) {
+                    this.getDeviceSource(deviceType, deviceSlot)?.onInputChangedObservable.notifyObservers({ inputIndex, previousState, currentState });
+                }
+            };
+        }
     }
 
     // Public Functions
@@ -132,7 +151,7 @@ export class DeviceSourceManager implements IDisposable {
      * @returns Current value of input
      */
     public getInput<T extends DeviceType>(type: T, inputIndex: DeviceInputs<T>, deviceSlot: number = this._firstDevice[type]): Nullable<number> {
-        if (!this._devices[type] || this._firstDevice[type] === undefined) {
+        if (!this._devices[type] || this._firstDevice[type] === undefined || this._devices[type][deviceSlot] === undefined) {
             return null;
         }
 
@@ -140,6 +159,29 @@ export class DeviceSourceManager implements IDisposable {
     }
 
     /**
+     * Gets a DeviceSource, given a type and slot
+     * @param deviceType Enum specifiying device type
+     * @param deviceSlot "Slot" or index that device is referenced in
+     * @returns DeviceSource object
+     */
+    public getDeviceSource(deviceType: DeviceType, deviceSlot: number = this._firstDevice[deviceType]): Nullable<DeviceSource<DeviceType>> {
+        if (!this._devices[deviceType] || this._firstDevice[deviceType] === undefined || this._devices[deviceType][deviceSlot] === undefined) {
+            return null;
+        }
+
+        return this._devices[deviceType][deviceSlot];
+    }
+
+    /**
+     * Gets an array of DeviceSource objects for a given device type
+     * @param deviceType Enum specifiying device type
+     * @returns Array of DeviceSource objects
+     */
+    public getDeviceSources(deviceType: DeviceType): ReadonlyArray<DeviceSource<DeviceType>> {
+        return this._devices[deviceType];
+    }
+
+    /**
      * Dispose of DeviceInputSystem and other parts
      */
     public dispose() {

+ 25 - 10
src/DeviceInput/deviceInputSystem.ts

@@ -35,12 +35,17 @@ export class DeviceInputSystem implements IDisposable {
     /**
      * Callback to be triggered when a device is connected
      */
-    public onDeviceConnected = (deviceName: string) => {};
+    public onDeviceConnected: (deviceName: string) => void;
 
     /**
      * Callback to be triggered when a device is disconnected
      */
-    public onDeviceDisconnected = (deviceName: string) => {};
+    public onDeviceDisconnected: (deviceName: string) => void;
+
+    /**
+     * Callback to be triggered when event driven input is updated
+     */
+    public observeInput: (deviceName: string, inputIndex: number, previousState: Nullable<number>, currentState: Nullable<number>) => void;
 
     // Private Members
     private _inputs: { [key: string]: Array<Nullable<number>> } = {};
@@ -163,7 +168,9 @@ export class DeviceInputSystem implements IDisposable {
 
             const kbKey = this._inputs[KEYBOARD_DEVICE];
             if (kbKey) {
-                //this.observeInput(KEYBOARD_DEVICE, evt.keyCode, kbKey[evt.keyCode], 1);
+                if (this.observeInput) {
+                    this.observeInput(KEYBOARD_DEVICE, evt.keyCode, kbKey[evt.keyCode], 1);
+                }
                 kbKey[evt.keyCode] = 1;
             }
         });
@@ -171,7 +178,9 @@ export class DeviceInputSystem implements IDisposable {
         this._keyboardUpEvent = ((evt) => {
             const kbKey = this._inputs[KEYBOARD_DEVICE];
             if (kbKey) {
-                //this.observeInput(KEYBOARD_DEVICE, evt.keyCode, kbKey[evt.keyCode], 0);
+                if (this.observeInput) {
+                    this.observeInput(KEYBOARD_DEVICE, evt.keyCode, kbKey[evt.keyCode], 0);
+                }
                 kbKey[evt.keyCode] = 0;
             }
         });
@@ -194,8 +203,10 @@ export class DeviceInputSystem implements IDisposable {
 
             const pointer = this._inputs[deviceName];
             if (pointer) {
-                //this.observeInput(deviceName, 0, pointer[0], evt.clientX);
-                //this.observeInput(deviceName, 1, pointer[1], evt.clientY);
+                if (this.observeInput) {
+                    this.observeInput(deviceName, 0, pointer[0], evt.clientX);
+                    this.observeInput(deviceName, 1, pointer[1], evt.clientY);
+                }
                 pointer[0] = evt.clientX;
                 pointer[1] = evt.clientY;
             }
@@ -211,9 +222,11 @@ export class DeviceInputSystem implements IDisposable {
 
             const pointer = this._inputs[deviceName];
             if (pointer) {
-                //this.observeInput(deviceName, 0, pointer[0], evt.clientX);
-                //this.observeInput(deviceName, 1, pointer[1], evt.clientY);
-                //this.observeInput(deviceName, evt.button + 2, pointer[evt.button + 2], 1);
+                if (this.observeInput) {
+                    this.observeInput(deviceName, 0, pointer[0], evt.clientX);
+                    this.observeInput(deviceName, 1, pointer[1], evt.clientY);
+                    this.observeInput(deviceName, evt.button + 2, pointer[evt.button + 2], 1);
+                }
                 pointer[0] = evt.clientX;
                 pointer[1] = evt.clientY;
                 pointer[evt.button + 2] = 1;
@@ -225,7 +238,9 @@ export class DeviceInputSystem implements IDisposable {
 
             const pointer = this._inputs[deviceName];
             if (pointer) {
-                //this.observeInput(deviceName, evt.button + 2, pointer[evt.button + 2], 0);
+                if (this.observeInput) {
+                    this.observeInput(deviceName, evt.button + 2, pointer[evt.button + 2], 0);
+                }
                 pointer[evt.button + 2] = 0;
             }
             if (evt.pointerType != "mouse") // Don't unregister the mouse