-
Notifications
You must be signed in to change notification settings - Fork 136
/
gamepads.go
76 lines (63 loc) · 1.71 KB
/
gamepads.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
package engo
import "sync"
// Gamepadbutton is a button on a Gamepad.
type GamepadButton struct {
lastState bool
currentState bool
}
func (b *GamepadButton) set(state bool) {
b.lastState = b.currentState
b.currentState = state
}
// State returns the raw state of a key.
func (b *GamepadButton) State() int {
if b.lastState {
if b.currentState {
return KeyStateDown
}
return KeyStateJustUp
}
if b.currentState {
return KeyStateJustDown
}
return KeyStateUp
}
// JustPressed returns whether a key was just pressed
func (b GamepadButton) JustPressed() bool {
return (!b.lastState && b.currentState)
}
// JustReleased returns whether a key was just released
func (b GamepadButton) JustReleased() bool {
return (b.lastState && !b.currentState)
}
// Up returns wheter a key is not being pressed
func (b GamepadButton) Up() bool {
return (!b.lastState && !b.currentState)
}
// Down returns wether a key is being pressed
func (b GamepadButton) Down() bool {
return (b.lastState && b.currentState)
}
// GamepadManager manages the gamepads
type GamepadManager struct {
mutex sync.RWMutex
gamepads map[string]*Gamepad
}
// NewGamepadManager creates a new GamepadManager
func NewGamepadManager() *GamepadManager {
return &GamepadManager{
gamepads: make(map[string]*Gamepad),
}
}
// RegisterGamepad registers the gamepad with the given name. It can return an
// error if no suitable gamepads are located.
func (gm *GamepadManager) Register(name string) error {
return gm.registerGamepadImpl(name)
}
// GetGamepad returns the gamepad previously registered with name.
func (gm *GamepadManager) GetGamepad(name string) *Gamepad {
return gm.gamepads[name]
}
func (gm *GamepadManager) update() {
gm.updateImpl()
}