public virtual void OnXboxInputUpdate(XboxControllerEventData eventData)
        {
            if (string.IsNullOrEmpty(GamePadName))
            {
                GamePadName = eventData.GamePadName;
            }

            if (XboxControllerMapping.GetButton_Down(SelectButton, eventData))
            {
                CurrentGestureState = GestureState.SelectButtonPressed;

                InputManager.Instance.RaiseSourceDown(eventData.InputSource, eventData.SourceId, InteractionSourcePressInfo.Select);

                HoldStartedRoutine = StartCoroutine(HandleHoldStarted(eventData));
            }

            if (XboxControllerMapping.GetButton_Pressed(SelectButton, eventData))
            {
                HandleNavigation(eventData);
            }

            if (XboxControllerMapping.GetButton_Up(SelectButton, eventData))
            {
                HandleSelectButtonReleased(eventData);
            }

            // Consume this event
            eventData.Use();
        }
        protected virtual void HandleNavigation(XboxControllerEventData eventData)
        {
            float displacementAlongX = XboxControllerMapping.GetAxis(HorizontalNavigationAxis, eventData);
            float displacementAlongY = XboxControllerMapping.GetAxis(VerticalNavigationAxis, eventData);

            if (displacementAlongX == 0.0f && displacementAlongY == 0.0f && CurrentGestureState != GestureState.NavigationStarted)
            {
                return;
            }

            NormalizedOffset.x = displacementAlongX;
            NormalizedOffset.y = displacementAlongY;
            NormalizedOffset.z = 0f;

            if (CurrentGestureState != GestureState.NavigationStarted)
            {
                if (CurrentGestureState == GestureState.HoldStarted)
                {
                    InputManager.Instance.RaiseHoldCanceled(eventData.InputSource, eventData.SourceId);
                }

                CurrentGestureState = GestureState.NavigationStarted;

                // Raise navigation started event.
                InputManager.Instance.RaiseNavigationStarted(eventData.InputSource, eventData.SourceId);
            }
            else
            {
                // Raise navigation updated event.
                InputManager.Instance.RaiseNavigationUpdated(eventData.InputSource, eventData.SourceId, NormalizedOffset);
            }
        }
Exemple #3
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 Awake()
        {
            base.Awake();

            if (mapping != null)
            {
                for (var i = 0; i < Enum.GetNames(typeof(XboxControllerMappingTypes)).Length; i++)
                {
                    XboxControllerMapping.SetMapping((XboxControllerMappingTypes)i, mapping[i].Value);
                }
            }

            PreviousForceActiveState = InputModule.forceModuleActive;
            PreviousHorizontalAxis   = InputModule.horizontalAxis;
            PreviousVerticalAxis     = InputModule.verticalAxis;
            PreviousSubmitButton     = InputModule.submitButton;
            PreviousCancelButton     = InputModule.cancelButton;
        }
Exemple #5
0
        protected virtual void Awake()
        {
            inputModule = FindObjectOfType <StandaloneInputModule>();

            if (inputModule == null)
            {
                Debug.LogError("Missing Standalone Input Module for Xbox Controller Source!\n" +
                               "Ensure you have an Event System in your scene.");
                return;
            }

            for (var i = 0; i < Enum.GetNames(typeof(XboxControllerMappingTypes)).Length; i++)
            {
                XboxControllerMapping.SetMapping((XboxControllerMappingTypes)i, mapping[i].Value);
            }

            previousForceActiveState = inputModule.forceModuleActive;
            previousHorizontalAxis   = inputModule.horizontalAxis;
            previousVerticalAxis     = inputModule.verticalAxis;
            previousSubmitButton     = inputModule.submitButton;
            previousCancelButton     = inputModule.cancelButton;
        }
        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;
        }
 protected static bool OnButton_Down(XboxControllerMappingTypes buttonType, XboxControllerEventData eventData)
 {
     return(XboxControllerMapping.GetButton_Down(buttonType, eventData));
 }