annotate semiconginev2/input.nim @ 1249:d83726af7abb

did: first triangles getting loaded from gltf
author sam <sam@basx.dev>
date Thu, 25 Jul 2024 22:41:24 +0700
parents 56781cc0fc7c
children b0f4c8ccd49a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
1 type
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
2 Input = object
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
3 keyIsDown: set[Key]
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
4 keyWasPressed: set[Key]
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
5 keyWasReleased: set[Key]
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
6 mouseIsDown: set[MouseButton]
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
7 mouseWasPressed: set[MouseButton]
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
8 mouseWasReleased: set[MouseButton]
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
9 mousePosition: Vec2f
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
10 mouseMove: Vec2f
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
11 mouseWheel: float32
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
12 windowWasResized: bool = true
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
13 windowIsMinimized: bool = false
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
14
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
15 # warning, shit is not thread safe
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
16 var input: Input
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
17
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
18 proc UpdateInputs*(): bool =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
19 # reset input states
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
20 input.keyWasPressed = {}
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
21 input.keyWasReleased = {}
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
22 input.mouseWasPressed = {}
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
23 input.mouseWasReleased = {}
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
24 input.mouseWheel = 0
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
25 input.mouseMove = NewVec2f()
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
26 input.windowWasResized = false
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
27
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
28 var killed = false
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
29 for event in vulkan.window.PendingEvents():
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
30 case event.eventType:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
31 of Quit:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
32 killed = true
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
33 of ResizedWindow:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
34 input.windowWasResized = true
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
35 of KeyPressed:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
36 input.keyWasPressed.incl event.key
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
37 input.keyIsDown.incl event.key
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
38 of KeyReleased:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
39 input.keyWasReleased.incl event.key
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
40 input.keyIsDown.excl event.key
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
41 of MousePressed:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
42 input.mouseWasPressed.incl event.button
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
43 input.mouseIsDown.incl event.button
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
44 of MouseReleased:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
45 input.mouseWasReleased.incl event.button
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
46 input.mouseIsDown.excl event.button
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
47 of MouseMoved:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
48 let newPos = NewVec2(float32(event.x), float32(event.y))
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
49 input.mouseMove = newPos - input.mousePosition
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
50 input.mousePosition = newPos
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
51 of MouseWheel:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
52 input.mouseWheel = event.amount
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
53 of MinimizedWindow:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
54 input.windowIsMinimized = true
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
55 of RestoredWindow:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
56 input.windowIsMinimized = false
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
57
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
58 return not killed
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
59
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
60 proc KeyIsDown*(key: Key): bool = key in input.keyIsDown
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
61 proc KeyWasPressed*(key: Key): bool = key in input.keyWasPressed
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
62 proc KeyWasPressed*(): bool = input.keyWasPressed.len > 0
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
63 proc KeyWasReleased*(key: Key): bool = key in input.keyWasReleased
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
64 proc MouseIsDown*(button: MouseButton): bool = button in input.mouseIsDown
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
65 proc MouseWasPressed*(): bool = input.mouseWasPressed.len > 0
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
66 proc MouseWasPressed*(button: MouseButton): bool = button in input.mouseWasPressed
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
67 proc MousePressedButtons*(): set[MouseButton] = input.mouseWasPressed
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
68 proc MouseWasReleased*(): bool = input.mouseWasReleased.len > 0
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
69 proc MouseWasReleased*(button: MouseButton): bool = button in input.mouseWasReleased
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
70 proc MouseReleasedButtons*(): set[MouseButton] = input.mouseWasReleased
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
71 proc MousePosition*(): Vec2f = input.mousePosition
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
72 proc MousePositionNormalized*(size: (int, int)): Vec2f =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
73 result.x = (input.mousePosition.x / float32(size[0])) * 2.0 - 1.0
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
74 result.y = (input.mousePosition.y / float32(size[1])) * 2.0 - 1.0
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
75 proc MouseMove*(): auto = input.mouseMove
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
76 proc MouseWheel*(): auto = input.mouseWheel
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
77 proc WindowWasResized*(): auto = input.windowWasResized
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
78 proc WindowIsMinimized*(): auto = input.windowIsMinimized
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
79
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
80 # actions as a slight abstraction over raw input
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
81
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
82 type
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
83 ActionMap = object
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
84 keyActions: Table[string, set[Key]]
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
85 mouseActions: Table[string, set[MouseButton]]
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
86
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
87 # warning, shit is not thread safe
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
88 var actionMap: ActionMap
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
89
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
90 proc MapAction*[T: enum](action: T, key: Key) =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
91 if not actionMap.keyActions.contains($action):
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
92 actionMap.keyActions[$action] = {}
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
93 actionMap.keyActions[$action].incl key
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
94
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
95 proc MapAction*[T: enum](action: T, button: MouseButton) =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
96 if not actionMap.mouseActions.contains($action):
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
97 actionMap.mouseActions[$action] = {}
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
98 actionMap.mouseActions[$action].incl button
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
99
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
100 proc MapAction*[T: enum](action: T, keys: openArray[Key|MouseButton]) =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
101 for key in keys:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
102 MapAction(action, key)
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
103
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
104 proc UnmapAction*[T: enum](action: T, key: Key) =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
105 if actionMap.keyActions.contains($action):
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
106 actionMap.keyActions[$action].excl(key)
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
107
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
108 proc UnmapAction*[T: enum](action: T, button: MouseButton) =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
109 if actionMap.mouseActions.contains($action):
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
110 actionMap.mouseActions[$action].excl(button)
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
111
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
112 proc UnmapAction*[T: enum](action: T) =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
113 if actionMap.keyActions.contains($action):
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
114 actionMap.keyActions[$action] = {}
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
115 if actionMap.mouseActions.contains($action):
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
116 actionMap.mouseActions[$action] = {}
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
117
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
118 proc SaveCurrentActionMapping*() =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
119 for name, keys in actionMap.keyActions.pairs:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
120 SystemStorage.Store(name, keys, table = "input_mapping_key")
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
121 for name, buttons in actionMap.mouseActions.pairs:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
122 SystemStorage.Store(name, buttons, table = "input_mapping_mouse")
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
123
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
124 proc LoadActionMapping*[T]() =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
125 reset(actionMap)
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
126 for name in SystemStorage.List(table = "input_mapping_key"):
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
127 let action = parseEnum[T](name)
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
128 let keys = SystemStorage.Load(name, set[Key](), table = "input_mapping_key")
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
129 for key in keys:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
130 MapAction(action, key)
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
131
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
132 proc ActionDown*[T](action: T): bool =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
133 if actionMap.keyActions.contains($action):
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
134 for key in actionMap.keyActions[$action]:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
135 if key in input.keyIsDown:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
136 return true
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
137 return false
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
138 if actionMap.mouseActions.contains($action):
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
139 for button in actionMap.mouseActions[$action]:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
140 if button in input.mouseIsDown:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
141 return true
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
142 return false
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
143
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
144 proc ActionPressed*[T](action: T): bool =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
145 if actionMap.keyActions.contains($action):
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
146 for key in actionMap.keyActions[$action]:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
147 if key in input.keyWasPressed:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
148 return true
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
149 elif actionMap.mouseActions.contains($action):
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
150 for button in actionMap.mouseActions[$action]:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
151 if button in input.mouseWasPressed:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
152 return true
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
153
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
154 proc ActionReleased*[T](action: T): bool =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
155 if actionMap.keyActions.contains($action):
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
156 for key in actionMap.keyActions[$action]:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
157 if key in input.keyWasReleased:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
158 return true
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
159 elif actionMap.mouseActions.contains($action):
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
160 for button in actionMap.mouseActions[$action]:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
161 if button in input.mouseWasReleased:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
162 return true
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
163
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
164 proc ActionValue*[T](action: T): float32 =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
165 if actionMap.keyActions.contains($action):
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
166 for key in actionMap.keyActions[$action]:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
167 if key in input.keyIsDown:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
168 return 1
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
169 elif actionMap.mouseActions.contains($action):
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
170 for button in actionMap.mouseActions[$action]:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
171 if button in input.mouseIsDown:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
172 return 1