mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
inpututil: Replace JustDisconnectedGamepadIDs with IsGamepadJustDisconnected (#505)
This commit is contained in:
parent
e4ee3100db
commit
a355d701ad
@ -32,12 +32,21 @@ const (
|
|||||||
screenHeight = 480
|
screenHeight = 480
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
gamepadIDs = map[int]struct{}{}
|
||||||
|
)
|
||||||
|
|
||||||
func update(screen *ebiten.Image) error {
|
func update(screen *ebiten.Image) error {
|
||||||
|
// Log the gamepad connection events.
|
||||||
for _, id := range inpututil.JustConnectedGamepadIDs() {
|
for _, id := range inpututil.JustConnectedGamepadIDs() {
|
||||||
log.Printf("gamepad connected: id: %d", id)
|
log.Printf("gamepad connected: id: %d", id)
|
||||||
|
gamepadIDs[id] = struct{}{}
|
||||||
}
|
}
|
||||||
for _, id := range inpututil.JustDisconnectedGamepadIDs() {
|
for id := range gamepadIDs {
|
||||||
log.Printf("gamepad disconnected: id: %d", id)
|
if inpututil.IsGamepadJustDisconnected(id) {
|
||||||
|
log.Printf("gamepad disconnected: id: %d", id)
|
||||||
|
delete(gamepadIDs, id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ids := ebiten.GamepadIDs()
|
ids := ebiten.GamepadIDs()
|
||||||
@ -56,7 +65,7 @@ func update(screen *ebiten.Image) error {
|
|||||||
pressedButtons[id] = append(pressedButtons[id], strconv.Itoa(int(b)))
|
pressedButtons[id] = append(pressedButtons[id], strconv.Itoa(int(b)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log some events.
|
// Log button events.
|
||||||
if inpututil.IsGamepadButtonJustPressed(id, b) {
|
if inpututil.IsGamepadButtonJustPressed(id, b) {
|
||||||
log.Printf("button pressed: id: %d, button: %d", id, b)
|
log.Printf("button pressed: id: %d, button: %d", id, b)
|
||||||
}
|
}
|
||||||
|
@ -243,22 +243,16 @@ func JustConnectedGamepadIDs() []int {
|
|||||||
return ids
|
return ids
|
||||||
}
|
}
|
||||||
|
|
||||||
// JustDisconnectedGamepadIDs returns gamepad IDs that are disconnected just in the current frame.
|
// IsGamepadJustDisconnected returns a boolean value indicating
|
||||||
|
// whether the gamepad of the given id is released just in the current frame.
|
||||||
//
|
//
|
||||||
// JustDisconnectedGamepadIDs might return nil when there is no disconnected gamepad.
|
// IsGamepadJustDisconnected is concurrent safe.
|
||||||
//
|
func IsGamepadJustDisconnected(id int) bool {
|
||||||
// JustDisconnectedGamepadIDs is concurrent safe.
|
|
||||||
func JustDisconnectedGamepadIDs() []int {
|
|
||||||
var ids []int
|
|
||||||
theInputState.m.RLock()
|
theInputState.m.RLock()
|
||||||
for id := range theInputState.prevGamepadIDs {
|
_, prev := theInputState.prevGamepadIDs[id]
|
||||||
if _, ok := theInputState.gamepadIDs[id]; !ok {
|
_, current := theInputState.gamepadIDs[id]
|
||||||
ids = append(ids, id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
theInputState.m.RUnlock()
|
theInputState.m.RUnlock()
|
||||||
sort.Ints(ids)
|
return prev && !current
|
||||||
return ids
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsGamepadButtonJustPressed returns a boolean value indicating
|
// IsGamepadButtonJustPressed returns a boolean value indicating
|
||||||
|
Loading…
Reference in New Issue
Block a user