ebiten: add type GamepadAxisType

Updates #1718
This commit is contained in:
Hajime Hoshi 2023-12-25 01:50:20 +09:00
parent 22715cd2d5
commit 08ae0f4a94
4 changed files with 16 additions and 12 deletions

View File

@ -59,7 +59,7 @@ func (v virtualGamepadButton) StandardGamepadButton() ebiten.StandardGamepadButt
const axisThreshold = 0.75 const axisThreshold = 0.75
type axis struct { type axis struct {
id int id ebiten.GamepadAxisType
positive bool positive bool
} }
@ -73,7 +73,7 @@ type gamepadConfig struct {
assignedButtons map[ebiten.GamepadButton]struct{} assignedButtons map[ebiten.GamepadButton]struct{}
assignedAxes map[axis]struct{} assignedAxes map[axis]struct{}
defaultAxesValues map[int]float64 defaultAxesValues map[ebiten.GamepadAxisType]float64
} }
func (c *gamepadConfig) SetGamepadID(id ebiten.GamepadID) { func (c *gamepadConfig) SetGamepadID(id ebiten.GamepadID) {
@ -122,9 +122,9 @@ func (c *gamepadConfig) initializeIfNeeded() {
// These default values are used to detect if an axis is actually pressed. // These default values are used to detect if an axis is actually pressed.
// For example, on PS4 controllers, L2/R2's axes value can be -1.0. // For example, on PS4 controllers, L2/R2's axes value can be -1.0.
if c.defaultAxesValues == nil { if c.defaultAxesValues == nil {
c.defaultAxesValues = map[int]float64{} c.defaultAxesValues = map[ebiten.GamepadAxisType]float64{}
na := ebiten.GamepadAxisCount(c.gamepadID) na := ebiten.GamepadAxisType(ebiten.GamepadAxisCount(c.gamepadID))
for a := 0; a < na; a++ { for a := ebiten.GamepadAxisType(0); a < na; a++ {
c.defaultAxesValues[a] = ebiten.GamepadAxisValue(c.gamepadID, a) c.defaultAxesValues[a] = ebiten.GamepadAxisValue(c.gamepadID, a)
} }
} }
@ -161,8 +161,8 @@ func (c *gamepadConfig) Scan(b virtualGamepadButton) bool {
} }
} }
na := ebiten.GamepadAxisCount(c.gamepadID) na := ebiten.GamepadAxisType(ebiten.GamepadAxisCount(c.gamepadID))
for a := 0; a < na; a++ { for a := ebiten.GamepadAxisType(0); a < na; a++ {
v := ebiten.GamepadAxisValue(c.gamepadID, a) v := ebiten.GamepadAxisValue(c.gamepadID, a)
const delta = 0.25 const delta = 0.25

View File

@ -60,8 +60,8 @@ func (g *Game) Update() error {
g.axes = map[ebiten.GamepadID][]string{} g.axes = map[ebiten.GamepadID][]string{}
g.pressedButtons = map[ebiten.GamepadID][]string{} g.pressedButtons = map[ebiten.GamepadID][]string{}
for id := range g.gamepadIDs { for id := range g.gamepadIDs {
maxAxis := ebiten.GamepadAxisCount(id) maxAxis := ebiten.GamepadAxisType(ebiten.GamepadAxisCount(id))
for a := 0; a < maxAxis; a++ { for a := ebiten.GamepadAxisType(0); a < maxAxis; a++ {
v := ebiten.GamepadAxisValue(id, a) v := ebiten.GamepadAxisValue(id, a)
g.axes[id] = append(g.axes[id], fmt.Sprintf("%d:%+0.2f", a, v)) g.axes[id] = append(g.axes[id], fmt.Sprintf("%d:%+0.2f", a, v))
} }

View File

@ -59,6 +59,10 @@ const (
GamepadButtonMax GamepadButton = GamepadButton31 GamepadButtonMax GamepadButton = GamepadButton31
) )
// GamepadAxisType represents a gamepad axis.
// This is the same as int for backward compatibility in v2.
type GamepadAxisType = int
// StandardGamepadButton represents a gamepad button in the standard layout. // StandardGamepadButton represents a gamepad button in the standard layout.
// //
// The layout and the button values are based on the web standard. // The layout and the button values are based on the web standard.

View File

@ -180,18 +180,18 @@ func GamepadAxisNum(id GamepadID) int {
// GamepadAxisValue returns a float value [-1.0 - 1.0] of the given gamepad (id)'s axis (axis). // GamepadAxisValue returns a float value [-1.0 - 1.0] of the given gamepad (id)'s axis (axis).
// //
// GamepadAxisValue is concurrent-safe. // GamepadAxisValue is concurrent-safe.
func GamepadAxisValue(id GamepadID, axis int) float64 { func GamepadAxisValue(id GamepadID, axis GamepadAxisType) float64 {
g := gamepad.Get(id) g := gamepad.Get(id)
if g == nil { if g == nil {
return 0 return 0
} }
return g.Axis(axis) return g.Axis(int(axis))
} }
// GamepadAxis returns a float value [-1.0 - 1.0] of the given gamepad (id)'s axis (axis). // GamepadAxis returns a float value [-1.0 - 1.0] of the given gamepad (id)'s axis (axis).
// //
// Deprecated: as of v2.2. Use GamepadAxisValue instead. // Deprecated: as of v2.2. Use GamepadAxisValue instead.
func GamepadAxis(id GamepadID, axis int) float64 { func GamepadAxis(id GamepadID, axis GamepadAxisType) float64 {
return GamepadAxisValue(id, axis) return GamepadAxisValue(id, axis)
} }