-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRWnd.cpp
More file actions
394 lines (381 loc) · 12.6 KB
/
Copy pathRWnd.cpp
File metadata and controls
394 lines (381 loc) · 12.6 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
#include "pch.h"
#include "RWnd.h"
namespace RA {
ShiftState ToShiftState(WPARAM wParam)
{
ShiftState res;
res.ctrl = wParam & MK_CONTROL;
res.shift = wParam & MK_SHIFT;
res.mouse_btn[0] = wParam & MK_LBUTTON;
res.mouse_btn[1] = wParam & MK_RBUTTON;
res.mouse_btn[2] = wParam & MK_MBUTTON;
res.mouse_btn[3] = wParam & MK_XBUTTON1;
res.mouse_btn[4] = wParam & MK_XBUTTON2;
return res;
}
LRESULT CALLBACK DefWndProc(
_In_ HWND hwnd,
_In_ UINT uMsg,
_In_ WPARAM wParam,
_In_ LPARAM lParam
) {
if (uMsg == WM_NCCREATE) {
SetPropW(hwnd, L"WndPtr", ((tagCREATESTRUCTW*)lParam)->lpCreateParams);
return true;
}
Window* w = (Window*)GetPropW(hwnd, L"WndPtr");
if (w) {
return w->WndProc(hwnd, uMsg, wParam, lParam);
}
else {
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
}
LRESULT Window::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
auto toCoord = [](LPARAM lParam)->glm::ivec2 {
glm::ivec2 p;
p.x = int(lParam & 0xffff);
p.y = int(lParam >> 16);
return p;
};
switch (uMsg) {
case WM_CLOSE:
DestroyWindow(m_hWnd);
m_hWnd = 0;
return 0;
case WM_DESTROY:
if (m_isMainWindow)
PostQuitMessage(0);
return 0;
case WM_LBUTTONDOWN:
MouseDown(0, toCoord(lParam), ToShiftState(wParam));
return 0;
case WM_RBUTTONDOWN:
MouseDown(1, toCoord(lParam), ToShiftState(wParam));
return 0;
case WM_MBUTTONDOWN:
MouseDown(2, toCoord(lParam), ToShiftState(wParam));
return 0;
case WM_XBUTTONDOWN:
MouseDown(int(wParam >> 16) + 2, toCoord(lParam), ToShiftState(wParam));
return 0;
case WM_LBUTTONUP:
MouseUp(0, toCoord(lParam), ToShiftState(wParam));
return 0;
case WM_RBUTTONUP:
MouseUp(1, toCoord(lParam), ToShiftState(wParam));
return 0;
case WM_MBUTTONUP:
MouseUp(2, toCoord(lParam), ToShiftState(wParam));
return 0;
case WM_XBUTTONUP:
MouseUp(int(wParam >> 16) + 2, toCoord(lParam), ToShiftState(wParam));
return 0;
case WM_MOUSEMOVE:
MouseMove(toCoord(lParam), ToShiftState(wParam));
return 0;
case WM_MOUSEWHEEL:
glm::ivec2 crd = toCoord(lParam);
POINT pt;
pt.x = crd.x;
pt.y = crd.y;
ScreenToClient(hwnd, &pt);
crd.x = pt.x;
crd.y = pt.y;
MouseWheel(crd, GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA, ToShiftState(wParam));
return 0;
case WM_LBUTTONDBLCLK:
MouseDblClick(0, toCoord(lParam), ToShiftState(wParam));
return 0;
case WM_RBUTTONDBLCLK:
MouseDblClick(1, toCoord(lParam), ToShiftState(wParam));
return 0;
case WM_MBUTTONDBLCLK:
MouseDblClick(2, toCoord(lParam), ToShiftState(wParam));
return 0;
case WM_XBUTTONDBLCLK:
MouseDblClick(int(wParam >> 16) + 2, toCoord(lParam), ToShiftState(wParam));
return 0;
case WM_KEYDOWN:
KeyDown(uint32_t(wParam), (lParam & (1 << 30)) != 0);
return 0;
case WM_KEYUP:
KeyUp(uint32_t(wParam), (lParam & (1 << 30)) != 0);
return 0;
case WM_PAINT: {
bool processed = false;
Paint(&processed);
if (processed) {
ValidateRect(hwnd, nullptr);
return 0;
}
break;
}
case WM_SIZE:
UINT width = LOWORD(lParam);
UINT height = HIWORD(lParam);
WindowResized(glm::ivec2(width, height));
return 0;
}
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
void Window::SetCaption(const std::wstring& txt)
{
SetWindowText(m_hWnd, txt.c_str());
}
Window::Window(std::wstring class_name, std::wstring wnd_caption, bool isMainWindow) {
m_isMainWindow = isMainWindow;
m_class_name = class_name;
WNDCLASSEXW wcex = { };
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wcex.lpfnWndProc = DefWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = GetModuleHandle(nullptr);
wcex.hIcon = LoadIcon(GetModuleHandle(nullptr), IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = 0;// (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = m_class_name.c_str();
wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
m_wndclass = RegisterClassExW(&wcex);
m_hWnd = CreateWindowExW(
WS_EX_OVERLAPPEDWINDOW | WS_EX_APPWINDOW,
m_class_name.c_str(),
wnd_caption.c_str(),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
0,
0,
GetModuleHandle(nullptr),
this
);
}
Window::~Window()
{
if (m_hWnd) DestroyWindow(m_hWnd);
UnregisterClassW(m_class_name.c_str(), GetModuleHandle(nullptr));
}
HWND Window::Handle() const
{
return m_hWnd;
}
void Window::Invalidate()
{
InvalidateRect(m_hWnd, nullptr, false);
}
void Window::MouseMove(const glm::ivec2& crd, const ShiftState& ss)
{
}
void Window::MouseDown(int btn, const glm::ivec2& crd, const ShiftState& ss)
{
}
void Window::MouseUp(int btn, const glm::ivec2& crd, const ShiftState& ss)
{
}
void Window::MouseWheel(const glm::ivec2& crd, int delta, const ShiftState& ss)
{
}
void Window::MouseDblClick(int btn, const glm::ivec2& crd, const ShiftState& ss)
{
}
void Window::KeyDown(uint32_t vKey, bool duplicate)
{
}
void Window::KeyUp(uint32_t vKey, bool duplicate)
{
}
void Window::Paint(bool* processed)
{
}
void Window::WindowResized(const glm::ivec2& new_size)
{
}
const DevicePtr& RenderWindow::GetDevice() const
{
return m_device;
}
RenderWindow::RenderWindow(std::wstring caption, bool isMainWindow, bool sRGB) : Window(L"RenderWndClass", caption, isMainWindow)
{
m_device = std::make_shared<Device>(Handle(), sRGB);
}
void RenderWindow::RenderScene()
{
}
void RenderWindow::Paint(bool* processed)
{
*processed = true;
RECT rct;
GetClientRect(Handle(), &rct);
if (rct.right - rct.left <= 0) return;
if (rct.bottom - rct.top <= 0) return;
m_device->BeginFrame();
m_device->States()->SetBlend(true, RA::Blend::one, RA::Blend::inv_src_alpha);
RenderScene();
m_device->PresentToWnd();
}
void MessageLoop(const std::function<void()> idle_proc)
{
MSG msg;
while (true) {
while (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) return;
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
idle_proc();
}
}
void MessageLoop()
{
MSG msg;
while (GetMessageW(&msg, 0, 0, 0)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
void UIRenderWindow::ControlsDraw_After()
{
}
void UIRenderWindow::ControlsDraw_Before()
{
}
void UIRenderWindow::UpdateDPIScale()
{
if (!m_control_global) return;
float dpi = GetDPIScale();
if (dpi != m_last_dpi_scale) {
m_last_dpi_scale = dpi;
m_control_global->SetDPIScale(m_last_dpi_scale);
m_control_global->Root()->SetSize(m_control_global->Root()->Size() * m_control_global->GetDPIScale());
}
}
float UIRenderWindow::GetDPIScale() const
{
return 1.0f;
}
void UIRenderWindow::MouseMove(const glm::ivec2& crd, const ShiftState& ss)
{
m_control_global->Process_MouseMove(crd, ss);
}
void UIRenderWindow::MouseDown(int btn, const glm::ivec2& crd, const ShiftState& ss)
{
m_control_global->Process_MouseDown(btn, crd, ss);
}
void UIRenderWindow::MouseUp(int btn, const glm::ivec2& crd, const ShiftState& ss)
{
m_control_global->Process_MouseUp(btn, crd, ss);
}
void UIRenderWindow::MouseWheel(const glm::ivec2& crd, int delta, const ShiftState& ss)
{
m_control_global->Process_MouseWheel(crd, delta, ss);
}
void UIRenderWindow::MouseDblClick(int btn, const glm::ivec2& crd, const ShiftState& ss)
{
m_control_global->Process_MouseDblClick(btn, crd, ss);
}
void UIRenderWindow::KeyDown(uint32_t vKey, bool duplicate)
{
m_control_global->Process_KeyDown(vKey, duplicate);
}
void UIRenderWindow::KeyUp(uint32_t vKey, bool duplicate)
{
m_control_global->Process_KeyUp(vKey, duplicate);
}
void UIRenderWindow::WindowResized(const glm::ivec2& new_size)
{
m_control_global->Root()->SetSize(glm::vec2(new_size) * m_control_global->GetDPIScale());
}
void UIRenderWindow::RenderScene()
{
RenderWindow::RenderScene();
m_control_global->SetDPIScale(GetDPIScale());
RECT rct;
GetClientRect(m_device->Window(), &rct);
if ((rct.right - rct.left) * (rct.bottom - rct.top) == 0) return;
m_ui_camera->UpdateFromWnd();
m_fbo->SetSizeFromWindow();
m_device->SetFrameBuffer(m_fbo);
m_fbo->Clear(0, { 0,0,0,0 });
ControlsDraw_Before();
m_control_global->Draw(m_ui_camera.get());
ControlsDraw_After();
m_fbo->BlitToDefaultFBO(0);
}
LRESULT UIRenderWindow::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (m_control_global)
m_control_global->Process_XInput();
UpdateDPIScale();
return RenderWindow::WndProc(hwnd, uMsg, wParam, lParam);
}
UIRenderWindow::UIRenderWindow(std::wstring caption, bool isMainWindow, bool sRGB) : RenderWindow(caption, isMainWindow, sRGB)
{
m_canvas_common = std::make_shared<CanvasCommonObject>(m_device);
m_control_global = std::make_unique<ControlGlobal>(m_canvas_common);
m_control_global->SetDPIScale(GetDPIScale());
RECT rct;
GetClientRect(Handle(), &rct);
m_control_global->Root()->SetSize(glm::vec2(rct.right, rct.bottom) * m_control_global->GetDPIScale());
m_ui_camera = std::make_unique<UICamera>(m_device);
m_fbo = FBB(m_device)->Color(sRGB ? RA::TextureFmt::RGBA8_SRGB : RA::TextureFmt::RGBA8)->Finish();
}
bool FPSCursor::UpdateAbsWindowPos()
{
RECT rct;
GetClientRect(m_wnd.Handle(), &rct);
POINT rct_min{ rct.left, rct.top };
POINT rct_max{ rct.right, rct.bottom };
ClientToScreen(m_wnd.Handle(), &rct_min);
ClientToScreen(m_wnd.Handle(), &rct_max);
glm::ivec4 prev_rect = m_wnd_rect;
m_wnd_rect = glm::ivec4(rct_min.x, rct_min.y, rct_max.x, rct_max.y);
return m_wnd_rect != prev_rect;
}
void FPSCursor::CaptureMouse()
{
if (m_enabled) {
RECT abs_rct{ m_wnd_rect.x, m_wnd_rect.y, m_wnd_rect.z, m_wnd_rect.w };
ClipCursor(&abs_rct);
SetCursorToCenter();
}
else {
ClipCursor(nullptr);
}
}
void FPSCursor::SetCursorToCenter()
{
SetCursorPos((m_wnd_rect.x + m_wnd_rect.z) / 2, (m_wnd_rect.y + m_wnd_rect.w) / 2);
}
bool FPSCursor::Enabled() const
{
return m_enabled;
}
void FPSCursor::SetEnabled(bool enabled)
{
if (m_enabled == enabled) return;
m_enabled = enabled;
UpdateAbsWindowPos();
CaptureMouse();
}
glm::ivec2 FPSCursor::ExtractMouseDelta()
{
if (!m_enabled) return { 0, 0 };
POINT cur_pt;
glm::ivec2 last_pt{ (m_wnd_rect.x + m_wnd_rect.z) / 2, (m_wnd_rect.y + m_wnd_rect.w) / 2 };
GetCursorPos(&cur_pt);
glm::ivec2 delta = glm::ivec2(cur_pt.x, cur_pt.y) - last_pt;
if (UpdateAbsWindowPos())
CaptureMouse();
SetCursorToCenter();
return delta;
}
FPSCursor::FPSCursor(const Window& wnd) : m_wnd(wnd)
{
}
}