-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamepad.go
274 lines (231 loc) · 7.06 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
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
// Copyright 2015 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
type virtualGamepadButton int
const (
virtualGamepadButtonLeft virtualGamepadButton = iota
virtualGamepadButtonRight
virtualGamepadButtonDown
virtualGamepadButtonButtonA
virtualGamepadButtonButtonB
)
var virtualGamepadButtons = []virtualGamepadButton{
virtualGamepadButtonLeft,
virtualGamepadButtonRight,
virtualGamepadButtonDown,
virtualGamepadButtonButtonA,
virtualGamepadButtonButtonB,
}
func (v virtualGamepadButton) StandardGamepadButton() ebiten.StandardGamepadButton {
switch v {
case virtualGamepadButtonLeft:
return ebiten.StandardGamepadButtonLeftLeft
case virtualGamepadButtonRight:
return ebiten.StandardGamepadButtonLeftRight
case virtualGamepadButtonDown:
return ebiten.StandardGamepadButtonLeftBottom
case virtualGamepadButtonButtonA:
return ebiten.StandardGamepadButtonRightBottom
case virtualGamepadButtonButtonB:
return ebiten.StandardGamepadButtonRightRight
default:
panic("not reached")
}
}
const axisThreshold = 0.75
type axis struct {
id int
positive bool
}
type gamepadConfig struct {
gamepadID ebiten.GamepadID
gamepadIDInitialized bool
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) SetGamepadID(id ebiten.GamepadID) {
c.gamepadID = id
c.gamepadIDInitialized = true
}
func (c *gamepadConfig) ResetGamepadID() {
c.gamepadID = 0
c.gamepadIDInitialized = false
}
func (c *gamepadConfig) IsGamepadIDInitialized() bool {
return c.gamepadIDInitialized
}
func (c *gamepadConfig) NeedsConfiguration() bool {
return !ebiten.IsStandardGamepadLayoutAvailable(c.gamepadID)
}
func (c *gamepadConfig) initializeIfNeeded() {
if !c.gamepadIDInitialized {
panic("not reached")
}
if ebiten.IsStandardGamepadLayoutAvailable(c.gamepadID) {
return
}
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{}
na := ebiten.GamepadAxisCount(c.gamepadID)
for a := 0; a < na; a++ {
c.defaultAxesValues[a] = ebiten.GamepadAxisValue(c.gamepadID, a)
}
}
}
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(b virtualGamepadButton) bool {
if !c.gamepadIDInitialized {
panic("not reached")
}
c.initializeIfNeeded()
delete(c.buttons, b)
delete(c.axes, b)
ebn := ebiten.GamepadButton(ebiten.GamepadButtonCount(c.gamepadID))
for eb := ebiten.GamepadButton(0); eb < ebn; eb++ {
if _, ok := c.assignedButtons[eb]; ok {
continue
}
if inpututil.IsGamepadButtonJustPressed(c.gamepadID, eb) {
c.buttons[b] = eb
c.assignedButtons[eb] = struct{}{}
return true
}
}
na := ebiten.GamepadAxisCount(c.gamepadID)
for a := 0; a < na; a++ {
v := ebiten.GamepadAxisValue(c.gamepadID, a)
const delta = 0.25
// 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]-delta || c.defaultAxesValues[a]+delta < v) {
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]-delta || c.defaultAxesValues[a]+delta < v) {
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 reports whether the given virtual button b is pressed.
func (c *gamepadConfig) IsButtonPressed(b virtualGamepadButton) bool {
if !c.gamepadIDInitialized {
panic("not reached")
}
if ebiten.IsStandardGamepadLayoutAvailable(c.gamepadID) {
if ebiten.IsStandardGamepadButtonPressed(c.gamepadID, b.StandardGamepadButton()) {
return true
}
const threshold = 0.7
switch b {
case virtualGamepadButtonLeft:
return ebiten.StandardGamepadAxisValue(c.gamepadID, ebiten.StandardGamepadAxisLeftStickHorizontal) < -threshold
case virtualGamepadButtonRight:
return ebiten.StandardGamepadAxisValue(c.gamepadID, ebiten.StandardGamepadAxisLeftStickHorizontal) > threshold
case virtualGamepadButtonDown:
return ebiten.StandardGamepadAxisValue(c.gamepadID, ebiten.StandardGamepadAxisLeftStickVertical) > threshold
}
return false
}
c.initializeIfNeeded()
bb, ok := c.buttons[b]
if ok {
return ebiten.IsGamepadButtonPressed(c.gamepadID, bb)
}
a, ok := c.axes[b]
if ok {
v := ebiten.GamepadAxisValue(c.gamepadID, a.id)
if a.positive {
return axisThreshold <= v && v <= 1.0
}
return -1.0 <= v && v <= -axisThreshold
}
return false
}
// IsButtonJustPressed reports whether the given virtual button b started to be pressed now.
func (c *gamepadConfig) IsButtonJustPressed(b virtualGamepadButton) bool {
if !c.gamepadIDInitialized {
panic("not reached")
}
if ebiten.IsStandardGamepadLayoutAvailable(c.gamepadID) {
return inpututil.IsStandardGamepadButtonJustPressed(c.gamepadID, b.StandardGamepadButton())
}
c.initializeIfNeeded()
bb, ok := c.buttons[b]
if ok {
return inpututil.IsGamepadButtonJustPressed(c.gamepadID, bb)
}
return false
}
// Name returns the pysical button's name for the given virtual button.
func (c *gamepadConfig) ButtonName(b virtualGamepadButton) string {
if !c.gamepadIDInitialized {
panic("not reached")
}
c.initializeIfNeeded()
bb, ok := c.buttons[b]
if ok {
return fmt.Sprintf("Button %d", bb)
}
a, ok := c.axes[b]
if ok {
if a.positive {
return fmt.Sprintf("Axis %d+", a.id)
}
return fmt.Sprintf("Axis %d-", a.id)
}
return ""
}