-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathInputManagerPlatform.cs
More file actions
158 lines (144 loc) · 5.98 KB
/
InputManagerPlatform.cs
File metadata and controls
158 lines (144 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using ImGuiNET;
using UImGui.Assets;
using UnityEngine;
namespace UImGui.Platform
{
// TODO: Check this feature and remove from here when checked and done.
// Implemented features:
// [x] Platform: Clipboard support.
// [x] Platform: Mouse cursor shape and visibility. Disable with io.ConfigFlags |= ImGuiConfigFlags.NoMouseCursorChange.
// [x] Platform: Keyboard arrays indexed using KeyCode codes, e.g. ImGui.IsKeyPressed(KeyCode.Space).
// [ ] Platform: Gamepad support. Enabled with io.ConfigFlags |= ImGuiConfigFlags.NavEnableGamepad.
// [~] Platform: IME support.
// [~] Platform: INI settings support.
/// <summary>
/// Platform bindings for ImGui in Unity in charge of: mouse/keyboard/gamepad inputs, cursor shape, timing, windowing.
/// </summary>
internal sealed class InputManagerPlatform : PlatformBase
{
private readonly Event _textInputEvent = new Event();
private readonly KeyCode[] _keyCodes = (KeyCode[])System.Enum.GetValues(typeof(KeyCode));
public InputManagerPlatform(CursorShapesAsset cursorShapes, IniSettingsAsset iniSettings) :
base(cursorShapes, iniSettings)
{ }
public override bool Initialize(ImGuiIOPtr io, UIOConfig config, string platformName)
{
base.Initialize(io, config, platformName);
return true;
}
public override void PrepareFrame(ImGuiIOPtr io, Rect displayRect)
{
base.PrepareFrame(io, displayRect);
UpdateKeyboard(io);
UpdateMouse(io);
UpdateCursor(io, ImGui.GetMouseCursor());
}
private void UpdateKeyboard(ImGuiIOPtr io)
{
// BUG: mod key make everything slow. Go to line
foreach (KeyCode keyCode in _keyCodes)
{
if (TryMapKeys(keyCode, out ImGuiKey imguikey))
{
io.AddKeyEvent(imguikey, Input.GetKey(keyCode));
}
}
// Handle mod keys separately as 2 buttons map to 1 key
io.AddKeyEvent(ImGuiKey.ModShift, Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift));
io.AddKeyEvent(ImGuiKey.ModCtrl, Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl));
io.AddKeyEvent(ImGuiKey.ModAlt, Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt));
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IOS
io.AddKeyEvent(ImGuiKey.ModSuper, Input.GetKey(KeyCode.LeftCommand) || Input.GetKey(KeyCode.RightCommand));
#else
io.AddKeyEvent(ImGuiKey.ModSuper, Input.GetKey(KeyCode.LeftWindows) || Input.GetKey(KeyCode.RightWindows));
#endif
// Text input.
while (Event.PopEvent(_textInputEvent))
{
if (_textInputEvent.rawType == EventType.KeyDown &&
_textInputEvent.character != 0 && _textInputEvent.character != '\n')
{
io.AddInputCharacter(_textInputEvent.character);
}
}
}
private static void UpdateMouse(ImGuiIOPtr io)
{
Vector2 mousePosition = Utils.ScreenToImGui(Input.mousePosition);
io.AddMousePosEvent(mousePosition.x, mousePosition.y);
io.AddMouseButtonEvent(0, Input.GetMouseButton(0));
io.AddMouseButtonEvent(1, Input.GetMouseButton(1));
io.AddMouseButtonEvent(2, Input.GetMouseButton(2));
io.AddMouseWheelEvent(Input.mouseScrollDelta.x, Input.mouseScrollDelta.y);
}
private static bool TryMapKeys(KeyCode key, out ImGuiKey imguikey)
{
static ImGuiKey KeyToImGuiKeyShortcut(KeyCode keyToConvert, KeyCode startKey1, ImGuiKey startKey2)
{
int changeFromStart1 = (int)keyToConvert - (int)startKey1;
return startKey2 + changeFromStart1;
}
imguikey = key switch
{
>= KeyCode.F1 and <= KeyCode.F12 => KeyToImGuiKeyShortcut(key, KeyCode.F1, ImGuiKey.F1),
>= KeyCode.Keypad0 and <= KeyCode.Keypad9 => KeyToImGuiKeyShortcut(key, KeyCode.Keypad0, ImGuiKey.Keypad0),
>= KeyCode.A and <= KeyCode.Z => KeyToImGuiKeyShortcut(key, KeyCode.A, ImGuiKey.A),
>= KeyCode.Alpha0 and <= KeyCode.Alpha9 => KeyToImGuiKeyShortcut(key, KeyCode.Alpha0, ImGuiKey._0),
KeyCode.LeftControl => ImGuiKey.LeftCtrl,
KeyCode.RightControl => ImGuiKey.RightCtrl,
KeyCode.LeftShift => ImGuiKey.LeftShift,
KeyCode.RightShift => ImGuiKey.RightShift,
KeyCode.LeftAlt => ImGuiKey.LeftAlt,
KeyCode.RightAlt => ImGuiKey.RightAlt,
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IOS
KeyCode.LeftCommand => ImGuiKey.LeftSuper,
KeyCode.RightCommand => ImGuiKey.RightSuper,
#else
KeyCode.LeftWindows => ImGuiKey.LeftSuper,
KeyCode.RightWindows => ImGuiKey.RightSuper,
#endif
KeyCode.Menu => ImGuiKey.Menu,
KeyCode.UpArrow => ImGuiKey.UpArrow,
KeyCode.DownArrow => ImGuiKey.DownArrow,
KeyCode.LeftArrow => ImGuiKey.LeftArrow,
KeyCode.RightArrow => ImGuiKey.RightArrow,
KeyCode.Return => ImGuiKey.Enter,
KeyCode.Escape => ImGuiKey.Escape,
KeyCode.Space => ImGuiKey.Space,
KeyCode.Tab => ImGuiKey.Tab,
KeyCode.Backspace => ImGuiKey.Backspace,
KeyCode.Insert => ImGuiKey.Insert,
KeyCode.Delete => ImGuiKey.Delete,
KeyCode.PageUp => ImGuiKey.PageUp,
KeyCode.PageDown => ImGuiKey.PageDown,
KeyCode.Home => ImGuiKey.Home,
KeyCode.End => ImGuiKey.End,
KeyCode.CapsLock => ImGuiKey.CapsLock,
KeyCode.ScrollLock => ImGuiKey.ScrollLock,
KeyCode.Print => ImGuiKey.PrintScreen,
KeyCode.Pause => ImGuiKey.Pause,
KeyCode.Numlock => ImGuiKey.NumLock,
KeyCode.KeypadDivide => ImGuiKey.KeypadDivide,
KeyCode.KeypadMultiply => ImGuiKey.KeypadMultiply,
KeyCode.KeypadMinus => ImGuiKey.KeypadSubtract,
KeyCode.KeypadPlus => ImGuiKey.KeypadAdd,
KeyCode.KeypadPeriod => ImGuiKey.KeypadDecimal,
KeyCode.KeypadEnter => ImGuiKey.KeypadEnter,
KeyCode.KeypadEquals => ImGuiKey.KeypadEqual,
KeyCode.Tilde => ImGuiKey.GraveAccent,
KeyCode.Minus => ImGuiKey.Minus,
KeyCode.Plus => ImGuiKey.Equal,
KeyCode.LeftBracket => ImGuiKey.LeftBracket,
KeyCode.RightBracket => ImGuiKey.RightBracket,
KeyCode.Semicolon => ImGuiKey.Semicolon,
KeyCode.Quote => ImGuiKey.Apostrophe,
KeyCode.Comma => ImGuiKey.Comma,
KeyCode.Period => ImGuiKey.Period,
KeyCode.Slash => ImGuiKey.Slash,
KeyCode.Backslash => ImGuiKey.Backslash,
_ => ImGuiKey.None
};
return imguikey != ImGuiKey.None;
}
}
}