diff --git a/Packages/com.unity.inputsystem/InputSystem/Plugins/InputForUI/InputSystemProvider.cs b/Packages/com.unity.inputsystem/InputSystem/Plugins/InputForUI/InputSystemProvider.cs index 492c5ec453..202c78b82d 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Plugins/InputForUI/InputSystemProvider.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Plugins/InputForUI/InputSystemProvider.cs @@ -90,22 +90,6 @@ public void Initialize() RegisterActions(_cfg); } - private void OnNextPreviousPerformed(InputAction.CallbackContext ctx) - { - if (ctx.control.device is Keyboard) - { - //TODO repeat rate - var keyboard = ctx.control.device as Keyboard; - DispatchFromCallback(Event.From(new NavigationEvent - { - type = NavigationEvent.Type.Move, - direction = keyboard.shiftKey.isPressed ? NavigationEvent.Direction.Previous : NavigationEvent.Direction.Next, - timestamp = _currentTime, - eventSource = EventSource.Keyboard, - })); - } - } - public void Shutdown() { UnregisterActions(_cfg); @@ -143,13 +127,19 @@ public void Update() _seenMouseEvents = false; } + //TODO: Refactor as there is no need for having almost the same implementation in the IM and ISX? private void DirectionNavigation(DiscreteTime currentTime) { - //TODO: Refactor as there is no need for having almost the same implementation in the IM and ISX? var(move, axesButtonWerePressed) = ReadCurrentNavigationMoveVector(); - var direction = NavigationEvent.DetermineMoveDirection(move); + // Checks for next/previous directions if no movement was detected + if (direction == NavigationEvent.Direction.None) + { + direction = ReadNextPreviousDirection(); + axesButtonWerePressed = _nextPreviousAction.WasPressedThisFrame(); + } + if (direction == NavigationEvent.Direction.None) { repeatHelper.Reset(); @@ -163,7 +153,7 @@ private void DirectionNavigation(DiscreteTime currentTime) type = NavigationEvent.Type.Move, direction = direction, timestamp = currentTime, - eventSource = GetEventSource(_moveAction.action.activeControl.device), + eventSource = GetEventSource(GetActiveDeviceFromDirection(direction)), playerId = kDefaultPlayerId, eventModifiers = _eventModifiers })); @@ -171,6 +161,24 @@ private void DirectionNavigation(DiscreteTime currentTime) } } + private InputDevice GetActiveDeviceFromDirection(NavigationEvent.Direction direction) + { + switch (direction) + { + case NavigationEvent.Direction.Left: + case NavigationEvent.Direction.Up: + case NavigationEvent.Direction.Right: + case NavigationEvent.Direction.Down: + return _moveAction.action.activeControl.device; + case NavigationEvent.Direction.Next: + case NavigationEvent.Direction.Previous: + return _nextPreviousAction.activeControl.device; + case NavigationEvent.Direction.None: + default: + return Keyboard.current; + } + } + private (Vector2, bool) ReadCurrentNavigationMoveVector() { var move = _moveAction.action.ReadValue(); @@ -179,9 +187,28 @@ private void DirectionNavigation(DiscreteTime currentTime) return (move, axisWasPressed); } + private NavigationEvent.Direction ReadNextPreviousDirection() + { + if (_nextPreviousAction.IsPressed()) + { + //TODO: For now it only deals with Keyboard, needs to deal with other devices if we can add bindings + // for Gamepad, etc + //TODO: An alternative could be to have an action for next and for previous since shortcut support does + // not work properly + if (_nextPreviousAction.activeControl.device is Keyboard) + { + var keyboard = _nextPreviousAction.activeControl.device as Keyboard; + // Return direction based on whether shift is pressed or not + return keyboard.shiftKey.isPressed ? NavigationEvent.Direction.Previous : NavigationEvent.Direction.Next; + } + } + + return NavigationEvent.Direction.None; + } + private static int SortEvents(Event a, Event b) { - return Event.Compare(a, b); + return Event.CompareType(a, b); } public void OnFocusChanged(bool focus) @@ -492,12 +519,9 @@ private void OnScrollWheelPerformed(InputAction.CallbackContext ctx) private void RegisterNextPreviousAction() { - _nextPreviousAction = new InputAction(name: "nextPreviousAction"); + _nextPreviousAction = new InputAction(name: "nextPreviousAction", type: InputActionType.Button); // TODO add more default bindings, or make them configurable _nextPreviousAction.AddBinding("/tab"); - if (_nextPreviousAction != null) - _nextPreviousAction.performed += OnNextPreviousPerformed; - _nextPreviousAction.Enable(); } @@ -505,7 +529,6 @@ private void UnregisterNextPreviousAction() { if (_nextPreviousAction != null) { - _nextPreviousAction.performed -= OnNextPreviousPerformed; _nextPreviousAction.Disable(); _nextPreviousAction = null; }