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