Mercurial > games > semicongine
comparison semiconginev2/input.nim @ 1256:bfb75c934f4e
add: window focus handling, improve window api a bit
author | sam <sam@basx.dev> |
---|---|
date | Sun, 28 Jul 2024 17:33:41 +0700 |
parents | b0f4c8ccd49a |
children |
comparison
equal
deleted
inserted
replaced
1255:2b5ca798f6d6 | 1256:bfb75c934f4e |
---|---|
10 mouseMove: Vec2f | 10 mouseMove: Vec2f |
11 mouseWheel: float32 | 11 mouseWheel: float32 |
12 windowWasResized: bool = true | 12 windowWasResized: bool = true |
13 windowIsMinimized: bool = false | 13 windowIsMinimized: bool = false |
14 lockMouse: bool = false | 14 lockMouse: bool = false |
15 hasFocus: bool = false | |
15 | 16 |
16 # warning, shit is not thread safe | 17 # warning, shit is not thread safe |
17 var input: Input | 18 var input = Input() |
18 | 19 |
19 proc UpdateInputs*(): bool = | 20 proc UpdateInputs*(): bool = |
20 # reset input states | 21 # reset input states |
21 input.keyWasPressed = {} | 22 input.keyWasPressed = {} |
22 input.keyWasReleased = {} | 23 input.keyWasReleased = {} |
24 input.mouseWasReleased = {} | 25 input.mouseWasReleased = {} |
25 input.mouseWheel = 0 | 26 input.mouseWheel = 0 |
26 input.mouseMove = NewVec2f() | 27 input.mouseMove = NewVec2f() |
27 input.windowWasResized = false | 28 input.windowWasResized = false |
28 | 29 |
29 if input.lockMouse: | 30 if input.lockMouse and input.hasFocus: |
30 SetMousePosition(vulkan.window, x=int(vulkan.swapchain.width div 2), y=int(vulkan.swapchain.height div 2)) | 31 SetMousePosition(vulkan.window, x=int(vulkan.swapchain.width div 2), y=int(vulkan.swapchain.height div 2)) |
31 | 32 |
32 var killed = false | 33 var killed = false |
33 for event in vulkan.window.PendingEvents(): | 34 for event in vulkan.window.PendingEvents(): |
34 case event.eventType: | 35 case event.eventType: |
56 input.mouseWheel = event.amount | 57 input.mouseWheel = event.amount |
57 of MinimizedWindow: | 58 of MinimizedWindow: |
58 input.windowIsMinimized = true | 59 input.windowIsMinimized = true |
59 of RestoredWindow: | 60 of RestoredWindow: |
60 input.windowIsMinimized = false | 61 input.windowIsMinimized = false |
62 of GotFocus: | |
63 input.hasFocus = true | |
64 of LostFocus: | |
65 input.hasFocus = false | |
61 | 66 |
62 return not killed | 67 return not killed |
63 | 68 |
64 proc KeyIsDown*(key: Key): bool = key in input.keyIsDown | 69 proc KeyIsDown*(key: Key): bool = key in input.keyIsDown |
65 proc KeyWasPressed*(key: Key): bool = key in input.keyWasPressed | 70 proc KeyWasPressed*(key: Key): bool = key in input.keyWasPressed |
79 proc MouseMove*(): Vec2f = input.mouseMove | 84 proc MouseMove*(): Vec2f = input.mouseMove |
80 proc MouseWheel*(): float32 = input.mouseWheel | 85 proc MouseWheel*(): float32 = input.mouseWheel |
81 proc WindowWasResized*(): auto = input.windowWasResized | 86 proc WindowWasResized*(): auto = input.windowWasResized |
82 proc WindowIsMinimized*(): auto = input.windowIsMinimized | 87 proc WindowIsMinimized*(): auto = input.windowIsMinimized |
83 proc LockMouse*(value: bool) = input.lockMouse = value | 88 proc LockMouse*(value: bool) = input.lockMouse = value |
89 proc HasFocus*(): bool = input.hasFocus | |
84 | 90 |
85 | 91 |
86 # actions as a slight abstraction over raw input | 92 # actions as a slight abstraction over raw input |
87 | 93 |
88 type | 94 type |