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