uidriver/js: Implement GamepadSDLID

Fixes #1053
This commit is contained in:
Hajime Hoshi 2020-02-04 23:59:57 +09:00
parent 4be49230de
commit d6d17a7e85
2 changed files with 10 additions and 4 deletions

View File

@ -83,7 +83,7 @@ func update(screen *ebiten.Image) error {
str := ""
if len(ids) > 0 {
for _, id := range ids {
str += fmt.Sprintf("Gamepad (ID: %d):\n", id)
str += fmt.Sprintf("Gamepad (ID: %d, SDL ID: %s):\n", id, ebiten.GamepadSDLID(id))
str += fmt.Sprintf(" Axes: %s\n", strings.Join(axes[id], ", "))
str += fmt.Sprintf(" Buttons: %s\n", strings.Join(pressedButtons[id], ", "))
str += "\n"

View File

@ -17,6 +17,7 @@
package js
import (
"encoding/hex"
"syscall/js"
"unicode"
@ -58,9 +59,14 @@ func (i *Input) CursorPosition() (x, y int) {
}
func (i *Input) GamepadSDLID(id int) string {
// TODO: Implement this. See the implementation of SDL:
// https://github.com/spurious/SDL-mirror/blob/master/src/joystick/emscripten/SDL_sysjoystick.c
return ""
// This emulates the implementation of EMSCRIPTEN_JoystickGetDeviceGUID.
// https://hg.libsdl.org/SDL/file/bc90ce38f1e2/src/joystick/emscripten/SDL_sysjoystick.c#l385
if len(i.gamepads) <= id {
return ""
}
var sdlid [16]byte
copy(sdlid[:], []byte(i.gamepads[id].name))
return hex.EncodeToString(sdlid[:])
}
// GamepadName returns a string containing some information about the controller.