mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
ui: Add GamepadIDs and remove IsGamepadPresent (#447)
This commit is contained in:
parent
0357f80411
commit
7bcc9ee79f
@ -32,28 +32,20 @@ const (
|
||||
)
|
||||
|
||||
func update(screen *ebiten.Image) error {
|
||||
const maxGamepadNum = 4
|
||||
presences := [maxGamepadNum]bool{}
|
||||
axes := [maxGamepadNum][]string{}
|
||||
pressedButtons := [maxGamepadNum][]string{}
|
||||
ids := ebiten.GamepadIDs()
|
||||
axes := map[int][]string{}
|
||||
pressedButtons := map[int][]string{}
|
||||
|
||||
for i := range presences {
|
||||
presences[i] = ebiten.IsGamepadPresent(i)
|
||||
}
|
||||
|
||||
for i := range axes {
|
||||
maxAxis := ebiten.GamepadAxisNum(i)
|
||||
for _, id := range ids {
|
||||
maxAxis := ebiten.GamepadAxisNum(id)
|
||||
for a := 0; a < maxAxis; a++ {
|
||||
v := ebiten.GamepadAxis(i, a)
|
||||
axes[i] = append(axes[i], fmt.Sprintf("%d:%0.2f", a, v))
|
||||
v := ebiten.GamepadAxis(id, a)
|
||||
axes[id] = append(axes[id], fmt.Sprintf("%d:%0.2f", a, v))
|
||||
}
|
||||
}
|
||||
|
||||
for i := range pressedButtons {
|
||||
maxButton := ebiten.GamepadButton(ebiten.GamepadButtonNum(i))
|
||||
for b := ebiten.GamepadButton(i); b < maxButton; b++ {
|
||||
if ebiten.IsGamepadButtonPressed(i, b) {
|
||||
pressedButtons[i] = append(pressedButtons[i], strconv.Itoa(int(b)))
|
||||
maxButton := ebiten.GamepadButton(ebiten.GamepadButtonNum(id))
|
||||
for b := ebiten.GamepadButton(id); b < maxButton; b++ {
|
||||
if ebiten.IsGamepadButtonPressed(id, b) {
|
||||
pressedButtons[id] = append(pressedButtons[id], strconv.Itoa(int(b)))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -63,14 +55,15 @@ func update(screen *ebiten.Image) error {
|
||||
}
|
||||
|
||||
str := ""
|
||||
for i, p := range presences {
|
||||
if !p {
|
||||
continue
|
||||
if len(ids) > 0 {
|
||||
for _, id := range ids {
|
||||
str += fmt.Sprintf("Gamepad (ID: %d):\n", id)
|
||||
str += fmt.Sprintf(" Axes: %s\n", strings.Join(axes[id], ", "))
|
||||
str += fmt.Sprintf(" Buttons: %s\n", strings.Join(pressedButtons[id], ", "))
|
||||
str += "\n"
|
||||
}
|
||||
str += fmt.Sprintf("Gamepad (ID: %d):\n", i)
|
||||
str += fmt.Sprintf(" Axes: %s\n", strings.Join(axes[i], ", "))
|
||||
str += fmt.Sprintf(" Buttons: %s\n", strings.Join(pressedButtons[i], ", "))
|
||||
str += "\n"
|
||||
} else {
|
||||
str = "Please connect your gamepad."
|
||||
}
|
||||
ebitenutil.DebugPrint(screen, str)
|
||||
return nil
|
||||
|
6
input.go
6
input.go
@ -56,13 +56,13 @@ func IsMouseButtonPressed(mouseButton MouseButton) bool {
|
||||
return ui.CurrentInput().IsMouseButtonPressed(ui.MouseButton(mouseButton))
|
||||
}
|
||||
|
||||
// IsGamepadPresent returns a boolean value indicating whether the gamepad for the given id exists.
|
||||
// GamepadIDs returns a slice indicating available gamepad IDs.
|
||||
//
|
||||
// This function is concurrent-safe.
|
||||
//
|
||||
// NOTE: Gamepad API is available only on desktops, Chrome and Firefox.
|
||||
func IsGamepadPresent(id int) bool {
|
||||
return ui.CurrentInput().IsGamepadPresent(id)
|
||||
func GamepadIDs() []int {
|
||||
return ui.CurrentInput().GamepadIDs()
|
||||
}
|
||||
|
||||
// GamepadAxisNum returns the number of axes of the gamepad (id).
|
||||
|
@ -31,13 +31,16 @@ func (i *Input) CursorPosition() (x, y int) {
|
||||
return adjustCursorPosition(i.cursorX, i.cursorY)
|
||||
}
|
||||
|
||||
func (i *Input) IsGamepadPresent(id int) bool {
|
||||
func (i *Input) GamepadIDs() []int {
|
||||
i.m.RLock()
|
||||
defer i.m.RUnlock()
|
||||
if len(i.gamepads) <= id {
|
||||
return false
|
||||
r := []int{}
|
||||
for id, g := range i.gamepads {
|
||||
if g.valid {
|
||||
r = append(r, id)
|
||||
}
|
||||
}
|
||||
return i.gamepads[id].valid
|
||||
return r
|
||||
}
|
||||
|
||||
func (i *Input) GamepadAxisNum(id int) int {
|
||||
|
Loading…
Reference in New Issue
Block a user