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); } }
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)); if (currentPointingSource == null && leftY > 0.8 && Math.Abs(leftX) < 0.3) { if (FocusManager.Instance.TryGetSinglePointer(out currentPointingSource)) { StartTeleport(); } } else if (currentPointingSource != null && new Vector2(leftX, leftY).magnitude < 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; }
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)); }