Skip to content

Commit

Permalink
ebiten: Introduce type GamepadID
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Oct 7, 2020
1 parent bb20e62 commit 809b7a3
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 95 deletions.
4 changes: 2 additions & 2 deletions examples/blocks/blocks/gamepad.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type axis struct {
}

type gamepadConfig struct {
gamepadID int
gamepadID ebiten.GamepadID
gamepadIDInitialized bool

current virtualGamepadButton
Expand All @@ -59,7 +59,7 @@ type gamepadConfig struct {
defaultAxesValues map[int]float64
}

func (c *gamepadConfig) SetGamepadID(id int) {
func (c *gamepadConfig) SetGamepadID(id ebiten.GamepadID) {
c.gamepadID = id
c.gamepadIDInitialized = true
}
Expand Down
2 changes: 1 addition & 1 deletion examples/blocks/blocks/gamepadscene.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

type GamepadScene struct {
gamepadID int
gamepadID ebiten.GamepadID
currentIndex int
countAfterSetting int
buttonStates []string
Expand Down
2 changes: 1 addition & 1 deletion examples/blocks/blocks/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Input struct {

// GamepadIDButtonPressed returns a gamepad ID where at least one button is pressed.
// If no button is pressed, GamepadIDButtonPressed returns -1.
func (i *Input) GamepadIDButtonPressed() int {
func (i *Input) GamepadIDButtonPressed() ebiten.GamepadID {
for _, id := range ebiten.GamepadIDs() {
for b := ebiten.GamepadButton(0); b <= ebiten.GamepadButtonMax; b++ {
if ebiten.IsGamepadButtonPressed(id, b) {
Expand Down
18 changes: 10 additions & 8 deletions examples/gamepad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ const (
)

type Game struct {
gamepadIDs map[int]struct{}
axes map[int][]string
pressedButtons map[int][]string
gamepadIDs map[ebiten.GamepadID]struct{}
axes map[ebiten.GamepadID][]string
pressedButtons map[ebiten.GamepadID][]string
}

func (g *Game) Update() error {
if g.gamepadIDs == nil {
g.gamepadIDs = map[int]struct{}{}
g.gamepadIDs = map[ebiten.GamepadID]struct{}{}
}

// Log the gamepad connection events.
Expand All @@ -56,8 +56,8 @@ func (g *Game) Update() error {
}
}

g.axes = map[int][]string{}
g.pressedButtons = map[int][]string{}
g.axes = map[ebiten.GamepadID][]string{}
g.pressedButtons = map[ebiten.GamepadID][]string{}
for id := range g.gamepadIDs {
maxAxis := ebiten.GamepadAxisNum(id)
for a := 0; a < maxAxis; a++ {
Expand Down Expand Up @@ -86,11 +86,13 @@ func (g *Game) Draw(screen *ebiten.Image) {
// Draw the current gamepad status.
str := ""
if len(g.gamepadIDs) > 0 {
ids := make([]int, 0, len(g.gamepadIDs))
ids := make([]ebiten.GamepadID, 0, len(g.gamepadIDs))
for id := range g.gamepadIDs {
ids = append(ids, id)
}
sort.Ints(ids)
sort.Slice(ids, func(a, b int) bool {
return ids[a] < ids[b]
})
for _, id := range ids {
str += fmt.Sprintf("Gamepad (ID: %d, SDL ID: %s):\n", id, ebiten.GamepadSDLID(id))
str += fmt.Sprintf(" Axes: %s\n", strings.Join(g.axes[id], ", "))
Expand Down
19 changes: 11 additions & 8 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,21 @@ func IsMouseButtonPressed(mouseButton MouseButton) bool {
return uiDriver().Input().IsMouseButtonPressed(driver.MouseButton(mouseButton))
}

// GamepadID represents a gamepad's identifier.
type GamepadID = driver.GamepadID

// GamepadSDLID returns a string with the GUID generated in the same way as SDL.
// To detect devices, see also the community project of gamepad devices database: https://github.com/gabomdq/SDL_GameControllerDB
//
// GamepadSDLID always returns an empty string on browsers and mobiles.
//
// GamepadSDLID is concurrent-safe.
func GamepadSDLID(id int) string {
func GamepadSDLID(id GamepadID) string {
return uiDriver().Input().GamepadSDLID(id)
}

// GamepadName returns a string with the name.
// This function may vary in how it returns descriptions for the same device across platforms
// This function may vary in how it returns descriptions for the same device across platforms.
// for example the following drivers/platforms see a Xbox One controller as the following:
//
// - Windows: "Xbox Controller"
Expand All @@ -124,7 +127,7 @@ func GamepadSDLID(id int) string {
// GamepadName always returns an empty string on mobiles.
//
// GamepadName is concurrent-safe.
func GamepadName(id int) string {
func GamepadName(id GamepadID) string {
return uiDriver().Input().GamepadName(id)
}

Expand All @@ -133,7 +136,7 @@ func GamepadName(id int) string {
// GamepadIDs is concurrent-safe.
//
// GamepadIDs always returns an empty slice on mobiles.
func GamepadIDs() []int {
func GamepadIDs() []GamepadID {
return uiDriver().Input().GamepadIDs()
}

Expand All @@ -142,7 +145,7 @@ func GamepadIDs() []int {
// GamepadAxisNum is concurrent-safe.
//
// GamepadAxisNum always returns 0 on mobiles.
func GamepadAxisNum(id int) int {
func GamepadAxisNum(id GamepadID) int {
return uiDriver().Input().GamepadAxisNum(id)
}

Expand All @@ -151,7 +154,7 @@ func GamepadAxisNum(id int) int {
// GamepadAxis is concurrent-safe.
//
// GamepadAxis always returns 0 on mobiles.
func GamepadAxis(id int, axis int) float64 {
func GamepadAxis(id GamepadID, axis int) float64 {
return uiDriver().Input().GamepadAxis(id, axis)
}

Expand All @@ -160,7 +163,7 @@ func GamepadAxis(id int, axis int) float64 {
// GamepadButtonNum is concurrent-safe.
//
// GamepadButtonNum always returns 0 on mobiles.
func GamepadButtonNum(id int) int {
func GamepadButtonNum(id GamepadID) int {
return uiDriver().Input().GamepadButtonNum(id)
}

Expand All @@ -175,7 +178,7 @@ func GamepadButtonNum(id int) int {
// There can be differences even between Chrome and Firefox.
//
// IsGamepadButtonPressed always returns false on mobiles.
func IsGamepadButtonPressed(id int, button GamepadButton) bool {
func IsGamepadButtonPressed(id GamepadID, button GamepadButton) bool {
return uiDriver().Input().IsGamepadButtonPressed(id, driver.GamepadButton(button))
}

Expand Down
50 changes: 26 additions & 24 deletions inpututil/inpututil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{},
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}

Expand All @@ -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)
}
}
Expand Down Expand Up @@ -221,24 +221,26 @@ 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 {
ids = append(ids, id)
}
}
theInputState.m.RUnlock()
sort.Ints(ids)
sort.Slice(ids, func(a, b int) bool {
return ids[a] < ids[b]
})
return ids
}

// IsGamepadJustDisconnected returns a boolean value indicating
// 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]
Expand All @@ -250,15 +252,15 @@ 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
}

// IsGamepadButtonJustReleased returns a boolean value indicating
// 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 {
Expand All @@ -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 {
Expand Down
16 changes: 9 additions & 7 deletions internal/driver/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@

package driver

type GamepadID int

type Input interface {
CursorPosition() (x, y int)
GamepadSDLID(id int) string
GamepadName(id int) string
GamepadAxis(id int, axis int) float64
GamepadAxisNum(id int) int
GamepadButtonNum(id int) int
GamepadIDs() []int
IsGamepadButtonPressed(id int, button GamepadButton) bool
GamepadSDLID(id GamepadID) string
GamepadName(id GamepadID) string
GamepadAxis(id GamepadID, axis int) float64
GamepadAxisNum(id GamepadID) int
GamepadButtonNum(id GamepadID) int
GamepadIDs() []GamepadID
IsGamepadButtonPressed(id GamepadID, button GamepadButton) bool
IsKeyPressed(key Key) bool
IsMouseButtonPressed(button MouseButton) bool
RuneBuffer() []rune
Expand Down
Loading

0 comments on commit 809b7a3

Please sign in to comment.