From dc4dc92dd01a4206a62339b40192816da279557a Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 10 Nov 2017 00:36:42 +0900 Subject: [PATCH] doc: Update gamepad.html --- docs/examples/gamepad.html | 52 +++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/docs/examples/gamepad.html b/docs/examples/gamepad.html index 9ad019e28..a75ef4d0d 100644 --- a/docs/examples/gamepad.html +++ b/docs/examples/gamepad.html @@ -41,44 +41,50 @@ import ( ) const ( - screenWidth = 320 - screenHeight = 240 + screenWidth = 640 + screenHeight = 480 ) func update(screen *ebiten.Image) error { - // TODO: API to get the available, lowest ID - const gamepadID = 0 - axes := []string{} - pressedButtons := []string{} + ids := ebiten.GamepadIDs() + axes := map[int][]string{} + pressedButtons := map[int][]string{} - maxAxis := ebiten.GamepadAxisNum(gamepadID) - for a := 0; a < maxAxis; a++ { - v := ebiten.GamepadAxis(gamepadID, a) - axes = append(axes, fmt.Sprintf("%d: %0.6f", 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 _, id := range ids { + maxAxis := ebiten.GamepadAxisNum(id) + for a := 0; a < maxAxis; a++ { + v := ebiten.GamepadAxis(id, a) + 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))) + } } } + if ebiten.IsRunningSlowly() { return nil } - str := `Gamepad - Axes: - {{.Axes}} - Pressed Buttons: {{.Buttons}}` - str = strings.Replace(str, "{{.Axes}}", strings.Join(axes, "\n "), -1) - str = strings.Replace(str, "{{.Buttons}}", strings.Join(pressedButtons, ", "), -1) + str := "" + if len(ids) > 0 { + for _, id := range ids { + str += fmt.Sprintf("Gamepad (ID: %d):\n", id) + str += fmt.Sprintf(" Axes: %s\n", strings.Join(axes[id], ", ")) + str += fmt.Sprintf(" Buttons: %s\n", strings.Join(pressedButtons[id], ", ")) + str += "\n" + } + } else { + str = "Please connect your gamepad." + } ebitenutil.DebugPrint(screen, str) return nil } 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) } }