Ejemplo n.º 1
0
        //アクションセットを更新する
        public void UpdateActionSetState(ActiveActionSets ActiveSets)
        {
            ReadyCheck(); //実行可能な状態かチェック

            EVRInputError inputError = EVRInputError.None;

            //サイズ取得
            var size = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(VRActiveActionSet_t));

            //更新処理実行
            inputError = vrinput.UpdateActionState(ActiveSets.Get(), size);

            //ここでエラーになることはそうそうない
            if (inputError != EVRInputError.None)
            {
                //致命的エラー
                throw new IOException(inputError.ToString());
            }
            return;
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public void Run()
        {
            var ev     = new VREvent_t();
            var evSize = (uint)Marshal.SizeOf(typeof(VREvent_t));

            var actionSets = new VRActiveActionSet_t[]
            {
                new VRActiveActionSet_t
                {
                    ulActionSet = inputSet,
                },
            };
            var actionSetSize = (uint)Marshal.SizeOf(typeof(VRActiveActionSet_t));

            var actionData     = new InputDigitalActionData_t();
            var actionDataSize = (uint)Marshal.SizeOf(typeof(InputDigitalActionData_t));

            Console.WriteLine("Brightness panic button is running. Input bindings may be changed through SteamVR's input bindings.");

            var sleepTime = (int)Math.Round(1000 * DT);

            while (running)
            {
                while (vrSystem.PollNextEvent(ref ev, evSize))
                {
                    switch ((EVREventType)ev.eventType)
                    {
                    case EVREventType.VREvent_DriverRequestedQuit:
                    case EVREventType.VREvent_Quit:
                        vrSystem.AcknowledgeQuit_Exiting();
                        running = false;
                        break;
                    }
                }

                var inputError = vrInput.UpdateActionState(actionSets, actionSetSize);
                if (inputError != EVRInputError.None)
                {
                    var message = inputError.ToString();
                    throw new Exception($"Failed to update action state: {message}");
                }

                inputError = vrInput.GetDigitalActionData(inputActivate, ref actionData, actionDataSize, 0);
                if (inputError != EVRInputError.None)
                {
                    var message = inputError.ToString();
                    throw new Exception($"Failed to get Activate action state: {message}");
                }
                var activatePressed = actionData.bChanged && actionData.bState;

                inputError = vrInput.GetDigitalActionData(inputResetAuto, ref actionData, actionDataSize, 0);
                if (inputError != EVRInputError.None)
                {
                    var message = inputError.ToString();
                    throw new Exception($"Failed to get Reset (Auto) action state: {message}");
                }
                var resetAutoPressed = actionData.bChanged && actionData.bState;

                inputError = vrInput.GetDigitalActionData(inputResetHold, ref actionData, actionDataSize, 0);
                if (inputError != EVRInputError.None)
                {
                    var message = inputError.ToString();
                    throw new Exception($"Failed to get Reset (Hold) action state: {message}");
                }
                var resetHoldChanged = actionData.bChanged;
                var resetHoldHeld    = actionData.bState;

                if (activatePressed)
                {
                    TriggerActivate();
                }

                if (initialBrightness.HasValue && resetAutoPressed)
                {
                    resetting = !resetting;
                    if (resetting)
                    {
                        Console.WriteLine("Starting automatic reset.");
                    }
                    else
                    {
                        Console.WriteLine("Cancelling automatic reset.");
                    }
                }

                if (initialBrightness.HasValue && resetHoldChanged)
                {
                    resetting = resetHoldHeld;
                    if (resetting)
                    {
                        Console.WriteLine("Starting held reset.");
                    }
                    else
                    {
                        Console.WriteLine("Cancelling held reset.");
                    }
                }

                if (resetting)
                {
                    resetting = TriggerReset(DT * RESET_SPEED);
                    if (resetting && (resetAutoPressed || resetHoldChanged))
                    {
                        try
                        {
                            resetSound.Play();
                        }
                        catch (FileNotFoundException) {}
                    }
                }

                Thread.Sleep(sleepTime);
            }
        }