-
Notifications
You must be signed in to change notification settings - Fork 11
/
gamepad.go
222 lines (207 loc) · 7.04 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
// Copyright 2021 Google LLC
//
// 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 input
import (
"os"
"regexp"
"github.com/hajimehoshi/ebiten/v2"
"github.com/divVerent/aaaaxy/internal/flag"
"github.com/divVerent/aaaaxy/internal/log"
)
var (
gamepadAxisOnThreshold = flag.Float64("gamepad_axis_on_threshold", 0.6, "Minimum amount to push the game pad for registering an action. Can be zero to accept any movement.")
gamepadAxisOffThreshold = flag.Float64("gamepad_axis_off_threshold", 0.4, "Maximum amount to push the game pad for unregistering an action. Can be zero to accept any movement.")
gamepadOverride = flag.String("gamepad_override", "", "Entries in SDL_GameControllerDB format to add/override gamepad support. Multiple entries are permitted and can be separated by newlines or semicolons. Can also be provided via $SDL_GAMECONTROLLERCONFIG environment variable.")
)
type (
padControls struct {
buttons []ebiten.StandardGamepadButton
axes []ebiten.StandardGamepadAxis
axisDirection float64
}
)
var (
leftPad = padControls{
buttons: []ebiten.StandardGamepadButton{
ebiten.StandardGamepadButtonLeftLeft,
},
axes: []ebiten.StandardGamepadAxis{
ebiten.StandardGamepadAxisLeftStickHorizontal,
ebiten.StandardGamepadAxisRightStickHorizontal,
},
axisDirection: -1,
}
rightPad = padControls{
buttons: []ebiten.StandardGamepadButton{
ebiten.StandardGamepadButtonLeftRight,
},
axes: []ebiten.StandardGamepadAxis{
ebiten.StandardGamepadAxisLeftStickHorizontal,
ebiten.StandardGamepadAxisRightStickHorizontal,
},
axisDirection: +1,
}
upPad = padControls{
buttons: []ebiten.StandardGamepadButton{
ebiten.StandardGamepadButtonLeftTop,
},
axes: []ebiten.StandardGamepadAxis{
ebiten.StandardGamepadAxisLeftStickVertical,
ebiten.StandardGamepadAxisRightStickVertical,
},
axisDirection: -1,
}
downPad = padControls{
buttons: []ebiten.StandardGamepadButton{
ebiten.StandardGamepadButtonLeftBottom,
},
axes: []ebiten.StandardGamepadAxis{
ebiten.StandardGamepadAxisLeftStickVertical,
ebiten.StandardGamepadAxisRightStickVertical,
},
axisDirection: +1,
}
jumpPad = padControls{
buttons: []ebiten.StandardGamepadButton{
ebiten.StandardGamepadButtonRightBottom,
ebiten.StandardGamepadButtonRightTop,
ebiten.StandardGamepadButtonFrontBottomRight,
},
}
actionPad = padControls{
buttons: []ebiten.StandardGamepadButton{
ebiten.StandardGamepadButtonRightLeft,
ebiten.StandardGamepadButtonRightRight,
ebiten.StandardGamepadButtonFrontBottomLeft,
},
}
exitPad = padControls{
buttons: []ebiten.StandardGamepadButton{
ebiten.StandardGamepadButtonFrontTopLeft,
ebiten.StandardGamepadButtonFrontTopRight,
ebiten.StandardGamepadButtonCenterLeft,
ebiten.StandardGamepadButtonCenterRight,
ebiten.StandardGamepadButtonCenterCenter,
},
}
// Ignore ebiten.StandardGamepadButtonLeftStick.
// Ignore ebiten.StandardGamepadButtonRightStick.
)
var (
// gamepadInvAxisOnThreshold is 1.0 divided by the variable gamepadAxisOnThreshold. Done to save a division for every axis test.
gamepadInvAxisOnThreshold float64
// gamepadInvAxisOffThreshold is 1.0 divided by the variable gamepadAxisOffThreshold. Done to save a division for every axis test.
gamepadInvAxisOffThreshold float64
// gamepads is the set of currently active gamepads. The boolean value should always be true, except during rescanning, where it's set to false temporarily to detect removed gamepads.
gamepads = map[ebiten.GamepadID]struct{}{}
// allGamepads is the set of all gamepads, even unsupported ones.
allGamepads = map[ebiten.GamepadID]bool{}
// allGamepadsList is the list of all gamepads. Global to reduce allocation.
allGamepadsList []ebiten.GamepadID
)
func (i *impulse) gamepadPressed() InputMap {
t := *gamepadAxisOnThreshold
if i.Held {
t = *gamepadAxisOffThreshold
}
for p := range gamepads {
for _, b := range i.padControls.buttons {
if ebiten.IsStandardGamepadButtonPressed(p, b) {
return Gamepad
}
}
for _, a := range i.padControls.axes {
if ebiten.StandardGamepadAxisValue(p, a)*i.padControls.axisDirection >= t {
return Gamepad
}
}
}
return NoInput
}
func gamepadScan() {
// List new gamepads.
allGamepadsList = ebiten.AppendGamepadIDs(allGamepadsList[:0])
// Detect added/removed gamepads.
for p := range allGamepads {
allGamepads[p] = false
}
for _, p := range allGamepadsList {
_, alreadyThere := allGamepads[p]
if alreadyThere {
allGamepads[p] = true
continue
}
log.Infof("gamepad %v added", ebiten.GamepadName(p))
allGamepads[p] = true
if !ebiten.IsStandardGamepadLayoutAvailable(p) {
log.Errorf("gamepad %v has no standard layout - cannot use", ebiten.GamepadName(p))
continue
}
// A good gamepad! Add it.
gamepads[p] = struct{}{}
}
for p, stillThere := range allGamepads {
if stillThere {
continue
}
log.Infof("gamepad %v removed", ebiten.GamepadName(p))
delete(allGamepads, p)
delete(gamepads, p)
}
}
func gamepadInit() {
// Note: we're also stripping spaces before/after a semicolon
// as a user might be putting some given they're usual in English,
// yet they're technically invalid in SDL_GameControllerDB format.
semiRE := regexp.MustCompile(`\s*;\s*`)
// Support the environment variable.
config := semiRE.ReplaceAllString(os.Getenv("SDL_GAMECONTROLLERCONFIG"), "\n")
if config != "" {
applied, err := ebiten.UpdateStandardGamepadLayoutMappings(config)
if err != nil {
log.Errorf("could not add SDL_GAMECONTROLLERCONFIG mappings: %v", err)
} else if applied {
log.Infof("SDL_GAMECONTROLLERCONFIG applied.")
} else {
log.Warningf("SDL_GAMECONTROLLERCONFIG set but not used on this platform.")
}
}
// Also support the flag. Note that the flag value is saved.
config = semiRE.ReplaceAllString(*gamepadOverride, "\n")
if config != "" {
applied, err := ebiten.UpdateStandardGamepadLayoutMappings(config)
if err != nil {
log.Errorf("could not add --gamepad_override mappings: %v", err)
} else if applied {
log.Infof("--gamepad_override applied.")
} else {
log.Warningf("--gamepad_override set but not used on this platform.")
}
}
}
func gamepadEasterEggKeyState() easterEggKeyState {
var state easterEggKeyState
for p := range gamepads {
if ebiten.IsStandardGamepadButtonPressed(p, ebiten.StandardGamepadButtonRightBottom) {
state |= easterEggA
}
if ebiten.IsStandardGamepadButtonPressed(p, ebiten.StandardGamepadButtonRightLeft) {
state |= easterEggX
}
if ebiten.IsStandardGamepadButtonPressed(p, ebiten.StandardGamepadButtonRightTop) {
state |= easterEggY
}
}
return state
}