|
@@ -8,28 +8,26 @@ import { Nullable } from '../types';
|
|
* will use. This class assumes that there will only be one
|
|
* will use. This class assumes that there will only be one
|
|
* pointer device and one keyboard.
|
|
* pointer device and one keyboard.
|
|
*/
|
|
*/
|
|
-export class DeviceInputSystem
|
|
|
|
-{
|
|
|
|
|
|
+export class DeviceInputSystem {
|
|
// Public Members
|
|
// Public Members
|
|
/** Constant string reference for mouse/pointer */
|
|
/** Constant string reference for mouse/pointer */
|
|
- public static readonly POINTER_DEVICE : string = "Pointer";
|
|
|
|
|
|
+ public static readonly POINTER_DEVICE: string = "Pointer";
|
|
/** Constant string reference for keyboard */
|
|
/** Constant string reference for keyboard */
|
|
- public static readonly KEYBOARD_DEVICE : string = "Keyboard";
|
|
|
|
|
|
+ public static readonly KEYBOARD_DEVICE: string = "Keyboard";
|
|
|
|
|
|
// Private Members
|
|
// Private Members
|
|
- private _inputs : Map<string, Array<number>> = new Map();
|
|
|
|
- private _onDeviceConnected : (deviceName : string) => void = () => {};
|
|
|
|
- private _onDeviceDisconnected : (deviceName : string) => void = () => {};
|
|
|
|
- private _keyboardActive : boolean = false;
|
|
|
|
- private _mouseActive : boolean = false;
|
|
|
|
|
|
+ private _inputs: Map<string, Array<number>> = new Map();
|
|
|
|
+ private _onDeviceConnected: (deviceName: string) => void = () => { };
|
|
|
|
+ private _onDeviceDisconnected: (deviceName: string) => void = () => { };
|
|
|
|
+ private _keyboardActive: boolean = false;
|
|
|
|
+ private _mouseActive: boolean = false;
|
|
private _elementToAttachTo: Nullable<HTMLElement>;
|
|
private _elementToAttachTo: Nullable<HTMLElement>;
|
|
|
|
|
|
/**
|
|
/**
|
|
* Default Constructor
|
|
* Default Constructor
|
|
* @param elementToAttachTo - element to attach events to (usually canvas)
|
|
* @param elementToAttachTo - element to attach events to (usually canvas)
|
|
*/
|
|
*/
|
|
- constructor(elementToAttachTo: Nullable<HTMLElement>)
|
|
|
|
- {
|
|
|
|
|
|
+ constructor(elementToAttachTo: Nullable<HTMLElement>) {
|
|
this._elementToAttachTo = elementToAttachTo;
|
|
this._elementToAttachTo = elementToAttachTo;
|
|
this.handleKeyActions();
|
|
this.handleKeyActions();
|
|
this.handleMouseActions();
|
|
this.handleMouseActions();
|
|
@@ -45,23 +43,18 @@ export class DeviceInputSystem
|
|
*
|
|
*
|
|
* @returns Current value of input
|
|
* @returns Current value of input
|
|
*/
|
|
*/
|
|
- public pollInput(deviceName : string, inputIndex : number) : number
|
|
|
|
- {
|
|
|
|
- if (this._inputs.has(deviceName))
|
|
|
|
- {
|
|
|
|
|
|
+ public pollInput(deviceName: string, inputIndex: number): number {
|
|
|
|
+ if (this._inputs.has(deviceName)) {
|
|
var device = this._inputs.get(deviceName);
|
|
var device = this._inputs.get(deviceName);
|
|
|
|
|
|
- if (device![inputIndex] != undefined)
|
|
|
|
- {
|
|
|
|
|
|
+ if (device![inputIndex] != undefined) {
|
|
return device![inputIndex];
|
|
return device![inputIndex];
|
|
}
|
|
}
|
|
- else
|
|
|
|
- {
|
|
|
|
|
|
+ else {
|
|
throw `Unable to find input ${inputIndex} on device ${deviceName}`;
|
|
throw `Unable to find input ${inputIndex} on device ${deviceName}`;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- else
|
|
|
|
- {
|
|
|
|
|
|
+ else {
|
|
throw `Unable to find device ${deviceName}`;
|
|
throw `Unable to find device ${deviceName}`;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -70,8 +63,7 @@ export class DeviceInputSystem
|
|
* onDeviceConnected: When a device is connected, perform user specified function
|
|
* onDeviceConnected: When a device is connected, perform user specified function
|
|
* @param callback Callback function to use when a device is connected
|
|
* @param callback Callback function to use when a device is connected
|
|
*/
|
|
*/
|
|
- public onDeviceConnected(callback : (deviceName : string) => void) : void
|
|
|
|
- {
|
|
|
|
|
|
+ public onDeviceConnected(callback: (deviceName: string) => void): void {
|
|
this._onDeviceConnected = callback;
|
|
this._onDeviceConnected = callback;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -79,8 +71,7 @@ export class DeviceInputSystem
|
|
* onDeviceDisconnected: When a device is disconnected, perform user specified function
|
|
* onDeviceDisconnected: When a device is disconnected, perform user specified function
|
|
* @param callback Callback function to use when a device is disconnected
|
|
* @param callback Callback function to use when a device is disconnected
|
|
*/
|
|
*/
|
|
- public onDeviceDisconnected(callback : (deviceName : string) => void) : void
|
|
|
|
- {
|
|
|
|
|
|
+ public onDeviceDisconnected(callback: (deviceName: string) => void): void {
|
|
this._onDeviceDisconnected = callback;
|
|
this._onDeviceDisconnected = callback;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -90,13 +81,10 @@ export class DeviceInputSystem
|
|
* @param deviceName Assigned name of device (may be SN)
|
|
* @param deviceName Assigned name of device (may be SN)
|
|
* @param numberOfInputs Number of input entries to create for given device
|
|
* @param numberOfInputs Number of input entries to create for given device
|
|
*/
|
|
*/
|
|
- private registerDevice(deviceName : string, numberOfInputs : number)
|
|
|
|
- {
|
|
|
|
- if (!this._inputs.has(deviceName))
|
|
|
|
- {
|
|
|
|
- var device : Array<number> = [];
|
|
|
|
- for (var i = 0; i < numberOfInputs; i++)
|
|
|
|
- {
|
|
|
|
|
|
+ private registerDevice(deviceName: string, numberOfInputs: number) {
|
|
|
|
+ if (!this._inputs.has(deviceName)) {
|
|
|
|
+ var device: Array<number> = [];
|
|
|
|
+ for (var i = 0; i < numberOfInputs; i++) {
|
|
device.push(0);
|
|
device.push(0);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -108,35 +96,30 @@ export class DeviceInputSystem
|
|
* deregisterDevice: Given a specific device name, remove that device from the device map
|
|
* deregisterDevice: Given a specific device name, remove that device from the device map
|
|
* @param deviceName Name of device to be removed
|
|
* @param deviceName Name of device to be removed
|
|
*/
|
|
*/
|
|
- private deregisterDevice(deviceName : string)
|
|
|
|
- {
|
|
|
|
|
|
+ private deregisterDevice(deviceName: string) {
|
|
this._inputs.delete(deviceName);
|
|
this._inputs.delete(deviceName);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* handleKeyActions: Handle all actions that come from keyboard interaction
|
|
* handleKeyActions: Handle all actions that come from keyboard interaction
|
|
*/
|
|
*/
|
|
- private handleKeyActions()
|
|
|
|
- {
|
|
|
|
|
|
+ private handleKeyActions() {
|
|
window.addEventListener("keydown", (evt) => {
|
|
window.addEventListener("keydown", (evt) => {
|
|
- if (!this._keyboardActive)
|
|
|
|
- {
|
|
|
|
|
|
+ if (!this._keyboardActive) {
|
|
this._keyboardActive = true;
|
|
this._keyboardActive = true;
|
|
this.registerDevice(DeviceInputSystem.KEYBOARD_DEVICE, 222);
|
|
this.registerDevice(DeviceInputSystem.KEYBOARD_DEVICE, 222);
|
|
this._onDeviceConnected(DeviceInputSystem.KEYBOARD_DEVICE);
|
|
this._onDeviceConnected(DeviceInputSystem.KEYBOARD_DEVICE);
|
|
}
|
|
}
|
|
|
|
|
|
let kbKey = this._inputs.get(DeviceInputSystem.KEYBOARD_DEVICE);
|
|
let kbKey = this._inputs.get(DeviceInputSystem.KEYBOARD_DEVICE);
|
|
- if (kbKey)
|
|
|
|
- {
|
|
|
|
|
|
+ if (kbKey) {
|
|
kbKey[evt.keyCode] = 1;
|
|
kbKey[evt.keyCode] = 1;
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
|
|
window.addEventListener("keyup", (evt) => {
|
|
window.addEventListener("keyup", (evt) => {
|
|
let kbKey = this._inputs.get(DeviceInputSystem.KEYBOARD_DEVICE);
|
|
let kbKey = this._inputs.get(DeviceInputSystem.KEYBOARD_DEVICE);
|
|
- if (kbKey)
|
|
|
|
- {
|
|
|
|
|
|
+ if (kbKey) {
|
|
kbKey[evt.keyCode] = 0;
|
|
kbKey[evt.keyCode] = 0;
|
|
}
|
|
}
|
|
});
|
|
});
|
|
@@ -145,48 +128,41 @@ export class DeviceInputSystem
|
|
/**
|
|
/**
|
|
* handleMouseActions: Handle all actions that come from mouse interaction
|
|
* handleMouseActions: Handle all actions that come from mouse interaction
|
|
*/
|
|
*/
|
|
- private handleMouseActions()
|
|
|
|
- {
|
|
|
|
|
|
+ private handleMouseActions() {
|
|
this._elementToAttachTo?.addEventListener("pointermove", (evt) => {
|
|
this._elementToAttachTo?.addEventListener("pointermove", (evt) => {
|
|
- if (!this._mouseActive)
|
|
|
|
- {
|
|
|
|
|
|
+ if (!this._mouseActive) {
|
|
this._mouseActive = true;
|
|
this._mouseActive = true;
|
|
this.registerDevice(DeviceInputSystem.POINTER_DEVICE, 5);
|
|
this.registerDevice(DeviceInputSystem.POINTER_DEVICE, 5);
|
|
this._onDeviceConnected(DeviceInputSystem.POINTER_DEVICE);
|
|
this._onDeviceConnected(DeviceInputSystem.POINTER_DEVICE);
|
|
}
|
|
}
|
|
|
|
|
|
let mouseX = this._inputs.get(DeviceInputSystem.POINTER_DEVICE);
|
|
let mouseX = this._inputs.get(DeviceInputSystem.POINTER_DEVICE);
|
|
- if (mouseX)
|
|
|
|
- {
|
|
|
|
|
|
+ if (mouseX) {
|
|
mouseX[0] = evt.clientX;
|
|
mouseX[0] = evt.clientX;
|
|
}
|
|
}
|
|
|
|
|
|
let mouseY = this._inputs.get(DeviceInputSystem.POINTER_DEVICE);
|
|
let mouseY = this._inputs.get(DeviceInputSystem.POINTER_DEVICE);
|
|
- if (mouseY)
|
|
|
|
- {
|
|
|
|
|
|
+ if (mouseY) {
|
|
mouseY[1] = evt.clientY;
|
|
mouseY[1] = evt.clientY;
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
|
|
this._elementToAttachTo?.addEventListener("pointerdown", (evt) => {
|
|
this._elementToAttachTo?.addEventListener("pointerdown", (evt) => {
|
|
- if (!this._mouseActive)
|
|
|
|
- {
|
|
|
|
|
|
+ if (!this._mouseActive) {
|
|
this._mouseActive = true;
|
|
this._mouseActive = true;
|
|
this.registerDevice(DeviceInputSystem.POINTER_DEVICE, 5);
|
|
this.registerDevice(DeviceInputSystem.POINTER_DEVICE, 5);
|
|
this._onDeviceConnected(DeviceInputSystem.POINTER_DEVICE);
|
|
this._onDeviceConnected(DeviceInputSystem.POINTER_DEVICE);
|
|
}
|
|
}
|
|
|
|
|
|
let mouseButton = this._inputs.get(DeviceInputSystem.POINTER_DEVICE);
|
|
let mouseButton = this._inputs.get(DeviceInputSystem.POINTER_DEVICE);
|
|
- if (mouseButton)
|
|
|
|
- {
|
|
|
|
|
|
+ if (mouseButton) {
|
|
mouseButton[evt.button] = 1;
|
|
mouseButton[evt.button] = 1;
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
|
|
this._elementToAttachTo?.addEventListener("pointerup", (evt) => {
|
|
this._elementToAttachTo?.addEventListener("pointerup", (evt) => {
|
|
let mouseButton = this._inputs.get(DeviceInputSystem.POINTER_DEVICE);
|
|
let mouseButton = this._inputs.get(DeviceInputSystem.POINTER_DEVICE);
|
|
- if (mouseButton)
|
|
|
|
- {
|
|
|
|
|
|
+ if (mouseButton) {
|
|
mouseButton[evt.button] = 0;
|
|
mouseButton[evt.button] = 0;
|
|
}
|
|
}
|
|
});
|
|
});
|
|
@@ -195,15 +171,14 @@ export class DeviceInputSystem
|
|
/**
|
|
/**
|
|
* handleGamepadActions: Handle all actions that come from gamepad interaction
|
|
* handleGamepadActions: Handle all actions that come from gamepad interaction
|
|
*/
|
|
*/
|
|
- private handleGamepadActions()
|
|
|
|
- {
|
|
|
|
- window.addEventListener("gamepadconnected", (evt : any) => {
|
|
|
|
|
|
+ private handleGamepadActions() {
|
|
|
|
+ window.addEventListener("gamepadconnected", (evt: any) => {
|
|
var deviceName = `${evt.gamepad.id}-${evt.gamepad.index}`;
|
|
var deviceName = `${evt.gamepad.id}-${evt.gamepad.index}`;
|
|
this.registerDevice(deviceName, evt.gamepad.buttons.length + evt.gamepad.axes.length);
|
|
this.registerDevice(deviceName, evt.gamepad.buttons.length + evt.gamepad.axes.length);
|
|
this._onDeviceConnected(deviceName);
|
|
this._onDeviceConnected(deviceName);
|
|
});
|
|
});
|
|
|
|
|
|
- window.addEventListener("gamepaddisconnected", (evt : any) => {
|
|
|
|
|
|
+ window.addEventListener("gamepaddisconnected", (evt: any) => {
|
|
var deviceName = `${evt.gamepad.id}-${evt.gamepad.index}`;
|
|
var deviceName = `${evt.gamepad.id}-${evt.gamepad.index}`;
|
|
this.deregisterDevice(deviceName);
|
|
this.deregisterDevice(deviceName);
|
|
this._onDeviceDisconnected(deviceName);
|
|
this._onDeviceDisconnected(deviceName);
|
|
@@ -213,31 +188,24 @@ export class DeviceInputSystem
|
|
/**
|
|
/**
|
|
* updateDevices: Update all non-event based devices with each frame
|
|
* updateDevices: Update all non-event based devices with each frame
|
|
*/
|
|
*/
|
|
- private updateDevices()
|
|
|
|
- {
|
|
|
|
|
|
+ private updateDevices() {
|
|
// Gamepads
|
|
// Gamepads
|
|
var gamepads = this.getGamePads();
|
|
var gamepads = this.getGamePads();
|
|
|
|
|
|
- for (var j = 0; j < gamepads.length; j++)
|
|
|
|
- {
|
|
|
|
|
|
+ for (var j = 0; j < gamepads.length; j++) {
|
|
let gp = gamepads[j];
|
|
let gp = gamepads[j];
|
|
|
|
|
|
- if (gp)
|
|
|
|
- {
|
|
|
|
- for (var i = 0; i < gp.buttons.length; i++)
|
|
|
|
- {
|
|
|
|
|
|
+ if (gp) {
|
|
|
|
+ for (var i = 0; i < gp.buttons.length; i++) {
|
|
let button = this._inputs.get(`${gp.id}-${gp.index}`);
|
|
let button = this._inputs.get(`${gp.id}-${gp.index}`);
|
|
- if (button)
|
|
|
|
- {
|
|
|
|
|
|
+ if (button) {
|
|
button[i] = gp.buttons[i].value;
|
|
button[i] = gp.buttons[i].value;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- for (var i = 0; i < gp.axes.length; i++)
|
|
|
|
- {
|
|
|
|
|
|
+ for (var i = 0; i < gp.axes.length; i++) {
|
|
let axis = this._inputs.get(`${gp.id}-${gp.index}`);
|
|
let axis = this._inputs.get(`${gp.id}-${gp.index}`);
|
|
- if (axis)
|
|
|
|
- {
|
|
|
|
|
|
+ if (axis) {
|
|
axis[i + gp.buttons.length] = gp.axes[i].valueOf();
|
|
axis[i + gp.buttons.length] = gp.axes[i].valueOf();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -251,8 +219,7 @@ export class DeviceInputSystem
|
|
* getGamePads: returns all gamepads
|
|
* getGamePads: returns all gamepads
|
|
* @returns array with active gamepads
|
|
* @returns array with active gamepads
|
|
*/
|
|
*/
|
|
- private getGamePads() : (Gamepad | null) []
|
|
|
|
- {
|
|
|
|
|
|
+ private getGamePads(): (Gamepad | null)[] {
|
|
return navigator.getGamepads();
|
|
return navigator.getGamepads();
|
|
}
|
|
}
|
|
}
|
|
}
|