forked from BabylonJS/Babylon.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeviceInputSystem.ts
More file actions
114 lines (99 loc) · 4.38 KB
/
Copy pathdeviceInputSystem.ts
File metadata and controls
114 lines (99 loc) · 4.38 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { Engine } from '../Engines/engine';
import { INative } from '../Engines/Native/nativeInterfaces';
import { Observable } from '../Misc/observable';
import { NativeDeviceInputSystemImpl } from './Implementations/nativeDeviceInputSystem';
import { WebDeviceInputSystemImpl } from './Implementations/webDeviceInputSystem';
import { DeviceType } from './InputDevices/deviceEnums';
import { IDeviceEvent, IDeviceInputSystem } from './Interfaces/inputInterfaces';
declare const _native: INative;
/**
* This class will take all inputs from Keyboard, Pointer, and
* any Gamepads and provide a polling system that all devices
* will use. This class assumes that there will only be one
* pointer device and one keyboard.
*/
export class DeviceInputSystem {
// Observables
/**
* Observable for devices being connected
*/
public readonly onDeviceConnectedObservable: Observable<{ deviceType: DeviceType; deviceSlot: number; }>;
/**
* Observable for devices being disconnected
*/
public readonly onDeviceDisconnectedObservable: Observable<{ deviceType: DeviceType; deviceSlot: number; }>;
/**
* Observable for changes to device input
*/
public readonly onInputChangedObservable: Observable<IDeviceEvent>;
private _deviceInputSystem: IDeviceInputSystem;
/** @hidden */
public static _Create(engine: Engine): DeviceInputSystem {
// If running in Babylon Native, then defer to the native input system, which has the same public contract
if (!engine.deviceInputSystem) {
let selectedDIS;
if (typeof _native !== 'undefined') {
selectedDIS = (_native.DeviceInputSystem) ? new NativeDeviceInputSystemImpl(new _native.DeviceInputSystem()) : new NativeDeviceInputSystemImpl();
}
else {
selectedDIS = new WebDeviceInputSystemImpl(engine);
}
if (selectedDIS) {
engine.deviceInputSystem = new DeviceInputSystem(selectedDIS);
}
}
return engine.deviceInputSystem;
}
/**
* DeviceInputSystem constructor
* @param deviceInputSystem Web or Native implementation of DeviceInputSystem
*/
constructor(deviceInputSystem: IDeviceInputSystem) {
this._deviceInputSystem = deviceInputSystem;
this.onDeviceConnectedObservable = new Observable();
this.onDeviceDisconnectedObservable = new Observable();
this.onInputChangedObservable = new Observable<IDeviceEvent>();
this._deviceInputSystem.onDeviceConnected = (deviceType, deviceSlot) => {
this.onDeviceConnectedObservable.notifyObservers({ deviceType, deviceSlot });
};
this._deviceInputSystem.onDeviceDisconnected = (deviceType, deviceSlot) => {
this.onDeviceDisconnectedObservable.notifyObservers({ deviceType, deviceSlot });
};
this._deviceInputSystem.onInputChanged = (deviceEvent) => {
this.onInputChangedObservable.notifyObservers(deviceEvent);
};
}
/**
* Configure events to talk with DeviceInputSystem
*/
public configureEvents() {
this._deviceInputSystem.configureEvents();
}
/**
* 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._deviceInputSystem.pollInput(deviceType, deviceSlot, inputIndex);
}
/**
* Check if there's an instance of device on given DeviceInputSystem
* @param deviceType Enum specifiying device type
* @returns
*/
public isDeviceAvailable(deviceType: DeviceType): boolean {
return this._deviceInputSystem.isDeviceAvailable(deviceType);
}
/**
* Dispose of DeviceInputSystem sub-elements
*/
public dispose() {
this.onDeviceConnectedObservable.clear();
this.onDeviceDisconnectedObservable.clear();
this.onInputChangedObservable.clear();
this._deviceInputSystem.dispose();
}
}