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 {
ID driver.GamepadID
ID int
Standard bool
ButtonNum int
AxisNum int
ButtonCount int
AxisCount int
ButtonPressed [32]bool
ButtonValues [32]float64
AxisValues [16]float64
@ -119,16 +119,16 @@ func AppendGamepads(gamepads []Gamepad) []Gamepad {
for _, g := range cGamepads {
gamepad := Gamepad{
ID: driver.GamepadID(g.id),
Standard: g.standard != 0,
ButtonNum: int(g.button_num),
AxisNum: int(g.axis_num),
ID: int(g.id),
Standard: g.standard != 0,
ButtonCount: int(g.button_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.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])
}
@ -161,7 +161,7 @@ func AppendTouches(touches []Touch) []Touch {
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))
}

View File

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

View File

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

View File

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

View File

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

View File

@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !ebitencbackend
// +build !ebitencbackend
package gamepad
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
// limitations under the License.
//go:build !ios
// +build !ios
//go:build !ios && !ebitencbackend
// +build !ios,!ebitencbackend
package gamepad

View File

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

View File

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

View File

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

View File

@ -20,191 +20,98 @@ package cbackend
import (
"time"
"github.com/hajimehoshi/ebiten/v2/internal/cbackend"
"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 {
i.m.Lock()
defer i.m.Unlock()
for _, g := range i.gamepads {
gamepadIDs = append(gamepadIDs, g.ID)
}
return gamepadIDs
return gamepad.AppendGamepadIDs(gamepadIDs)
}
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 {
return ""
g := gamepad.Get(id)
if g == nil {
return ""
}
return g.Name()
}
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]
g := gamepad.Get(id)
if g == nil {
return 0
}
return 0
return g.Axis(axis)
}
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
g := gamepad.Get(id)
if g == nil {
return 0
}
return 0
return g.AxisCount()
}
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
g := gamepad.Get(id)
if g == nil {
return 0
}
return 0
return g.ButtonCount()
}
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]
g := gamepad.Get(id)
if g == nil {
return false
}
return false
return g.Button(int(button))
}
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]
g := gamepad.Get(id)
if g == nil {
return false
}
return false
return g.IsStandardButtonPressed(button)
}
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
g := gamepad.Get(id)
if g == nil {
return false
}
return false
return g.IsStandardLayoutAvailable()
}
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]
g := gamepad.Get(id)
if g == nil {
return 0
}
return 0
return g.StandardAxisValue(axis)
}
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]
g := gamepad.Get(id)
if g == nil {
return 0
}
return 0
return g.StandardButtonValue(button)
}
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/driver"
"github.com/hajimehoshi/ebiten/v2/internal/gamepad"
)
type Input struct {
@ -35,7 +36,7 @@ func (i *Input) update(context driver.UIContext) {
i.m.Lock()
defer i.m.Unlock()
i.updateGamepads()
gamepad.Update()
i.touches = i.touches[:0]
i.touches = cbackend.AppendTouches(i.touches)