-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinput.go
293 lines (261 loc) · 7.47 KB
/
input.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package noodle
//Key is a Keycode representation of a keyboard character.
// use the https://keycode.info/ tool to help identify missing key codes
type Key int
const (
//Dash -
KeyDash = Key(189)
KeyApostrophe = Key(222)
KeySemicolon = Key(186)
KeyEquals = Key(187)
KeyComma = Key(188)
KeyPeriod = Key(190)
KeySlash = Key(191)
KeyBackslash = Key(220)
KeyBackspace = Key(8)
KeyTab = Key(9)
KeyCapsLock = Key(20)
KeySpace = Key(32)
KeyEnter = Key(13)
KeyEscape = Key(27)
KeyInsert = Key(45)
KeyPrintScreen = Key(42)
KeyDelete = Key(46)
KeyPageUp = Key(33)
KeyPageDown = Key(34)
KeyHome = Key(36)
KeyEnd = Key(35)
KeyPause = Key(19)
KeyScrollLock = Key(145)
KeyArrowLeft = Key(37)
KeyArrowRight = Key(39)
KeyArrowDown = Key(40)
KeyArrowUp = Key(38)
KeyLeftBracket = Key(219)
KeyLeftShift = Key(16)
KeyLeftControl = Key(17)
KeyLeftSuper = Key(73)
KeyLeftAlt = Key(18)
KeyRightBracket = Key(221)
KeyRightShift = Key(16)
KeyRightControl = Key(17)
KeyRightSuper = Key(73)
KeyRightAlt = Key(18)
KeyZero = Key(48)
KeyOne = Key(49)
KeyTwo = Key(50)
KeyThree = Key(51)
KeyFour = Key(52)
KeyFive = Key(53)
KeySix = Key(54)
KeySeven = Key(55)
KeyEight = Key(56)
KeyNine = Key(57)
KeyF1 = Key(112)
KeyF2 = Key(113)
KeyF3 = Key(114)
KeyF4 = Key(115)
KeyF5 = Key(116)
KeyF6 = Key(117)
KeyF7 = Key(118)
KeyF8 = Key(119)
KeyF9 = Key(120)
KeyF10 = Key(121)
KeyF11 = Key(122)
KeyF12 = Key(123)
KeyA = Key(65)
KeyB = Key(66)
KeyC = Key(67)
KeyD = Key(68)
KeyE = Key(69)
KeyF = Key(70)
KeyG = Key(71)
KeyH = Key(72)
KeyI = Key(73)
KeyJ = Key(74)
KeyK = Key(75)
KeyL = Key(76)
KeyM = Key(77)
KeyN = Key(78)
KeyO = Key(79)
KeyP = Key(80)
KeyQ = Key(81)
KeyR = Key(82)
KeyS = Key(83)
KeyT = Key(84)
KeyU = Key(85)
KeyV = Key(86)
KeyW = Key(87)
KeyX = Key(88)
KeyY = Key(89)
KeyZ = Key(90)
KeyNumLock = Key(144)
KeyNumMultiply = Key(106)
KeyNumDivide = Key(111)
KeyNumAdd = Key(107)
KeyNumSubtract = Key(109)
KeyNumZero = Key(96)
KeyNumOne = Key(97)
KeyNumTwo = Key(98)
KeyNumThree = Key(99)
KeyNumFour = Key(100)
KeyNumFive = Key(101)
KeyNumSix = Key(102)
KeyNumSeven = Key(103)
KeyNumEight = Key(104)
KeyNumNine = Key(105)
KeyNumDecimal = Key(110)
KeyNumEnter = Key(13)
)
type keyState int
const (
keyStateUp keyState = iota
keyStatePrePressed
keyStatePressed
keyStateDown
keyStatePreRelease
keyStateRelease
)
//InputHandler handles the different states of the input
type InputHandler struct {
mouseX int //mouseX position
mouseY int //mouseY position
mouseScrollDeltaPrevious float32 //mouseScrollDeltaPrevious is the scroll before the frame
mouseScrollDelta float32 //mouseScrollDelta is the scroll after the frame
buttonStates [5]keyState
keyStates map[Key]keyState
}
func newInput() *InputHandler {
i := InputHandler{}
i.mouseX = 0
i.mouseY = 0
i.keyStates = make(map[Key]keyState, 6)
return &i
}
/// Stores the mouse position
func (i *InputHandler) setMousePosition(x, y int) {
i.mouseX = x
i.mouseY = y
}
// setMouseScroll sets the mouse delta scroll
func (i *InputHandler) setMouseScroll(delta float32) {
i.mouseScrollDeltaPrevious = delta
}
//Sets the mouse Down State
func (i *InputHandler) setMouseDown(button int) {
i.buttonStates[button] = keyStatePrePressed
}
//Sets the mouse up state
func (i *InputHandler) setMouseUp(button int) {
i.buttonStates[button] = keyStatePreRelease
}
func (i *InputHandler) setKeyDown(key int) {
i.keyStates[Key(key)] = keyStatePrePressed
}
func (i *InputHandler) setKeyUp(key int) {
i.keyStates[Key(key)] = keyStatePreRelease
}
//Processes the input changes
func (i *InputHandler) update() {
//Update the scroll
i.mouseScrollDelta = i.mouseScrollDeltaPrevious
i.mouseScrollDeltaPrevious = 0
//Handle mouse inputs
for index, state := range i.buttonStates {
if state == keyStatePrePressed {
i.buttonStates[index] = keyStatePressed
} else if state == keyStatePressed {
i.buttonStates[index] = keyStateDown
}
if state == keyStatePreRelease {
i.buttonStates[index] = keyStateRelease
} else if state == keyStateRelease {
i.buttonStates[index] = keyStateUp
}
}
//Handle key inputs. These are a bit tricker
for index, state := range i.keyStates {
//Pressed
if state == keyStatePrePressed {
i.keyStates[index] = keyStatePressed
} else if state == keyStatePressed {
i.keyStates[index] = keyStateDown
}
//Released
if state == keyStatePreRelease {
i.keyStates[index] = keyStateRelease
} else if state == keyStateRelease {
i.keyStates[index] = keyStateUp
//Key is up, so lets remove the index so we dont loop it again
delete(i.keyStates, index)
}
}
}
//GetMouseX gets the current mouse position on the screen in pixels
func (i *InputHandler) GetMouseX() int { return i.mouseX }
//GetMouseY gets the current mouse position on the screen in pixels
func (i *InputHandler) GetMouseY() int { return i.mouseY }
//GetMousePosition gets the current mouse position
func (i *InputHandler) GetMousePosition() Vector2 {
return NewVector2(float32(i.mouseX), float32(i.mouseY))
}
//GetMouseScroll gets the current mouse scroll delta
func (i *InputHandler) GetMouseScroll() float32 { return i.mouseScrollDelta }
// GetButton gets if the current button is pressed
func (i *InputHandler) GetButton(button int) bool {
if i.buttonStates[button] >= keyStatePressed {
return true
}
return false
}
// GetButtonDown gets if the current button has started being pressed
func (i *InputHandler) GetButtonDown(button int) bool {
if i.buttonStates[button] == keyStatePressed {
return true
}
return false
}
//GetButtonUp gets if the current button was released
func (i *InputHandler) GetButtonUp(button int) bool {
if i.buttonStates[button] == keyStateRelease {
return true
}
return false
}
// GetKey gets if the current key is pressed
func (i *InputHandler) GetKey(key Key) bool {
if i.keyStates[key] >= keyStatePressed {
return true
}
return false
}
// GetKeyDown gets if the current key has started being pressed
func (i *InputHandler) GetKeyDown(key Key) bool {
if i.keyStates[key] == keyStatePressed {
return true
}
return false
}
//GetKeyUp gets if the current key was released
func (i *InputHandler) GetKeyUp(key Key) bool {
if i.keyStates[key] == keyStateRelease {
return true
}
return false
}
//GetAxis gets a normalized 1D axis
func (i *InputHandler) GetAxis(negativeKey, positiveKey Key) float32 {
sum := float32(0)
if i.GetKey(negativeKey) {
sum -= 1.0
}
if i.GetKey(positiveKey) {
sum += 1.0
}
return sum
}
//GetAxis2D gets a normalized 2D axis based on the keys being pressed
func (i *InputHandler) GetAxis2D(negHorizontalKey, posHorizontalKey, negVerticalKey, posVerticalKey Key) Vector2 {
axis := Vector2{i.GetAxis(negHorizontalKey, posHorizontalKey), i.GetAxis(negVerticalKey, posVerticalKey)}
return axis.Normalize()
}