From 680d8c8db7c58a4865a1a46d8dd68f5c6fefdd49 Mon Sep 17 00:00:00 2001 From: "benoit.alain" Date: Thu, 23 May 2024 16:23:58 -0400 Subject: [PATCH 1/2] Fixed ISXB-766, ISXB-808, ISXB-704, conditional to trunk PR #49932. --- .../Editor/Settings/InputSettingsProvider.cs | 12 ++++++ .../InputSystem/IInputRuntime.cs | 2 + .../InputSystem/InputManager.cs | 24 +++++++++++ .../InputSystem/InputSettings.cs | 41 +++++++++++++++++++ .../InputSystem/InputSystem.cs | 2 + .../InputSystem/NativeInputRuntime.cs | 19 +++++++++ .../Plugins/InputForUI/InputSystemProvider.cs | 18 +++++--- .../Plugins/UI/InputSystemUIInputModule.cs | 11 ++--- .../Tests/TestFixture/InputTestRuntime.cs | 2 + 9 files changed, 121 insertions(+), 10 deletions(-) diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/Settings/InputSettingsProvider.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/Settings/InputSettingsProvider.cs index a700ed025e..a7664e02d6 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/Settings/InputSettingsProvider.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/Settings/InputSettingsProvider.cs @@ -122,6 +122,10 @@ public override void OnGUI(string searchContext) if (!runInBackground) EditorGUILayout.HelpBox("Focus change behavior can only be changed if 'Run In Background' is enabled in Player Settings.", MessageType.Info); +#if UNITY_6000_0_OR_NEWER + EditorGUILayout.PropertyField(m_ScrollDeltaBehavior, m_ScrollDeltaBehaviorContent); +#endif + EditorGUILayout.Space(); EditorGUILayout.PropertyField(m_CompensateForScreenOrientation, m_CompensateForScreenOrientationContent); @@ -266,6 +270,7 @@ private void InitializeWithCurrentSettings() // Look up properties. m_SettingsObject = new SerializedObject(m_Settings); m_UpdateMode = m_SettingsObject.FindProperty("m_UpdateMode"); + m_ScrollDeltaBehavior = m_SettingsObject.FindProperty("m_ScrollDeltaBehavior"); m_CompensateForScreenOrientation = m_SettingsObject.FindProperty("m_CompensateForScreenOrientation"); m_BackgroundBehavior = m_SettingsObject.FindProperty("m_BackgroundBehavior"); m_EditorInputBehaviorInPlayMode = m_SettingsObject.FindProperty("m_EditorInputBehaviorInPlayMode"); @@ -281,6 +286,9 @@ private void InitializeWithCurrentSettings() m_ShortcutKeysConsumeInputs = m_SettingsObject.FindProperty("m_ShortcutKeysConsumeInputs"); m_UpdateModeContent = new GUIContent("Update Mode", "When should the Input System be updated?"); +#if UNITY_6000_0_OR_NEWER + m_ScrollDeltaBehaviorContent = new GUIContent("Scroll Delta Behavior", "What value range should be used for scroll wheel delta?"); +#endif m_CompensateForScreenOrientationContent = new GUIContent("Compensate Orientation", "Whether sensor input on mobile devices should be transformed to be relative to the current device orientation."); m_BackgroundBehaviorContent = new GUIContent("Background Behavior", "If runInBackground is true (and in standalone *development* players and the editor), " + "determines what happens to InputDevices and events when the application moves in and out of running in the foreground.\n\n" @@ -404,6 +412,7 @@ private static string[] FindInputSettingsInProject() [NonSerialized] private int m_SettingsDirtyCount; [NonSerialized] private SerializedObject m_SettingsObject; [NonSerialized] private SerializedProperty m_UpdateMode; + [NonSerialized] private SerializedProperty m_ScrollDeltaBehavior; [NonSerialized] private SerializedProperty m_CompensateForScreenOrientation; [NonSerialized] private SerializedProperty m_BackgroundBehavior; [NonSerialized] private SerializedProperty m_EditorInputBehaviorInPlayMode; @@ -427,6 +436,9 @@ private static string[] FindInputSettingsInProject() [NonSerialized] private GUIStyle m_NewAssetButtonStyle; private GUIContent m_UpdateModeContent; +#if UNITY_6000_0_OR_NEWER + private GUIContent m_ScrollDeltaBehaviorContent; +#endif private GUIContent m_CompensateForScreenOrientationContent; private GUIContent m_BackgroundBehaviorContent; private GUIContent m_EditorInputBehaviorInPlayModeContent; diff --git a/Packages/com.unity.inputsystem/InputSystem/IInputRuntime.cs b/Packages/com.unity.inputsystem/InputSystem/IInputRuntime.cs index 82056ec957..62a17562c5 100644 --- a/Packages/com.unity.inputsystem/InputSystem/IInputRuntime.cs +++ b/Packages/com.unity.inputsystem/InputSystem/IInputRuntime.cs @@ -174,6 +174,8 @@ internal unsafe interface IInputRuntime Vector2 screenSize { get; } ScreenOrientation screenOrientation { get; } + bool allowPlatformSpecificInputForScrollWheelDelta { get; set; } + float scrollWheelDeltaPerTick { get; } // If analytics are enabled, the runtime receives analytics events from the input manager. // See InputAnalytics. diff --git a/Packages/com.unity.inputsystem/InputSystem/InputManager.cs b/Packages/com.unity.inputsystem/InputSystem/InputManager.cs index 9b608602e7..dbb44a8c42 100644 --- a/Packages/com.unity.inputsystem/InputSystem/InputManager.cs +++ b/Packages/com.unity.inputsystem/InputSystem/InputManager.cs @@ -159,6 +159,21 @@ public InputUpdateType defaultUpdateType } } + public InputSettings.ScrollDeltaBehavior scrollDeltaBehavior + { + get => m_ScrollDeltaBehavior; + set + { + if (m_ScrollDeltaBehavior == value) + return; + + m_ScrollDeltaBehavior = value; + + InputRuntime.s_Instance.allowPlatformSpecificInputForScrollWheelDelta = + m_ScrollDeltaBehavior == InputSettings.ScrollDeltaBehavior.KeepPlatformSpecificInputRange; + } + } + public float pollingFrequency { get => m_PollingFrequency; @@ -1851,6 +1866,8 @@ internal void InitializeData() m_UpdateMask |= InputUpdateType.Editor; #endif + m_ScrollDeltaBehavior = InputSettings.ScrollDeltaBehavior.UniformAcrossAllPlatforms; + // Default polling frequency is 60 Hz. m_PollingFrequency = 60; @@ -2067,6 +2084,8 @@ internal struct AvailableDevice private InputUpdateType m_CurrentUpdate; internal InputStateBuffers m_StateBuffers; + private InputSettings.ScrollDeltaBehavior m_ScrollDeltaBehavior; + #if UNITY_EDITOR // remember time offset to correctly restore it after editor mode is done private double latestNonEditorTimeOffsetToRealtimeSinceStartup; @@ -2596,6 +2615,8 @@ internal void ApplySettings() #endif updateMask = newUpdateMask; + scrollDeltaBehavior = m_Settings.scrollDeltaBehavior; + ////TODO: optimize this so that we don't repeatedly recreate state if we add/remove multiple devices //// (same goes for not resolving actions repeatedly) @@ -3801,6 +3822,7 @@ internal struct SerializedState public InputStateBuffers buffers; public InputUpdate.SerializedState updateState; public InputUpdateType updateMask; + public InputSettings.ScrollDeltaBehavior scrollDeltaBehavior; public InputMetrics metrics; public InputSettings settings; public InputActionAsset actions; @@ -3845,6 +3867,7 @@ internal SerializedState SaveState() buffers = m_StateBuffers, updateState = InputUpdate.Save(), updateMask = m_UpdateMask, + scrollDeltaBehavior = m_ScrollDeltaBehavior, metrics = m_Metrics, settings = m_Settings, #if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS @@ -3862,6 +3885,7 @@ internal void RestoreStateWithoutDevices(SerializedState state) m_StateBuffers = state.buffers; m_LayoutRegistrationVersion = state.layoutRegistrationVersion + 1; updateMask = state.updateMask; + scrollDeltaBehavior = state.scrollDeltaBehavior; m_Metrics = state.metrics; m_PollingFrequency = state.pollingFrequency; diff --git a/Packages/com.unity.inputsystem/InputSystem/InputSettings.cs b/Packages/com.unity.inputsystem/InputSystem/InputSettings.cs index c9cdafa369..b36a79710f 100644 --- a/Packages/com.unity.inputsystem/InputSystem/InputSettings.cs +++ b/Packages/com.unity.inputsystem/InputSystem/InputSettings.cs @@ -84,6 +84,23 @@ public UpdateMode updateMode } } + /// + /// Controls how platform-specific input should be converted when returning delta values for scroll wheel input actions. + /// By default, the range used for the delta is converted to be uniform across all platforms. + /// + /// The conversion behavior. + public ScrollDeltaBehavior scrollDeltaBehavior + { + get => m_ScrollDeltaBehavior; + set + { + if (m_ScrollDeltaBehavior == value) + return; + m_ScrollDeltaBehavior = value; + OnChange(); + } + } + /// /// If true, sensors that deliver rotation values on handheld devices will automatically adjust /// rotations when the screen orientation changes. @@ -748,6 +765,7 @@ public void SetInternalFeatureFlag(string featureName, bool enabled) [Tooltip("Determine when Unity processes events. By default, accumulated input events are flushed out before each fixed update and " + "before each dynamic update. This setting can be used to restrict event processing to only where the application needs it.")] [SerializeField] private UpdateMode m_UpdateMode = UpdateMode.ProcessEventsInDynamicUpdate; + [SerializeField] private ScrollDeltaBehavior m_ScrollDeltaBehavior = ScrollDeltaBehavior.UniformAcrossAllPlatforms; [SerializeField] private int m_MaxEventBytesPerUpdate = 5 * 1024 * 1024; [SerializeField] private int m_MaxQueuedEventsPerUpdate = 1000; @@ -845,6 +863,29 @@ public enum UpdateMode ProcessEventsManually, } + /// + /// How platform-specific input should be converted when returning delta values for scroll wheel input actions. + /// + public enum ScrollDeltaBehavior + { + /// + /// The range used for the delta is converted to be uniform across all platforms. + /// + /// + /// The resulting range will be [-1, 1] regardless of the platform used. + /// + UniformAcrossAllPlatforms = 0, + + /// + /// The range used for the delta is the same as returned by the platform input. + /// + /// + /// The range will typically be [-120, 120] on Windows and [-1, 1] on MacOS and Linux. + /// Other platforms may have different ranges. + /// + KeepPlatformSpecificInputRange = 1 + } + /// /// Determines how the applications behaves when running in the background. See . /// diff --git a/Packages/com.unity.inputsystem/InputSystem/InputSystem.cs b/Packages/com.unity.inputsystem/InputSystem/InputSystem.cs index c7f78b18bd..7bdb495e8f 100644 --- a/Packages/com.unity.inputsystem/InputSystem/InputSystem.cs +++ b/Packages/com.unity.inputsystem/InputSystem/InputSystem.cs @@ -3423,6 +3423,8 @@ public static bool runInBackground set => s_Manager.m_Runtime.runInBackground = value; } + internal static float scrollWheelDeltaPerTick => InputRuntime.s_Instance.scrollWheelDeltaPerTick; + ////REVIEW: restrict metrics to editor and development builds? /// /// Get various up-to-date metrics about the input system. diff --git a/Packages/com.unity.inputsystem/InputSystem/NativeInputRuntime.cs b/Packages/com.unity.inputsystem/InputSystem/NativeInputRuntime.cs index 6bba685046..8246a0f6ed 100644 --- a/Packages/com.unity.inputsystem/InputSystem/NativeInputRuntime.cs +++ b/Packages/com.unity.inputsystem/InputSystem/NativeInputRuntime.cs @@ -281,6 +281,25 @@ private void OnFocusChanged(bool focus) public Vector2 screenSize => new Vector2(Screen.width, Screen.height); public ScreenOrientation screenOrientation => Screen.orientation; + public bool allowPlatformSpecificInputForScrollWheelDelta + { +#if UNITY_6000_0_OR_NEWER + get => NativeInputSystem.allowPlatformSpecificInputForScrollWheelDelta; + set => NativeInputSystem.allowPlatformSpecificInputForScrollWheelDelta = value; +#else + get; set; +#endif + } + + public float scrollWheelDeltaPerTick + { +#if UNITY_6000_0_OR_NEWER + get { return NativeInputSystem.GetScrollWheelDeltaPerTick(); } +#else + get => 1.0f; +#endif + } + public bool isInBatchMode => Application.isBatchMode; #if UNITY_EDITOR diff --git a/Packages/com.unity.inputsystem/InputSystem/Plugins/InputForUI/InputSystemProvider.cs b/Packages/com.unity.inputsystem/InputSystem/Plugins/InputForUI/InputSystemProvider.cs index 215f246772..dc9455142c 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Plugins/InputForUI/InputSystemProvider.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Plugins/InputForUI/InputSystemProvider.cs @@ -45,7 +45,11 @@ internal class InputSystemProvider : IEventProviderImpl NavigationEventRepeatHelper m_RepeatHelper = new(); bool m_ResetSeenEventsOnUpdate; +#if UNITY_6000_0_OR_NEWER + const float kScrollUGUIScaleFactor = UIElements.WheelEvent.scrollDeltaPerTick; +#else const float kScrollUGUIScaleFactor = 3.0f; +#endif static Action s_OnRegisterActions; @@ -529,8 +533,9 @@ void OnClickPerformed(InputAction.CallbackContext ctx, EventSource eventSource, void OnScrollWheelPerformed(InputAction.CallbackContext ctx) { - var scrollDelta = ctx.ReadValue(); - if (scrollDelta.sqrMagnitude < k_SmallestReportedMovementSqrDist) + // ISXB-704: convert potentially platform-specific input value to uniform ticks before sending them to UI. + var scrollTicks = ctx.ReadValue() / InputSystem.scrollWheelDeltaPerTick; + if (scrollTicks.sqrMagnitude < k_SmallestReportedMovementSqrDist) return; var eventSource = GetEventSource(ctx); @@ -550,9 +555,12 @@ void OnScrollWheelPerformed(InputAction.CallbackContext ctx) targetDisplay = Mouse.current.displayIndex.ReadValue(); } - // Make it look similar to IMGUI event scroll values. - scrollDelta.x *= kScrollUGUIScaleFactor; - scrollDelta.y *= -kScrollUGUIScaleFactor; + // Make scrollDelta look similar to IMGUI event scroll values. + var scrollDelta = new Vector2 + { + x = scrollTicks.x * kScrollUGUIScaleFactor, + y = -scrollTicks.y * kScrollUGUIScaleFactor + }; DispatchFromCallback(Event.From(new PointerEvent { diff --git a/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs b/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs index 66c4f95bb1..18fa0c6f95 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs @@ -2062,8 +2062,6 @@ private bool CheckForRemovedDevice(ref InputAction.CallbackContext context) return false; } - internal const float kPixelPerLine = 20; - private void OnScrollCallback(InputAction.CallbackContext context) { var index = GetPointerStateIndexFor(ref context); @@ -2071,9 +2069,12 @@ private void OnScrollCallback(InputAction.CallbackContext context) return; ref var state = ref GetPointerStateForIndex(index); - // The old input system reported scroll deltas in lines, we report pixels. - // Need to scale as the UI system expects lines. - state.scrollDelta = context.ReadValue() * (1 / kPixelPerLine); + + var scrollDelta = context.ReadValue(); + + // ISXB-704: convert potentially platform-specific input value to BaseInputModule convention. + state.scrollDelta = scrollDelta * (scrollWheelDeltaPerTick / InputSystem.scrollWheelDeltaPerTick); + #if UNITY_2022_3_OR_NEWER state.eventData.displayIndex = GetDisplayIndexFor(context.control); #endif diff --git a/Packages/com.unity.inputsystem/Tests/TestFixture/InputTestRuntime.cs b/Packages/com.unity.inputsystem/Tests/TestFixture/InputTestRuntime.cs index 39b89389c1..b52e43e7c6 100644 --- a/Packages/com.unity.inputsystem/Tests/TestFixture/InputTestRuntime.cs +++ b/Packages/com.unity.inputsystem/Tests/TestFixture/InputTestRuntime.cs @@ -371,6 +371,8 @@ public struct PairedUser public Vector2 screenSize { get; set; } = new Vector2(1024, 768); public ScreenOrientation screenOrientation { set; get; } = ScreenOrientation.Portrait; + public bool allowPlatformSpecificInputForScrollWheelDelta { get; set; } + public float scrollWheelDeltaPerTick { get; } = 1.0f; public List userAccountPairings { From 8ecdb0155bc8dd4b24bd9e9c16c2e66dfee1db48 Mon Sep 17 00:00:00 2001 From: benoitalain Date: Thu, 30 May 2024 10:20:44 -0400 Subject: [PATCH 2/2] Updated for latest trunk PR changes (5f81a20). --- Packages/com.unity.inputsystem/InputSystem/IInputRuntime.cs | 2 +- Packages/com.unity.inputsystem/InputSystem/InputManager.cs | 4 ++-- .../com.unity.inputsystem/InputSystem/NativeInputRuntime.cs | 6 +++--- .../Tests/TestFixture/InputTestRuntime.cs | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Packages/com.unity.inputsystem/InputSystem/IInputRuntime.cs b/Packages/com.unity.inputsystem/InputSystem/IInputRuntime.cs index 62a17562c5..559a752cc9 100644 --- a/Packages/com.unity.inputsystem/InputSystem/IInputRuntime.cs +++ b/Packages/com.unity.inputsystem/InputSystem/IInputRuntime.cs @@ -174,7 +174,7 @@ internal unsafe interface IInputRuntime Vector2 screenSize { get; } ScreenOrientation screenOrientation { get; } - bool allowPlatformSpecificInputForScrollWheelDelta { get; set; } + bool normalizeScrollWheelDelta { get; set; } float scrollWheelDeltaPerTick { get; } // If analytics are enabled, the runtime receives analytics events from the input manager. diff --git a/Packages/com.unity.inputsystem/InputSystem/InputManager.cs b/Packages/com.unity.inputsystem/InputSystem/InputManager.cs index dbb44a8c42..1857c501d6 100644 --- a/Packages/com.unity.inputsystem/InputSystem/InputManager.cs +++ b/Packages/com.unity.inputsystem/InputSystem/InputManager.cs @@ -169,8 +169,8 @@ public InputSettings.ScrollDeltaBehavior scrollDeltaBehavior m_ScrollDeltaBehavior = value; - InputRuntime.s_Instance.allowPlatformSpecificInputForScrollWheelDelta = - m_ScrollDeltaBehavior == InputSettings.ScrollDeltaBehavior.KeepPlatformSpecificInputRange; + InputRuntime.s_Instance.normalizeScrollWheelDelta = + m_ScrollDeltaBehavior == InputSettings.ScrollDeltaBehavior.UniformAcrossAllPlatforms; } } diff --git a/Packages/com.unity.inputsystem/InputSystem/NativeInputRuntime.cs b/Packages/com.unity.inputsystem/InputSystem/NativeInputRuntime.cs index 8246a0f6ed..cef232aa93 100644 --- a/Packages/com.unity.inputsystem/InputSystem/NativeInputRuntime.cs +++ b/Packages/com.unity.inputsystem/InputSystem/NativeInputRuntime.cs @@ -281,11 +281,11 @@ private void OnFocusChanged(bool focus) public Vector2 screenSize => new Vector2(Screen.width, Screen.height); public ScreenOrientation screenOrientation => Screen.orientation; - public bool allowPlatformSpecificInputForScrollWheelDelta + public bool normalizeScrollWheelDelta { #if UNITY_6000_0_OR_NEWER - get => NativeInputSystem.allowPlatformSpecificInputForScrollWheelDelta; - set => NativeInputSystem.allowPlatformSpecificInputForScrollWheelDelta = value; + get => NativeInputSystem.normalizeScrollWheelDelta; + set => NativeInputSystem.normalizeScrollWheelDelta = value; #else get; set; #endif diff --git a/Packages/com.unity.inputsystem/Tests/TestFixture/InputTestRuntime.cs b/Packages/com.unity.inputsystem/Tests/TestFixture/InputTestRuntime.cs index b52e43e7c6..218fe1010a 100644 --- a/Packages/com.unity.inputsystem/Tests/TestFixture/InputTestRuntime.cs +++ b/Packages/com.unity.inputsystem/Tests/TestFixture/InputTestRuntime.cs @@ -371,7 +371,7 @@ public struct PairedUser public Vector2 screenSize { get; set; } = new Vector2(1024, 768); public ScreenOrientation screenOrientation { set; get; } = ScreenOrientation.Portrait; - public bool allowPlatformSpecificInputForScrollWheelDelta { get; set; } + public bool normalizeScrollWheelDelta { get; set; } = true; public float scrollWheelDeltaPerTick { get; } = 1.0f; public List userAccountPairings