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