Improve example/gamepad

This commit is contained in:
Hajime Hoshi 2015-01-12 13:33:21 +09:00
parent a6b7eab71b
commit a500d3a3f8
4 changed files with 61 additions and 16 deletions

View File

@ -15,6 +15,7 @@
package main package main
import ( import (
"fmt"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil" "github.com/hajimehoshi/ebiten/ebitenutil"
"log" "log"
@ -28,13 +29,29 @@ const (
) )
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
pressed := []string{} const gamepadID = 0
for b := ebiten.GamepadButton(0); b < 16; b++ { axes := []string{}
if ebiten.IsGamepadButtonPressed(0, b) { pressedButtons := []string{}
pressed = append(pressed, strconv.Itoa(int(b)))
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) ebitenutil.DebugPrint(screen, str)
return nil return nil
} }

View File

@ -36,12 +36,22 @@ func IsMouseButtonPressed(mouseButton MouseButton) bool {
return ui.IsMouseButtonPressed(ui.MouseButton(mouseButton)) return ui.IsMouseButtonPressed(ui.MouseButton(mouseButton))
} }
func GamepadAxis(j int, dir int) float64 { // TODO: docs
return ui.GamepadAxis(j, dir)
func GamepadAxisNum(id int) int {
return ui.GamepadAxisNum(id)
} }
func IsGamepadButtonPressed(j int, button GamepadButton) bool { func GamepadAxis(id int, axis int) float64 {
return ui.IsGamepadButtonPressed(j, ui.GamepadButton(button)) 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. // NewImage returns an empty image.

View File

@ -26,18 +26,32 @@ func IsMouseButtonPressed(button MouseButton) bool {
return currentInput.mouseButtonPressed[button] return currentInput.mouseButtonPressed[button]
} }
func GamepadAxis(j int, dir int) float64 { func GamepadAxisNum(id int) int {
if len(currentInput.gamepads) <= j { if len(currentInput.gamepads) <= id {
return 0 return 0
} }
return currentInput.gamepads[j].axes[dir] return currentInput.gamepads[id].axisNum
} }
func IsGamepadButtonPressed(j int, button GamepadButton) bool { func GamepadAxis(id int, axis int) float64 {
if len(currentInput.gamepads) <= j { 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 false
} }
return currentInput.gamepads[j].buttonPressed[button] return currentInput.gamepads[id].buttonPressed[button]
} }
var currentInput input var currentInput input
@ -51,6 +65,8 @@ type input struct {
} }
type gamePad struct { type gamePad struct {
axes [2]float64 axisNum int
axes [16]float64
buttonNum int
buttonPressed [256]bool buttonPressed [256]bool
} }

View File

@ -45,6 +45,7 @@ func (i *input) update(window *glfw.Window, scale int) error {
if err != nil { if err != nil {
return err return err
} }
i.gamepads[j].axisNum = len(axes32)
for a := 0; a < len(i.gamepads[j].axes); a++ { for a := 0; a < len(i.gamepads[j].axes); a++ {
if len(axes32) <= a { if len(axes32) <= a {
i.gamepads[j].axes[a] = 0 i.gamepads[j].axes[a] = 0
@ -56,6 +57,7 @@ func (i *input) update(window *glfw.Window, scale int) error {
if err != nil { if err != nil {
return err return err
} }
i.gamepads[j].buttonNum = len(buttons)
for b := 0; b < len(i.gamepads[j].buttonPressed); b++ { for b := 0; b < len(i.gamepads[j].buttonPressed); b++ {
if len(buttons) <= b { if len(buttons) <= b {
i.gamepads[j].buttonPressed[b] = false i.gamepads[j].buttonPressed[b] = false