examples/gamepad: Show multiple gamepad status

This commit is contained in:
Hajime Hoshi 2017-10-26 03:44:49 +09:00
parent 600baf2cc7
commit 79e46c555e

View File

@ -27,32 +27,37 @@ import (
) )
const ( const (
screenWidth = 320 screenWidth = 640
screenHeight = 240 screenHeight = 480
) )
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
const gamepadID = 0 const maxGamepadNum = 4
presences := [4]bool{} presences := [maxGamepadNum]bool{}
axes := []string{} axes := [maxGamepadNum][]string{}
pressedButtons := []string{} pressedButtons := [maxGamepadNum][]string{}
for i := range presences { for i := range presences {
presences[i] = ebiten.IsGamepadPresent(i) presences[i] = ebiten.IsGamepadPresent(i)
} }
maxAxis := ebiten.GamepadAxisNum(gamepadID) for i := range axes {
for a := 0; a < maxAxis; a++ { maxAxis := ebiten.GamepadAxisNum(i)
v := ebiten.GamepadAxis(gamepadID, a) for a := 0; a < maxAxis; a++ {
axes = append(axes, fmt.Sprintf("%d: %0.6f", a, v)) v := ebiten.GamepadAxis(i, a)
} axes[i] = append(axes[i], fmt.Sprintf("%d:%0.2f", a, v))
maxButton := ebiten.GamepadButton(ebiten.GamepadButtonNum(gamepadID))
for b := ebiten.GamepadButton(gamepadID); b < maxButton; b++ {
if ebiten.IsGamepadButtonPressed(gamepadID, b) {
pressedButtons = append(pressedButtons, strconv.Itoa(int(b)))
} }
} }
for i := range pressedButtons {
maxButton := ebiten.GamepadButton(ebiten.GamepadButtonNum(i))
for b := ebiten.GamepadButton(i); b < maxButton; b++ {
if ebiten.IsGamepadButtonPressed(i, b) {
pressedButtons[i] = append(pressedButtons[i], strconv.Itoa(int(b)))
}
}
}
if ebiten.IsRunningSlowly() { if ebiten.IsRunningSlowly() {
return nil return nil
} }
@ -64,22 +69,24 @@ func update(screen *ebiten.Image) error {
} }
} }
str := `Gamepad ({{.GamepadIDs}}) str := fmt.Sprintf("Gamepad (%s)\n", strings.Join(ids, ","))
str += "\n"
Gamepad (ID: {{.GamepadID}}) status: for i, p := range presences {
Axes: if !p {
{{.Axes}} continue
Pressed Buttons: {{.Buttons}}` }
str = strings.Replace(str, "{{.GamepadIDs}}", strings.Join(ids, ","), -1) str += fmt.Sprintf("Gamepad (ID: %d):\n", i)
str = strings.Replace(str, "{{.GamepadID}}", strconv.Itoa(gamepadID), -1) str += fmt.Sprintf(" Axes: %s\n", strings.Join(axes[i], ", "))
str = strings.Replace(str, "{{.Axes}}", strings.Join(axes, "\n "), -1) str += fmt.Sprintf(" Buttons: %s\n", strings.Join(pressedButtons[i], ", "))
str = strings.Replace(str, "{{.Buttons}}", strings.Join(pressedButtons, ", "), -1) str += "\n"
}
ebitenutil.DebugPrint(screen, str) ebitenutil.DebugPrint(screen, str)
return nil return nil
} }
func main() { func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Gamepad (Ebiten Demo)"); err != nil { if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Gamepad (Ebiten Demo)"); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }