ebiten: Introduce type GamepadID

Updates #604
This commit is contained in:
Hajime Hoshi
2020-10-08 01:08:30 +09:00
parent bb20e62435
commit 809b7a3afa
13 changed files with 104 additions and 95 deletions
+26 -24
View File
@@ -30,11 +30,11 @@ type inputState struct {
mouseButtonDurations map[ebiten.MouseButton]int
prevMouseButtonDurations map[ebiten.MouseButton]int
gamepadIDs map[int]struct{}
prevGamepadIDs map[int]struct{}
gamepadIDs map[ebiten.GamepadID]struct{}
prevGamepadIDs map[ebiten.GamepadID]struct{}
gamepadButtonDurations map[int][]int
prevGamepadButtonDurations map[int][]int
gamepadButtonDurations map[ebiten.GamepadID][]int
prevGamepadButtonDurations map[ebiten.GamepadID][]int
touchDurations map[int]int
prevTouchDurations map[int]int
@@ -49,11 +49,11 @@ var theInputState = &inputState{
mouseButtonDurations: map[ebiten.MouseButton]int{},
prevMouseButtonDurations: map[ebiten.MouseButton]int{},
gamepadIDs: map[int]struct{}{},
prevGamepadIDs: map[int]struct{}{},
gamepadIDs: map[ebiten.GamepadID]struct{}{},
prevGamepadIDs: map[ebiten.GamepadID]struct{}{},
gamepadButtonDurations: map[int][]int{},
prevGamepadButtonDurations: map[int][]int{},
gamepadButtonDurations: map[ebiten.GamepadID][]int{},
prevGamepadButtonDurations: map[ebiten.GamepadID][]int{},
touchDurations: map[int]int{},
prevTouchDurations: map[int]int{},
@@ -97,18 +97,18 @@ func (i *inputState) update() {
// Gamepads
// Copy the gamepad IDs.
i.prevGamepadIDs = map[int]struct{}{}
i.prevGamepadIDs = map[ebiten.GamepadID]struct{}{}
for id := range i.gamepadIDs {
i.prevGamepadIDs[id] = struct{}{}
}
// Copy the gamepad button durations.
i.prevGamepadButtonDurations = map[int][]int{}
i.prevGamepadButtonDurations = map[ebiten.GamepadID][]int{}
for id, ds := range i.gamepadButtonDurations {
i.prevGamepadButtonDurations[id] = append([]int{}, ds...)
}
i.gamepadIDs = map[int]struct{}{}
i.gamepadIDs = map[ebiten.GamepadID]struct{}{}
for _, id := range ebiten.GamepadIDs() {
i.gamepadIDs[id] = struct{}{}
if _, ok := i.gamepadButtonDurations[id]; !ok {
@@ -123,13 +123,13 @@ func (i *inputState) update() {
}
}
}
idsToDelete := []int{}
gamepadIDsToDelete := []ebiten.GamepadID{}
for id := range i.gamepadButtonDurations {
if _, ok := i.gamepadIDs[id]; !ok {
idsToDelete = append(idsToDelete, id)
gamepadIDsToDelete = append(gamepadIDsToDelete, id)
}
}
for _, id := range idsToDelete {
for _, id := range gamepadIDsToDelete {
delete(i.gamepadButtonDurations, id)
}
@@ -146,13 +146,13 @@ func (i *inputState) update() {
ids[id] = struct{}{}
i.touchDurations[id]++
}
idsToDelete = []int{}
touchIDsToDelete := []int{}
for id := range i.touchDurations {
if _, ok := ids[id]; !ok {
idsToDelete = append(idsToDelete, id)
touchIDsToDelete = append(touchIDsToDelete, id)
}
}
for _, id := range idsToDelete {
for _, id := range touchIDsToDelete {
delete(i.touchDurations, id)
}
}
@@ -221,8 +221,8 @@ func MouseButtonPressDuration(button ebiten.MouseButton) int {
// JustConnectedGamepadIDs might return nil when there is no connected gamepad.
//
// JustConnectedGamepadIDs is concurrent safe.
func JustConnectedGamepadIDs() []int {
var ids []int
func JustConnectedGamepadIDs() []ebiten.GamepadID {
var ids []ebiten.GamepadID
theInputState.m.RLock()
for id := range theInputState.gamepadIDs {
if _, ok := theInputState.prevGamepadIDs[id]; !ok {
@@ -230,7 +230,9 @@ func JustConnectedGamepadIDs() []int {
}
}
theInputState.m.RUnlock()
sort.Ints(ids)
sort.Slice(ids, func(a, b int) bool {
return ids[a] < ids[b]
})
return ids
}
@@ -238,7 +240,7 @@ func JustConnectedGamepadIDs() []int {
// whether the gamepad of the given id is released just in the current frame.
//
// IsGamepadJustDisconnected is concurrent safe.
func IsGamepadJustDisconnected(id int) bool {
func IsGamepadJustDisconnected(id ebiten.GamepadID) bool {
theInputState.m.RLock()
_, prev := theInputState.prevGamepadIDs[id]
_, current := theInputState.gamepadIDs[id]
@@ -250,7 +252,7 @@ func IsGamepadJustDisconnected(id int) bool {
// whether the given gamepad button of the gamepad id is pressed just in the current frame.
//
// IsGamepadButtonJustPressed is concurrent safe.
func IsGamepadButtonJustPressed(id int, button ebiten.GamepadButton) bool {
func IsGamepadButtonJustPressed(id ebiten.GamepadID, button ebiten.GamepadButton) bool {
return GamepadButtonPressDuration(id, button) == 1
}
@@ -258,7 +260,7 @@ func IsGamepadButtonJustPressed(id int, button ebiten.GamepadButton) bool {
// whether the given gamepad button of the gamepad id is released just in the current frame.
//
// IsGamepadButtonJustReleased is concurrent safe.
func IsGamepadButtonJustReleased(id int, button ebiten.GamepadButton) bool {
func IsGamepadButtonJustReleased(id ebiten.GamepadID, button ebiten.GamepadButton) bool {
theInputState.m.RLock()
prev := 0
if _, ok := theInputState.prevGamepadButtonDurations[id]; ok {
@@ -275,7 +277,7 @@ func IsGamepadButtonJustReleased(id int, button ebiten.GamepadButton) bool {
// GamepadButtonPressDuration returns how long the gamepad button of the gamepad id is pressed in frames.
//
// GamepadButtonPressDuration is concurrent safe.
func GamepadButtonPressDuration(id int, button ebiten.GamepadButton) int {
func GamepadButtonPressDuration(id ebiten.GamepadID, button ebiten.GamepadButton) int {
theInputState.m.RLock()
s := 0
if _, ok := theInputState.gamepadButtonDurations[id]; ok {