forked from quasilyte/ebitengine-input
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
389 lines (357 loc) · 12.3 KB
/
handler.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package input
import (
"math"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
// Handler is used to associate a keymap with an abstract input consumer.
//
// The ID bound to the handler is used to distinguish which gamepad is
// related to this handler.
//
// You usually need to create the input handlers only once and carry
// them through the game using your preferred method.
//
// If any game object needs to handle the input, they need an input handler object.
type Handler struct {
id int
keymap Keymap
sys *System
}
// EventInfo holds extra information about the input device event.
//
// Pos carries the event location, if available.
// Pos is a click location for mouse events.
// Pos is a tap location for screen touch events.
// Use HasPos() predicate to know whether there is a pos associated
// with the event to distinguish between (0, 0) pos and lack of pos info.
type EventInfo struct {
kind keyKind
hasPos bool
Pos Vec
}
// HasPos reports whether this event has a position associated with it.
// Use Pos field to get the pos value.
func (e EventInfo) HasPos() bool { return e.hasPos }
// IsTouchEvent reports whether this event was triggered by a screen touch device.
func (e EventInfo) IsTouchEvent() bool { return e.kind == keyTouch }
// IsKeyboardEvent reports whether this event was triggered by a keyboard device.
func (e EventInfo) IsKeyboardEvent() bool { return e.kind == keyKeyboard }
// IsMouseEvent reports whether this event was triggered by a mouse device.
func (e EventInfo) IsMouseEvent() bool { return e.kind == keyMouse }
// IsGamepadEvent reports whether this event was triggered by a gamepad device.
func (e EventInfo) IsGamepadEvent() bool {
switch e.kind {
case keyGamepad, keyGamepadLeftStick, keyGamepadRightStick:
return true
default:
return false
}
}
// GamepadConnected reports whether the gamepad associated with this handler is connected.
// The gamepad ID is the handler ID used during the handler creation.
//
// There should be at least one call to the System.Update() before this function
// can return the correct results.
func (h *Handler) GamepadConnected() bool {
for _, id := range h.sys.gamepadIDs {
if id == ebiten.GamepadID(h.id) {
return true
}
}
return false
}
// TouchEventsEnabled reports whether this handler can receive screen touch events.
func (h *Handler) TouchEventsEnabled() bool {
return h.sys.touchEnabled
}
// TapPos is like CursorPos(), but for the screen tapping.
// If there is no screen tapping in this frame, it returns false.
func (h *Handler) TapPos() (Vec, bool) {
return h.sys.touchTapPos, h.sys.touchHasTap
}
// CursorPos returns the current mouse cursor position on the screen.
func (h *Handler) CursorPos() Vec {
return h.sys.cursorPos
}
// DefaultInputMask returns the input mask suitable for functions like ActionKeyNames.
//
// If gamepad is connected, it returns GamepadInput mask.
// Otherwise it returns KeyboardInput+MouseInput mask.
// This is good enough for the simplest games, but you may to implement this
// logic inside your game if you need something more complicated.
func (h *Handler) DefaultInputMask() InputDeviceKind {
if h.GamepadConnected() {
return GamepadInput
}
return KeyboardInput | MouseInput
}
// ActionKeyNames returns a list of key names associated by this action.
//
// It filters the results by a given input device mask.
// If you want to include all input device keys, use AnyInput value.
//
// This function is useful when you want to display a list of keys
// the player should press in order to activate some action.
//
// The filtering is useful to avoid listing the unrelated options.
// For example, if player uses the gamepad, it could be weird to
// show keyboard options listed. For the simple cases, you can use
// DefaultInputMask() method to get the mask that will try to avoid
// that situation. See its comment to learn more.
func (h *Handler) ActionKeyNames(action Action, mask InputDeviceKind) []string {
keys, ok := h.keymap[action]
if !ok {
return nil
}
gamepadConnected := h.GamepadConnected()
result := make([]string, 0, len(keys))
for _, k := range keys {
enabled := true
switch k.kind {
case keyKeyboard:
enabled = mask&KeyboardInput != 0
case keyMouse:
enabled = mask&MouseInput != 0
case keyGamepad, keyGamepadLeftStick, keyGamepadRightStick:
enabled = gamepadConnected && (mask&GamepadInput != 0)
case keyTouch:
enabled = h.sys.touchEnabled && (mask&TouchInput != 0)
}
if enabled {
result = append(result, k.name)
}
}
return result
}
// JustPressedActionInfo is like ActionIsJustPressed, but with more information.
//
// The second return value is false is given action is not activated.
//
// The first return value will hold the extra event info.
// See EventInfo comment to learn more.
func (h *Handler) JustPressedActionInfo(action Action) (EventInfo, bool) {
var info EventInfo
keys, ok := h.keymap[action]
if !ok {
return info, false
}
isPressed := false
for _, k := range keys {
if !h.keyIsJustPressed(k) {
continue
}
isPressed = true
info.kind = k.kind
switch k.kind {
case keyMouse, keyMouseWithCtrl, keyMouseWithShift, keyMouseWithCtrlShift:
info.Pos = h.sys.cursorPos
info.hasPos = true
return info, true
case keyTouch:
info.Pos = h.sys.touchTapPos
info.hasPos = true
return info, true
case keyWheel:
info.Pos = h.sys.wheel
info.hasPos = true
return info, true
}
}
return info, isPressed
}
// ActionIsJustPressed is like ebitenutil.IsKeyJustPressed, but operates
// on the action level and works with any kinds of "keys".
// It returns true if any of the keys bound to the action was pressed during this frame.
func (h *Handler) ActionIsJustPressed(action Action) bool {
keys, ok := h.keymap[action]
if !ok {
return false
}
for _, k := range keys {
if h.keyIsJustPressed(k) {
return true
}
}
return false
}
// ActionIsPressed is like ebiten.IsKeyPressed, but operates
// on the action level and works with any kinds of "keys".
// It returns true if any of the keys bound to the action is being pressed.
func (h *Handler) ActionIsPressed(action Action) bool {
keys, ok := h.keymap[action]
if !ok {
return false
}
for _, k := range keys {
if h.keyIsPressed(k) {
return true
}
}
return false
}
func (h *Handler) keyIsJustPressed(k Key) bool {
switch k.kind {
case keyTouch:
if k.code == int(touchTap) {
return h.sys.touchHasTap
}
return false
case keyGamepad:
return h.gamepadKeyIsJustPressed(k)
case keyGamepadLeftStick:
return h.gamepadStickIsJustPressed(stickCode(k.code), ebiten.StandardGamepadAxisLeftStickHorizontal, ebiten.StandardGamepadAxisLeftStickVertical)
case keyGamepadRightStick:
return h.gamepadStickIsJustPressed(stickCode(k.code), ebiten.StandardGamepadAxisRightStickHorizontal, ebiten.StandardGamepadAxisRightStickVertical)
case keyMouse:
return inpututil.IsMouseButtonJustPressed(ebiten.MouseButton(k.code))
case keyMouseWithCtrl:
return ebiten.IsKeyPressed(ebiten.KeyControl) &&
inpututil.IsMouseButtonJustPressed(ebiten.MouseButton(k.code))
case keyMouseWithShift:
return ebiten.IsKeyPressed(ebiten.KeyShift) &&
inpututil.IsMouseButtonJustPressed(ebiten.MouseButton(k.code))
case keyMouseWithCtrlShift:
return ebiten.IsKeyPressed(ebiten.KeyControl) &&
ebiten.IsKeyPressed(ebiten.KeyShift) &&
inpututil.IsMouseButtonJustPressed(ebiten.MouseButton(k.code))
case keyKeyboardWithCtrl:
return ebiten.IsKeyPressed(ebiten.KeyControl) &&
inpututil.IsKeyJustPressed(ebiten.Key(k.code))
case keyKeyboardWithShift:
return ebiten.IsKeyPressed(ebiten.KeyShift) &&
inpututil.IsKeyJustPressed(ebiten.Key(k.code))
case keyKeyboardWithCtrlShift:
return ebiten.IsKeyPressed(ebiten.KeyControl) &&
ebiten.IsKeyPressed(ebiten.KeyShift) &&
inpututil.IsKeyJustPressed(ebiten.Key(k.code))
case keyWheel:
return h.wheelIsJustPressed(wheelCode(k.code))
default:
return inpututil.IsKeyJustPressed(ebiten.Key(k.code))
}
}
func (h *Handler) keyIsPressed(k Key) bool {
switch k.kind {
case keyGamepad:
return h.gamepadKeyIsPressed(k)
case keyGamepadLeftStick:
return h.gamepadStickIsPressed(stickCode(k.code), ebiten.StandardGamepadAxisLeftStickHorizontal, ebiten.StandardGamepadAxisLeftStickVertical)
case keyGamepadRightStick:
return h.gamepadStickIsPressed(stickCode(k.code), ebiten.StandardGamepadAxisRightStickHorizontal, ebiten.StandardGamepadAxisRightStickVertical)
case keyMouse:
return ebiten.IsMouseButtonPressed(ebiten.MouseButton(k.code))
case keyMouseWithCtrl:
return ebiten.IsKeyPressed(ebiten.KeyControl) &&
ebiten.IsMouseButtonPressed(ebiten.MouseButton(k.code))
case keyMouseWithShift:
return ebiten.IsKeyPressed(ebiten.KeyShift) &&
ebiten.IsMouseButtonPressed(ebiten.MouseButton(k.code))
case keyKeyboardWithCtrl:
return ebiten.IsKeyPressed(ebiten.KeyControl) &&
ebiten.IsKeyPressed(ebiten.Key(k.code))
case keyKeyboardWithShift:
return ebiten.IsKeyPressed(ebiten.KeyShift) &&
ebiten.IsKeyPressed(ebiten.Key(k.code))
default:
return ebiten.IsKeyPressed(ebiten.Key(k.code))
}
}
func (h *Handler) isDPadAxisActive(code int, vec Vec) bool {
switch ebiten.StandardGamepadButton(code) {
case ebiten.StandardGamepadButtonLeftTop:
return vec.Y == -1
case ebiten.StandardGamepadButtonLeftRight:
return vec.X == 1
case ebiten.StandardGamepadButtonLeftBottom:
return vec.Y == 1
case ebiten.StandardGamepadButtonLeftLeft:
return vec.X == -1
}
return false
}
func (h *Handler) wheelIsJustPressed(code wheelCode) bool {
switch code {
case wheelDown:
return h.sys.wheel.Y > 0
case wheelUp:
return h.sys.wheel.Y < 0
case wheelVertical:
return h.sys.wheel.Y != 0
default:
return false
}
}
func (h *Handler) gamepadKeyIsJustPressed(k Key) bool {
if h.gamepadInfo().model == gamepadStandard {
return inpututil.IsStandardGamepadButtonJustPressed(ebiten.GamepadID(h.id), ebiten.StandardGamepadButton(k.code))
}
if h.gamepadInfo().model == gamepadFirefoxXinput && isDPadButton(k.code) {
return !h.isDPadAxisActive(k.code, h.getStickPrevVec(6, 7)) &&
h.isDPadAxisActive(k.code, h.getStickVec(6, 7))
}
return inpututil.IsGamepadButtonJustPressed(ebiten.GamepadID(h.id), h.mappedGamepadKey(k.code))
}
func (h *Handler) gamepadKeyIsPressed(k Key) bool {
if h.gamepadInfo().model == gamepadStandard {
return ebiten.IsStandardGamepadButtonPressed(ebiten.GamepadID(h.id), ebiten.StandardGamepadButton(k.code))
}
if h.gamepadInfo().model == gamepadFirefoxXinput && isDPadButton(k.code) {
return h.isDPadAxisActive(k.code, h.getStickVec(6, 7))
}
return ebiten.IsGamepadButtonPressed(ebiten.GamepadID(h.id), h.mappedGamepadKey(k.code))
}
func (h *Handler) gamepadStickIsActive(code stickCode, vec Vec) bool {
if vecLen(vec) < 0.5 {
return false
}
switch code {
case stickUp:
angle := angleNormalized(vecAngle(vec))
return angle > (math.Pi+math.Pi/4) && angle <= (2*math.Pi-math.Pi/4)
case stickRight:
angle := angleNormalized(vecAngle(vec))
return angle <= (math.Pi/4) || angle > (2*math.Pi-math.Pi/4)
case stickDown:
angle := angleNormalized(vecAngle(vec))
return angle > (math.Pi/4) && angle <= (math.Pi-math.Pi/4)
case stickLeft:
angle := angleNormalized(vecAngle(vec))
return angle > (math.Pi-math.Pi/4) && angle <= (math.Pi+math.Pi/4)
}
return false
}
func (h *Handler) gamepadStickIsJustPressed(code stickCode, axis1, axis2 ebiten.StandardGamepadAxis) bool {
return !h.gamepadStickIsActive(code, h.getStickPrevVec(axis1, axis2)) &&
h.gamepadStickIsActive(code, h.getStickVec(int(axis1), int(axis2)))
}
func (h *Handler) gamepadStickIsPressed(code stickCode, axis1, axis2 ebiten.StandardGamepadAxis) bool {
vec := h.getStickVec(int(axis1), int(axis2))
return h.gamepadStickIsActive(code, vec)
}
func (h *Handler) getStickPrevVec(axis1, axis2 ebiten.StandardGamepadAxis) Vec {
return Vec{
X: h.gamepadInfo().prevAxisValues[axis1],
Y: h.gamepadInfo().prevAxisValues[axis2],
}
}
func (h *Handler) getStickVec(axis1, axis2 int) Vec {
return Vec{
X: h.gamepadInfo().axisValues[axis1],
Y: h.gamepadInfo().axisValues[axis2],
}
}
func (h *Handler) gamepadInfo() *gamepadInfo {
return &h.sys.gamepadInfo[h.id]
}
func (h *Handler) mappedGamepadKey(keyCode int) ebiten.GamepadButton {
b := ebiten.StandardGamepadButton(keyCode)
switch h.gamepadInfo().model {
case gamepadMicront:
return microntToXbox(b)
case gamepadFirefoxXinput:
return firefoxXinputToXbox(b)
default:
return ebiten.GamepadButton(keyCode)
}
}