forked from BabylonJS/Babylon.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnativeDeviceInputSystem.ts
More file actions
93 lines (79 loc) · 3.75 KB
/
Copy pathnativeDeviceInputSystem.ts
File metadata and controls
93 lines (79 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { Nullable } from "../../types";
import { DeviceEventFactory } from "../Helpers/eventFactory";
import { DeviceType } from "../InputDevices/deviceEnums";
import { IDeviceEvent, IDeviceInputSystem, INativeInput } from "../Interfaces/inputInterfaces";
/** @hidden */
export class NativeDeviceInputSystemImpl implements IDeviceInputSystem {
public onDeviceConnected = (deviceType: DeviceType, deviceSlot: number) => { };
public onDeviceDisconnected = (deviceType: DeviceType, deviceSlot: number) => { };
public onInputChanged = (deviceEvent: IDeviceEvent) => { };
private readonly _nativeInput: INativeInput;
public constructor(nativeInput?: INativeInput) {
this._nativeInput = nativeInput || this._createDummyNativeInput();
this._nativeInput.onDeviceConnected = (deviceType, deviceSlot) => {
this.onDeviceConnected(deviceType, deviceSlot);
};
this._nativeInput.onDeviceDisconnected = (deviceType, deviceSlot) => {
this.onDeviceDisconnected(deviceType, deviceSlot);
};
this._nativeInput.onInputChanged = (deviceType, deviceSlot, inputIndex, previousState, currentState, eventData) => {
const evt = DeviceEventFactory.CreateDeviceEvent(deviceType, deviceSlot, inputIndex, currentState, this);
let deviceEvent = evt as IDeviceEvent;
deviceEvent.deviceType = deviceType;
deviceEvent.deviceSlot = deviceSlot;
deviceEvent.inputIndex = inputIndex;
deviceEvent.previousState = previousState;
deviceEvent.currentState = currentState;
this.onInputChanged(deviceEvent);
};
}
/**
* Configures events to work with an engine's active element
*/
public configureEvents(): void {
// Do nothing
}
// Public functions
/**
* Checks for current device input value, given an id and input index. Throws exception if requested device not initialized.
* @param deviceType Enum specifiying device type
* @param deviceSlot "Slot" or index that device is referenced in
* @param inputIndex Id of input to be checked
* @returns Current value of input
*/
public pollInput(deviceType: DeviceType, deviceSlot: number, inputIndex: number): number {
return this._nativeInput.pollInput(deviceType, deviceSlot, inputIndex);
}
/**
* Check for a specific device in the DeviceInputSystem
* @param deviceType Type of device to check for
* @returns bool with status of device's existence
*/
public isDeviceAvailable(deviceType: DeviceType) {
//TODO: FIx native side first
return (deviceType === DeviceType.Mouse || deviceType === DeviceType.Touch);
}
/**
* Dispose of all the observables
*/
public dispose(): void {
this.onDeviceConnected = () => { };
this.onDeviceDisconnected = () => { };
this.onInputChanged = () => { };
}
/**
* For versions of BabylonNative that don't have the NativeInput plugin initialized, create a dummy version
* @returns Object with dummy functions
*/
private _createDummyNativeInput() {
let nativeInput = {
onDeviceConnected: (deviceType: DeviceType, deviceSlot: number) => { },
onDeviceDisconnected: (deviceType: DeviceType, deviceSlot: number) => { },
onInputChanged: (deviceType: DeviceType, deviceSlot: number, inputIndex: number, previousState: Nullable<number>, currentState: Nullable<number>, eventData?: any) => { },
pollInput: () => { return 0; },
isDeviceAvailable: () => { return false; },
dispose: () => { },
};
return nativeInput;
}
}