Created
March 5, 2023 10:54
-
-
Save CodeSmile-0000011110110111/7d11ec73188dcc610d4c37aa1e294b72 to your computer and use it in GitHub Desktop.
Handles input in OnSceneGUI and other "On..GUI" methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Public Domain | |
// Usage: Add an instance of EditorInputState to an editor script, create the instance on Awake. | |
// Then call m_InputState.Update() in every On*GUI method you implement and then query the m_InputState | |
// wherever you need to test for input. | |
using System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UIElements; | |
namespace CodeSmile.Tile.UnityEditor | |
{ | |
public sealed partial class EditorInputState | |
{ | |
private readonly Dictionary<MouseButton, bool> m_ButtonPressed; | |
private readonly Dictionary<KeyCode, bool> m_KeyPressed; | |
private bool m_TouchDown; | |
public bool IsTouchDown => m_TouchDown; | |
public EditorInputState() | |
{ | |
m_ButtonPressed = new Dictionary<MouseButton, bool> | |
{ | |
{ MouseButton.LeftMouse, false }, | |
{ MouseButton.MiddleMouse, false }, | |
{ MouseButton.RightMouse, false }, | |
}; | |
m_KeyPressed = new Dictionary<KeyCode, bool>(); | |
var keyCodes = Enum.GetValues(typeof(KeyCode)); | |
foreach (KeyCode keyCode in keyCodes) | |
{ | |
if (m_KeyPressed.ContainsKey(keyCode) == false) | |
m_KeyPressed.Add(keyCode, false); | |
} | |
} | |
public bool IsButtonDown(MouseButton button) => m_ButtonPressed[button]; | |
public bool IsKeyDown(KeyCode keyCode) => m_KeyPressed[keyCode]; | |
private void SetKeyPressed(bool down) => m_KeyPressed[Event.current.keyCode] = down; | |
private void SetButtonPressed(bool down) | |
{ | |
if (Event.current.button < m_ButtonPressed.Count) | |
m_ButtonPressed[MouseButtonFromEvent()] = down; | |
} | |
private MouseButton MouseButtonFromEvent() => (MouseButton)Mathf.Clamp(Event.current.button, 0, 2); | |
private void SetTouchPressed(bool down) => m_TouchDown = down; | |
public void Update() | |
{ | |
switch (Event.current.type) | |
{ | |
case EventType.TouchDown: | |
SetTouchPressed(true); | |
break; | |
case EventType.TouchUp: | |
SetTouchPressed(false); | |
break; | |
case EventType.MouseDown: | |
SetButtonPressed(true); | |
break; | |
case EventType.MouseUp: | |
SetButtonPressed(false); | |
break; | |
case EventType.KeyDown: | |
SetKeyPressed(true); | |
break; | |
case EventType.KeyUp: | |
SetKeyPressed(false); | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment