예제 #1
0
        private void HandleGamepad()
        {
            if (EnableTeleport && !fadeControl.Busy)
            {
                //float leftX = Input.GetAxis(useCustomMapping ? LeftThumbstickX : XboxControllerMapping.GetMapping(HorizontalStrafe));
                //float leftY = Input.GetAxis(useCustomMapping ? LeftThumbstickY : XboxControllerMapping.GetMapping(ForwardMovement));

                float leftTrigger = Input.GetAxis(InputMappingAxisUtility.CONTROLLER_LEFT_TRIGGER);
                //float rightTrigger = Input.GetAxis(InputMappingAxisUtility.CONTROLLER_RIGHT_TRIGGER);

                //if (currentPointingSource == null && leftY > 0.8 && Math.Abs(leftX) < 0.3)
                if (currentPointingSource == null && leftTrigger > 0.8)
                {
                    if (FocusManager.Instance.TryGetSinglePointer(out currentPointingSource))
                    {
                        StartTeleport();
                    }
                }
                //else if (currentPointingSource != null && new Vector2(leftX, leftY).magnitude < 0.2)
                else if (currentPointingSource != null && leftTrigger < 0.2)
                {
                    FinishTeleport();
                }
            }

            if (EnableStrafe && currentPointingSource == null && !fadeControl.Busy)
            {
                float leftX = Input.GetAxis(useCustomMapping ? LeftThumbstickX : XboxControllerMapping.GetMapping(HorizontalStrafe));
                float leftY = Input.GetAxis(useCustomMapping ? LeftThumbstickY : XboxControllerMapping.GetMapping(ForwardMovement));

                if (leftX < -0.8 && Math.Abs(leftY) < 0.3)
                {
                    DoStrafe(Vector3.left * StrafeAmount);
                }
                else if (leftX > 0.8 && Math.Abs(leftY) < 0.3)
                {
                    DoStrafe(Vector3.right * StrafeAmount);
                }
                else if (leftY < -0.8 && Math.Abs(leftX) < 0.3)
                {
                    DoStrafe(Vector3.back * StrafeAmount);
                }
            }

            if (EnableRotation && currentPointingSource == null && !fadeControl.Busy)
            {
                float rightX = Input.GetAxis(useCustomMapping ? RightThumbstickX : XboxControllerMapping.GetMapping(HorizontalRotation));
                float rightY = Input.GetAxis(useCustomMapping ? RightThumbstickY : XboxControllerMapping.GetMapping(VerticalRotation));

                if (rightX < -0.8 && Math.Abs(rightY) < 0.3)
                {
                    DoRotation(-RotationSize);
                }
                else if (rightX > 0.8 && Math.Abs(rightY) < 0.3)
                {
                    DoRotation(RotationSize);
                }
            }
        }
        protected override void RefreshDevices()
        {
            var joystickNames = Input.GetJoystickNames();

            if (joystickNames.Length <= 0)
            {
                return;
            }

            bool devicesChanged = LastDeviceList == null;

            if (LastDeviceList != null && joystickNames.Length == LastDeviceList.Length)
            {
                for (int i = 0; i < LastDeviceList.Length; i++)
                {
                    if (!joystickNames[i].Equals(LastDeviceList[i]))
                    {
                        devicesChanged = true;
                        if (LastDeviceList == null)
                        {
                            LastDeviceList = joystickNames;
                        }
                    }
                }
            }

            if (LastDeviceList != null && devicesChanged)
            {
                foreach (var gamePadInputSource in gamePadInputDatas)
                {
                    // Reset our input module to it's previous state.
                    inputModule.forceModuleActive = previousForceActiveState;
                    inputModule.verticalAxis      = previousVerticalAxis;
                    inputModule.horizontalAxis    = previousHorizontalAxis;
                    inputModule.submitButton      = previousSubmitButton;
                    inputModule.cancelButton      = previousCancelButton;

                    InputManager.Instance.RaiseGamePadLost(this, gamePadInputSource.Key, LastDeviceList[gamePadInputSource.Key]);
                }

                gamePadInputDatas.Clear();
            }

            for (var i = 0; i < joystickNames.Length; i++)
            {
                if (string.IsNullOrEmpty(joystickNames[i]) || gamePadInputDatas.ContainsKey((uint)i))
                {
                    continue;
                }

                if (joystickNames[i].Contains(XboxController) ||
                    joystickNames[i].Contains(XboxOneForWindows) ||
                    joystickNames[i].Contains(XboxBluetoothGamePad) ||
                    joystickNames[i].Contains(XboxWirelessController))
                {
                    // We will only register the first device we find.  Input is taken from all joysticks.
                    if (gamePadInputDatas.Count != 0)
                    {
                        return;
                    }

                    sourceId       = (uint)i;
                    controllerData = new XboxControllerData();
                    gamePadInputDatas.Add(sourceId, controllerData);

                    // Setup the Input Module to use our custom axis settings.
                    inputModule.forceModuleActive = true;
                    inputModule.verticalAxis      = XboxControllerMapping.GetMapping(verticalAxis);
                    inputModule.horizontalAxis    = XboxControllerMapping.GetMapping(horizontalAxis);
                    inputModule.submitButton      = XboxControllerMapping.GetMapping(submitButton);
                    inputModule.cancelButton      = XboxControllerMapping.GetMapping(cancelButton);

                    InputManager.Instance.RaiseGamePadDetected(this, sourceId, joystickNames[i]);
                }
                else
                {
                    Debug.LogWarning("Unimplemented Controller type Detected: " + joystickNames[i]);
                }
            }

            LastDeviceList        = joystickNames;
            LastDeviceUpdateCount = joystickNames.Length;
        }
        protected override void RefreshDevices()
        {
            var joystickNames = Input.GetJoystickNames();

            if (joystickNames.Length <= 0)
            {
                return;
            }

            bool devicesChanged = LastDeviceList == null;

            if (LastDeviceList != null && joystickNames.Length == LastDeviceList.Length)
            {
                for (int i = 0; i < LastDeviceList.Length; i++)
                {
                    if (!joystickNames[i].Equals(LastDeviceList[i]))
                    {
                        devicesChanged = true;
                        if (LastDeviceList == null)
                        {
                            LastDeviceList = joystickNames;
                        }
                    }
                }
            }

            if (LastDeviceList != null && devicesChanged)
            {
                foreach (var gamePadInputSource in gamePadInputDatas)
                {
                    InputManager.Instance.RaiseSourceLost(this, gamePadInputSource.Key);
                }

                gamePadInputDatas.Clear();

                if (gamePadInputDatas.Count == 0)
                {
                    // Reset our input module to it's previous state.
                    InputModule.forceModuleActive = PreviousForceActiveState;
                    InputModule.verticalAxis      = PreviousVerticalAxis;
                    InputModule.horizontalAxis    = PreviousHorizontalAxis;
                    InputModule.submitButton      = PreviousSubmitButton;
                    InputModule.cancelButton      = PreviousCancelButton;
                }
            }

            motionControllerCount = 0;

            for (var i = 0; i < joystickNames.Length; i++)
            {
                if (joystickNames[i].Contains(MotionControllerLeft) ||
                    joystickNames[i].Contains(MotionControllerRight))
                {
                    // If we don't have any matching joystick types, continue.
                    // If we have motion controllers connected we override the xbox input.
                    motionControllerCount++;
                    continue;
                }

                if (joystickNames[i].Contains(XboxController) ||
                    joystickNames[i].Contains(XboxOneForWindows) ||
                    joystickNames[i].Contains(XboxBluetoothGamePad) ||
                    joystickNames[i].Contains(XboxWirelessController))
                {
                    // We will only register the first device we find.  Input is taken from all joysticks.
                    if (gamePadInputDatas.Count != 0)
                    {
                        return;
                    }

                    SourceId       = (uint)i;
                    controllerData = new XboxControllerData {
                        GamePadName = joystickNames[i]
                    };
                    gamePadInputDatas.Add(SourceId, controllerData);

                    InputManager.Instance.RaiseSourceDetected(this, SourceId);

                    // Setup the Input Module to use our custom axis settings.
                    InputModule.forceModuleActive = true;

                    if (verticalAxis != XboxControllerMappingTypes.None)
                    {
                        InputModule.verticalAxis = XboxControllerMapping.GetMapping(verticalAxis);
                    }

                    if (horizontalAxis != XboxControllerMappingTypes.None)
                    {
                        InputModule.horizontalAxis = XboxControllerMapping.GetMapping(horizontalAxis);
                    }

                    if (submitButton != XboxControllerMappingTypes.None)
                    {
                        InputModule.submitButton = XboxControllerMapping.GetMapping(submitButton);
                    }

                    if (cancelButton != XboxControllerMappingTypes.None)
                    {
                        InputModule.cancelButton = XboxControllerMapping.GetMapping(cancelButton);
                    }
                }
            }

            LastDeviceList        = joystickNames;
            LastDeviceUpdateCount = joystickNames.Length;
        }