inpututil: Add AppendJustConnectedGamepadIDs

Updates #1705
This commit is contained in:
Hajime Hoshi 2021-07-10 22:32:15 +09:00
parent a79c287bb7
commit e8ea4046cb
2 changed files with 23 additions and 11 deletions

View File

@ -35,6 +35,7 @@ const (
) )
type Game struct { type Game struct {
gamepadIDsBuf []ebiten.GamepadID
gamepadIDs map[ebiten.GamepadID]struct{} gamepadIDs map[ebiten.GamepadID]struct{}
axes map[ebiten.GamepadID][]string axes map[ebiten.GamepadID][]string
pressedButtons map[ebiten.GamepadID][]string pressedButtons map[ebiten.GamepadID][]string
@ -46,7 +47,8 @@ func (g *Game) Update() error {
} }
// Log the gamepad connection events. // Log the gamepad connection events.
for _, id := range inpututil.JustConnectedGamepadIDs() { g.gamepadIDsBuf = inpututil.AppendJustConnectedGamepadIDs(g.gamepadIDsBuf[:0])
for _, id := range g.gamepadIDsBuf {
log.Printf("gamepad connected: id: %d", id) log.Printf("gamepad connected: id: %d", id)
g.gamepadIDs[id] = struct{}{} g.gamepadIDs[id] = struct{}{}
} }

View File

@ -185,7 +185,7 @@ func AppendPressedKeys(keys []ebiten.Key) []ebiten.Key {
// PressedKeys returns a set of currently pressed keyboard keys. // PressedKeys returns a set of currently pressed keyboard keys.
// //
// Deprecated: as of v2.2.0. Use AppendPressedKeys instead. // Deprecated: as of v2.2. Use AppendPressedKeys instead.
func PressedKeys() []ebiten.Key { func PressedKeys() []ebiten.Key {
return AppendPressedKeys(nil) return AppendPressedKeys(nil)
} }
@ -249,24 +249,34 @@ func MouseButtonPressDuration(button ebiten.MouseButton) int {
return s return s
} }
// JustConnectedGamepadIDs returns gamepad IDs that are connected just in the current frame. // AppendJustConnectedGamepadIDs appends gamepad IDs that are connected just in the current frame to gamepadIDs,
// and returns the extended buffer.
// Giving a slice that already has enough capacity works efficiently.
// //
// JustConnectedGamepadIDs might return nil when there is no connected gamepad. // AppendJustConnectedGamepadIDs might append nothing when there is no connected gamepad.
// //
// JustConnectedGamepadIDs is concurrent safe. // AppendJustConnectedGamepadIDs is concurrent safe.
func JustConnectedGamepadIDs() []ebiten.GamepadID { func AppendJustConnectedGamepadIDs(gamepadIDs []ebiten.GamepadID) []ebiten.GamepadID {
var ids []ebiten.GamepadID origLen := len(gamepadIDs)
theInputState.m.RLock() theInputState.m.RLock()
for id := range theInputState.gamepadIDs { for id := range theInputState.gamepadIDs {
if _, ok := theInputState.prevGamepadIDs[id]; !ok { if _, ok := theInputState.prevGamepadIDs[id]; !ok {
ids = append(ids, id) gamepadIDs = append(gamepadIDs, id)
} }
} }
theInputState.m.RUnlock() theInputState.m.RUnlock()
sort.Slice(ids, func(a, b int) bool { s := gamepadIDs[origLen:]
return ids[a] < ids[b] sort.Slice(s, func(a, b int) bool {
return s[a] < s[b]
}) })
return ids return gamepadIDs
}
// JustConnectedGamepadIDs returns gamepad IDs that are connected just in the current frame.
//
// Deprecated: as of v2.2. Use AppendJustConnectedGamepadIDs instead.
func JustConnectedGamepadIDs() []ebiten.GamepadID {
return AppendJustConnectedGamepadIDs(nil)
} }
// IsGamepadJustDisconnected returns a boolean value indicating // IsGamepadJustDisconnected returns a boolean value indicating