2020-10-03 20:19:44 +10:00
|
|
|
|
using System;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using UnityEngine;
|
2020-10-23 01:50:33 +11:00
|
|
|
|
using ExplorerBeta.Helpers;
|
2020-10-03 20:19:44 +10:00
|
|
|
|
|
2020-10-23 01:50:33 +11:00
|
|
|
|
namespace ExplorerBeta.Input
|
2020-10-03 20:19:44 +10:00
|
|
|
|
{
|
2020-10-12 20:15:41 +11:00
|
|
|
|
public class InputSystem : IAbstractInput
|
2020-10-03 20:19:44 +10:00
|
|
|
|
{
|
2020-10-18 21:41:04 +11:00
|
|
|
|
public static Type TKeyboard => m_tKeyboard ?? (m_tKeyboard = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Keyboard"));
|
|
|
|
|
private static Type m_tKeyboard;
|
2020-10-03 20:19:44 +10:00
|
|
|
|
|
2020-10-18 21:41:04 +11:00
|
|
|
|
public static Type TMouse => m_tMouse ?? (m_tMouse = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Mouse"));
|
|
|
|
|
private static Type m_tMouse;
|
2020-10-03 20:19:44 +10:00
|
|
|
|
|
2020-10-18 21:41:04 +11:00
|
|
|
|
public static Type TKey => m_tKey ?? (m_tKey = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Key"));
|
|
|
|
|
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-10-18 21:41:04 +11:00
|
|
|
|
public Vector2 MousePosition => (Vector2)m_readVector2InputMethod.Invoke(MousePositionInfo, new object[0]);
|
2020-10-03 20:19:44 +10:00
|
|
|
|
|
2020-10-12 20:15:41 +11:00
|
|
|
|
public bool GetKeyDown(KeyCode key)
|
2020-10-03 20:19:44 +10:00
|
|
|
|
{
|
|
|
|
|
var parsedKey = Enum.Parse(TKey, key.ToString());
|
2020-10-18 21:41:04 +11:00
|
|
|
|
var actualKey = m_kbIndexer.GetValue(CurrentKeyboard, new object[] { parsedKey });
|
2020-10-03 20:19:44 +10:00
|
|
|
|
|
2020-10-18 21:41:04 +11:00
|
|
|
|
return (bool)m_btnWasPressedProp.GetValue(actualKey, null);
|
2020-10-03 20:19:44 +10:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-12 20:15:41 +11:00
|
|
|
|
public bool GetKey(KeyCode key)
|
2020-10-03 20:19:44 +10:00
|
|
|
|
{
|
|
|
|
|
var parsed = Enum.Parse(TKey, key.ToString());
|
2020-10-18 21:41:04 +11:00
|
|
|
|
var actualKey = m_kbIndexer.GetValue(CurrentKeyboard, new object[] { parsed });
|
2020-10-03 20:19:44 +10:00
|
|
|
|
|
2020-10-18 21:41:04 +11:00
|
|
|
|
return (bool)m_btnIsPressedProp.GetValue(actualKey, null);
|
2020-10-03 20:19:44 +10:00
|
|
|
|
}
|
|
|
|
|
|
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-10-12 20:15:41 +11:00
|
|
|
|
public void Init()
|
2020-10-03 20:19:44 +10:00
|
|
|
|
{
|
|
|
|
|
ExplorerCore.Log("Initializing new InputSystem support...");
|
|
|
|
|
|
2020-10-18 21:41:04 +11:00
|
|
|
|
m_kbCurrentProp = TKeyboard.GetProperty("current");
|
|
|
|
|
m_kbIndexer = TKeyboard.GetProperty("Item", new Type[] { TKey });
|
2020-10-03 20:19:44 +10:00
|
|
|
|
|
|
|
|
|
var btnControl = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Controls.ButtonControl");
|
2020-10-18 21:41:04 +11:00
|
|
|
|
m_btnIsPressedProp = btnControl.GetProperty("isPressed");
|
|
|
|
|
m_btnWasPressedProp = btnControl.GetProperty("wasPressedThisFrame");
|
2020-10-03 20:19:44 +10:00
|
|
|
|
|
2020-10-18 21:41:04 +11:00
|
|
|
|
m_mouseCurrentProp = TMouse.GetProperty("current");
|
|
|
|
|
m_leftButtonProp = TMouse.GetProperty("leftButton");
|
|
|
|
|
m_rightButtonProp = TMouse.GetProperty("rightButton");
|
2020-10-03 20:19:44 +10:00
|
|
|
|
|
2020-10-18 21:41:04 +11:00
|
|
|
|
m_positionProp = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Pointer")
|
2020-10-03 20:19:44 +10:00
|
|
|
|
.GetProperty("position");
|
|
|
|
|
|
2020-10-18 21:41:04 +11:00
|
|
|
|
m_readVector2InputMethod = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.InputControl`1")
|
2020-10-03 20:19:44 +10:00
|
|
|
|
.MakeGenericType(typeof(Vector2))
|
|
|
|
|
.GetMethod("ReadValue");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|