Mercurial > games > semicongine
comparison semiconginev2/platform/windows/window.nim @ 1218:56781cc0fc7c compiletime-tests
did: renamge main package
author | sam <sam@basx.dev> |
---|---|
date | Wed, 17 Jul 2024 21:01:37 +0700 |
parents | semicongine/platform/windows/window.nim@804ff842d377 |
children |
comparison
equal
deleted
inserted
replaced
1217:f819a874058f | 1218:56781cc0fc7c |
---|---|
1 import ../../thirdparty/winim/winim/inc/[windef, winuser, wincon, winbase] | |
2 import ../../thirdparty/winim/winim/[winstr, utils] | |
3 | |
4 include ./virtualkey_map | |
5 | |
6 type | |
7 NativeWindow* = object | |
8 hinstance*: HINSTANCE | |
9 hwnd*: HWND | |
10 g_wpPrev: WINDOWPLACEMENT | |
11 | |
12 | |
13 # sorry, have to use module-global variable to capture windows events | |
14 var currentEvents: seq[Event] | |
15 | |
16 template CheckWin32Result*(call: untyped) = | |
17 let value = call | |
18 if value == 0: | |
19 raise newException(Exception, "Win32 error: " & astToStr(call) & " returned " & $value) | |
20 | |
21 let | |
22 andCursorMask = [0xff] | |
23 xorCursorMask = [0x00] | |
24 invisibleCursor = CreateCursor(0, 0, 0, 1, 1, pointer(addr andCursorMask), pointer(addr xorCursorMask)) | |
25 defaultCursor = LoadCursor(0, IDC_ARROW) | |
26 var currentCursor = defaultCursor | |
27 | |
28 proc MapLeftRightKeys(key: INT, lparam: LPARAM): INT = | |
29 case key | |
30 of VK_SHIFT: | |
31 MapVirtualKey(UINT((lParam and 0x00ff0000) shr 16), MAPVK_VSC_TO_VK_EX) | |
32 of VK_CONTROL: | |
33 if (lParam and 0x01000000) == 0: VK_LCONTROL else: VK_RCONTROL | |
34 of VK_MENU: | |
35 if (lParam and 0x01000000) == 0: VK_LMENU else: VK_RMENU | |
36 else: | |
37 key | |
38 | |
39 proc WindowHandler(hwnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM): LRESULT {.stdcall.} = | |
40 case uMsg | |
41 of WM_DESTROY: | |
42 currentEvents.add(Event(eventType: EventType.Quit)) | |
43 of WM_KEYDOWN, WM_SYSKEYDOWN: | |
44 let key = MapLeftRightKeys(INT(wParam), lParam) | |
45 currentEvents.add(Event(eventType: KeyPressed, key: KeyTypeMap.getOrDefault(key, Key.UNKNOWN))) | |
46 of WM_KEYUP, WM_SYSKEYUP: | |
47 let key = MapLeftRightKeys(INT(wParam), lParam) | |
48 currentEvents.add(Event(eventType: KeyReleased, key: KeyTypeMap.getOrDefault(key, Key.UNKNOWN))) | |
49 of WM_LBUTTONDOWN: | |
50 currentEvents.add(Event(eventType: MousePressed, button: MouseButton.Mouse1)) | |
51 of WM_LBUTTONUP: | |
52 currentEvents.add(Event(eventType: MouseReleased, button: MouseButton.Mouse1)) | |
53 of WM_MBUTTONDOWN: | |
54 currentEvents.add(Event(eventType: MousePressed, button: MouseButton.Mouse2)) | |
55 of WM_MBUTTONUP: | |
56 currentEvents.add(Event(eventType: MouseReleased, button: MouseButton.Mouse2)) | |
57 of WM_RBUTTONDOWN: | |
58 currentEvents.add(Event(eventType: MousePressed, button: MouseButton.Mouse3)) | |
59 of WM_RBUTTONUP: | |
60 currentEvents.add(Event(eventType: MouseReleased, button: MouseButton.Mouse3)) | |
61 of WM_MOUSEMOVE: | |
62 currentEvents.add(Event(eventType: MouseMoved, x: GET_X_LPARAM(lParam), y: GET_Y_LPARAM(lParam))) | |
63 of WM_MOUSEWHEEL: | |
64 currentEvents.add(Event(eventType: MouseWheel, amount: float32(GET_WHEEL_DELTA_WPARAM(wParam)) / WHEEL_DELTA)) | |
65 of WM_SIZING: | |
66 currentEvents.add(Event(eventType: ResizedWindow)) | |
67 of WM_SIZE: | |
68 if wParam == SIZE_MINIMIZED: | |
69 currentEvents.add(Event(eventType: MinimizedWindow)) | |
70 elif wParam == SIZE_RESTORED: | |
71 currentEvents.add(Event(eventType: RestoredWindow)) | |
72 of WM_SETCURSOR: | |
73 if LOWORD(lParam) == HTCLIENT: | |
74 SetCursor(currentCursor) | |
75 return 1 | |
76 else: | |
77 return DefWindowProc(hwnd, uMsg, wParam, lParam) | |
78 else: | |
79 return DefWindowProc(hwnd, uMsg, wParam, lParam) | |
80 | |
81 | |
82 proc CreateWindow*(title: string): NativeWindow = | |
83 when DEBUG: | |
84 AllocConsole() | |
85 discard stdin.reopen("conIN$", fmRead) | |
86 discard stdout.reopen("conOUT$", fmWrite) | |
87 discard stderr.reopen("conOUT$", fmWrite) | |
88 | |
89 result.hInstance = HINSTANCE(GetModuleHandle(nil)) | |
90 var | |
91 windowClassName = T"EngineWindowClass" | |
92 windowName = T(title) | |
93 windowClass = WNDCLASSEX( | |
94 cbSize: UINT(WNDCLASSEX.sizeof), | |
95 lpfnWndProc: WindowHandler, | |
96 hInstance: result.hInstance, | |
97 lpszClassName: windowClassName, | |
98 hcursor: currentCursor, | |
99 ) | |
100 | |
101 if(RegisterClassEx(addr(windowClass)) == 0): | |
102 raise newException(Exception, "Unable to register window class") | |
103 | |
104 result.hwnd = CreateWindowEx( | |
105 DWORD(0), | |
106 windowClassName, | |
107 windowName, | |
108 DWORD(WS_OVERLAPPEDWINDOW), | |
109 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, | |
110 HMENU(0), | |
111 HINSTANCE(0), | |
112 result.hInstance, | |
113 nil | |
114 ) | |
115 | |
116 result.g_wpPrev.length = UINT(sizeof(WINDOWPLACEMENT)) | |
117 discard result.hwnd.ShowWindow(SW_SHOW) | |
118 | |
119 proc SetTitle*(window: NativeWindow, title: string) = | |
120 window.hwnd.SetWindowText(T(title)) | |
121 | |
122 # inspired by the one and only, Raymond Chen | |
123 # https://devblogs.microsoft.com/oldnewthing/20100412-00/?p=14353 | |
124 proc Fullscreen*(window: var NativeWindow, enable: bool) = | |
125 let dwStyle: DWORD = GetWindowLong(window.hwnd, GWL_STYLE) | |
126 if enable: | |
127 var mi = MONITORINFO(cbSize: DWORD(sizeof(MONITORINFO))) | |
128 if GetWindowPlacement(window.hwnd, addr window.g_wpPrev) and GetMonitorInfo(MonitorFromWindow(window.hwnd, MONITOR_DEFAULTTOPRIMARY), addr mi): | |
129 SetWindowLong(window.hwnd, GWL_STYLE, dwStyle and (not WS_OVERLAPPEDWINDOW)) | |
130 SetWindowPos(window.hwnd, HWND_TOP, mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right - mi.rcMonitor.left, mi.rcMonitor.bottom - mi.rcMonitor.top, SWP_NOOWNERZORDER or SWP_FRAMECHANGED) | |
131 else: | |
132 SetWindowLong(window.hwnd, GWL_STYLE, dwStyle or WS_OVERLAPPEDWINDOW) | |
133 SetWindowPlacement(window.hwnd, addr window.g_wpPrev) | |
134 SetWindowPos(window.hwnd, HWND(0), 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE or SWP_NOZORDER or SWP_NOOWNERZORDER or SWP_FRAMECHANGED) | |
135 | |
136 proc HideSystemCursor*(window: NativeWindow) = | |
137 currentCursor = invisibleCursor | |
138 SetCursor(currentCursor) | |
139 | |
140 proc ShowSystemCursor*(window: NativeWindow) = | |
141 currentCursor = defaultCursor | |
142 SetCursor(currentCursor) | |
143 | |
144 proc Destroy*(window: NativeWindow) = | |
145 discard | |
146 | |
147 proc Size*(window: NativeWindow): (int, int) = | |
148 var rect: RECT | |
149 CheckWin32Result GetWindowRect(window.hwnd, addr(rect)) | |
150 (int(rect.right - rect.left), int(rect.bottom - rect.top)) | |
151 | |
152 proc PendingEvents*(window: NativeWindow): seq[Event] = | |
153 # empty queue | |
154 currentEvents = newSeq[Event]() | |
155 var msg: MSG | |
156 # fill queue | |
157 while PeekMessage(addr(msg), window.hwnd, 0, 0, PM_REMOVE): | |
158 TranslateMessage(addr(msg)) | |
159 DispatchMessage(addr(msg)) | |
160 return currentEvents | |
161 | |
162 proc GetMousePosition*(window: NativeWindow): Option[Vec2f] = | |
163 var p: POINT | |
164 let res = GetCursorPos(addr(p)) | |
165 if res: | |
166 return some(Vec2f([float32(p.x), float32(p.y)])) | |
167 return none(Vec2f) |