/// <summary> /// Tries to create a sensor listener if it is supported /// </summary> /// <param name="type"></param> /// <returns></returns> public AndroidSensorListener TryGetSensorListener(SensorType type) { var listener = new AndroidSensorListener(type); if (!listener.Enable()) { listener.Dispose(); return(null); } listener.Disable(); return(listener); }
public override void Initialize(InputManager inputManager) { var context = inputManager.Game.Context as GameContextAndroid; uiControl = context.Control; // Create android pointer and keyboard keyboard = new KeyboardAndroid(this, uiControl); pointer = new PointerAndroid(this, uiControl); RegisterDevice(keyboard); RegisterDevice(pointer); // Create android sensors if ((accelerometerListener = TryGetSensorListener(SensorType.Accelerometer)) != null) { accelerometerSensor = new AccelerometerSensor(this, "Android"); RegisterDevice(accelerometerSensor); } if ((linearAccelerationListener = TryGetSensorListener(SensorType.LinearAcceleration)) != null) { userAccelerationSensor = new UserAccelerationSensor(this, "Android"); RegisterDevice(userAccelerationSensor); } if ((gyroscopeListener = TryGetSensorListener(SensorType.Gyroscope)) != null) { gyroscopeSensor = new GyroscopeSensor(this, "Android"); RegisterDevice(gyroscopeSensor); } if ((gravityListener = TryGetSensorListener(SensorType.Gravity)) != null) { gravitySensor = new GravitySensor(this, "Android"); RegisterDevice(gravitySensor); } if ((orientationListener = TryGetSensorListener(SensorType.RotationVector)) != null) { orientationSensor = new OrientationSensor(this, "Android"); compassSensor = new CompassSensor(this, "Android"); RegisterDevice(orientationSensor); RegisterDevice(compassSensor); } }
public void UpdateSensorPair <TSensor>(AndroidSensorListener listener, TSensor sensor, Action <TSensor, AndroidSensorListener> updater) where TSensor : Sensor { if (listener != null) { bool enable = sensor.IsEnabled; if (enable != listener.Enabled) { if (enable) { listener.Enable(); } else { listener.Disable(); } } if (enable) { updater.Invoke(sensor, listener); } } }