コード例 #1
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
                });
            }
        }
コード例 #2
0
        private void OnNewDeviceImpConnected(object sender, NewDeviceImpConnectedArgs args)
        {
            if (sender == null)
            {
                throw new ArgumentNullException(nameof(sender));
            }

            IInputDriverImp driver = sender as IInputDriverImp;

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

            if (args == null || args.InputDeviceImp == null)
            {
                throw new ArgumentNullException(nameof(args), "Device or InputDeviceImp must not be null");
            }

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

            if (_inputDevices.TryGetValue(deviceKey, out existingDevice))
            {
                existingDevice.Reconnect(args.InputDeviceImp);
            }
            else
            {
                existingDevice           = CreateInputDevice(args.InputDeviceImp);
                _inputDevices[deviceKey] = existingDevice;
            }

            // Bubble up event to user code
            InputDeviceConnected?.Invoke(this, new DeviceConnectionArgs {
                InputDevice = existingDevice
            });
        }
コード例 #3
0
        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();
        }