mobile/ebitenmobileview: refactoring

This commit is contained in:
Hajime Hoshi 2022-01-07 17:50:49 +09:00
parent 7e08333dd9
commit f51d691d87

View File

@ -211,14 +211,23 @@ func UpdateTouchesOnAndroid(action int, id int, x, y int) {
} }
} }
func gamepadFromGamepadID(id driver.GamepadID) *mobile.Gamepad {
for i, g := range gamepads {
if g.ID == id {
return &gamepads[i]
}
}
return nil
}
func OnKeyDownOnAndroid(keyCode int, unicodeChar int, source int, deviceID int) { func OnKeyDownOnAndroid(keyCode int, unicodeChar int, source int, deviceID int) {
switch { switch {
case source&sourceGamepad == sourceGamepad: case source&sourceGamepad == sourceGamepad:
// A gamepad can be detected as a keyboard. Detect the device as a gamepad first. // A gamepad can be detected as a keyboard. Detect the device as a gamepad first.
if button, ok := androidKeyToGamepadButton[keyCode]; ok { if button, ok := androidKeyToGamepadButton[keyCode]; ok {
id := gamepadIDFromDeviceID(deviceID) id := gamepadIDFromDeviceID(deviceID)
g, ok := gamepads[id] g := gamepadFromGamepadID(id)
if !ok { if g == nil {
return return
} }
g.Buttons[button] = true g.Buttons[button] = true
@ -243,8 +252,8 @@ func OnKeyUpOnAndroid(keyCode int, source int, deviceID int) {
// A gamepad can be detected as a keyboard. Detect the device as a gamepad first. // A gamepad can be detected as a keyboard. Detect the device as a gamepad first.
if button, ok := androidKeyToGamepadButton[keyCode]; ok { if button, ok := androidKeyToGamepadButton[keyCode]; ok {
id := gamepadIDFromDeviceID(deviceID) id := gamepadIDFromDeviceID(deviceID)
g, ok := gamepads[id] g := gamepadFromGamepadID(id)
if !ok { if g == nil {
return return
} }
g.Buttons[button] = false g.Buttons[button] = false
@ -261,9 +270,9 @@ func OnKeyUpOnAndroid(keyCode int, source int, deviceID int) {
} }
func OnGamepadAxesChanged(deviceID int, axisID int, value float32) { func OnGamepadAxesChanged(deviceID int, axisID int, value float32) {
did := gamepadIDFromDeviceID(deviceID) id := gamepadIDFromDeviceID(deviceID)
g, ok := gamepads[did] g := gamepadFromGamepadID(id)
if !ok { if g == nil {
return return
} }
aid, ok := androidAxisIDToAxisID[axisID] aid, ok := androidAxisIDToAxisID[axisID]
@ -302,33 +311,37 @@ func OnGamepadAdded(deviceID int, name string, buttonNum int, axisNum int, descr
sdlid[15] = byte(axisMask >> 8) sdlid[15] = byte(axisMask >> 8)
id := gamepadIDFromDeviceID(deviceID) id := gamepadIDFromDeviceID(deviceID)
gamepads[id] = &mobile.Gamepad{ gamepads = append(gamepads, mobile.Gamepad{
ID: id, ID: id,
SDLID: hex.EncodeToString(sdlid[:]), SDLID: hex.EncodeToString(sdlid[:]),
Name: name, Name: name,
ButtonNum: buttonNum, ButtonNum: buttonNum,
AxisNum: axisNum, AxisNum: axisNum,
} })
updateGamepads() updateGamepads()
} }
func OnInputDeviceRemoved(deviceID int) { func OnInputDeviceRemoved(deviceID int) {
if id, ok := deviceIDToGamepadID[deviceID]; ok { if id, ok := deviceIDToGamepadID[deviceID]; ok {
delete(gamepads, id) idx := -1
for i, g := range gamepads {
if g.ID == id {
idx = i
break
}
}
if idx >= 0 {
lastIdx := len(gamepads) - 1
gamepads[idx], gamepads[lastIdx] = gamepads[lastIdx], gamepads[idx]
gamepads = gamepads[:len(gamepads)-1]
}
delete(deviceIDToGamepadID, deviceID) delete(deviceIDToGamepadID, deviceID)
} }
updateGamepads() updateGamepads()
} }
var ( var gamepads []mobile.Gamepad
gamepads = map[driver.GamepadID]*mobile.Gamepad{}
gamepadSlice []mobile.Gamepad
)
func updateGamepads() { func updateGamepads() {
gamepadSlice = gamepadSlice[:0] mobile.Get().UpdateGamepads(gamepads)
for _, g := range gamepads {
gamepadSlice = append(gamepadSlice, *g)
}
mobile.Get().UpdateGamepads(gamepadSlice)
} }