From 31fa7865745b1ef23717908f90992c876e01ba4b Mon Sep 17 00:00:00 2001 From: Sinai Date: Thu, 22 Apr 2021 03:57:11 +1000 Subject: [PATCH] add MouseScrollDelta support to InputManager --- src/Core/Input/IHandleInput.cs | 1 + src/Core/Input/InputManager.cs | 2 ++ src/Core/Input/InputSystem.cs | 29 ++++++++++++++++++++++++----- src/Core/Input/LegacyInput.cs | 4 ++++ src/Core/Input/NoInput.cs | 1 + 5 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/Core/Input/IHandleInput.cs b/src/Core/Input/IHandleInput.cs index 6fe0bfe..5326f1e 100644 --- a/src/Core/Input/IHandleInput.cs +++ b/src/Core/Input/IHandleInput.cs @@ -6,6 +6,7 @@ namespace UnityExplorer.Core.Input public interface IHandleInput { Vector2 MousePosition { get; } + Vector2 MouseScrollDelta { get; } bool GetKeyDown(KeyCode key); bool GetKey(KeyCode key); diff --git a/src/Core/Input/InputManager.cs b/src/Core/Input/InputManager.cs index e905312..849b8cc 100644 --- a/src/Core/Input/InputManager.cs +++ b/src/Core/Input/InputManager.cs @@ -28,6 +28,8 @@ namespace UnityExplorer.Core.Input public static BaseInputModule UIInput => m_inputModule.UIModule; + public static Vector2 MouseScrollDelta => m_inputModule.MouseScrollDelta; + public static void ActivateUIModule() => m_inputModule.ActivateModule(); public static void AddUIModule() diff --git a/src/Core/Input/InputSystem.cs b/src/Core/Input/InputSystem.cs index 3f6ac33..a7b0309 100644 --- a/src/Core/Input/InputSystem.cs +++ b/src/Core/Input/InputSystem.cs @@ -24,15 +24,18 @@ namespace UnityExplorer.Core.Input m_mouseCurrentProp = TMouse.GetProperty("current"); m_leftButtonProp = TMouse.GetProperty("leftButton"); m_rightButtonProp = TMouse.GetProperty("rightButton"); + m_scrollDeltaProp = TMouse.GetProperty("scroll"); m_positionProp = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.Pointer") .GetProperty("position"); - m_readVector2InputMethod = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.InputControl`1") + ReadV2ControlMethod = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.InputControl`1") .MakeGenericType(typeof(Vector2)) .GetMethod("ReadValue"); } + #region reflection cache + public static Type TKeyboard => m_tKeyboard ?? (m_tKeyboard = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.Keyboard")); private static Type m_tKeyboard; @@ -62,10 +65,17 @@ namespace UnityExplorer.Core.Input private static object m_rmb; private static PropertyInfo m_rightButtonProp; + private static MethodInfo ReadV2ControlMethod; + private static object MousePositionInfo => m_pos ?? (m_pos = m_positionProp.GetValue(CurrentMouse, null)); private static object m_pos; private static PropertyInfo m_positionProp; - private static MethodInfo m_readVector2InputMethod; + + private static object MouseScrollInfo => m_scrollInfo ?? (m_scrollInfo = m_scrollDeltaProp.GetValue(CurrentMouse, null)); + private static object m_scrollInfo; + private static PropertyInfo m_scrollDeltaProp; + + #endregion public Vector2 MousePosition { @@ -73,12 +83,21 @@ namespace UnityExplorer.Core.Input { try { - return (Vector2)m_readVector2InputMethod.Invoke(MousePositionInfo, new object[0]); + return (Vector2)ReadV2ControlMethod.Invoke(MousePositionInfo, new object[0]); } - catch + catch { return Vector2.zero; } + } + } + + public Vector2 MouseScrollDelta + { + get + { + try { - return Vector2.zero; + return (Vector2)ReadV2ControlMethod.Invoke(MouseScrollInfo, new object[0]); } + catch { return Vector2.zero; } } } diff --git a/src/Core/Input/LegacyInput.cs b/src/Core/Input/LegacyInput.cs index 8463cc4..e6ded8f 100644 --- a/src/Core/Input/LegacyInput.cs +++ b/src/Core/Input/LegacyInput.cs @@ -13,6 +13,7 @@ namespace UnityExplorer.Core.Input ExplorerCore.Log("Initializing Legacy Input support..."); m_mousePositionProp = TInput.GetProperty("mousePosition"); + m_mouseDeltaProp = TInput.GetProperty("mouseScrollDelta"); m_getKeyMethod = TInput.GetMethod("GetKey", new Type[] { typeof(KeyCode) }); m_getKeyDownMethod = TInput.GetMethod("GetKeyDown", new Type[] { typeof(KeyCode) }); m_getMouseButtonMethod = TInput.GetMethod("GetMouseButton", new Type[] { typeof(int) }); @@ -23,6 +24,7 @@ namespace UnityExplorer.Core.Input private static Type m_tInput; private static PropertyInfo m_mousePositionProp; + private static PropertyInfo m_mouseDeltaProp; private static MethodInfo m_getKeyMethod; private static MethodInfo m_getKeyDownMethod; private static MethodInfo m_getMouseButtonMethod; @@ -30,6 +32,8 @@ namespace UnityExplorer.Core.Input public Vector2 MousePosition => (Vector3)m_mousePositionProp.GetValue(null, null); + public Vector2 MouseScrollDelta => (Vector2)m_mouseDeltaProp.GetValue(null, null); + public bool GetKey(KeyCode key) => (bool)m_getKeyMethod.Invoke(null, new object[] { key }); public bool GetKeyDown(KeyCode key) => (bool)m_getKeyDownMethod.Invoke(null, new object[] { key }); diff --git a/src/Core/Input/NoInput.cs b/src/Core/Input/NoInput.cs index 159767e..f56140f 100644 --- a/src/Core/Input/NoInput.cs +++ b/src/Core/Input/NoInput.cs @@ -8,6 +8,7 @@ namespace UnityExplorer.Core.Input public class NoInput : IHandleInput { public Vector2 MousePosition => Vector2.zero; + public Vector2 MouseScrollDelta => Vector2.zero; public bool GetKey(KeyCode key) => false; public bool GetKeyDown(KeyCode key) => false;