comparison src/zamikongine/platform/windows/win32.nim @ 38:c3c963e7c1a6

did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
author Sam <sam@basx.dev>
date Wed, 18 Jan 2023 09:52:03 +0700
parents a3d46f434616
children
comparison
equal deleted inserted replaced
37:6859bcfabc62 38:c3c963e7c1a6
1 import winim 1 import winim
2 2
3 import ./virtualkey_map
3 import ../../events 4 import ../../events
4 5
5 type 6 type
6 NativeWindow* = object 7 NativeWindow* = object
7 hinstance*: HINSTANCE 8 hinstance*: HINSTANCE
8 hwnd*: HWND 9 hwnd*: HWND
9 10
11 # sorry, have to use module-global variable to capture windows events
10 var currentEvents: seq[Event] 12 var currentEvents: seq[Event]
11 13
12 template checkWin32Result*(call: untyped) = 14 template checkWin32Result*(call: untyped) =
13 let value = call 15 let value = call
14 if value != 0: 16 if value != 0:
15 raise newException(Exception, "Win32 error: " & astToStr(call) & " returned " & $value) 17 raise newException(Exception, "Win32 error: " & astToStr(call) & " returned " & $value)
16 18
19
20 proc MapLeftRightKeys(key: INT, lparam: LPARAM): INT =
21 case key
22 of VK_SHIFT:
23 MapVirtualKey(UINT((lParam and 0x00ff0000) shr 16), MAPVK_VSC_TO_VK_EX)
24 of VK_CONTROL:
25 if (lParam and 0x01000000) == 0: VK_LCONTROL else: VK_RCONTROL
26 of VK_MENU:
27 if (lParam and 0x01000000) == 0: VK_LMENU else: VK_RMENU
28 else:
29 key
30
17 proc WindowHandler(hwnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM): LRESULT {.stdcall.} = 31 proc WindowHandler(hwnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM): LRESULT {.stdcall.} =
18 case uMsg 32 case uMsg
19 of WM_DESTROY: 33 of WM_DESTROY:
20 currentEvents.add(Event(eventType: events.EventType.Quit)) 34 currentEvents.add(Event(eventType: events.EventType.Quit))
35 of WM_KEYDOWN, WM_SYSKEYDOWN:
36 let key = MapLeftRightKeys(INT(wParam), lParam)
37 currentEvents.add(Event(eventType: KeyPressed, key: KeyTypeMap.getOrDefault(key, Key.UNKNOWN)))
38 of WM_KEYUP, WM_SYSKEYUP:
39 let key = MapLeftRightKeys(INT(wParam), lParam)
40 currentEvents.add(Event(eventType: KeyReleased, key: KeyTypeMap.getOrDefault(key, Key.UNKNOWN)))
41 of WM_LBUTTONDOWN:
42 currentEvents.add(Event(eventType: MousePressed, button: MouseButton.Mouse1))
43 of WM_LBUTTONUP:
44 currentEvents.add(Event(eventType: MouseReleased, button: MouseButton.Mouse1))
45 of WM_MBUTTONDOWN:
46 currentEvents.add(Event(eventType: MousePressed, button: MouseButton.Mouse2))
47 of WM_MBUTTONUP:
48 currentEvents.add(Event(eventType: MouseReleased, button: MouseButton.Mouse2))
49 of WM_RBUTTONDOWN:
50 currentEvents.add(Event(eventType: MousePressed, button: MouseButton.Mouse3))
51 of WM_RBUTTONUP:
52 currentEvents.add(Event(eventType: MouseReleased, button: MouseButton.Mouse3))
53 of WM_MOUSEMOVE:
54 currentEvents.add(Event(eventType: events.MouseMoved, x: GET_X_LPARAM(lParam), y: GET_Y_LPARAM(lParam)))
21 else: 55 else:
22 return DefWindowProc(hwnd, uMsg, wParam, lParam) 56 return DefWindowProc(hwnd, uMsg, wParam, lParam)
23 57
24 58
25 proc createWindow*(title: string): NativeWindow = 59 proc createWindow*(title: string): NativeWindow =
47 HINSTANCE(0), 81 HINSTANCE(0),
48 result.hInstance, 82 result.hInstance,
49 nil 83 nil
50 ) 84 )
51 85
52 discard ShowWindow(result.hwnd, 1) 86 discard ShowWindow(result.hwnd, SW_SHOW)
87 discard ShowCursor(false)
53 88
54 proc trash*(window: NativeWindow) = 89 proc trash*(window: NativeWindow) =
55 discard 90 discard
56 91
57 proc size*(window: NativeWindow): (int, int) = 92 proc size*(window: NativeWindow): (int, int) =
58 var rect: RECT 93 var rect: RECT
59 checkWin32Result GetWindowRect(window.hwnd, addr(rect)) 94 checkWin32Result GetWindowRect(window.hwnd, addr(rect))
60 (int(rect.right - rect.left), int(rect.bottom - rect.top)) 95 (int(rect.right - rect.left), int(rect.bottom - rect.top))
61 96
62 proc pendingEvents*(window: NativeWindow): seq[Event] = 97 proc pendingEvents*(window: NativeWindow): seq[Event] =
98 # empty queue
63 currentEvents = newSeq[Event]() 99 currentEvents = newSeq[Event]()
64 var msg: MSG 100 var msg: MSG
101 # fill queue
65 while PeekMessage(addr(msg), window.hwnd, 0, 0, PM_REMOVE): 102 while PeekMessage(addr(msg), window.hwnd, 0, 0, PM_REMOVE):
66 DispatchMessage(addr(msg)) 103 DispatchMessage(addr(msg))
67 return currentEvents 104 return currentEvents