internal/uidriver/cbackend: refactoring: separate the gamepad part

This commit is contained in:
Hajime Hoshi 2022-02-05 17:34:18 +09:00
parent 1570c506ae
commit 3470caf5b1

View File

@ -19,7 +19,6 @@ package cbackend
import (
"sync"
"time"
"github.com/hajimehoshi/ebiten/v2/internal/cbackend"
"github.com/hajimehoshi/ebiten/v2/internal/driver"
@ -36,8 +35,7 @@ func (i *Input) update(context driver.UIContext) {
i.m.Lock()
defer i.m.Unlock()
i.gamepads = i.gamepads[:0]
i.gamepads = cbackend.AppendGamepads(i.gamepads)
i.updateGamepads()
i.touches = i.touches[:0]
i.touches = cbackend.AppendTouches(i.touches)
@ -53,16 +51,6 @@ func (i *Input) AppendInputChars(runes []rune) []rune {
return nil
}
func (i *Input) AppendGamepadIDs(gamepadIDs []driver.GamepadID) []driver.GamepadID {
i.m.Lock()
defer i.m.Unlock()
for _, g := range i.gamepads {
gamepadIDs = append(gamepadIDs, g.ID)
}
return gamepadIDs
}
func (i *Input) AppendTouchIDs(touchIDs []driver.TouchID) []driver.TouchID {
i.m.Lock()
defer i.m.Unlock()
@ -77,84 +65,6 @@ func (i *Input) CursorPosition() (x, y int) {
return 0, 0
}
func (i *Input) GamepadSDLID(id driver.GamepadID) string {
return ""
}
func (i *Input) GamepadName(id driver.GamepadID) string {
return ""
}
func (i *Input) GamepadAxisValue(id driver.GamepadID, axis int) float64 {
i.m.Lock()
defer i.m.Unlock()
for _, g := range i.gamepads {
if g.ID != id {
continue
}
if axis < 0 {
return 0
}
if g.AxisNum <= axis {
return 0
}
if len(g.AxisValues) <= axis {
return 0
}
return g.AxisValues[axis]
}
return 0
}
func (i *Input) GamepadAxisNum(id driver.GamepadID) int {
i.m.Lock()
defer i.m.Unlock()
for _, g := range i.gamepads {
if g.ID != id {
continue
}
return g.AxisNum
}
return 0
}
func (i *Input) GamepadButtonNum(id driver.GamepadID) int {
i.m.Lock()
defer i.m.Unlock()
for _, g := range i.gamepads {
if g.ID != id {
continue
}
return g.ButtonNum
}
return 0
}
func (i *Input) IsGamepadButtonPressed(id driver.GamepadID, button driver.GamepadButton) bool {
i.m.Lock()
defer i.m.Unlock()
for _, g := range i.gamepads {
if g.ID != id {
continue
}
if button < 0 {
return false
}
if g.ButtonNum <= int(button) {
return false
}
if len(g.ButtonPressed) <= int(button) {
return false
}
return g.ButtonPressed[button]
}
return false
}
func (i *Input) IsKeyPressed(key driver.Key) bool {
return false
}
@ -163,94 +73,6 @@ func (i *Input) IsMouseButtonPressed(button driver.MouseButton) bool {
return false
}
func (i *Input) IsStandardGamepadButtonPressed(id driver.GamepadID, button driver.StandardGamepadButton) bool {
i.m.Lock()
defer i.m.Unlock()
for _, g := range i.gamepads {
if g.ID != id {
continue
}
if !g.Standard {
return false
}
if button < 0 {
return false
}
if g.ButtonNum <= int(button) {
return false
}
if len(g.ButtonPressed) <= int(button) {
return false
}
return g.ButtonPressed[button]
}
return false
}
func (i *Input) IsStandardGamepadLayoutAvailable(id driver.GamepadID) bool {
i.m.Lock()
defer i.m.Unlock()
for _, g := range i.gamepads {
if g.ID != id {
continue
}
return g.Standard
}
return false
}
func (i *Input) StandardGamepadAxisValue(id driver.GamepadID, axis driver.StandardGamepadAxis) float64 {
i.m.Lock()
defer i.m.Unlock()
for _, g := range i.gamepads {
if g.ID != id {
continue
}
if !g.Standard {
return 0
}
if axis < 0 {
return 0
}
if g.AxisNum <= int(axis) {
return 0
}
if len(g.AxisValues) <= int(axis) {
return 0
}
return g.AxisValues[axis]
}
return 0
}
func (i *Input) StandardGamepadButtonValue(id driver.GamepadID, button driver.StandardGamepadButton) float64 {
i.m.Lock()
defer i.m.Unlock()
for _, g := range i.gamepads {
if g.ID != id {
continue
}
if !g.Standard {
return 0
}
if button < 0 {
return 0
}
if g.ButtonNum <= int(button) {
return 0
}
if len(g.ButtonValues) <= int(button) {
return 0
}
return g.ButtonValues[button]
}
return 0
}
func (i *Input) TouchPosition(id driver.TouchID) (x, y int) {
i.m.Lock()
defer i.m.Unlock()
@ -263,10 +85,6 @@ func (i *Input) TouchPosition(id driver.TouchID) (x, y int) {
return 0, 0
}
func (i *Input) VibrateGamepad(id driver.GamepadID, duration time.Duration, strongMagnitude float64, weakMagnitude float64) {
cbackend.VibrateGamepad(id, duration, strongMagnitude, weakMagnitude)
}
func (i *Input) Wheel() (xoff, yoff float64) {
return 0, 0
}