-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppWindow_WndProc.cpp
More file actions
412 lines (356 loc) · 12 KB
/
AppWindow_WndProc.cpp
File metadata and controls
412 lines (356 loc) · 12 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include "AppWindow_Internal.h"
#include "core/Log.h"
#include <windowsx.h>
LRESULT CALLBACK AppWindow::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (msg == WM_NCCREATE)
{
const CREATESTRUCTW* cs = reinterpret_cast<const CREATESTRUCTW*>(lParam);
auto* self = static_cast<AppWindow*>(cs->lpCreateParams);
SetWindowLongPtrW(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(self));
self->m_hwnd = hwnd;
}
auto* self = reinterpret_cast<AppWindow*>(GetWindowLongPtrW(hwnd, GWLP_USERDATA));
if (self)
return self->HandleMsg(hwnd, msg, wParam, lParam);
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
LRESULT AppWindow::HandleMsg(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
// Feed ImGui first so it can update WantCaptureMouse/Keyboard state.
bool imguiConsumed = false;
bool uiWantsMouse = false;
bool uiWantsKeyboard = false;
bool uiWantsText = false;
#if defined(COLONY_WITH_IMGUI)
const bool uiActive = m_impl && m_impl->imguiInitialized && m_impl->overlayVisible && m_impl->imgui.enabled;
if (uiActive)
{
imguiConsumed = m_impl->imgui.handleWndProc(hwnd, msg, wParam, lParam);
uiWantsMouse = m_impl->imgui.wantsMouse();
uiWantsKeyboard = m_impl->imgui.wantsKeyboard();
uiWantsText = m_impl->imgui.wantsTextInput();
}
#endif
switch (msg)
{
case WM_DESTROY:
{
if (m_impl)
{
// Persist the latest settings even if we didn't get a chance to
// run the debounced autosave.
m_impl->settings.windowWidth = static_cast<int>(m_width);
m_impl->settings.windowHeight = static_cast<int>(m_height);
m_impl->settings.vsync = m_vsync;
m_impl->settings.fullscreen = m_impl->fullscreen.IsFullscreen();
colony::appwin::SaveUserSettings(m_impl->settings);
#if defined(COLONY_WITH_IMGUI)
if (m_impl->imguiInitialized)
{
m_impl->imgui.shutdown();
m_impl->imguiInitialized = false;
}
#endif
}
PostQuitMessage(0);
return 0;
}
case WM_ENTERSIZEMOVE:
{
if (m_impl)
m_impl->inSizeMove = true;
return 0;
}
case WM_EXITSIZEMOVE:
{
if (m_impl)
{
m_impl->inSizeMove = false;
if (m_impl->pendingResizeW != 0 && m_impl->pendingResizeH != 0)
{
m_width = m_impl->pendingResizeW;
m_height = m_impl->pendingResizeH;
m_gfx.Resize(m_width, m_height);
// Only persist a windowed size.
if (!m_impl->fullscreen.IsFullscreen())
{
m_impl->settings.windowWidth = static_cast<int>(m_width);
m_impl->settings.windowHeight = static_cast<int>(m_height);
MarkSettingsDirty();
}
colony::input::InputEvent ev{};
ev.type = colony::input::InputEventType::WindowResize;
ev.resize.width = m_width;
ev.resize.height = m_height;
m_impl->input.Push(ev);
m_impl->pendingResizeW = 0;
m_impl->pendingResizeH = 0;
}
}
return 0;
}
case WM_SIZE:
{
const UINT w = LOWORD(lParam);
const UINT h = HIWORD(lParam);
if (wParam == SIZE_MINIMIZED)
{
m_width = w;
m_height = h;
return 0;
}
if (w == 0 || h == 0)
return 0;
m_width = w;
m_height = h;
if (m_impl && m_impl->inSizeMove)
{
m_impl->pendingResizeW = w;
m_impl->pendingResizeH = h;
return 0;
}
m_gfx.Resize(w, h);
if (m_impl)
{
// Only persist a windowed size.
if (!m_impl->fullscreen.IsFullscreen())
{
m_impl->settings.windowWidth = static_cast<int>(w);
m_impl->settings.windowHeight = static_cast<int>(h);
MarkSettingsDirty();
}
colony::input::InputEvent ev{};
ev.type = colony::input::InputEventType::WindowResize;
ev.resize.width = w;
ev.resize.height = h;
m_impl->input.Push(ev);
}
return 0;
}
case WM_ACTIVATEAPP:
{
if (m_impl)
{
m_impl->active = (wParam != FALSE);
if (wParam == FALSE)
{
colony::input::InputEvent ev{};
ev.type = colony::input::InputEventType::FocusLost;
m_impl->input.Push(ev);
}
UpdateTitle();
}
return 0;
}
case WM_SYSKEYDOWN:
{
const bool isRepeat = (lParam & (1 << 30)) != 0;
if (!isRepeat && wParam == VK_RETURN && (lParam & (1 << 29)))
{
ToggleFullscreen();
return 0;
}
break;
}
case WM_KEYDOWN:
{
const bool isRepeat = (lParam & (1 << 30)) != 0;
if (isRepeat)
break;
if (wParam == VK_ESCAPE)
{
PostQuitMessage(0);
return 0;
}
if (wParam == VK_F1)
{
ToggleOverlay();
return 0;
}
if (wParam == VK_F11)
{
ToggleFullscreen();
return 0;
}
if (wParam == 'V')
{
// Avoid stealing "V" from ImGui text inputs.
if (!uiWantsText)
ToggleVsync();
return 0;
}
break;
}
case WM_INPUT:
{
if (m_impl)
{
LONG dx = 0, dy = 0;
if (m_impl->mouse.OnRawInput(hwnd, lParam, dx, dy))
{
if (!uiWantsMouse)
{
colony::input::InputEvent ev{};
ev.type = colony::input::InputEventType::MouseDelta;
ev.mouseDelta.dx = dx;
ev.mouseDelta.dy = dy;
m_impl->input.Push(ev);
}
}
}
return 0;
}
case WM_MOUSEMOVE:
{
if (m_impl)
{
const int x = GET_X_LPARAM(lParam);
const int y = GET_Y_LPARAM(lParam);
m_impl->mouse.OnMouseMove(x, y);
if (!uiWantsMouse)
{
colony::input::InputEvent ev{};
ev.type = colony::input::InputEventType::MouseMove;
ev.mouseMove.x = x;
ev.mouseMove.y = y;
m_impl->input.Push(ev);
}
}
return 0;
}
case WM_LBUTTONDOWN:
{
if (m_impl)
{
const int x = GET_X_LPARAM(lParam);
const int y = GET_Y_LPARAM(lParam);
m_impl->mouse.OnLButtonDown(hwnd, x, y);
if (!uiWantsMouse)
{
colony::input::InputEvent ev{};
ev.type = colony::input::InputEventType::MouseButtonDown;
ev.mouseButtonDown.button = colony::input::MouseButton::Left;
m_impl->input.Push(ev);
}
}
return 0;
}
case WM_LBUTTONUP:
{
if (m_impl)
{
const int x = GET_X_LPARAM(lParam);
const int y = GET_Y_LPARAM(lParam);
m_impl->mouse.OnLButtonUp(hwnd, x, y);
if (!uiWantsMouse)
{
colony::input::InputEvent ev{};
ev.type = colony::input::InputEventType::MouseButtonUp;
ev.mouseButtonUp.button = colony::input::MouseButton::Left;
m_impl->input.Push(ev);
}
}
return 0;
}
case WM_RBUTTONDOWN:
{
if (m_impl)
{
const int x = GET_X_LPARAM(lParam);
const int y = GET_Y_LPARAM(lParam);
m_impl->mouse.OnRButtonDown(hwnd, x, y);
if (!uiWantsMouse)
{
colony::input::InputEvent ev{};
ev.type = colony::input::InputEventType::MouseButtonDown;
ev.mouseButtonDown.button = colony::input::MouseButton::Right;
m_impl->input.Push(ev);
}
}
return 0;
}
case WM_RBUTTONUP:
{
if (m_impl)
{
const int x = GET_X_LPARAM(lParam);
const int y = GET_Y_LPARAM(lParam);
m_impl->mouse.OnRButtonUp(hwnd, x, y);
if (!uiWantsMouse)
{
colony::input::InputEvent ev{};
ev.type = colony::input::InputEventType::MouseButtonUp;
ev.mouseButtonUp.button = colony::input::MouseButton::Right;
m_impl->input.Push(ev);
}
}
return 0;
}
case WM_MBUTTONDOWN:
{
if (m_impl)
{
const int x = GET_X_LPARAM(lParam);
const int y = GET_Y_LPARAM(lParam);
m_impl->mouse.OnMButtonDown(hwnd, x, y);
if (!uiWantsMouse)
{
colony::input::InputEvent ev{};
ev.type = colony::input::InputEventType::MouseButtonDown;
ev.mouseButtonDown.button = colony::input::MouseButton::Middle;
m_impl->input.Push(ev);
}
}
return 0;
}
case WM_MBUTTONUP:
{
if (m_impl)
{
const int x = GET_X_LPARAM(lParam);
const int y = GET_Y_LPARAM(lParam);
m_impl->mouse.OnMButtonUp(hwnd, x, y);
if (!uiWantsMouse)
{
colony::input::InputEvent ev{};
ev.type = colony::input::InputEventType::MouseButtonUp;
ev.mouseButtonUp.button = colony::input::MouseButton::Middle;
m_impl->input.Push(ev);
}
}
return 0;
}
case WM_MOUSEWHEEL:
{
if (m_impl && !uiWantsMouse)
{
const int delta = GET_WHEEL_DELTA_WPARAM(wParam);
colony::input::InputEvent ev{};
ev.type = colony::input::InputEventType::MouseWheel;
ev.mouseWheel.delta = delta;
m_impl->input.Push(ev);
}
return 0;
}
case WM_SETCURSOR:
{
if (m_impl)
{
// If ImGui is actively capturing mouse, let its Win32 backend
// drive the cursor shape (resize, text input, etc.).
if (imguiConsumed)
return TRUE;
const auto cur = m_impl->mouse.DesiredCursor();
SetCursor(LoadCursorW(nullptr, cur));
return TRUE;
}
break;
}
default:
break;
}
if (imguiConsumed)
return 0;
return DefWindowProcW(hwnd, msg, wParam, lParam);
}