forked from flavius-st/TheAdventure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.cs
More file actions
214 lines (184 loc) · 7.13 KB
/
Copy pathInput.cs
File metadata and controls
214 lines (184 loc) · 7.13 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using Silk.NET.SDL;
namespace TheAdventure
{
public unsafe class Input
{
private Sdl _sdl;
private GameWindow _gameWindow;
private GameRenderer _renderer;
byte[] _mouseButtonStates = new byte[(int)MouseButton.Count];
public EventHandler<(int x, int y)> OnMouseClick;
public Input(Sdl sdl, GameWindow window, GameRenderer renderer)
{
_sdl = sdl;
_gameWindow = window;
_renderer = renderer;
}
private static HashSet<KeyCode> pressedKeys = new HashSet<KeyCode>();
public static void KeyDown(KeyCode key)
{
pressedKeys.Add(key);
}
public static void KeyUp(KeyCode key)
{
pressedKeys.Remove(key);
}
public static bool IsKeyPressed(KeyCode key)
{
return pressedKeys.Contains(key);
}
public bool IsKeyAPressed(){
ReadOnlySpan<byte> _keyboardState = new(_sdl.GetKeyboardState(null), (int)KeyCode.Count);
return _keyboardState[(int)KeyCode.A] == 1;
}
public bool IsKeyBPressed(){
ReadOnlySpan<byte> _keyboardState = new(_sdl.GetKeyboardState(null), (int)KeyCode.Count);
return _keyboardState[(int)KeyCode.B] == 1;
}
public bool IsLeftPressed()
{
ReadOnlySpan<byte> _keyboardState = new(_sdl.GetKeyboardState(null), (int)KeyCode.Count);
return _keyboardState[(int)KeyCode.Left] == 1;
}
public bool IsRightPressed()
{
ReadOnlySpan<byte> _keyboardState = new(_sdl.GetKeyboardState(null), (int)KeyCode.Count);
return _keyboardState[(int)KeyCode.Right] == 1;
}
public bool IsUpPressed()
{
ReadOnlySpan<byte> _keyboardState = new(_sdl.GetKeyboardState(null), (int)KeyCode.Count);
return _keyboardState[(int)KeyCode.Up] == 1;
}
public bool IsDownPressed()
{
ReadOnlySpan<byte> _keyboardState = new(_sdl.GetKeyboardState(null), (int)KeyCode.Count);
return _keyboardState[(int)KeyCode.Down] == 1;
}
public bool IsWPressed()
{
ReadOnlySpan<byte> _keyboardState = new(_sdl.GetKeyboardState(null), (int)KeyCode.Count);
return _keyboardState[(int)KeyCode.W] == 1;
}
public bool ProcessInput()
{
var currentTime = DateTimeOffset.UtcNow;
Event ev = new Event();
var mouseX = 0;
var mouseY = 0;
while (_sdl.PollEvent(ref ev) != 0)
{
if (ev.Type == (uint)EventType.Quit)
{
return true;
}
switch (ev.Type)
{
case (uint)EventType.Windowevent:
{
switch (ev.Window.Event)
{
case (byte)WindowEventID.Shown:
case (byte)WindowEventID.Exposed:
{
break;
}
case (byte)WindowEventID.Hidden:
{
break;
}
case (byte)WindowEventID.Moved:
{
break;
}
case (byte)WindowEventID.SizeChanged:
{
break;
}
case (byte)WindowEventID.Minimized:
case (byte)WindowEventID.Maximized:
case (byte)WindowEventID.Restored:
break;
case (byte)WindowEventID.Enter:
{
break;
}
case (byte)WindowEventID.Leave:
{
break;
}
case (byte)WindowEventID.FocusGained:
{
break;
}
case (byte)WindowEventID.FocusLost:
{
break;
}
case (byte)WindowEventID.Close:
{
break;
}
case (byte)WindowEventID.TakeFocus:
{
unsafe
{
_sdl.SetWindowInputFocus(_sdl.GetWindowFromID(ev.Window.WindowID));
}
break;
}
}
break;
}
case (uint)EventType.Fingermotion:
{
break;
}
case (uint)EventType.Mousemotion:
{
break;
}
case (uint)EventType.Fingerdown:
{
_mouseButtonStates[(byte)MouseButton.Primary] = 1;
break;
}
case (uint)EventType.Mousebuttondown:
{
mouseX = ev.Motion.X;
mouseY = ev.Motion.Y;
_mouseButtonStates[ev.Button.Button] = 1;
if (ev.Button.Button == (byte)MouseButton.Primary)
{
OnMouseClick?.Invoke(this, (mouseX, mouseY));
}
break;
}
case (uint)EventType.Fingerup:
{
_mouseButtonStates[(byte)MouseButton.Primary] = 0;
break;
}
case (uint)EventType.Mousebuttonup:
{
_mouseButtonStates[ev.Button.Button] = 0;
break;
}
case (uint)EventType.Mousewheel:
{
break;
}
case (uint)EventType.Keyup:
{
break;
}
case (uint)EventType.Keydown:
{
break;
}
}
}
return false;
}
}
}