ebiten: use the DB's name for GamepadName

Updates #1949
This commit is contained in:
Hajime Hoshi 2022-01-25 21:13:59 +09:00
parent cdf2335f5a
commit fb3a022327
5 changed files with 20 additions and 0 deletions

View File

@ -163,6 +163,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
standard = " (Standard Layout)"
}
str += fmt.Sprintf("Gamepad (ID: %d, SDL ID: %s)%s:\n", id, ebiten.GamepadSDLID(id), standard)
str += fmt.Sprintf(" Name: %s\n", ebiten.GamepadName(id))
str += fmt.Sprintf(" Axes: %s\n", strings.Join(g.axes[id], ", "))
str += fmt.Sprintf(" Buttons: %s\n", strings.Join(g.pressedButtons[id], ", "))
if ebiten.IsStandardGamepadLayoutAvailable(id) {

View File

@ -163,6 +163,9 @@ func (g *Gamepad) update() {
func (g *Gamepad) Name() string {
// This is immutable and doesn't have to be protected by a mutex.
if name := gamepaddb.Name(g.sdlID); name != "" {
return name
}
return g.name
}

View File

@ -126,6 +126,7 @@ type mapping struct {
}
var (
gamepadNames = map[string]string{}
gamepadButtonMappings = map[string]map[driver.StandardGamepadButton]*mapping{}
gamepadAxisMappings = map[string]map[driver.StandardGamepadAxis]*mapping{}
mappingsM sync.RWMutex
@ -203,6 +204,8 @@ func processLine(line string, platform platform) error {
// There is no corresponding button in the Web standard gamepad layout.
}
gamepadNames[id] = tokens[1]
return nil
}
@ -378,6 +381,13 @@ type GamepadState interface {
Hat(index int) int
}
func Name(id string) string {
mappingsM.RLock()
defer mappingsM.RUnlock()
return gamepadNames[id]
}
func AxisValue(id string, axis driver.StandardGamepadAxis, state GamepadState) float64 {
mappingsM.RLock()
defer mappingsM.RUnlock()

View File

@ -119,6 +119,9 @@ func (i *Input) GamepadName(id driver.GamepadID) string {
if len(i.gamepads) <= int(id) {
return ""
}
if name := gamepaddb.Name(i.gamepads[id].guid); name != "" {
return name
}
return i.gamepads[id].name
}

View File

@ -67,6 +67,9 @@ func (i *Input) GamepadName(id driver.GamepadID) string {
if g.ID != id {
continue
}
if name := gamepaddb.Name(g.SDLID); name != "" {
return name
}
return g.Name
}
return ""