ui: Add GamepadGUID and GamepadName functions (#1035)

Fixes #1034
This commit is contained in:
Jake Bentvelzen 2020-01-10 00:11:32 +11:00 committed by Hajime Hoshi
parent 6ee587301c
commit c17946bb82
7 changed files with 125 additions and 0 deletions

View File

@ -92,6 +92,29 @@ func IsMouseButtonPressed(mouseButton MouseButton) bool {
return uiDriver().Input().IsMouseButtonPressed(driver.MouseButton(mouseButton))
}
// GamepadGUID returns a string with the uuid.
//
// GamepadGUID is concurrent-safe.
//
// GamepadGUID always returns an empty string on browsers and mobiles.
func GamepadGUID(id int) string {
return uiDriver().Input().GamepadGUID(id)
}
// GamepadName returns a string with the name.
// 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"
// - Chrome: "Xbox 360 Controller (XInput STANDARD GAMEPAD)"
// - Firefox: "xinput"
//
// GamepadName is concurrent-safe.
//
// GamepadName always returns an empty string on mobiles.
func GamepadName(id int) string {
return uiDriver().Input().GamepadName(id)
}
// GamepadIDs returns a slice indicating available gamepad IDs.
//
// GamepadIDs is concurrent-safe.

View File

@ -16,6 +16,8 @@ package driver
type Input interface {
CursorPosition() (x, y int)
GamepadGUID(id int) string
GamepadName(id int) string
GamepadAxis(id int, axis int) float64
GamepadAxisNum(id int) int
GamepadButtonNum(id int) int

View File

@ -235,6 +235,14 @@ func CreateWindow(width, height int, title string, monitor *Monitor, share *Wind
return theWindows.add(w), nil
}
func (j Joystick) GetGUID() string {
return glfw.Joystick(j).GetGUID()
}
func (j Joystick) GetName() string {
return glfw.Joystick(j).GetName()
}
func (j Joystick) GetAxes() []float32 {
return glfw.Joystick(j).GetAxes()
}

View File

@ -311,6 +311,40 @@ func CreateWindow(width, height int, title string, monitor *Monitor, share *Wind
return theGLFWWindows.add(w), nil
}
func (j Joystick) GetGUID() string {
ptr := glfwDLL.call("glfwGetJoystickGUID", uintptr(j))
panicError()
var backed [256]byte
as := backed[:0]
for i := int32(0); ; i++ {
b := *(*byte)(unsafe.Pointer(ptr))
ptr += unsafe.Sizeof(byte(0))
if b == 0 {
break
}
as = append(as, b)
}
r := string(as)
return r
}
func (j Joystick) GetName() string {
ptr := glfwDLL.call("glfwGetJoystickName", uintptr(j))
panicError()
var backed [256]byte
as := backed[:0]
for i := int32(0); ; i++ {
b := *(*byte)(unsafe.Pointer(ptr))
ptr += unsafe.Sizeof(byte(0))
if b == 0 {
break
}
as = append(as, b)
}
r := string(as)
return r
}
func (j Joystick) GetAxes() []float32 {
var l int32
ptr := glfwDLL.call("glfwGetJoystickAxes", uintptr(j), uintptr(unsafe.Pointer(&l)))

View File

@ -29,6 +29,8 @@ import (
type gamePad struct {
valid bool
guid string
name string
axisNum int
axes [16]float64
buttonNum int
@ -85,6 +87,36 @@ func (i *Input) GamepadIDs() []int {
return r
}
func (i *Input) GamepadGUID(id int) string {
if !i.ui.isRunning() {
return ""
}
var r string
_ = i.ui.t.Call(func() error {
if len(i.gamepads) <= id {
return nil
}
r = i.gamepads[id].guid
return nil
})
return r
}
func (i *Input) GamepadName(id int) string {
if !i.ui.isRunning() {
return ""
}
var r string
_ = i.ui.t.Call(func() error {
if len(i.gamepads) <= id {
return nil
}
r = i.gamepads[id].name
return nil
})
return r
}
func (i *Input) GamepadAxisNum(id int) int {
if !i.ui.isRunning() {
return 0
@ -324,6 +356,8 @@ func (i *Input) update(window *glfw.Window, context driver.UIContext) {
continue
}
i.gamepads[id].valid = true
i.gamepads[id].guid = id.GetGUID()
i.gamepads[id].name = id.GetName()
axes32 := id.GetAxes()
i.gamepads[id].axisNum = len(axes32)

View File

@ -31,6 +31,7 @@ type pos struct {
type gamePad struct {
valid bool
name string
axisNum int
axes [16]float64
buttonNum int
@ -56,6 +57,20 @@ func (i *Input) CursorPosition() (x, y int) {
return int(xf), int(yf)
}
func (i *Input) GamepadGUID(id int) string {
return ""
}
// GamepadName returns a string containing some information about the controller.
// A PS2 controller returned "810-3-USB Gamepad" on Firefox
// A Xbox 360 controller returned "xinput" on Firefox and "Xbox 360 Controller (XInput STANDARD GAMEPAD)" on Chrome
func (i *Input) GamepadName(id int) string {
if len(i.gamepads) <= id {
return ""
}
return i.gamepads[id].name
}
func (i *Input) GamepadIDs() []int {
if len(i.gamepads) == 0 {
return nil
@ -233,6 +248,7 @@ func (i *Input) UpdateGamepads() {
continue
}
i.gamepads[id].valid = true
i.gamepads[id].name = gamepad.Get("id").String()
axes := gamepad.Get("axes")
axesNum := axes.Get("length").Int()

View File

@ -42,6 +42,14 @@ func (i *Input) GamepadIDs() []int {
return nil
}
func (i *Input) GamepadGUID(id int) string {
return ""
}
func (i *Input) GamepadName(id int) string {
return ""
}
func (i *Input) GamepadAxisNum(id int) int {
return 0
}