-
Notifications
You must be signed in to change notification settings - Fork 1
/
gamepad.go
174 lines (149 loc) · 4.19 KB
/
gamepad.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
// Copy from github.com/hajimehoshi/ebiten/example/blocks
package input
import (
"fmt"
"github.com/hajimehoshi/ebiten"
)
type virtualGamepadButton int
const (
virtualGamepadButtonLeft virtualGamepadButton = iota
virtualGamepadButtonRight
virtualGamepadButtonDown
virtualGamepadButtonButtonA
virtualGamepadButtonButtonB
)
var virtualGamepadButtons = []virtualGamepadButton{
virtualGamepadButtonLeft,
virtualGamepadButtonRight,
virtualGamepadButtonDown,
virtualGamepadButtonButtonA,
virtualGamepadButtonButtonB,
}
const axisThreshold = 0.75
type axis struct {
id int
positive bool
}
type gamepadConfig struct {
current virtualGamepadButton
buttons map[virtualGamepadButton]ebiten.GamepadButton
axes map[virtualGamepadButton]axis
assignedButtons map[ebiten.GamepadButton]struct{}
assignedAxes map[axis]struct{}
defaultAxesValues map[int]float64
}
func (c *gamepadConfig) initializeIfNeeded() {
if c.buttons == nil {
c.buttons = map[virtualGamepadButton]ebiten.GamepadButton{}
}
if c.axes == nil {
c.axes = map[virtualGamepadButton]axis{}
}
if c.assignedButtons == nil {
c.assignedButtons = map[ebiten.GamepadButton]struct{}{}
}
if c.assignedAxes == nil {
c.assignedAxes = map[axis]struct{}{}
}
// Set default values.
// It is assumed that all axes are not pressed here.
//
// These default values are used to detect if an axis is actually pressed.
// For example, on PS4 controllers, L2/R2's axes valuse can be -1.0.
if c.defaultAxesValues == nil {
c.defaultAxesValues = map[int]float64{}
const gamepadID = 0
na := ebiten.GamepadAxisNum(gamepadID)
for a := 0; a < na; a++ {
c.defaultAxesValues[a] = ebiten.GamepadAxis(gamepadID, a)
}
}
}
// AnyGamepadAbstractButtonPressed returns the specified button was pressed or not.
func AnyGamepadAbstractButtonPressed(i *Input) bool {
for _, b := range virtualGamepadButtons {
if i.gamepadConfig.IsButtonPressed(b) {
return true
}
}
return false
}
func (c *gamepadConfig) Reset() {
c.buttons = nil
c.axes = nil
c.assignedButtons = nil
c.assignedAxes = nil
}
// Scan scans the current input state and assigns the given virtual gamepad button b
// to the current (pysical) pressed buttons of the gamepad.
func (c *gamepadConfig) Scan(gamepadID int, b virtualGamepadButton) bool {
c.initializeIfNeeded()
delete(c.buttons, b)
delete(c.axes, b)
ebn := ebiten.GamepadButton(ebiten.GamepadButtonNum(gamepadID))
for eb := ebiten.GamepadButton(0); eb < ebn; eb++ {
if _, ok := c.assignedButtons[eb]; ok {
continue
}
if ebiten.IsGamepadButtonPressed(gamepadID, eb) {
c.buttons[b] = eb
c.assignedButtons[eb] = struct{}{}
return true
}
}
na := ebiten.GamepadAxisNum(gamepadID)
for a := 0; a < na; a++ {
v := ebiten.GamepadAxis(gamepadID, a)
// Check |v| < 1.0 because there is a bug that a button returns
// an axis value wrongly and the value may be over 1 on some platforms.
if axisThreshold <= v && v <= 1.0 && v != c.defaultAxesValues[a] {
if _, ok := c.assignedAxes[axis{a, true}]; !ok {
c.axes[b] = axis{a, true}
c.assignedAxes[axis{a, true}] = struct{}{}
return true
}
}
if -1.0 <= v && v <= -axisThreshold && v != c.defaultAxesValues[a] {
if _, ok := c.assignedAxes[axis{a, false}]; !ok {
c.axes[b] = axis{a, false}
c.assignedAxes[axis{a, false}] = struct{}{}
return true
}
}
}
return false
}
// IsButtonPressed returns a boolean value indicating whether
// the given virtual button b is pressed.
func (c *gamepadConfig) IsButtonPressed(b virtualGamepadButton) bool {
c.initializeIfNeeded()
bb, ok := c.buttons[b]
if ok {
return ebiten.IsGamepadButtonPressed(0, bb)
}
a, ok := c.axes[b]
if !ok {
return false
}
v := ebiten.GamepadAxis(0, a.id)
if a.positive {
return axisThreshold <= v && v <= 1.0
}
return -1.0 <= v && v <= -axisThreshold
}
// Name returns the pysical button's name for the given virtual button.
func (c *gamepadConfig) ButtonName(b virtualGamepadButton) string {
c.initializeIfNeeded()
bb, ok := c.buttons[b]
if ok {
return fmt.Sprintf("Button %d", bb)
}
a, ok := c.axes[b]
if !ok {
return ""
}
if a.positive {
return fmt.Sprintf("Axis %d+", a.id)
}
return fmt.Sprintf("Axis %d-", a.id)
}