Skip to content

Commit

Permalink
inpututil: Add AppendJustConnectedGamepadIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Jul 10, 2021
1 parent a79c287 commit e8ea404
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
4 changes: 3 additions & 1 deletion examples/gamepad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
)

type Game struct {
gamepadIDsBuf []ebiten.GamepadID
gamepadIDs map[ebiten.GamepadID]struct{}
axes map[ebiten.GamepadID][]string
pressedButtons map[ebiten.GamepadID][]string
Expand All @@ -46,7 +47,8 @@ func (g *Game) Update() error {
}

// 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)
g.gamepadIDs[id] = struct{}{}
}
Expand Down
30 changes: 20 additions & 10 deletions inpututil/inpututil.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func AppendPressedKeys(keys []ebiten.Key) []ebiten.Key {

// 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 {
return AppendPressedKeys(nil)
}
Expand Down Expand Up @@ -249,24 +249,34 @@ func MouseButtonPressDuration(button ebiten.MouseButton) int {
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.
func JustConnectedGamepadIDs() []ebiten.GamepadID {
var ids []ebiten.GamepadID
// AppendJustConnectedGamepadIDs is concurrent safe.
func AppendJustConnectedGamepadIDs(gamepadIDs []ebiten.GamepadID) []ebiten.GamepadID {
origLen := len(gamepadIDs)
theInputState.m.RLock()
for id := range theInputState.gamepadIDs {
if _, ok := theInputState.prevGamepadIDs[id]; !ok {
ids = append(ids, id)
gamepadIDs = append(gamepadIDs, id)
}
}
theInputState.m.RUnlock()
sort.Slice(ids, func(a, b int) bool {
return ids[a] < ids[b]
s := gamepadIDs[origLen:]
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
Expand Down

0 comments on commit e8ea404

Please sign in to comment.