Example #1
0
        public unsafe override bool GatherInput(XRControllerState[] state_controllers, out int controllerCount, out bool leftSet, out int leftSetIndex, out bool rightSet, out int rightSetIndex, out SideToSet sideToSet)
        {
            // defaults
            GatherInputDefaults(out controllerCount, out leftSet, out leftSetIndex, out rightSet, out rightSetIndex, out sideToSet);

            // reset controllers
            ResetControllers(state_controllers);

            // validate OpenVR is avaliable
            if (!isInit)
            {
                return(false);
            }
            if (system == null || !system.IsInputAvailable() || input.IsUsingLegacyInput())
            {
                return(false);
            }

            // get controller connection status and side
            for (uint i = 0; i != OpenVR.k_unMaxTrackedDeviceCount; ++i)
            {
                if (!system.IsTrackedDeviceConnected(i))
                {
                    continue;
                }
                if (system.GetTrackedDeviceClass(i) != ETrackedDeviceClass.Controller)
                {
                    continue;
                }


                // get controller type
                ETrackedPropertyError e = ETrackedPropertyError.TrackedProp_Success;
                system.GetStringTrackedDeviceProperty(i, ETrackedDeviceProperty.Prop_ControllerType_String, OpenVR_Shared.propertyText, (uint)OpenVR_Shared.propertyText.Capacity, ref e);
                if (e != ETrackedPropertyError.TrackedProp_Success)
                {
                    continue;
                }

                // ignore gamepads
                if (OpenVR_Shared.propertyText.Equals(OpenVR_Shared.propertyText_Gamepad))
                {
                    continue;
                }

                // get controller
                var controller = state_controllers[controllerCount];
                controller.connected = true;

                // get controller type
                if (OpenVR_Shared.propertyText.Equals(OpenVR_Shared.propertyText_ViveController))
                {
                    controller.type = XRInputControllerType.HTCVive;
                }
                else if (OpenVR_Shared.propertyText.Equals(OpenVR_Shared.propertyText_ViveCosmosController))
                {
                    controller.type = XRInputControllerType.HTCViveCosmos;
                }
                else if (OpenVR_Shared.propertyText.Equals(OpenVR_Shared.propertyText_IndexController))
                {
                    controller.type = XRInputControllerType.ValveIndex;
                }
                else if (OpenVR_Shared.propertyText.ToString().StartsWith(OpenVR_Shared.propertyText_Oculus.ToString()))
                {
                    controller.type = XRInputControllerType.Oculus;
                }
                else if (OpenVR_Shared.propertyText.Equals(OpenVR_Shared.propertyText_WMR))
                {
                    controller.type = XRInputControllerType.WMR;
                }
                else if (OpenVR_Shared.propertyText.Equals(OpenVR_Shared.propertyText_WMR_G2))
                {
                    controller.type = XRInputControllerType.WMR_G2;
                }

                // update controller side
                var role = system.GetControllerRoleForTrackedDeviceIndex(i);
                switch (role)
                {
                case ETrackedControllerRole.LeftHand:
                    controller.side = XRControllerSide.Left;
                    leftSet         = true;
                    leftSetIndex    = controllerCount;
                    leftHand        = (int)i;
                    break;

                case ETrackedControllerRole.RightHand:
                    controller.side = XRControllerSide.Right;
                    rightSet        = true;
                    rightSetIndex   = controllerCount;
                    rightHand       = (int)i;
                    break;

                default: controller.side = XRControllerSide.Unknown; break;
                }

                state_controllers[controllerCount] = controller;
                ++controllerCount;
            }

            // pre-finish/pre-resolve unknown controller sides
            GatherInputFinish(state_controllers, controllerCount, ref leftSet, ref leftSetIndex, ref rightSet, ref rightSetIndex, ref sideToSet);

            // update inputs
            var error = input.UpdateActionState(actionSets, (uint)Marshal.SizeOf <VRActiveActionSet_t>());

            if (error != EVRInputError.None)
            {
                Debug.LogError("UpdateActionState: " + error.ToString());
            }

            // get hands
            var controllerRight = new XRControllerState();
            var controllerLeft  = new XRControllerState();

            if (rightSet)
            {
                controllerRight = state_controllers[rightSetIndex];
            }
            if (leftSet)
            {
                controllerLeft = state_controllers[leftSetIndex];
            }

            // update bumper buttons/touch
            controllerRight.buttonBumper.Update(GetButtonState(viveAction_BumperButton, viveSource_RightHand));
            controllerLeft.buttonBumper.Update(GetButtonState(viveAction_BumperButton, viveSource_LeftHand));
            controllerRight.touchBumper.Update(GetButtonState(viveAction_BumperTouch, viveSource_RightHand));
            controllerLeft.touchBumper.Update(GetButtonState(viveAction_BumperTouch, viveSource_LeftHand));

            // update trigger buttons/touch
            controllerRight.buttonTrigger.Update(GetButtonState(viveAction_TriggerButton, viveSource_RightHand));
            controllerLeft.buttonTrigger.Update(GetButtonState(viveAction_TriggerButton, viveSource_LeftHand));
            controllerRight.touchTrigger.Update(GetButtonState(viveAction_TriggerTouch, viveSource_RightHand));
            controllerLeft.touchTrigger.Update(GetButtonState(viveAction_TriggerTouch, viveSource_LeftHand));

            // update grip buttons/touch
            controllerRight.buttonGrip.Update(GetButtonState(viveAction_GripButton, viveSource_RightHand));
            controllerLeft.buttonGrip.Update(GetButtonState(viveAction_GripButton, viveSource_LeftHand));
            controllerRight.touchGrip.Update(GetButtonState(viveAction_GripTouch, viveSource_RightHand));
            controllerLeft.touchGrip.Update(GetButtonState(viveAction_GripTouch, viveSource_LeftHand));

            // update menu buttons/touch
            controllerRight.buttonMenu.Update(GetButtonState(viveAction_MenuButton, viveSource_RightHand));
            controllerLeft.buttonMenu.Update(GetButtonState(viveAction_MenuButton, viveSource_LeftHand));
            controllerRight.touchMenu.Update(GetButtonState(viveAction_MenuTouch, viveSource_RightHand));
            controllerLeft.touchMenu.Update(GetButtonState(viveAction_MenuTouch, viveSource_LeftHand));

            // update button/touch 1
            controllerRight.button1.Update(GetButtonState(viveAction_Button1, viveSource_RightHand));
            controllerLeft.button1.Update(GetButtonState(viveAction_Button1, viveSource_LeftHand));
            controllerRight.touch1.Update(GetButtonState(viveAction_Touch1, viveSource_RightHand));
            controllerLeft.touch1.Update(GetButtonState(viveAction_Touch1, viveSource_LeftHand));

            // update button/touch 2
            controllerRight.button2.Update(GetButtonState(viveAction_Button2, viveSource_RightHand));
            controllerLeft.button2.Update(GetButtonState(viveAction_Button2, viveSource_LeftHand));
            controllerRight.touch2.Update(GetButtonState(viveAction_Touch2, viveSource_RightHand));
            controllerLeft.touch2.Update(GetButtonState(viveAction_Touch2, viveSource_LeftHand));

            // update grip
            if (controllerRight.type == XRInputControllerType.HTCVive || controllerRight.type == XRInputControllerType.WMR)
            {
                controllerRight.grip.Update(controllerRight.buttonGrip.on ? 1 : 0);// simulate analog state
            }
            else
            {
                controllerRight.grip.Update(GetAnalogState(viveAction_Grip, viveSource_RightHand).x);
            }

            if (controllerLeft.type == XRInputControllerType.HTCVive || controllerLeft.type == XRInputControllerType.WMR)
            {
                controllerLeft.grip.Update(controllerLeft.buttonGrip.on ? 1 : 0);// simulate analog state
            }
            else
            {
                controllerLeft.grip.Update(GetAnalogState(viveAction_Grip, viveSource_LeftHand).x);
            }

            // update triggers
            controllerRight.trigger.Update(GetAnalogState(viveAction_Trigger, viveSource_RightHand).x);
            controllerLeft.trigger.Update(GetAnalogState(viveAction_Trigger, viveSource_LeftHand).x);

            // update trackpads / touch / button
            if (controllerRight.type == XRInputControllerType.HTCVive)
            {
                controllerRight.joystick.Update(GetAnalogState(viveAction_Touchpad1, viveSource_RightHand));
                controllerRight.buttonJoystick.Update(GetButtonState(viveAction_Touchpad1_Button, viveSource_RightHand));
            }
            else
            {
                controllerRight.joystick2.Update(GetAnalogState(viveAction_Touchpad1, viveSource_RightHand));
                controllerRight.buttonJoystick2.Update(GetButtonState(viveAction_Touchpad1_Button, viveSource_RightHand));
            }

            if (controllerLeft.type == XRInputControllerType.HTCVive)
            {
                controllerLeft.joystick.Update(GetAnalogState(viveAction_Touchpad1, viveSource_LeftHand));
                controllerLeft.buttonJoystick.Update(GetButtonState(viveAction_Touchpad1_Button, viveSource_LeftHand));
            }
            else
            {
                controllerLeft.joystick2.Update(GetAnalogState(viveAction_Touchpad1, viveSource_LeftHand));
                controllerLeft.buttonJoystick2.Update(GetButtonState(viveAction_Touchpad1_Button, viveSource_LeftHand));
            }


            // update joysticks / touch / button
            if (controllerRight.type != XRInputControllerType.HTCVive)
            {
                controllerRight.joystick.Update(GetAnalogState(viveAction_Joystick1, viveSource_RightHand));
                controllerRight.touchJoystick.Update(GetButtonState(viveAction_Joystick1_Touch, viveSource_RightHand));
                controllerRight.buttonJoystick.Update(GetButtonState(viveAction_Joystick1_Button, viveSource_RightHand));
            }

            if (controllerLeft.type != XRInputControllerType.HTCVive)
            {
                controllerLeft.joystick.Update(GetAnalogState(viveAction_Joystick1, viveSource_LeftHand));
                controllerLeft.touchJoystick.Update(GetButtonState(viveAction_Joystick1_Touch, viveSource_LeftHand));
                controllerLeft.buttonJoystick.Update(GetButtonState(viveAction_Joystick1_Button, viveSource_LeftHand));
            }

            // copy back hand updates
            if (rightSet)
            {
                state_controllers[rightSetIndex] = controllerRight;
            }
            if (leftSet)
            {
                state_controllers[leftSetIndex] = controllerLeft;
            }

            return(true);
        }