internal/gamepad: port the gamepad part for c-backend

This commit is contained in:
Hajime Hoshi 2022-02-05 18:30:17 +09:00
parent 5443fc312a
commit bf1cb035eb
13 changed files with 216 additions and 163 deletions

View File

@ -71,10 +71,10 @@ import (
) )
type Gamepad struct { type Gamepad struct {
ID driver.GamepadID ID int
Standard bool Standard bool
ButtonNum int ButtonCount int
AxisNum int AxisCount int
ButtonPressed [32]bool ButtonPressed [32]bool
ButtonValues [32]float64 ButtonValues [32]float64
AxisValues [16]float64 AxisValues [16]float64
@ -119,16 +119,16 @@ func AppendGamepads(gamepads []Gamepad) []Gamepad {
for _, g := range cGamepads { for _, g := range cGamepads {
gamepad := Gamepad{ gamepad := Gamepad{
ID: driver.GamepadID(g.id), ID: int(g.id),
Standard: g.standard != 0, Standard: g.standard != 0,
ButtonNum: int(g.button_num), ButtonCount: int(g.button_num),
AxisNum: int(g.axis_num), AxisCount: int(g.axis_num),
} }
for i := 0; i < gamepad.ButtonNum; i++ { for i := 0; i < gamepad.ButtonCount; i++ {
gamepad.ButtonPressed[i] = g.button_pressed[i] != 0 gamepad.ButtonPressed[i] = g.button_pressed[i] != 0
gamepad.ButtonValues[i] = float64(g.button_values[i]) gamepad.ButtonValues[i] = float64(g.button_values[i])
} }
for i := 0; i < gamepad.AxisNum; i++ { for i := 0; i < gamepad.AxisCount; i++ {
gamepad.AxisValues[i] = float64(g.axis_values[i]) gamepad.AxisValues[i] = float64(g.axis_values[i])
} }
@ -161,7 +161,7 @@ func AppendTouches(touches []Touch) []Touch {
return touches return touches
} }
func VibrateGamepad(id driver.GamepadID, duration time.Duration, strongMagnitude float64, weakMagnitude float64) { func VibrateGamepad(id int, duration time.Duration, strongMagnitude float64, weakMagnitude float64) {
C.EbitenVibrateGamepad(C.int(id), C.double(float64(duration)/float64(time.Second)), C.double(strongMagnitude), C.double(weakMagnitude)) C.EbitenVibrateGamepad(C.int(id), C.double(float64(duration)/float64(time.Second)), C.double(strongMagnitude), C.double(weakMagnitude))
} }

View File

@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//go:build ios //go:build ios && !ebitencbackend
// +build ios // +build ios,!ebitencbackend
package gamepad package gamepad

View File

@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//go:build !android //go:build !android && !ebitencbackend
// +build !android // +build !android,!ebitencbackend
package gamepad package gamepad

View File

@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//go:build !ebitencbackend
// +build !ebitencbackend
package gamepad package gamepad
import ( import (

View File

@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//go:build !ebitencbackend
// +build !ebitencbackend
package gamepad package gamepad
import ( import (

View File

@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//go:build !ebitencbackend
// +build !ebitencbackend
package gamepad package gamepad
import ( import (

View File

@ -0,0 +1,133 @@
// Copyright 2022 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build ebitencbackend
// +build ebitencbackend
package gamepad
import (
"time"
"github.com/hajimehoshi/ebiten/v2/internal/cbackend"
)
type nativeGamepads struct {
gamepads []cbackend.Gamepad
ids map[int]struct{}
}
func (*nativeGamepads) init(gamepads *gamepads) error {
return nil
}
func (g *nativeGamepads) update(gamepads *gamepads) error {
g.gamepads = g.gamepads[:0]
g.gamepads = cbackend.AppendGamepads(g.gamepads)
for id := range g.ids {
delete(g.ids, id)
}
for _, gp := range g.gamepads {
if g.ids == nil {
g.ids = map[int]struct{}{}
}
g.ids[gp.ID] = struct{}{}
gamepad := gamepads.find(func(gamepad *Gamepad) bool {
return gamepad.id == gp.ID
})
if gamepad == nil {
gamepad = gamepads.add("", "")
gamepad.id = gp.ID
gamepad.standard = gp.Standard
gamepad.axisValues = make([]float64, gp.AxisCount)
gamepad.buttonPressed = make([]bool, gp.ButtonCount)
gamepad.buttonValues = make([]float64, gp.ButtonCount)
}
gamepad.m.Lock()
copy(gamepad.axisValues, gp.AxisValues[:])
copy(gamepad.buttonValues, gp.ButtonValues[:])
copy(gamepad.buttonPressed, gp.ButtonPressed[:])
gamepad.m.Unlock()
}
// Remove an unused gamepads.
gamepads.remove(func(gamepad *Gamepad) bool {
_, ok := g.ids[gamepad.id]
return !ok
})
return nil
}
type nativeGamepad struct {
id int
standard bool
axisValues []float64
buttonPressed []bool
buttonValues []float64
}
func (*nativeGamepad) update(gamepad *gamepads) error {
return nil
}
func (g *nativeGamepad) hasOwnStandardLayoutMapping() bool {
return g.standard
}
func (g *nativeGamepad) axisCount() int {
return len(g.axisValues)
}
func (g *nativeGamepad) buttonCount() int {
return len(g.buttonValues)
}
func (g *nativeGamepad) hatCount() int {
return 0
}
func (g *nativeGamepad) axisValue(axis int) float64 {
if axis < 0 || axis >= len(g.axisValues) {
return 0
}
return g.axisValues[axis]
}
func (g *nativeGamepad) isButtonPressed(button int) bool {
if button < 0 || button >= len(g.buttonPressed) {
return false
}
return g.buttonPressed[button]
}
func (g *nativeGamepad) buttonValue(button int) float64 {
if button < 0 || button >= len(g.buttonValues) {
return 0
}
return g.buttonValues[button]
}
func (*nativeGamepad) hatState(hat int) int {
return hatCentered
}
func (g *nativeGamepad) vibrate(duration time.Duration, strongMagnitude float64, weakMagnitude float64) {
cbackend.VibrateGamepad(g.id, duration, strongMagnitude, weakMagnitude)
}

View File

@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//go:build !ios //go:build !ios && !ebitencbackend
// +build !ios // +build !ios,!ebitencbackend
package gamepad package gamepad

View File

@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//go:build ios //go:build ios && !ebitencbackend
// +build ios // +build ios,!ebitencbackend
package gamepad package gamepad

View File

@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//go:build !android //go:build !android && !ebitencbackend
// +build !android // +build !android,!ebitencbackend
package gamepad package gamepad

View File

@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//go:build !ebitencbackend
// +build !ebitencbackend
package gamepad package gamepad
import ( import (

View File

@ -20,191 +20,98 @@ package cbackend
import ( import (
"time" "time"
"github.com/hajimehoshi/ebiten/v2/internal/cbackend"
"github.com/hajimehoshi/ebiten/v2/internal/driver" "github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/gamepad"
) )
func (i *Input) updateGamepads() {
i.gamepads = i.gamepads[:0]
i.gamepads = cbackend.AppendGamepads(i.gamepads)
}
func (i *Input) AppendGamepadIDs(gamepadIDs []driver.GamepadID) []driver.GamepadID { func (i *Input) AppendGamepadIDs(gamepadIDs []driver.GamepadID) []driver.GamepadID {
i.m.Lock() return gamepad.AppendGamepadIDs(gamepadIDs)
defer i.m.Unlock()
for _, g := range i.gamepads {
gamepadIDs = append(gamepadIDs, g.ID)
}
return gamepadIDs
} }
func (i *Input) GamepadSDLID(id driver.GamepadID) string { func (i *Input) GamepadSDLID(id driver.GamepadID) string {
return "" g := gamepad.Get(id)
if g == nil {
return ""
}
return g.SDLID()
} }
func (i *Input) GamepadName(id driver.GamepadID) string { func (i *Input) GamepadName(id driver.GamepadID) string {
return "" g := gamepad.Get(id)
if g == nil {
return ""
}
return g.Name()
} }
func (i *Input) GamepadAxisValue(id driver.GamepadID, axis int) float64 { func (i *Input) GamepadAxisValue(id driver.GamepadID, axis int) float64 {
i.m.Lock() g := gamepad.Get(id)
defer i.m.Unlock() if g == nil {
return 0
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 return g.Axis(axis)
} }
func (i *Input) GamepadAxisNum(id driver.GamepadID) int { func (i *Input) GamepadAxisNum(id driver.GamepadID) int {
i.m.Lock() g := gamepad.Get(id)
defer i.m.Unlock() if g == nil {
return 0
for _, g := range i.gamepads {
if g.ID != id {
continue
}
return g.AxisNum
} }
return 0 return g.AxisCount()
} }
func (i *Input) GamepadButtonNum(id driver.GamepadID) int { func (i *Input) GamepadButtonNum(id driver.GamepadID) int {
i.m.Lock() g := gamepad.Get(id)
defer i.m.Unlock() if g == nil {
return 0
for _, g := range i.gamepads {
if g.ID != id {
continue
}
return g.ButtonNum
} }
return 0 return g.ButtonCount()
} }
func (i *Input) IsGamepadButtonPressed(id driver.GamepadID, button driver.GamepadButton) bool { func (i *Input) IsGamepadButtonPressed(id driver.GamepadID, button driver.GamepadButton) bool {
i.m.Lock() g := gamepad.Get(id)
defer i.m.Unlock() if g == nil {
return false
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 return g.Button(int(button))
} }
func (i *Input) IsStandardGamepadButtonPressed(id driver.GamepadID, button driver.StandardGamepadButton) bool { func (i *Input) IsStandardGamepadButtonPressed(id driver.GamepadID, button driver.StandardGamepadButton) bool {
i.m.Lock() g := gamepad.Get(id)
defer i.m.Unlock() if g == nil {
return false
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 return g.IsStandardButtonPressed(button)
} }
func (i *Input) IsStandardGamepadLayoutAvailable(id driver.GamepadID) bool { func (i *Input) IsStandardGamepadLayoutAvailable(id driver.GamepadID) bool {
i.m.Lock() g := gamepad.Get(id)
defer i.m.Unlock() if g == nil {
return false
for _, g := range i.gamepads {
if g.ID != id {
continue
}
return g.Standard
} }
return false return g.IsStandardLayoutAvailable()
} }
func (i *Input) StandardGamepadAxisValue(id driver.GamepadID, axis driver.StandardGamepadAxis) float64 { func (i *Input) StandardGamepadAxisValue(id driver.GamepadID, axis driver.StandardGamepadAxis) float64 {
i.m.Lock() g := gamepad.Get(id)
defer i.m.Unlock() if g == nil {
return 0
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 return g.StandardAxisValue(axis)
} }
func (i *Input) StandardGamepadButtonValue(id driver.GamepadID, button driver.StandardGamepadButton) float64 { func (i *Input) StandardGamepadButtonValue(id driver.GamepadID, button driver.StandardGamepadButton) float64 {
i.m.Lock() g := gamepad.Get(id)
defer i.m.Unlock() if g == nil {
return 0
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 return g.StandardButtonValue(button)
} }
func (i *Input) VibrateGamepad(id driver.GamepadID, duration time.Duration, strongMagnitude float64, weakMagnitude float64) { func (i *Input) VibrateGamepad(id driver.GamepadID, duration time.Duration, strongMagnitude float64, weakMagnitude float64) {
cbackend.VibrateGamepad(id, duration, strongMagnitude, weakMagnitude) g := gamepad.Get(id)
if g == nil {
return
}
g.Vibrate(duration, strongMagnitude, weakMagnitude)
} }

View File

@ -22,6 +22,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/cbackend" "github.com/hajimehoshi/ebiten/v2/internal/cbackend"
"github.com/hajimehoshi/ebiten/v2/internal/driver" "github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/gamepad"
) )
type Input struct { type Input struct {
@ -35,7 +36,7 @@ func (i *Input) update(context driver.UIContext) {
i.m.Lock() i.m.Lock()
defer i.m.Unlock() defer i.m.Unlock()
i.updateGamepads() gamepad.Update()
i.touches = i.touches[:0] i.touches = i.touches[:0]
i.touches = cbackend.AppendTouches(i.touches) i.touches = cbackend.AppendTouches(i.touches)