Repository navigation
Expand file tree
/
Copy pathSDL3Input.cpp
More file actions
259 lines (229 loc) · 6.24 KB
/
Copy pathSDL3Input.cpp
File metadata and controls
259 lines (229 loc) · 6.24 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include "SDL3Input.h"
#include "Keys.h" // from deki-input
#include <deki/Engine.h>
#include <deki/providers/IRenderSystem.h>
namespace DekiSdl3
{
namespace
{
// Mouse events arrive in window pixels, but the engine's screen-to-world math
// expects framebuffer pixels. The window is larger than the framebuffer by
// windowScale, so coordinates are scaled down before dispatch. Window and
// framebuffer share an aspect ratio, so a straight ratio is exact, with no
// letterbox offset.
void WindowToFramebuffer(SDL_WindowID windowID, float& x, float& y)
{
SDL_Window* window = SDL_GetWindowFromID(windowID);
if (!window)
{
return;
}
int winW = 0, winH = 0;
SDL_GetWindowSize(window, &winW, &winH);
if (winW <= 0 || winH <= 0)
{
return;
}
Deki::IRenderSystem* rs = Deki::Engine::GetInstance().GetRenderSystem();
if (!rs)
{
return;
}
int32_t fbW = rs->GetScreenWidth();
int32_t fbH = rs->GetScreenHeight();
if (fbW <= 0 || fbH <= 0)
{
return;
}
x *= static_cast<float>(fbW) / static_cast<float>(winW);
y *= static_cast<float>(fbH) / static_cast<float>(winH);
}
} // namespace
SDL3Input::SDL3Input()
: initialized(false),
m_QuitFlag(false),
m_MouseX(0),
m_MouseY(0),
m_MousePressed(false)
{
}
SDL3Input::~SDL3Input()
{
Shutdown();
}
bool SDL3Input::Initialize()
{
if (initialized)
{
return true;
}
initialized = true;
m_QuitFlag = false;
return true;
}
void SDL3Input::Shutdown()
{
initialized = false;
}
void SDL3Input::Update()
{
if (!initialized)
{
return;
}
SDL_Event event;
while (SDL_PollEvent(&event))
{
ProcessSDLEvent(event);
}
// Update mouse state (scale window pixels down to framebuffer pixels)
float mx, my;
SDL_MouseButtonFlags mouseState = SDL_GetMouseState(&mx, &my);
if (SDL_Window* focus = SDL_GetMouseFocus())
{
WindowToFramebuffer(SDL_GetWindowID(focus), mx, my);
}
m_MouseX = (int32_t)mx;
m_MouseY = (int32_t)my;
m_MousePressed = (mouseState & SDL_BUTTON_LMASK) != 0;
}
void SDL3Input::ProcessSDLEvent(const SDL_Event& event)
{
DekiInput::InputEvent inputEvent;
inputEvent.timestamp = static_cast<uint32_t>(SDL_GetTicks());
if (event.type == SDL_EVENT_QUIT)
{
m_QuitFlag = true;
inputEvent.type = DekiInput::InputEventType::AppQuit;
NotifyCallbacks(inputEvent);
return;
}
switch (event.type)
{
case SDL_EVENT_MOUSE_MOTION:
{
float ex = event.motion.x, ey = event.motion.y;
WindowToFramebuffer(event.motion.windowID, ex, ey);
inputEvent.type = DekiInput::InputEventType::MouseMove;
inputEvent.x = (int32_t)ex;
inputEvent.y = (int32_t)ey;
NotifyCallbacks(inputEvent);
break;
}
case SDL_EVENT_MOUSE_BUTTON_DOWN:
{
float ex = event.button.x, ey = event.button.y;
WindowToFramebuffer(event.button.windowID, ex, ey);
inputEvent.type = DekiInput::InputEventType::MouseButtonDown;
inputEvent.x = (int32_t)ex;
inputEvent.y = (int32_t)ey;
inputEvent.pressed = true;
NotifyCallbacks(inputEvent);
break;
}
case SDL_EVENT_MOUSE_BUTTON_UP:
{
float ex = event.button.x, ey = event.button.y;
WindowToFramebuffer(event.button.windowID, ex, ey);
inputEvent.type = DekiInput::InputEventType::MouseButtonUp;
inputEvent.x = (int32_t)ex;
inputEvent.y = (int32_t)ey;
inputEvent.pressed = false;
NotifyCallbacks(inputEvent);
break;
}
case SDL_EVENT_KEY_DOWN:
{
// 0: a key the engine has no code for (F1, Shift). Reported, it
// would read as "key 0 pressed" to everything that asks.
uint32_t genericKey = ConvertSDLKeyToGeneric(event.key.key);
if (genericKey == 0)
{
break;
}
m_KeyStates[genericKey] = true;
inputEvent.type = DekiInput::InputEventType::KeyDown;
inputEvent.key = genericKey;
inputEvent.pressed = true;
NotifyCallbacks(inputEvent);
}
break;
case SDL_EVENT_KEY_UP:
{
uint32_t genericKey = ConvertSDLKeyToGeneric(event.key.key);
if (genericKey == 0)
{
break;
}
m_KeyStates[genericKey] = false;
inputEvent.type = DekiInput::InputEventType::KeyUp;
inputEvent.key = genericKey;
inputEvent.pressed = false;
NotifyCallbacks(inputEvent);
}
break;
}
}
void SDL3Input::NotifyCallbacks(const DekiInput::InputEvent& event)
{
for (const auto& callback : m_EventCallbacks)
{
callback(event);
}
}
uint32_t SDL3Input::ConvertSDLKeyToGeneric(SDL_Keycode sdlKey)
{
namespace Keys = DekiInput::Keys;
switch (sdlKey)
{
case SDLK_RETURN: return Keys::Enter;
case SDLK_ESCAPE: return Keys::Esc;
case SDLK_BACKSPACE: return Keys::Backspace;
case SDLK_TAB: return Keys::Tab;
case SDLK_DELETE: return Keys::Delete;
case SDLK_UP: return Keys::Up;
case SDLK_DOWN: return Keys::Down;
case SDLK_LEFT: return Keys::Left;
case SDLK_RIGHT: return Keys::Right;
default:
if (sdlKey >= 32 && sdlKey <= 126)
{
return sdlKey;
}
return 0;
}
}
void SDL3Input::RegisterEventCallback(const DekiInput::InputEventCallback& callback)
{
m_EventCallbacks.push_back(callback);
}
bool SDL3Input::IsInitialized() const
{
return initialized;
}
bool SDL3Input::GetPointerPosition(int32_t* x, int32_t* y) const
{
if (x)
{
*x = m_MouseX;
}
if (y)
{
*y = m_MouseY;
}
return true;
}
bool SDL3Input::IsKeyPressed(uint32_t key) const
{
auto it = m_KeyStates.find(key);
if (it != m_KeyStates.end())
{
return it->second;
}
return false;
}
bool SDL3Input::CheckForQuit() const
{
return m_QuitFlag;
}
} // namespace DekiSdl3