uidriver/js: Refactoring: Use a map for gamepad states

This change improves the consistency with touches.
This commit is contained in:
Hajime Hoshi 2020-12-27 17:54:00 +09:00
parent 3d48922d8c
commit 8e6792ca23

View File

@ -60,8 +60,7 @@ type pos struct {
Y int Y int
} }
type gamePad struct { type gamepad struct {
valid bool
name string name string
axisNum int axisNum int
axes [16]float64 axes [16]float64
@ -77,7 +76,7 @@ type Input struct {
cursorY int cursorY int
wheelX float64 wheelX float64
wheelY float64 wheelY float64
gamepads [16]gamePad gamepads map[driver.GamepadID]gamepad
touches map[driver.TouchID]pos touches map[driver.TouchID]pos
runeBuffer []rune runeBuffer []rune
ui *UserInterface ui *UserInterface
@ -113,41 +112,50 @@ func (i *Input) GamepadIDs() []driver.GamepadID {
if len(i.gamepads) == 0 { if len(i.gamepads) == 0 {
return nil return nil
} }
var r []driver.GamepadID var r []driver.GamepadID
for id, g := range i.gamepads { for id := range i.gamepads {
if g.valid { r = append(r, id)
r = append(r, driver.GamepadID(id))
}
} }
return r return r
} }
func (i *Input) GamepadAxisNum(id driver.GamepadID) int { func (i *Input) GamepadAxisNum(id driver.GamepadID) int {
if len(i.gamepads) <= int(id) { g, ok := i.gamepads[id]
if !ok {
return 0 return 0
} }
return i.gamepads[id].axisNum return g.axisNum
} }
func (i *Input) GamepadAxis(id driver.GamepadID, axis int) float64 { func (i *Input) GamepadAxis(id driver.GamepadID, axis int) float64 {
if len(i.gamepads) <= int(id) { g, ok := i.gamepads[id]
if !ok {
return 0 return 0
} }
return i.gamepads[id].axes[axis] if g.axisNum <= axis {
return 0
}
return g.axes[axis]
} }
func (i *Input) GamepadButtonNum(id driver.GamepadID) int { func (i *Input) GamepadButtonNum(id driver.GamepadID) int {
if len(i.gamepads) <= int(id) { g, ok := i.gamepads[id]
if !ok {
return 0 return 0
} }
return i.gamepads[id].buttonNum return g.buttonNum
} }
func (i *Input) IsGamepadButtonPressed(id driver.GamepadID, button driver.GamepadButton) bool { func (i *Input) IsGamepadButtonPressed(id driver.GamepadID, button driver.GamepadButton) bool {
if len(i.gamepads) <= int(id) { g, ok := i.gamepads[id]
if !ok {
return false return false
} }
return i.gamepads[id].buttonPressed[button] if g.buttonNum <= int(button) {
return false
}
return g.buttonPressed[button]
} }
func (i *Input) TouchIDs() []driver.TouchID { func (i *Input) TouchIDs() []driver.TouchID {
@ -280,41 +288,46 @@ func (i *Input) updateGamepads() {
if !nav.Truthy() { if !nav.Truthy() {
return return
} }
if !nav.Get("getGamepads").Truthy() { if !nav.Get("getGamepads").Truthy() {
return return
} }
i.gamepads = map[driver.GamepadID]gamepad{}
gamepads := nav.Call("getGamepads") gamepads := nav.Call("getGamepads")
l := gamepads.Get("length").Int() l := gamepads.Length()
for id := 0; id < l; id++ { for idx := 0; idx < l; idx++ {
i.gamepads[id].valid = false gp := gamepads.Index(idx)
gamepad := gamepads.Index(id) if !gp.Truthy() {
if !gamepad.Truthy() {
continue continue
} }
i.gamepads[id].valid = true
i.gamepads[id].name = gamepad.Get("id").String()
axes := gamepad.Get("axes") id := driver.GamepadID(gp.Get("index").Int())
g := gamepad{}
g.name = gp.Get("id").String()
axes := gp.Get("axes")
axesNum := axes.Get("length").Int() axesNum := axes.Get("length").Int()
i.gamepads[id].axisNum = axesNum g.axisNum = axesNum
for a := 0; a < len(i.gamepads[id].axes); a++ { for a := 0; a < len(g.axes); a++ {
if axesNum <= a { if axesNum <= a {
i.gamepads[id].axes[a] = 0 break
continue
} }
i.gamepads[id].axes[a] = axes.Index(a).Float() g.axes[a] = axes.Index(a).Float()
} }
buttons := gamepad.Get("buttons") buttons := gp.Get("buttons")
buttonsNum := buttons.Get("length").Int() buttonsNum := buttons.Get("length").Int()
i.gamepads[id].buttonNum = buttonsNum g.buttonNum = buttonsNum
for b := 0; b < len(i.gamepads[id].buttonPressed); b++ { for b := 0; b < len(g.buttonPressed); b++ {
if buttonsNum <= b { if buttonsNum <= b {
i.gamepads[id].buttonPressed[b] = false break
continue
} }
i.gamepads[id].buttonPressed[b] = buttons.Index(b).Get("pressed").Bool() g.buttonPressed[b] = buttons.Index(b).Get("pressed").Bool()
} }
i.gamepads[id] = g
} }
} }
@ -402,8 +415,8 @@ func (i *Input) updateForGo2Cpp() {
} }
i.touches = map[driver.TouchID]pos{} i.touches = map[driver.TouchID]pos{}
maxID := go2cpp.Get("touchCount").Int() touchCount := go2cpp.Get("touchCount").Int()
for idx := 0; idx < maxID; idx++ { for idx := 0; idx < touchCount; idx++ {
id := go2cpp.Call("getTouchPositionId", idx) id := go2cpp.Call("getTouchPositionId", idx)
x := go2cpp.Call("getTouchPositionX", idx) x := go2cpp.Call("getTouchPositionX", idx)
y := go2cpp.Call("getTouchPositionY", idx) y := go2cpp.Call("getTouchPositionY", idx)