浏览代码

Removed observed stuff. Will re-add later (WIP)

Dave Solares 5 年之前
父节点
当前提交
b183c8e535
共有 2 个文件被更改,包括 95 次插入58 次删除
  1. 87 45
      src/DeviceInput/InputDevices/deviceSourceManager.ts
  2. 8 13
      src/DeviceInput/deviceInputSystem.ts

+ 87 - 45
src/DeviceInput/InputDevices/deviceSourceManager.ts

@@ -6,6 +6,66 @@ import { Nullable } from '../../types';
 import { Observable } from '../../Misc/observable';
 
 /**
+ * Class that handles all input for a specific device
+ */
+export class DeviceSource<T extends DeviceType> {
+    /**
+     * Observable to handle device input changes per device
+     */
+    private _deviceInputSystem: DeviceInputSystem;
+    private _touchPoints: Array<string>;
+
+    /**
+     * Default Constructor
+     * @param deviceInputSystem Reference to DeviceInputSystem
+     * @param deviceName Name of device to be used by DeviceInputSystem
+     * @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) {
+        this._deviceInputSystem = deviceInputSystem;
+
+        if (deviceType == DeviceType.Touch) {
+            this._touchPoints = new Array<string>();
+        }
+    }
+
+    /**
+     * Get input for specific input
+     * @param inputIndex
+     * @returns Input value from DeviceInputSystem
+     */
+    public getInput(inputIndex: T): Nullable<number> {
+        if (this.deviceType == DeviceType.Touch) {
+            return this._deviceInputSystem.pollInput(this._touchPoints[this.deviceSlot], inputIndex);
+        }
+
+        return this._deviceInputSystem.pollInput(this.deviceName, inputIndex);
+    }
+
+    /**
+     * Add specific point to array of touch points (DeviceType.Touch only)
+     * @param name Name of Specific Touch Point
+     */
+    public addTouchPoints(name: string) {
+        if (this.deviceType == DeviceType.Touch) {
+            this._touchPoints.push(name)
+        }
+    }
+
+    /**
+     * Remove specific point from array of touch points (DeviceType.Touch only)
+     * @param name Name of Specific Touch Point
+     */
+    public removeTouchPoints(name: string) {
+        if (this.deviceType == DeviceType.Touch) {
+            const touchIndex = this._touchPoints.indexOf(name);
+            this._touchPoints.splice(touchIndex, 1);
+        }
+    }
+}
+
+/**
  * Class to keep track of devices
  */
 export class DeviceSourceManager implements IDisposable {
@@ -13,26 +73,25 @@ export class DeviceSourceManager implements IDisposable {
     /**
      * Observable to be triggered when before a device is connected
      */
-    public onBeforeDeviceConnectedObservable = new Observable<{deviceType: DeviceType, deviceSlot: number}>();
+    public onBeforeDeviceConnectedObservable = new Observable<{ deviceType: DeviceType, deviceSlot: number }>();
 
     /**
      * Observable to be triggered when before a device is disconnected
      */
-    public onBeforeDeviceDisconnectedObservable = new Observable<{deviceType: DeviceType, deviceSlot: number}>();
+    public onBeforeDeviceDisconnectedObservable = new Observable<{ deviceType: DeviceType, deviceSlot: number }>();
 
     /**
      * Observable to be triggered when after a device is connected
      */
-    public onAfterDeviceConnectedObservable = new Observable<{deviceType: DeviceType, deviceSlot: number}>();
+    public onAfterDeviceConnectedObservable = new Observable<{ deviceType: DeviceType, deviceSlot: number }>();
 
     /**
      * Observable to be triggered when after a device is disconnected
      */
-    public onAfterDeviceDisconnectedObservable = new Observable<{deviceType: DeviceType, deviceSlot: number}>();
+    public onAfterDeviceDisconnectedObservable = new Observable<{ deviceType: DeviceType, deviceSlot: number }>();
 
     // Private Members
-    private _devices: Array<Array<string>>;
-    private _touchPoints: Array<string>;
+    private _devices: Array<Array<DeviceSource<DeviceType>>>;
     private _firstDevice: Array<number>;
     private _deviceInputSystem: DeviceInputSystem;
 
@@ -42,7 +101,7 @@ export class DeviceSourceManager implements IDisposable {
      */
     constructor(engine: Engine) {
         const numberOfDeviceTypes = Object.keys(DeviceType).length / 2;
-        this._devices = new Array<Array<string>>(numberOfDeviceTypes);
+        this._devices = new Array<Array<DeviceSource<DeviceType>>>(numberOfDeviceTypes);
         this._firstDevice = new Array<number>(numberOfDeviceTypes);
         this._deviceInputSystem = new DeviceInputSystem(engine);
 
@@ -50,17 +109,17 @@ export class DeviceSourceManager implements IDisposable {
             const deviceType = this._getDeviceTypeFromName(deviceName);
             const deviceSlot = this._getDeviceSlot(deviceName);
 
-            this.onBeforeDeviceConnectedObservable.notifyObservers({deviceType, deviceSlot});
+            this.onBeforeDeviceConnectedObservable.notifyObservers({ deviceType, deviceSlot });
             this._addDevice(deviceName);
-            this.onAfterDeviceConnectedObservable.notifyObservers({deviceType, deviceSlot});
+            this.onAfterDeviceConnectedObservable.notifyObservers({ deviceType, deviceSlot });
         };
         this._deviceInputSystem.onDeviceDisconnected = (deviceName) => {
             const deviceType = this._getDeviceTypeFromName(deviceName);
             const deviceSlot = this._getDeviceSlot(deviceName);
 
-            this.onBeforeDeviceDisconnectedObservable.notifyObservers({deviceType, deviceSlot});
+            this.onBeforeDeviceDisconnectedObservable.notifyObservers({ deviceType, deviceSlot });
             this._removeDevice(deviceName);
-            this.onAfterDeviceDisconnectedObservable.notifyObservers({deviceType, deviceSlot});
+            this.onAfterDeviceDisconnectedObservable.notifyObservers({ deviceType, deviceSlot });
         };
     }
 
@@ -73,16 +132,11 @@ 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) {
             return null;
         }
-        else if (type == DeviceType.Touch)
-        {
-            return this._deviceInputSystem.pollInput(this._touchPoints[deviceSlot], inputIndex);
-        }
 
-        return this._deviceInputSystem.pollInput(this._devices[type][deviceSlot], inputIndex);
+        return this._devices[type][deviceSlot].getInput(inputIndex);
     }
 
     /**
@@ -101,25 +155,20 @@ export class DeviceSourceManager implements IDisposable {
         const deviceSlot = this._getDeviceSlot(deviceName);
         const deviceType = this._getDeviceTypeFromName(deviceName);
 
-        if (!this._devices[deviceType])
-        {
-            this._devices[deviceType] = new Array<string>();
+        if (!this._devices[deviceType]) {
+            this._devices[deviceType] = new Array<DeviceSource<DeviceType>>();
 
-            if (deviceType == DeviceType.Touch)
-            {
-                this._touchPoints = new Array<string>();
-                this._devices[deviceType][deviceSlot] = POINTER_DEVICE;
+            if (deviceType == DeviceType.Touch) {
+                this._devices[deviceType][deviceSlot] = new DeviceSource<DeviceType>(this._deviceInputSystem, POINTER_DEVICE, DeviceType.Touch, deviceSlot);
             }
         }
 
         // If device is a touch device, update only touch points.  Otherwise, add new device.
-        if (deviceType == DeviceType.Touch)
-        {
-            this._touchPoints.push(deviceName);
+        if (deviceType == DeviceType.Touch) {
+            this._devices[deviceType][deviceSlot].addTouchPoints(deviceName);
         }
-        else
-        {
-            this._devices[deviceType][deviceSlot] = deviceName;
+        else {
+            this._devices[deviceType][deviceSlot] = new DeviceSource<DeviceType>(this._deviceInputSystem, deviceName, deviceType, deviceSlot);
         }
 
         this._updateFirstDevices(deviceType);
@@ -133,13 +182,10 @@ export class DeviceSourceManager implements IDisposable {
         const deviceSlot = this._getDeviceSlot(deviceName);
         const deviceType = this._getDeviceTypeFromName(deviceName);
 
-        if (deviceType == DeviceType.Touch)
-        {
-            const touchIndex = this._touchPoints.indexOf(deviceName);
-            this._touchPoints.splice(touchIndex, 1);
+        if (deviceType == DeviceType.Touch) {
+            this._devices[deviceType][deviceSlot].removeTouchPoints(deviceName);
         }
-        else
-        {
+        else {
             delete this._devices[deviceType][deviceSlot];
         }
     }
@@ -165,8 +211,7 @@ export class DeviceSourceManager implements IDisposable {
      * @param type Type of Device
      */
     private _updateFirstDevices(type: DeviceType) {
-        switch (type)
-        {
+        switch (type) {
             case DeviceType.Keyboard:
             case DeviceType.Mouse:
             case DeviceType.Touch:
@@ -179,10 +224,8 @@ export class DeviceSourceManager implements IDisposable {
                 let i = 0;
                 let first = -1;
 
-                while (first < 0 && i < this._devices[type].length)
-                {
-                    if (this._devices[type][i])
-                    {
+                while (first < 0 && i < this._devices[type].length) {
+                    if (this._devices[type][i]) {
                         first = i;
                     }
                     i++;
@@ -198,8 +241,7 @@ export class DeviceSourceManager implements IDisposable {
      * @param deviceName Name of Device from DeviceInputSystem
      * @returns DeviceType enum value
      */
-    private _getDeviceTypeFromName(deviceName: string): DeviceType
-    {
+    private _getDeviceTypeFromName(deviceName: string): DeviceType {
         if (deviceName == KEYBOARD_DEVICE) {
             return DeviceType.Keyboard;
         }

+ 8 - 13
src/DeviceInput/deviceInputSystem.ts

@@ -42,11 +42,6 @@ export class DeviceInputSystem implements IDisposable {
      */
     public onDeviceDisconnected = (deviceName: string) => {};
 
-    /**
-     * Callback to be triggered when keyboard/mouse/pointer changes input values
-     */
-    public observeInput = (deviceName: string, inputIndex: number, previousState: Nullable<number>, currentState: Nullable<number>) => { };
-
     // Private Members
     private _inputs: { [key: string]: Array<Nullable<number>> } = {};
     private _gamepads: Array<string>;
@@ -168,7 +163,7 @@ export class DeviceInputSystem implements IDisposable {
 
             const kbKey = this._inputs[KEYBOARD_DEVICE];
             if (kbKey) {
-                this.observeInput(KEYBOARD_DEVICE, evt.keyCode, kbKey[evt.keyCode], 1);
+                //this.observeInput(KEYBOARD_DEVICE, evt.keyCode, kbKey[evt.keyCode], 1);
                 kbKey[evt.keyCode] = 1;
             }
         });
@@ -176,7 +171,7 @@ 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);
+                //this.observeInput(KEYBOARD_DEVICE, evt.keyCode, kbKey[evt.keyCode], 0);
                 kbKey[evt.keyCode] = 0;
             }
         });
@@ -199,8 +194,8 @@ 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, 0, pointer[0], evt.clientX);
+                //this.observeInput(deviceName, 1, pointer[1], evt.clientY);
                 pointer[0] = evt.clientX;
                 pointer[1] = evt.clientY;
             }
@@ -216,9 +211,9 @@ 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);
+                //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;
@@ -230,7 +225,7 @@ export class DeviceInputSystem implements IDisposable {
 
             const pointer = this._inputs[deviceName];
             if (pointer) {
-                this.observeInput(deviceName, evt.button + 2, pointer[evt.button + 2], 0);
+                //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