doc: Update gamepad.html

This commit is contained in:
Hajime Hoshi 2017-11-10 00:36:42 +09:00
parent 37ca48dc38
commit dc4dc92dd0

View File

@ -41,44 +41,50 @@ import (
) )
const ( const (
screenWidth = 320 screenWidth = 640
screenHeight = 240 screenHeight = 480
) )
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
// TODO: API to get the available, lowest ID ids := ebiten.GamepadIDs()
const gamepadID = 0 axes := map[int][]string{}
axes := []string{} pressedButtons := map[int][]string{}
pressedButtons := []string{}
maxAxis := ebiten.GamepadAxisNum(gamepadID) for _, id := range ids {
maxAxis := ebiten.GamepadAxisNum(id)
for a := 0; a < maxAxis; a++ { for a := 0; a < maxAxis; a++ {
v := ebiten.GamepadAxis(gamepadID, a) v := ebiten.GamepadAxis(id, a)
axes = append(axes, fmt.Sprintf("%d: %0.6f", a, v)) axes[id] = append(axes[id], fmt.Sprintf("%d:%0.2f", a, v))
}
maxButton := ebiten.GamepadButton(ebiten.GamepadButtonNum(id))
for b := ebiten.GamepadButton(id); b < maxButton; b++ {
if ebiten.IsGamepadButtonPressed(id, b) {
pressedButtons[id] = append(pressedButtons[id], strconv.Itoa(int(b)))
}
}
} }
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)))
}
}
if ebiten.IsRunningSlowly() { if ebiten.IsRunningSlowly() {
return nil return nil
} }
str := `Gamepad str := ""
Axes: if len(ids) > 0 {
{{.Axes}} for _, id := range ids {
Pressed Buttons: {{.Buttons}}` str += fmt.Sprintf("Gamepad (ID: %d):\n", id)
str = strings.Replace(str, "{{.Axes}}", strings.Join(axes, "\n "), -1) str += fmt.Sprintf(" Axes: %s\n", strings.Join(axes[id], ", "))
str = strings.Replace(str, "{{.Buttons}}", strings.Join(pressedButtons, ", "), -1) str += fmt.Sprintf(" Buttons: %s\n", strings.Join(pressedButtons[id], ", "))
str += "\n"
}
} else {
str = "Please connect your gamepad."
}
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)
} }
} }