ui: Add IsGamepadPresent

Fixes #447
This commit is contained in:
Hajime Hoshi 2017-10-26 02:59:06 +09:00
parent 05e220e105
commit 600baf2cc7
5 changed files with 41 additions and 2 deletions

View File

@ -32,11 +32,15 @@ const (
)
func update(screen *ebiten.Image) error {
// TODO: API to get the available, lowest ID
const gamepadID = 0
presences := [4]bool{}
axes := []string{}
pressedButtons := []string{}
for i := range presences {
presences[i] = ebiten.IsGamepadPresent(i)
}
maxAxis := ebiten.GamepadAxisNum(gamepadID)
for a := 0; a < maxAxis; a++ {
v := ebiten.GamepadAxis(gamepadID, a)
@ -53,10 +57,21 @@ func update(screen *ebiten.Image) error {
return nil
}
str := `Gamepad
ids := []string{}
for i, p := range presences {
if p {
ids = append(ids, strconv.Itoa(i))
}
}
str := `Gamepad ({{.GamepadIDs}})
Gamepad (ID: {{.GamepadID}}) status:
Axes:
{{.Axes}}
Pressed Buttons: {{.Buttons}}`
str = strings.Replace(str, "{{.GamepadIDs}}", strings.Join(ids, ","), -1)
str = strings.Replace(str, "{{.GamepadID}}", strconv.Itoa(gamepadID), -1)
str = strings.Replace(str, "{{.Axes}}", strings.Join(axes, "\n "), -1)
str = strings.Replace(str, "{{.Buttons}}", strings.Join(pressedButtons, ", "), -1)
ebitenutil.DebugPrint(screen, str)

View File

@ -56,6 +56,15 @@ func IsMouseButtonPressed(mouseButton MouseButton) bool {
return ui.CurrentInput().IsMouseButtonPressed(ui.MouseButton(mouseButton))
}
// IsGamepadPresent returns a boolean value indicating whether the gamepad for the given id exists.
//
// This function is concurrent-safe.
//
// NOTE: Gamepad API is available only on desktops, Chrome and Firefox.
func IsGamepadPresent(id int) bool {
return ui.CurrentInput().IsGamepadPresent(id)
}
// GamepadAxisNum returns the number of axes of the gamepad (id).
//
// This function is concurrent-safe.

View File

@ -31,6 +31,15 @@ func (i *Input) CursorPosition() (x, y int) {
return adjustCursorPosition(i.cursorX, i.cursorY)
}
func (i *Input) IsGamepadPresent(id int) bool {
i.m.RLock()
defer i.m.RUnlock()
if len(i.gamepads) <= id {
return false
}
return i.gamepads[id].valid
}
func (i *Input) GamepadAxisNum(id int) int {
i.m.RLock()
defer i.m.RUnlock()
@ -78,6 +87,7 @@ func (in *Input) Touches() []Touch {
}
type gamePad struct {
valid bool
axisNum int
axes [16]float64
buttonNum int

View File

@ -112,9 +112,12 @@ func (i *Input) update(window *glfw.Window, scale float64) {
i.cursorX = int(x / scale)
i.cursorY = int(y / scale)
for id := glfw.Joystick(0); id < glfw.Joystick(len(i.gamepads)); id++ {
i.gamepads[id].valid = false
if !glfw.JoystickPresent(id) {
continue
}
i.gamepads[id].valid = true
axes32 := glfw.GetJoystickAxes(id)
i.gamepads[id].axisNum = len(axes32)
for a := 0; a < len(i.gamepads[id].axes); a++ {

View File

@ -139,10 +139,12 @@ func (i *Input) updateGamepads() {
gamepads := nav.Call("getGamepads")
l := gamepads.Get("length").Int()
for id := 0; id < l; id++ {
i.gamepads[id].valid = false
gamepad := gamepads.Index(id)
if gamepad == js.Undefined || gamepad == nil {
continue
}
i.gamepads[id].valid = true
axes := gamepad.Get("axes")
axesNum := axes.Get("length").Int()