Beispiel #1
0
        private void OnDeviceImpDisconnected(object sender, DeviceImpDisconnectedArgs args)
        {
            if (sender == null)
            {
                throw new ArgumentNullException(nameof(sender));
            }
            IInputDriverImp driver = sender as IInputDriverImp;

            if (driver == null)
            {
                throw new InvalidOperationException("Device disconnecting from unknown driver " + sender.ToString());
            }

            string      deviceKey = driver.DriverId + "_" + args.Id;
            InputDevice existingDevice;

            if (_inputDevices.TryGetValue(deviceKey, out existingDevice))
            {
                existingDevice.Disconnect();
            }
            else
            {
                throw new InvalidOperationException("Driver " + driver.DriverId + " trying to disconnect unknown device " + args.Id);
            }

            // Bubble up event to user code
            InputDeviceDisconnected?.Invoke(this, new DeviceConnectionArgs {
                InputDevice = existingDevice
            });
        }
Beispiel #2
0
        /// <summary>
        /// Registers the type of input device available.
        /// </summary>
        /// <param name="match"></param>
        /// <param name="creator"></param>
        public void RegisterInputDeviceType(MatchFunc match, CreatorFunc creator)
        {
            if (match == null)
            {
                throw new ArgumentNullException(nameof(match));
            }
            if (creator == null)
            {
                throw new ArgumentNullException(nameof(creator));
            }

            _specialDeviceCreators.Add(new SpecialDeviceCreator {
                Match = match, Creator = creator
            });

            // Reconnect any existing devices matching the predicate
            // List<string> matchingDevices = (from device in _inputDevices.Values where device.DeviceImp != null && match(device.DeviceImp) select device.Id).ToList();
            List <string> matchingDevices = new List <string>();

            foreach (var device in _inputDevices.Values)
            {
                if (device.DeviceImp != null && match(device.DeviceImp))
                {
                    matchingDevices.Add(device.Id);
                }
            }

            foreach (var devId in matchingDevices)
            {
                InputDevice dev = _inputDevices[devId];

                // Set device to disconnected state
                dev.Disconnect();

                // Inform interested users about disconnection
                InputDeviceDisconnected?.Invoke(this, new DeviceConnectionArgs {
                    InputDevice = dev
                });

                IInputDeviceImp inputDeviceImp = dev.DeviceImp;

                // Remove device from the list
                _inputDevices.Remove(devId);

                dev = creator(inputDeviceImp);
                _inputDevices[devId] = dev;

                // no need to call reconnect since device is constructed from scratch

                // Inform interested users about the newly connected device.
                InputDeviceConnected?.Invoke(this, new DeviceConnectionArgs {
                    InputDevice = dev
                });
            }
        }
Beispiel #3
0
 /// <summary>
 /// Should be called from the application framework before the application stops. Typically not to be called by user code unless
 /// users implement their own application framework.
 /// </summary>
 public void Dispose()
 {
     foreach (var device in _inputDevices.Values)
     {
         InputDeviceDisconnected?.Invoke(this, new DeviceConnectionArgs {
             InputDevice = device
         });
         device.Disconnect();
     }
     _inputDevices.Clear();
     foreach (var driver in _inputDrivers.Values)
     {
         driver.Dispose();
     }
     _inputDrivers.Clear();
 }
        private void UpdateConnectedControllers()
        {
            var allControllers   = new List <IVRInputDevice>();
            var disconnectedList = new List <IVRInputDevice>();
            var connectedList    = new List <IVRInputDevice>();

            var ctrlMask = OVRInput.GetConnectedControllers();

            // NOTE: Controller tests here are in order of priority. Active hand controllers take priority over headset

            #region Controller
            var leftHandConnected  = OVRUtils.IsLimbConnected(VRAvatarLimbType.LeftHand);
            var rightHandConnected = OVRUtils.IsLimbConnected(VRAvatarLimbType.RightHand);

            Debug.Log($"Left Hand Connected: {leftHandConnected}");
            Debug.Log($"Right Hand Connected: {rightHandConnected}");

            // The order the controllers are added currently determines the PrimaryInput however,
            // It does not seem to determine the primary pointer.
            if (rightHandConnected)
            {
                mPrimaryController = mPrimaryController ?? new GearVRController(VRInputDeviceHand.Right);

                if (!mInputDevices.Contains(mPrimaryController))
                {
                    connectedList.Add(mPrimaryController);
                }

                allControllers.Add(mPrimaryController);
            }
            else
            {
                disconnectedList.Add(mPrimaryController);
            }

            if (leftHandConnected)
            {
                mSecondaryController = mSecondaryController ?? new GearVRController(VRInputDeviceHand.Left);

                if (!mInputDevices.Contains(mSecondaryController))
                {
                    connectedList.Add(mSecondaryController);
                }

                allControllers.Add(mSecondaryController);
            }
            else
            {
                disconnectedList.Add(mSecondaryController);
            }
            #endregion

            #region Headset (Swipe-pad)

            if (Headset is GearVRHeadset)
            {
                var gearVRHeadset = Headset as GearVRHeadset;

                if ((ctrlMask & OVRInput.Controller.Touchpad) != 0)
                {
                    if (!mHeadsetInputConnected)
                    {
                        connectedList.Add(gearVRHeadset);
                        mHeadsetInputConnected = true;
                    }

                    allControllers.Add(gearVRHeadset);
                }
                else if (Headset != null)
                {
                    disconnectedList.Add(gearVRHeadset);
                    mHeadsetInputConnected = false;
                }
            }
            #endregion

            // Update internal state
            mInputDevices            = allControllers.ToArray();
            mConnectedControllerMask = ctrlMask;

            foreach (var device in disconnectedList)
            {
                InputDeviceDisconnected?.Invoke(this, device);
            }

            foreach (var device in connectedList)
            {
                InputDeviceConnected?.Invoke(this, device);
            }

            // Force an update of input devices
            UpdateInputDevices();
        }