From e2c600cae39eca98d412a5e00fa4e7e3b6ce5953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Freire?= Date: Wed, 12 Apr 2023 16:33:38 +0200 Subject: [PATCH 1/2] Update renaming of Event.CompareType --- .../InputSystem/Plugins/InputForUI/InputSystemProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Packages/com.unity.inputsystem/InputSystem/Plugins/InputForUI/InputSystemProvider.cs b/Packages/com.unity.inputsystem/InputSystem/Plugins/InputForUI/InputSystemProvider.cs index 492c5ec453..864d1119e6 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Plugins/InputForUI/InputSystemProvider.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Plugins/InputForUI/InputSystemProvider.cs @@ -181,7 +181,7 @@ private void DirectionNavigation(DiscreteTime currentTime) private static int SortEvents(Event a, Event b) { - return Event.Compare(a, b); + return Event.CompareType(a, b); } public void OnFocusChanged(bool focus) From 4db40089bb46fc5edff9cabfe95a4605da3100bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Freire?= Date: Wed, 12 Apr 2023 16:37:28 +0200 Subject: [PATCH 2/2] Add Next/Previous navigation events repetition Currently it doesn't deal with keys other than tab and shift+tab. Discussion needs to happen to allow configuration for this with the Input System. --- .../Plugins/InputForUI/InputSystemProvider.cs | 71 ++++++++++++------- 1 file changed, 47 insertions(+), 24 deletions(-) diff --git a/Packages/com.unity.inputsystem/InputSystem/Plugins/InputForUI/InputSystemProvider.cs b/Packages/com.unity.inputsystem/InputSystem/Plugins/InputForUI/InputSystemProvider.cs index 864d1119e6..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,6 +187,25 @@ 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.CompareType(a, b); @@ -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; }