2020-10-03 20:19:44 +10:00
|
|
|
|
using System;
|
|
|
|
|
using System.Reflection;
|
2021-03-18 17:17:29 +11:00
|
|
|
|
using UnityExplorer.Core.Unity;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
using UnityEngine;
|
2020-11-20 17:12:40 +11:00
|
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
|
using UnityExplorer.UI;
|
|
|
|
|
using System.Collections.Generic;
|
2021-04-05 16:28:30 +10:00
|
|
|
|
using UnityExplorer.UI.Inspectors;
|
2020-10-03 20:19:44 +10:00
|
|
|
|
|
2021-03-18 17:17:29 +11:00
|
|
|
|
namespace UnityExplorer.Core.Input
|
2020-10-03 20:19:44 +10:00
|
|
|
|
{
|
2020-10-28 06:39:26 +11:00
|
|
|
|
public class InputSystem : IHandleInput
|
2020-10-03 20:19:44 +10:00
|
|
|
|
{
|
2020-10-28 07:14:00 +11:00
|
|
|
|
public InputSystem()
|
|
|
|
|
{
|
|
|
|
|
ExplorerCore.Log("Initializing new InputSystem support...");
|
|
|
|
|
|
|
|
|
|
m_kbCurrentProp = TKeyboard.GetProperty("current");
|
|
|
|
|
m_kbIndexer = TKeyboard.GetProperty("Item", new Type[] { TKey });
|
|
|
|
|
|
2021-03-18 17:17:29 +11:00
|
|
|
|
var btnControl = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.Controls.ButtonControl");
|
2020-10-28 07:14:00 +11:00
|
|
|
|
m_btnIsPressedProp = btnControl.GetProperty("isPressed");
|
|
|
|
|
m_btnWasPressedProp = btnControl.GetProperty("wasPressedThisFrame");
|
|
|
|
|
|
|
|
|
|
m_mouseCurrentProp = TMouse.GetProperty("current");
|
|
|
|
|
m_leftButtonProp = TMouse.GetProperty("leftButton");
|
|
|
|
|
m_rightButtonProp = TMouse.GetProperty("rightButton");
|
|
|
|
|
|
2021-03-18 17:17:29 +11:00
|
|
|
|
m_positionProp = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.Pointer")
|
2020-10-28 07:14:00 +11:00
|
|
|
|
.GetProperty("position");
|
|
|
|
|
|
2021-03-18 17:17:29 +11:00
|
|
|
|
m_readVector2InputMethod = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.InputControl`1")
|
2020-10-28 07:14:00 +11:00
|
|
|
|
.MakeGenericType(typeof(Vector2))
|
|
|
|
|
.GetMethod("ReadValue");
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 17:17:29 +11:00
|
|
|
|
public static Type TKeyboard => m_tKeyboard ?? (m_tKeyboard = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.Keyboard"));
|
2020-10-18 21:41:04 +11:00
|
|
|
|
private static Type m_tKeyboard;
|
2020-10-03 20:19:44 +10:00
|
|
|
|
|
2021-03-18 17:17:29 +11:00
|
|
|
|
public static Type TMouse => m_tMouse ?? (m_tMouse = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.Mouse"));
|
2020-10-18 21:41:04 +11:00
|
|
|
|
private static Type m_tMouse;
|
2020-10-03 20:19:44 +10:00
|
|
|
|
|
2021-03-18 17:17:29 +11:00
|
|
|
|
public static Type TKey => m_tKey ?? (m_tKey = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.Key"));
|
2020-10-18 21:41:04 +11:00
|
|
|
|
private static Type m_tKey;
|
2020-10-03 20:19:44 +10:00
|
|
|
|
|
2020-10-18 21:41:04 +11:00
|
|
|
|
private static PropertyInfo m_btnIsPressedProp;
|
|
|
|
|
private static PropertyInfo m_btnWasPressedProp;
|
2020-10-03 20:19:44 +10:00
|
|
|
|
|
2020-10-18 21:41:04 +11:00
|
|
|
|
private static object CurrentKeyboard => m_currentKeyboard ?? (m_currentKeyboard = m_kbCurrentProp.GetValue(null, null));
|
|
|
|
|
private static object m_currentKeyboard;
|
|
|
|
|
private static PropertyInfo m_kbCurrentProp;
|
|
|
|
|
private static PropertyInfo m_kbIndexer;
|
2020-10-03 20:19:44 +10:00
|
|
|
|
|
2020-10-18 21:41:04 +11:00
|
|
|
|
private static object CurrentMouse => m_currentMouse ?? (m_currentMouse = m_mouseCurrentProp.GetValue(null, null));
|
|
|
|
|
private static object m_currentMouse;
|
|
|
|
|
private static PropertyInfo m_mouseCurrentProp;
|
2020-10-03 20:19:44 +10:00
|
|
|
|
|
2020-10-18 21:41:04 +11:00
|
|
|
|
private static object LeftMouseButton => m_lmb ?? (m_lmb = m_leftButtonProp.GetValue(CurrentMouse, null));
|
|
|
|
|
private static object m_lmb;
|
|
|
|
|
private static PropertyInfo m_leftButtonProp;
|
2020-10-03 20:19:44 +10:00
|
|
|
|
|
2020-10-18 21:41:04 +11:00
|
|
|
|
private static object RightMouseButton => m_rmb ?? (m_rmb = m_rightButtonProp.GetValue(CurrentMouse, null));
|
|
|
|
|
private static object m_rmb;
|
|
|
|
|
private static PropertyInfo m_rightButtonProp;
|
2020-10-03 20:19:44 +10:00
|
|
|
|
|
2020-10-18 21:41:04 +11:00
|
|
|
|
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;
|
2020-10-03 20:19:44 +10:00
|
|
|
|
|
2020-11-20 17:12:40 +11:00
|
|
|
|
public Vector2 MousePosition
|
2020-10-03 20:19:44 +10:00
|
|
|
|
{
|
2020-11-20 17:12:40 +11:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return (Vector2)m_readVector2InputMethod.Invoke(MousePositionInfo, new object[0]);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return Vector2.zero;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-03 20:19:44 +10:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-20 17:12:40 +11:00
|
|
|
|
internal static Dictionary<KeyCode, object> ActualKeyDict = new Dictionary<KeyCode, object>();
|
|
|
|
|
|
|
|
|
|
internal object GetActualKey(KeyCode key)
|
2020-10-03 20:19:44 +10:00
|
|
|
|
{
|
2020-11-20 17:12:40 +11:00
|
|
|
|
if (!ActualKeyDict.ContainsKey(key))
|
|
|
|
|
{
|
|
|
|
|
var s = key.ToString();
|
|
|
|
|
if (s.Contains("Control"))
|
|
|
|
|
s = s.Replace("Control", "Ctrl");
|
|
|
|
|
else if (s.Contains("Return"))
|
|
|
|
|
s = "Enter";
|
2020-10-03 20:19:44 +10:00
|
|
|
|
|
2020-11-20 17:12:40 +11:00
|
|
|
|
var parsed = Enum.Parse(TKey, s);
|
|
|
|
|
var actualKey = m_kbIndexer.GetValue(CurrentKeyboard, new object[] { parsed });
|
|
|
|
|
|
|
|
|
|
ActualKeyDict.Add(key, actualKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ActualKeyDict[key];
|
2020-10-03 20:19:44 +10:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-20 17:12:40 +11:00
|
|
|
|
public bool GetKeyDown(KeyCode key) => (bool)m_btnWasPressedProp.GetValue(GetActualKey(key), null);
|
|
|
|
|
|
|
|
|
|
public bool GetKey(KeyCode key) => (bool)m_btnIsPressedProp.GetValue(GetActualKey(key), null);
|
|
|
|
|
|
2020-10-12 20:15:41 +11:00
|
|
|
|
public bool GetMouseButtonDown(int btn)
|
2020-10-03 20:19:44 +10:00
|
|
|
|
{
|
|
|
|
|
switch (btn)
|
|
|
|
|
{
|
2020-10-18 21:41:04 +11:00
|
|
|
|
case 0: return (bool)m_btnWasPressedProp.GetValue(LeftMouseButton, null);
|
|
|
|
|
case 1: return (bool)m_btnWasPressedProp.GetValue(RightMouseButton, null);
|
2020-10-03 20:19:44 +10:00
|
|
|
|
// case 2: return (bool)_btnWasPressedProp.GetValue(MiddleMouseButton, null);
|
|
|
|
|
default: throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-12 20:15:41 +11:00
|
|
|
|
public bool GetMouseButton(int btn)
|
2020-10-03 20:19:44 +10:00
|
|
|
|
{
|
|
|
|
|
switch (btn)
|
|
|
|
|
{
|
2020-10-18 21:41:04 +11:00
|
|
|
|
case 0: return (bool)m_btnIsPressedProp.GetValue(LeftMouseButton, null);
|
|
|
|
|
case 1: return (bool)m_btnIsPressedProp.GetValue(RightMouseButton, null);
|
2020-10-03 20:19:44 +10:00
|
|
|
|
// case 2: return (bool)_btnIsPressedProp.GetValue(MiddleMouseButton, null);
|
|
|
|
|
default: throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-20 17:12:40 +11:00
|
|
|
|
|
|
|
|
|
// UI Input
|
|
|
|
|
|
2021-04-05 16:28:30 +10:00
|
|
|
|
public Type TInputSystemUIInputModule
|
|
|
|
|
=> m_tUIInputModule
|
|
|
|
|
?? (m_tUIInputModule = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.UI.InputSystemUIInputModule"));
|
|
|
|
|
internal Type m_tUIInputModule;
|
2020-11-20 17:12:40 +11:00
|
|
|
|
|
2021-04-05 16:28:30 +10:00
|
|
|
|
public BaseInputModule UIModule => m_newInputModule;
|
|
|
|
|
internal BaseInputModule m_newInputModule;
|
2020-11-20 17:12:40 +11:00
|
|
|
|
|
|
|
|
|
public void AddUIInputModule()
|
|
|
|
|
{
|
2021-04-05 16:28:30 +10:00
|
|
|
|
if (TInputSystemUIInputModule == null)
|
|
|
|
|
{
|
|
|
|
|
ExplorerCore.LogWarning("Unable to find UI Input Module Type, Input will not work!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var assetType = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.InputActionAsset");
|
2021-04-07 17:20:09 +10:00
|
|
|
|
m_newInputModule = RuntimeProvider.Instance.AddComponent<BaseInputModule>(UIManager.CanvasRoot, TInputSystemUIInputModule);
|
|
|
|
|
var asset = RuntimeProvider.Instance.CreateScriptable(assetType);
|
|
|
|
|
|
2021-04-05 16:28:30 +10:00
|
|
|
|
inputExtensions = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.InputActionSetupExtensions");
|
|
|
|
|
|
|
|
|
|
var addMap = inputExtensions.GetMethod("AddActionMap", new Type[] { assetType, typeof(string) });
|
|
|
|
|
var map = addMap.Invoke(null, new object[] { asset, "UI" });
|
|
|
|
|
|
|
|
|
|
CreateAction(map, "point", new[] { "<Mouse>/position" }, "point");
|
|
|
|
|
CreateAction(map, "click", new[] { "<Mouse>/leftButton" }, "leftClick");
|
|
|
|
|
CreateAction(map, "rightClick", new[] { "<Mouse>/rightButton" }, "rightClick");
|
|
|
|
|
CreateAction(map, "scrollWheel", new[] { "<Mouse>/scroll" }, "scrollWheel");
|
|
|
|
|
|
|
|
|
|
UI_Enable = map.GetType().GetMethod("Enable");
|
|
|
|
|
UI_Enable.Invoke(map, new object[0]);
|
|
|
|
|
UI_ActionMap = map;
|
2020-11-20 17:12:40 +11:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 16:28:30 +10:00
|
|
|
|
private Type inputExtensions;
|
|
|
|
|
private object UI_ActionMap;
|
|
|
|
|
private MethodInfo UI_Enable;
|
|
|
|
|
|
|
|
|
|
private void CreateAction(object map, string actionName, string[] bindings, string propertyName)
|
2020-11-20 17:12:40 +11:00
|
|
|
|
{
|
2021-04-05 16:28:30 +10:00
|
|
|
|
var addAction = inputExtensions.GetMethod("AddAction");
|
|
|
|
|
var pointAction = addAction.Invoke(null, new object[] { map, actionName, default, null, null, null, null, null });
|
2020-11-20 17:12:40 +11:00
|
|
|
|
|
2021-04-05 16:28:30 +10:00
|
|
|
|
var inputActionType = pointAction.GetType();
|
|
|
|
|
var addBinding = inputExtensions.GetMethod("AddBinding",
|
|
|
|
|
new Type[] { inputActionType, typeof(string), typeof(string), typeof(string), typeof(string) });
|
2020-11-20 17:12:40 +11:00
|
|
|
|
|
2021-04-05 16:28:30 +10:00
|
|
|
|
foreach (string binding in bindings)
|
|
|
|
|
addBinding.Invoke(null, new object[] { pointAction, binding, null, null, null });
|
|
|
|
|
|
|
|
|
|
var inputRef = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.InputActionReference")
|
|
|
|
|
.GetMethod("Create")
|
|
|
|
|
.Invoke(null, new object[] { pointAction });
|
|
|
|
|
|
|
|
|
|
TInputSystemUIInputModule
|
|
|
|
|
.GetProperty(propertyName)
|
|
|
|
|
.SetValue(m_newInputModule, inputRef, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ActivateModule()
|
|
|
|
|
{
|
|
|
|
|
m_newInputModule.ActivateModule();
|
|
|
|
|
UI_Enable.Invoke(UI_ActionMap, new object[0]);
|
2020-11-20 17:12:40 +11:00
|
|
|
|
}
|
2020-10-03 20:19:44 +10:00
|
|
|
|
}
|
2021-04-07 17:20:09 +10:00
|
|
|
|
}
|