nativeDeviceInputSystem.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { DeviceEventFactory } from "./eventFactory.js";
  2. import { DeviceType } from "./InputDevices/deviceEnums.js";
  3. /** @internal */
  4. export class NativeDeviceInputSystem {
  5. constructor(onDeviceConnected, onDeviceDisconnected, onInputChanged) {
  6. this._nativeInput = _native.DeviceInputSystem
  7. ? new _native.DeviceInputSystem(onDeviceConnected, onDeviceDisconnected, (deviceType, deviceSlot, inputIndex, currentState) => {
  8. const evt = DeviceEventFactory.CreateDeviceEvent(deviceType, deviceSlot, inputIndex, currentState, this);
  9. onInputChanged(deviceType, deviceSlot, evt);
  10. })
  11. : this._createDummyNativeInput();
  12. }
  13. // Public functions
  14. /**
  15. * Checks for current device input value, given an id and input index. Throws exception if requested device not initialized.
  16. * @param deviceType Enum specifying device type
  17. * @param deviceSlot "Slot" or index that device is referenced in
  18. * @param inputIndex Id of input to be checked
  19. * @returns Current value of input
  20. */
  21. pollInput(deviceType, deviceSlot, inputIndex) {
  22. return this._nativeInput.pollInput(deviceType, deviceSlot, inputIndex);
  23. }
  24. /**
  25. * Check for a specific device in the DeviceInputSystem
  26. * @param deviceType Type of device to check for
  27. * @returns bool with status of device's existence
  28. */
  29. isDeviceAvailable(deviceType) {
  30. //TODO: FIx native side first
  31. return deviceType === DeviceType.Mouse || deviceType === DeviceType.Touch;
  32. }
  33. /**
  34. * Dispose of all the observables
  35. */
  36. dispose() {
  37. this._nativeInput.dispose();
  38. }
  39. /**
  40. * For versions of BabylonNative that don't have the NativeInput plugin initialized, create a dummy version
  41. * @returns Object with dummy functions
  42. */
  43. _createDummyNativeInput() {
  44. const nativeInput = {
  45. pollInput: () => {
  46. return 0;
  47. },
  48. isDeviceAvailable: () => {
  49. return false;
  50. },
  51. dispose: () => { },
  52. };
  53. return nativeInput;
  54. }
  55. }
  56. //# sourceMappingURL=nativeDeviceInputSystem.js.map