-
Notifications
You must be signed in to change notification settings - Fork 2
/
window.d
560 lines (467 loc) · 18.8 KB
/
window.d
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Devisualization (Richard Andrew Cattermole)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
module devisualization.window.window;
import devisualization.window.interfaces.window;
public import devisualization.window.interfaces.window : WindowConfig, Windowable;
public import devisualization.window.interfaces.events : MouseButtons, Keys, KeyModifiers;
public import devisualization.window.interfaces.context : WindowContextType, IContext;
public import devisualization.window.context;
import devisualization.window.interfaces.eventable;
import std.conv : to;
class Window : Windowable {
private {
HWND hwnd_;
HICON previousIcon_;
wstring lastTitle;
bool hasBeenClosed_;
WindowConfig config_;
IContext context_ = null;
}
this(T...)(T config) { this(WindowConfig(config)); }
this(WindowConfig config) {
config_ = config;
title = config.title;
Window this_ = this;
hwnd_ = createWindow(config.x, config.y, config.width, config.height, &windowHandler, &this_);
}
static {
void messageLoop() {
import core.thread : Thread;
import core.time : dur;
while(true) {
while(Window.messageLoopIteration()) {}
Thread.sleep(dur!"msecs"(50));
}
}
bool messageLoopIteration(uint minBlocking = 0, uint maxNonBlocking = 1) {
MSG msg;
uint i;
while (i < minBlocking) {
int ret = GetMessageW(&msg, null, 0, 0);
TranslateMessage(&msg);
DispatchMessageW(&msg);
i++;
}
i = 0;
while (i < maxNonBlocking) {
int ret = PeekMessageW(&msg, null, 0, 0, PM_REMOVE);
if (ret == 0) {
return false;
} else {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
i++;
}
return true;
}
}
@property {
HWND hwnd()
in {
assert(!hasBeenClosed_);
} body {
return hwnd_;
}
void title(string value)
in {
assert(!hasBeenClosed_);
} body {
title(to!wstring(value));
}
void title(dstring value)
in {
assert(!hasBeenClosed_);
} body {
title(to!wstring(value));
}
void title(wstring value)
in {
assert(!hasBeenClosed_);
} body {
if (value != ""w && value[$-1] != '\0')
value ~= '\0';
else if (value == ""w)
value = "\0"w;
lastTitle = value;
SetWindowTextW(hwnd_, cast(ushort*)lastTitle.ptr);
}
void size(uint width, uint height)
in {
assert(!hasBeenClosed_);
} body {
// calculates correct width/height size of window
RECT rect;
rect.top = 0;
rect.left = 0;
rect.right = width;
rect.bottom = height;
auto style = GetWindowLongW(hwnd_, GWL_STYLE);
if (style == dwFullscreen) {
SetWindowLongW(hwnd_, GWL_STYLE, dwStyle);
SetWindowLongW(hwnd_, GWL_EXSTYLE, dwExStyle);
}
AdjustWindowRectEx(&rect, dwStyle, false, dwExStyle);
width = rect.right;
height = rect.bottom;
// calculates correct width/height size of window
SetWindowPos(hwnd_, null, 0, 0, width, height, SWP_NOMOVE | SWP_NOOWNERZORDER);
}
void move(int x, int y)
in {
assert(!hasBeenClosed_);
} body {
SetWindowPos(hwnd_, null, x, y, 0, 0, SWP_NOSIZE | SWP_NOOWNERZORDER);
}
void canResize(bool can = true)
in {
assert(!hasBeenClosed_);
} body {
auto style = GetWindowLongW(hwnd_, GWL_STYLE);
if (can)
style |= WS_SIZEBOX | WS_MAXIMIZEBOX;
else
style &= ~(WS_SIZEBOX | WS_MAXIMIZEBOX);
SetWindowLongW(hwnd_, GWL_STYLE, style);
}
void fullscreen(bool isfs = true)
in {
assert(!hasBeenClosed_);
} body {
if (isfs) {
SetWindowLongW(hwnd_, GWL_STYLE, dwFullscreen);
SetWindowLongW(hwnd_, GWL_EXSTYLE, dwExFullscreen);
int cx = GetSystemMetrics(SM_CXSCREEN);
int cy = GetSystemMetrics(SM_CYSCREEN);
SetWindowPos(hwnd_, HWND_TOP, 0, 0, cx, cy, SWP_SHOWWINDOW);
} else {
canResize = true;
}
}
void close()
in {
assert(!hasBeenClosed_);
} body {
DestroyWindow(hwnd_);
CloseWindow(hwnd_);
hasBeenClosed_ = true;
}
IContext context()
in {
assert(!hasBeenClosed_);
} body {
return context_;
}
}
override {
void show()
in {
assert(!hasBeenClosed_);
} body {
ShowWindow(hwnd_, SW_SHOW);
}
void hide()
in {
assert(!hasBeenClosed_);
} body {
ShowWindow(hwnd_, SW_HIDE);
}
void icon(Image image)
in {
assert(!hasBeenClosed_);
assert(image !is null);
} body {
ubyte[4][] data;
foreach(pixel; image.rgba.allPixels) {
data ~= [pixel.b_ubyte, pixel.g_ubyte, pixel.r_ubyte, pixel.a_ubyte];
}
HBITMAP bitmap = CreateBitmap(cast(uint)image.width, cast(uint)image.height, 1, 32, data.ptr);
HBITMAP hbmMask = CreateCompatibleBitmap(GetDC(hwnd_), cast(uint)image.width, cast(uint)image.height);
ICONINFO ii;
ii.fIcon = TRUE;
ii.hbmColor = bitmap;
ii.hbmMask = hbmMask;
HICON hIcon = CreateIconIndirect(&ii);
DeleteObject(hbmMask);
if (hIcon) {
SendMessageW(hwnd_, WM_SETICON, cast(WPARAM)ICON_BIG, cast(LPARAM)hIcon);
SendMessageW(hwnd_, WM_SETICON, cast(WPARAM)ICON_SMALL, cast(LPARAM)hIcon);
}
}
deprecated("Use Devisualization.Image method instead")
void icon(ushort width, ushort height, ubyte[3][] idata, ubyte[3]* transparent = null)
in {
assert(!hasBeenClosed_);
assert(width * height == data.length, "Icon pixels length must be equal to width * height");
} body {
ubyte[4][] data;
foreach(v; idata) {
data ~= [v[2], v[1], v[0], 0];
}
HBITMAP bitmap = CreateBitmap(width, height, 1, 32, data.ptr);
HBITMAP hbmMask;
if (transparent is null)
hbmMask = CreateCompatibleBitmap(GetDC(hwnd_), width, height);
else {
COLORREF crTransparent = RGB((*transparent)[0], (*transparent)[1], (*transparent)[2]);
HDC hdcMem, hdcMem2;
BITMAP bm;
GetObjectA(bitmap, BITMAP.sizeof, &bm);
hbmMask = CreateBitmap(width, height, 1, 1, null);
hdcMem = CreateCompatibleDC(GetDC(hwnd_));
hdcMem2 = CreateCompatibleDC(GetDC(hwnd_));
SelectObject(hdcMem, bitmap);
SelectObject(hdcMem2, hbmMask);
SetBkColor(hdcMem, crTransparent);
BitBlt(hdcMem2, 0, 0, width, height, hdcMem, 0, 0, SRCCOPY);
BitBlt(hdcMem, 0, 0, width, height, hdcMem2, 0, 0, SRCINVERT);
DeleteDC(hdcMem);
DeleteDC(hdcMem2);
}
ICONINFO ii;
ii.fIcon = TRUE;
ii.hbmColor = bitmap;
ii.hbmMask = hbmMask;
HICON hIcon = CreateIconIndirect(&ii);
DeleteObject(hbmMask);
if (hIcon) {
SendMessageW(hwnd_, WM_SETICON, cast(WPARAM)ICON_BIG, cast(LPARAM)hIcon);
SendMessageW(hwnd_, WM_SETICON, cast(WPARAM)ICON_SMALL, cast(LPARAM)hIcon);
}
}
bool hasBeenClosed() { return hasBeenClosed_; }
}
mixin Eventing!("onDraw", Windowable);
mixin Eventing!("onMove", Windowable, int, int);
mixin Eventing!("onResize", Windowable, uint, uint);
mixin Eventing!("onClose", Windowable);
mixin Eventing!("onMouseDown", Windowable, MouseButtons, int, int);
mixin Eventing!("onMouseMove", Windowable, int, int);
mixin Eventing!("onMouseUp", Windowable, MouseButtons);
mixin Eventing!("onKeyDown", Windowable, Keys, KeyModifiers);
mixin Eventing!("onKeyUp", Windowable, Keys, KeyModifiers);
}
private {
import windows;
enum DWORD dwExStyle = 0;
enum DWORD dwStyle = WS_OVERLAPPEDWINDOW;
enum wstring WindowClassName = "Devisualization.Window window\0"w;
enum DWORD dwFullscreen = WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
enum DWORD dwExFullscreen = WS_EX_APPWINDOW | WS_EX_TOPMOST;
// wasn't in windows bindings woops
enum GCL_HICON = -14;
enum SRCCOPY = 0xCC0020;
enum SRCINVERT = 0x660046;
COLORREF RGB(ubyte r, ubyte g, ubyte b) {
r = r & 0xFF;
g = g & 0xFF;
b = b & 0xFF;
return cast(COLORREF)((b << 16) | (g << 8) | r);
}
// more actual code
HWND createWindow(int x, int y, uint width, uint height, WindowProc wProc, Window* windowPtr) {
static HINSTANCE hInstance;
if (hInstance is HINSTANCE.init)
hInstance = GetModuleHandleA(null);
WNDCLASSW wc;
wc.lpfnWndProc = wProc;
wc.hInstance = hInstance;
wc.lpszClassName = cast(ushort*)WindowClassName.ptr;
wc.hCursor = LoadCursorW(null, cast(ushort*)IDC_ARROW);
wc.style |= CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
if (!RegisterClassW(&wc))
throw new WindowNotCreatable();
// calculates correct width/height size of window
RECT rect;
rect.top = 0;
rect.left = 0;
rect.right = width;
rect.bottom = height;
AdjustWindowRectEx(&rect, dwStyle, false, dwExStyle);
width = rect.right;
height = rect.bottom;
// calculates correct width/height size of window
HWND hwnd = CreateWindowExW(
dwExStyle,
cast(ushort*)WindowClassName.ptr,
null,
dwStyle,
x, y,
width, height,
null,
null,
hInstance,
windowPtr);
if (hwnd is null)
throw new WindowNotCreatable();
InvalidateRgn(hwnd, null, true);
return hwnd;
}
extern(Windows) {
alias WindowProc = LRESULT function(HWND hwnd, uint uMsg, WPARAM wParam, LPARAM lParam);
LRESULT windowHandler(HWND hwnd, uint uMsg, WPARAM wParam, LPARAM lParam) {
/*
* Handles creational arguments aka the current window
*/
Window window;
switch(uMsg) {
case WM_CREATE:
CREATESTRUCTW pCreate = *cast(CREATESTRUCTW*)lParam;
void* pState = pCreate.lpCreateParams;
version(X86_64) {
SetWindowLongPtrW(hwnd, GWLP_USERDATA, *cast(ulong*)pState);
} else {
SetWindowLongW(hwnd, GWLP_USERDATA, *cast(uint*)pState);
}
return cast(LRESULT)0;
default:
version(X86_64) {
window = cast(Window)cast(ulong*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
} else {
window = cast(Window)cast(uint*)GetWindowLongW(hwnd, GWLP_USERDATA);
}
}
/*
* Normal flow handling
*/
switch(uMsg) {
case WM_DESTROY:
return cast(LRESULT)0;
case WM_PAINT:
if (window.context_ is null) {
if ((window.config_.contextType | WindowContextType.Opengl3Plus) == WindowContextType.Opengl3Plus || (window.config_.contextType | WindowContextType.OpenglLegacy) == WindowContextType.OpenglLegacy) {
window.context_ = new OpenglContext(window, window.config_);
} else if (window.config_.contextType == WindowContextType.Direct3D) {
version(Have_directx_d) {
window.context_ = new Direct3dContext(window, window.config_);
}
} else if (window.config_.contextType == WindowContextType.Buffer2D) {
window.context_ = new Buffer2DContext(window, window.config_);
}
}
window.onDraw();
return cast(LRESULT)0;
case WM_SIZE:
window.onResize(LOWORD(lParam), HIWORD(lParam));
return cast(LRESULT)0;
case WM_MOVE:
window.onMove(LOWORD(lParam), HIWORD(lParam));
return cast(LRESULT)0;
case WM_LBUTTONDOWN:
window.onMouseDown(MouseButtons.Left, LOWORD(lParam), HIWORD(lParam));
return cast(LRESULT)0;
case WM_MBUTTONDOWN:
window.onMouseDown(MouseButtons.Middle, LOWORD(lParam), HIWORD(lParam));
return cast(LRESULT)0;
case WM_RBUTTONDOWN:
window.onMouseDown(MouseButtons.Right, LOWORD(lParam), HIWORD(lParam));
return cast(LRESULT)0;
case WM_MOUSEMOVE:
window.onMouseMove(LOWORD(lParam), HIWORD(lParam));
return cast(LRESULT)0;
case WM_LBUTTONUP:
window.onMouseUp(MouseButtons.Left);
return cast(LRESULT)0;
case WM_MBUTTONUP:
window.onMouseUp(MouseButtons.Middle);
return cast(LRESULT)0;
case WM_RBUTTONUP:
window.onMouseUp(MouseButtons.Right);
return cast(LRESULT)0;
case WM_SYSKEYDOWN:
case WM_KEYDOWN:
window.onKeyDown(convertWinKeys(cast(uint)wParam), convertWinKeyModifiers());
return cast(LRESULT)0;
case WM_SYSKEYUP:
case WM_KEYUP:
window.onKeyUp(convertWinKeys(cast(uint)wParam), convertWinKeyModifiers());
return cast(LRESULT)0;
case WM_CLOSE:
window.onClose();
if (window.countOnClose() == 0)
window.close();
return cast(LRESULT)0;
default:
break;
}
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
}
Keys convertWinKeys(uint code) {
switch (code)
{
case VK_OEM_1: return Keys.Semicolon;
case VK_OEM_2: return Keys.Slash;
case VK_OEM_PLUS: return Keys.Equals;
case VK_OEM_MINUS: return Keys.Hyphen;
case VK_OEM_4: return Keys.LeftBracket;
case VK_OEM_6: return Keys.RightBracket;
case VK_OEM_COMMA: return Keys.Comma;
case VK_OEM_PERIOD: return Keys.Period;
case VK_OEM_7: return Keys.Quote;
case VK_OEM_5: return Keys.Backslash;
case VK_OEM_3: return Keys.Tilde;
case VK_ESCAPE: return Keys.Escape;
case VK_SPACE: return Keys.Space;
case VK_RETURN: return Keys.Enter;
case VK_BACK: return Keys.Backspace;
case VK_TAB: return Keys.Tab;
case VK_PRIOR: return Keys.PageUp;
case VK_NEXT: return Keys.PageDown;
case VK_END: return Keys.End;
case VK_HOME: return Keys.Home;
case VK_INSERT: return Keys.Insert;
case VK_DELETE: return Keys.Delete;
case VK_ADD: return Keys.Add;
case VK_SUBTRACT: return Keys.Subtract;
case VK_MULTIPLY: return Keys.Multiply;
case VK_DIVIDE: return Keys.Divide;
case VK_PAUSE: return Keys.Pause;
case VK_LEFT: return Keys.Left;
case VK_RIGHT: return Keys.Right;
case VK_UP: return Keys.Up;
case VK_DOWN: return Keys.Down;
default:
if (code >= VK_F1 && code <= VK_F12)
return cast(Keys)(Keys.F1 + code - VK_F1);
else if (code >= VK_NUMPAD0 && code <= VK_NUMPAD9)
return cast(Keys)(Keys.Numpad0 + code - VK_NUMPAD0);
else if (code >= 'A' && code <= 'Z')
return cast(Keys)(Keys.A + code - 'A');
else if (code >= '0' && code <= '9')
return cast(Keys)(Keys.Number0 + code - '0');
}
return Keys.Unknown;
}
KeyModifiers convertWinKeyModifiers() {
KeyModifiers ret;
if (HIWORD(GetKeyState(VK_MENU)) != 0)
ret |= KeyModifiers.Alt;
if (HIWORD(GetKeyState(VK_CONTROL)) != 0)
ret |= KeyModifiers.Control;
if (HIWORD(GetKeyState(VK_SHIFT)) != 0 || LOWORD(GetKeyState(VK_CAPITAL)) != 0)
ret |= KeyModifiers.Shift;
return ret;
}
}