mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +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 {
|
func update(screen *ebiten.Image) error {
|
||||||
const maxGamepadNum = 4
|
ids := ebiten.GamepadIDs()
|
||||||
presences := [maxGamepadNum]bool{}
|
axes := map[int][]string{}
|
||||||
axes := [maxGamepadNum][]string{}
|
pressedButtons := map[int][]string{}
|
||||||
pressedButtons := [maxGamepadNum][]string{}
|
|
||||||
|
|
||||||
for i := range presences {
|
for _, id := range ids {
|
||||||
presences[i] = ebiten.IsGamepadPresent(i)
|
maxAxis := ebiten.GamepadAxisNum(id)
|
||||||
}
|
|
||||||
|
|
||||||
for i := range axes {
|
|
||||||
maxAxis := ebiten.GamepadAxisNum(i)
|
|
||||||
for a := 0; a < maxAxis; a++ {
|
for a := 0; a < maxAxis; a++ {
|
||||||
v := ebiten.GamepadAxis(i, a)
|
v := ebiten.GamepadAxis(id, a)
|
||||||
axes[i] = append(axes[i], fmt.Sprintf("%d:%0.2f", a, v))
|
axes[id] = append(axes[id], fmt.Sprintf("%d:%0.2f", a, v))
|
||||||
}
|
}
|
||||||
}
|
maxButton := ebiten.GamepadButton(ebiten.GamepadButtonNum(id))
|
||||||
|
for b := ebiten.GamepadButton(id); b < maxButton; b++ {
|
||||||
for i := range pressedButtons {
|
if ebiten.IsGamepadButtonPressed(id, b) {
|
||||||
maxButton := ebiten.GamepadButton(ebiten.GamepadButtonNum(i))
|
pressedButtons[id] = append(pressedButtons[id], strconv.Itoa(int(b)))
|
||||||
for b := ebiten.GamepadButton(i); b < maxButton; b++ {
|
|
||||||
if ebiten.IsGamepadButtonPressed(i, b) {
|
|
||||||
pressedButtons[i] = append(pressedButtons[i], strconv.Itoa(int(b)))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -63,14 +55,15 @@ func update(screen *ebiten.Image) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
str := ""
|
str := ""
|
||||||
for i, p := range presences {
|
if len(ids) > 0 {
|
||||||
if !p {
|
for _, id := range ids {
|
||||||
continue
|
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)
|
} else {
|
||||||
str += fmt.Sprintf(" Axes: %s\n", strings.Join(axes[i], ", "))
|
str = "Please connect your gamepad."
|
||||||
str += fmt.Sprintf(" Buttons: %s\n", strings.Join(pressedButtons[i], ", "))
|
|
||||||
str += "\n"
|
|
||||||
}
|
}
|
||||||
ebitenutil.DebugPrint(screen, str)
|
ebitenutil.DebugPrint(screen, str)
|
||||||
return nil
|
return nil
|
||||||
|
6
input.go
6
input.go
@ -56,13 +56,13 @@ func IsMouseButtonPressed(mouseButton MouseButton) bool {
|
|||||||
return ui.CurrentInput().IsMouseButtonPressed(ui.MouseButton(mouseButton))
|
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.
|
// This function is concurrent-safe.
|
||||||
//
|
//
|
||||||
// NOTE: Gamepad API is available only on desktops, Chrome and Firefox.
|
// NOTE: Gamepad API is available only on desktops, Chrome and Firefox.
|
||||||
func IsGamepadPresent(id int) bool {
|
func GamepadIDs() []int {
|
||||||
return ui.CurrentInput().IsGamepadPresent(id)
|
return ui.CurrentInput().GamepadIDs()
|
||||||
}
|
}
|
||||||
|
|
||||||
// GamepadAxisNum returns the number of axes of the gamepad (id).
|
// 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)
|
return adjustCursorPosition(i.cursorX, i.cursorY)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) IsGamepadPresent(id int) bool {
|
func (i *Input) GamepadIDs() []int {
|
||||||
i.m.RLock()
|
i.m.RLock()
|
||||||
defer i.m.RUnlock()
|
defer i.m.RUnlock()
|
||||||
if len(i.gamepads) <= id {
|
r := []int{}
|
||||||
return false
|
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 {
|
func (i *Input) GamepadAxisNum(id int) int {
|
||||||
|
Loading…
Reference in New Issue
Block a user