diff --git a/example/gamepad/main.go b/example/gamepad/main.go index dd4f285b0..540b54202 100644 --- a/example/gamepad/main.go +++ b/example/gamepad/main.go @@ -15,6 +15,7 @@ package main import ( + "fmt" "github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten/ebitenutil" "log" @@ -28,13 +29,29 @@ const ( ) func update(screen *ebiten.Image) error { - pressed := []string{} - for b := ebiten.GamepadButton(0); b < 16; b++ { - if ebiten.IsGamepadButtonPressed(0, b) { - pressed = append(pressed, strconv.Itoa(int(b))) + const gamepadID = 0 + axes := []string{} + pressedButtons := []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))) } } - str := "Pressed Keys: " + strings.Join(pressed, ", ") + + 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) ebitenutil.DebugPrint(screen, str) return nil } diff --git a/gamecontext.go b/gamecontext.go index e1bc2c770..ee376cfc8 100644 --- a/gamecontext.go +++ b/gamecontext.go @@ -36,12 +36,22 @@ func IsMouseButtonPressed(mouseButton MouseButton) bool { return ui.IsMouseButtonPressed(ui.MouseButton(mouseButton)) } -func GamepadAxis(j int, dir int) float64 { - return ui.GamepadAxis(j, dir) +// TODO: docs + +func GamepadAxisNum(id int) int { + return ui.GamepadAxisNum(id) } -func IsGamepadButtonPressed(j int, button GamepadButton) bool { - return ui.IsGamepadButtonPressed(j, ui.GamepadButton(button)) +func GamepadAxis(id int, axis int) float64 { + return ui.GamepadAxis(id, axis) +} + +func GamepadButtonNum(id int) int { + return ui.GamepadButtonNum(id) +} + +func IsGamepadButtonPressed(id int, button GamepadButton) bool { + return ui.IsGamepadButtonPressed(id, ui.GamepadButton(button)) } // NewImage returns an empty image. diff --git a/internal/ui/input.go b/internal/ui/input.go index 988ea035c..69ca66c69 100644 --- a/internal/ui/input.go +++ b/internal/ui/input.go @@ -26,18 +26,32 @@ func IsMouseButtonPressed(button MouseButton) bool { return currentInput.mouseButtonPressed[button] } -func GamepadAxis(j int, dir int) float64 { - if len(currentInput.gamepads) <= j { +func GamepadAxisNum(id int) int { + if len(currentInput.gamepads) <= id { return 0 } - return currentInput.gamepads[j].axes[dir] + return currentInput.gamepads[id].axisNum } -func IsGamepadButtonPressed(j int, button GamepadButton) bool { - if len(currentInput.gamepads) <= j { +func GamepadAxis(id int, axis int) float64 { + if len(currentInput.gamepads) <= id { + return 0 + } + return currentInput.gamepads[id].axes[axis] +} + +func GamepadButtonNum(id int) int { + if len(currentInput.gamepads) <= id { + return 0 + } + return currentInput.gamepads[id].buttonNum +} + +func IsGamepadButtonPressed(id int, button GamepadButton) bool { + if len(currentInput.gamepads) <= id { return false } - return currentInput.gamepads[j].buttonPressed[button] + return currentInput.gamepads[id].buttonPressed[button] } var currentInput input @@ -51,6 +65,8 @@ type input struct { } type gamePad struct { - axes [2]float64 + axisNum int + axes [16]float64 + buttonNum int buttonPressed [256]bool } diff --git a/internal/ui/input_glfw.go b/internal/ui/input_glfw.go index f7cffb8fe..559ae8722 100644 --- a/internal/ui/input_glfw.go +++ b/internal/ui/input_glfw.go @@ -45,6 +45,7 @@ func (i *input) update(window *glfw.Window, scale int) error { if err != nil { return err } + i.gamepads[j].axisNum = len(axes32) for a := 0; a < len(i.gamepads[j].axes); a++ { if len(axes32) <= a { i.gamepads[j].axes[a] = 0 @@ -56,6 +57,7 @@ func (i *input) update(window *glfw.Window, scale int) error { if err != nil { return err } + i.gamepads[j].buttonNum = len(buttons) for b := 0; b < len(i.gamepads[j].buttonPressed); b++ { if len(buttons) <= b { i.gamepads[j].buttonPressed[b] = false