internal/uidriver: refactoring: remove the gamepad part

Use the gamepad package instead.
This commit is contained in:
Hajime Hoshi 2022-02-05 22:21:41 +09:00
parent e6e0c6d850
commit 3bdd8097b5
8 changed files with 75 additions and 495 deletions

View File

@ -16,6 +16,7 @@ package ebiten
import (
"github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/gamepad"
"github.com/hajimehoshi/ebiten/v2/internal/gamepaddb"
)
@ -128,7 +129,11 @@ type GamepadID = driver.GamepadID
//
// GamepadSDLID is concurrent-safe.
func GamepadSDLID(id GamepadID) string {
return uiDriver().Input().GamepadSDLID(id)
g := gamepad.Get(id)
if g == nil {
return ""
}
return g.SDLID()
}
// GamepadName returns a string with the name.
@ -141,7 +146,11 @@ func GamepadSDLID(id GamepadID) string {
//
// GamepadName is concurrent-safe.
func GamepadName(id GamepadID) string {
return uiDriver().Input().GamepadName(id)
g := gamepad.Get(id)
if g == nil {
return ""
}
return g.Name()
}
// AppendGamepadIDs appends available gamepad IDs to gamepadIDs, and returns the extended buffer.
@ -149,7 +158,7 @@ func GamepadName(id GamepadID) string {
//
// AppendGamepadIDs is concurrent-safe.
func AppendGamepadIDs(gamepadIDs []GamepadID) []GamepadID {
return uiDriver().Input().AppendGamepadIDs(gamepadIDs)
return gamepad.AppendGamepadIDs(gamepadIDs)
}
// GamepadIDs returns a slice indicating available gamepad IDs.
@ -163,14 +172,22 @@ func GamepadIDs() []GamepadID {
//
// GamepadAxisNum is concurrent-safe.
func GamepadAxisNum(id GamepadID) int {
return uiDriver().Input().GamepadAxisNum(id)
g := gamepad.Get(id)
if g == nil {
return 0
}
return g.AxisCount()
}
// GamepadAxisValue returns a float value [-1.0 - 1.0] of the given gamepad (id)'s axis (axis).
//
// GamepadAxisValue is concurrent-safe.
func GamepadAxisValue(id GamepadID, axis int) float64 {
return uiDriver().Input().GamepadAxisValue(id, axis)
g := gamepad.Get(id)
if g == nil {
return 0
}
return g.Axis(axis)
}
// GamepadAxis returns a float value [-1.0 - 1.0] of the given gamepad (id)'s axis (axis).
@ -184,7 +201,13 @@ func GamepadAxis(id GamepadID, axis int) float64 {
//
// GamepadButtonNum is concurrent-safe.
func GamepadButtonNum(id GamepadID) int {
return uiDriver().Input().GamepadButtonNum(id)
g := gamepad.Get(id)
if g == nil {
return 0
}
// For backward compatibility, hats are treated as buttons in GLFW.
return g.ButtonCount() + g.HatCount()*4
}
// IsGamepadButtonPressed reports whether the given button of the gamepad (id) is pressed or not.
@ -197,7 +220,23 @@ func GamepadButtonNum(id GamepadID) int {
// The relationships between physical buttons and buttion IDs depend on environments.
// There can be differences even between Chrome and Firefox.
func IsGamepadButtonPressed(id GamepadID, button GamepadButton) bool {
return uiDriver().Input().IsGamepadButtonPressed(id, button)
g := gamepad.Get(id)
if g == nil {
return false
}
nbuttons := g.ButtonCount()
if int(button) < nbuttons {
return g.Button(int(button))
}
// For backward compatibility, hats are treated as buttons in GLFW.
if hat := (int(button) - nbuttons) / 4; hat < g.HatCount() {
dir := (int(button) - nbuttons) % 4
return g.Hat(hat)&(1<<dir) != 0
}
return false
}
// StandardGamepadAxisValue returns a float value [-1.0 - 1.0] of the given gamepad (id)'s standard axis (axis).
@ -206,7 +245,11 @@ func IsGamepadButtonPressed(id GamepadID, button GamepadButton) bool {
//
// StandardGamepadAxisValue is concurrent safe.
func StandardGamepadAxisValue(id GamepadID, axis StandardGamepadAxis) float64 {
return uiDriver().Input().StandardGamepadAxisValue(id, axis)
g := gamepad.Get(id)
if g == nil {
return 0
}
return g.StandardAxisValue(axis)
}
// StandardGamepadButtonValue returns a float value [0.0 - 1.0] of the given gamepad (id)'s standard button (button).
@ -215,7 +258,11 @@ func StandardGamepadAxisValue(id GamepadID, axis StandardGamepadAxis) float64 {
//
// StandardGamepadButtonValue is concurrent safe.
func StandardGamepadButtonValue(id GamepadID, button StandardGamepadButton) float64 {
return uiDriver().Input().StandardGamepadButtonValue(id, button)
g := gamepad.Get(id)
if g == nil {
return 0
}
return g.StandardButtonValue(button)
}
// IsStandardGamepadButtonPressed reports whether the given gamepad (id)'s standard gamepad button (button) is pressed.
@ -224,14 +271,22 @@ func StandardGamepadButtonValue(id GamepadID, button StandardGamepadButton) floa
//
// IsStandardGamepadButtonPressed is concurrent safe.
func IsStandardGamepadButtonPressed(id GamepadID, button StandardGamepadButton) bool {
return uiDriver().Input().IsStandardGamepadButtonPressed(id, button)
g := gamepad.Get(id)
if g == nil {
return false
}
return g.IsStandardButtonPressed(button)
}
// IsStandardGamepadLayoutAvailable reports whether the gamepad (id) has a standard gamepad layout mapping.
//
// IsStandardGamepadLayoutAvailable is concurrent-safe.
func IsStandardGamepadLayoutAvailable(id GamepadID) bool {
return uiDriver().Input().IsStandardGamepadLayoutAvailable(id)
g := gamepad.Get(id)
if g == nil {
return false
}
return g.IsStandardLayoutAvailable()
}
// UpdateStandardGamepadLayoutMappings parses the specified string mappings in SDL_GameControllerDB format and

View File

@ -14,32 +14,16 @@
package driver
import (
"time"
)
type GamepadID int
type TouchID int
type Input interface {
AppendInputChars(runes []rune) []rune
AppendGamepadIDs(gamepadIDs []GamepadID) []GamepadID
AppendTouchIDs(touchIDs []TouchID) []TouchID
CursorPosition() (x, y int)
GamepadSDLID(id GamepadID) string
GamepadName(id GamepadID) string
GamepadAxisValue(id GamepadID, axis int) float64
GamepadAxisNum(id GamepadID) int
GamepadButtonNum(id GamepadID) int
IsGamepadButtonPressed(id GamepadID, button GamepadButton) bool
IsKeyPressed(key Key) bool
IsMouseButtonPressed(button MouseButton) bool
IsStandardGamepadButtonPressed(id GamepadID, button StandardGamepadButton) bool
IsStandardGamepadLayoutAvailable(id GamepadID) bool
StandardGamepadAxisValue(id GamepadID, axis StandardGamepadAxis) float64
StandardGamepadButtonValue(id GamepadID, button StandardGamepadButton) float64
TouchPosition(id TouchID) (x, y int)
VibrateGamepad(id GamepadID, duration time.Duration, strongMagnitude float64, weakMagnitude float64)
Wheel() (xoff, yoff float64)
}

View File

@ -1,117 +0,0 @@
// 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 cbackend
import (
"time"
"github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/gamepad"
)
func (i *Input) AppendGamepadIDs(gamepadIDs []driver.GamepadID) []driver.GamepadID {
return gamepad.AppendGamepadIDs(gamepadIDs)
}
func (i *Input) GamepadSDLID(id driver.GamepadID) string {
g := gamepad.Get(id)
if g == nil {
return ""
}
return g.SDLID()
}
func (i *Input) GamepadName(id driver.GamepadID) string {
g := gamepad.Get(id)
if g == nil {
return ""
}
return g.Name()
}
func (i *Input) GamepadAxisValue(id driver.GamepadID, axis int) float64 {
g := gamepad.Get(id)
if g == nil {
return 0
}
return g.Axis(axis)
}
func (i *Input) GamepadAxisNum(id driver.GamepadID) int {
g := gamepad.Get(id)
if g == nil {
return 0
}
return g.AxisCount()
}
func (i *Input) GamepadButtonNum(id driver.GamepadID) int {
g := gamepad.Get(id)
if g == nil {
return 0
}
return g.ButtonCount()
}
func (i *Input) IsGamepadButtonPressed(id driver.GamepadID, button driver.GamepadButton) bool {
g := gamepad.Get(id)
if g == nil {
return false
}
return g.Button(int(button))
}
func (i *Input) IsStandardGamepadButtonPressed(id driver.GamepadID, button driver.StandardGamepadButton) bool {
g := gamepad.Get(id)
if g == nil {
return false
}
return g.IsStandardButtonPressed(button)
}
func (i *Input) IsStandardGamepadLayoutAvailable(id driver.GamepadID) bool {
g := gamepad.Get(id)
if g == nil {
return false
}
return g.IsStandardLayoutAvailable()
}
func (i *Input) StandardGamepadAxisValue(id driver.GamepadID, axis driver.StandardGamepadAxis) float64 {
g := gamepad.Get(id)
if g == nil {
return 0
}
return g.StandardAxisValue(axis)
}
func (i *Input) StandardGamepadButtonValue(id driver.GamepadID, button driver.StandardGamepadButton) float64 {
g := gamepad.Get(id)
if g == nil {
return 0
}
return g.StandardButtonValue(button)
}
func (i *Input) VibrateGamepad(id driver.GamepadID, duration time.Duration, strongMagnitude float64, weakMagnitude float64) {
g := gamepad.Get(id)
if g == nil {
return
}
g.Vibrate(duration, strongMagnitude, weakMagnitude)
}

View File

@ -1,131 +0,0 @@
// 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 !android && !js && !ios
// +build !android,!js,!ios
package glfw
import (
"time"
"github.com/hajimehoshi/ebiten/v2/internal/driver"
gamepadpkg "github.com/hajimehoshi/ebiten/v2/internal/gamepad"
)
func (i *Input) AppendGamepadIDs(gamepadIDs []driver.GamepadID) []driver.GamepadID {
return gamepadpkg.AppendGamepadIDs(gamepadIDs)
}
func (i *Input) GamepadSDLID(id driver.GamepadID) string {
g := gamepadpkg.Get(id)
if g == nil {
return ""
}
return g.SDLID()
}
func (i *Input) GamepadName(id driver.GamepadID) string {
g := gamepadpkg.Get(id)
if g == nil {
return ""
}
return g.Name()
}
func (i *Input) GamepadAxisNum(id driver.GamepadID) int {
g := gamepadpkg.Get(id)
if g == nil {
return 0
}
return g.AxisCount()
}
func (i *Input) GamepadAxisValue(id driver.GamepadID, axis int) float64 {
g := gamepadpkg.Get(id)
if g == nil {
return 0
}
return g.Axis(axis)
}
func (i *Input) GamepadButtonNum(id driver.GamepadID) int {
g := gamepadpkg.Get(id)
if g == nil {
return 0
}
// For backward compatibility, hats are treated as buttons in GLFW.
return g.ButtonCount() + g.HatCount()*4
}
func (i *Input) IsGamepadButtonPressed(id driver.GamepadID, button driver.GamepadButton) bool {
g := gamepadpkg.Get(id)
if g == nil {
return false
}
nbuttons := g.ButtonCount()
if int(button) < nbuttons {
return g.Button(int(button))
}
// For backward compatibility, hats are treated as buttons in GLFW.
if hat := (int(button) - nbuttons) / 4; hat < g.HatCount() {
dir := (int(button) - nbuttons) % 4
return g.Hat(hat)&(1<<dir) != 0
}
return false
}
func (i *Input) IsStandardGamepadLayoutAvailable(id driver.GamepadID) bool {
g := gamepadpkg.Get(id)
if g == nil {
return false
}
return g.IsStandardLayoutAvailable()
}
func (i *Input) StandardGamepadAxisValue(id driver.GamepadID, axis driver.StandardGamepadAxis) float64 {
g := gamepadpkg.Get(id)
if g == nil {
return 0
}
return g.StandardAxisValue(axis)
}
func (i *Input) StandardGamepadButtonValue(id driver.GamepadID, button driver.StandardGamepadButton) float64 {
g := gamepadpkg.Get(id)
if g == nil {
return 0
}
return g.StandardButtonValue(button)
}
func (i *Input) IsStandardGamepadButtonPressed(id driver.GamepadID, button driver.StandardGamepadButton) bool {
g := gamepadpkg.Get(id)
if g == nil {
return false
}
return g.IsStandardButtonPressed(button)
}
func (i *Input) VibrateGamepad(id driver.GamepadID, duration time.Duration, strongMagnitude float64, weakMagnitude float64) {
g := gamepadpkg.Get(id)
if g == nil {
return
}
g.Vibrate(duration, strongMagnitude, weakMagnitude)
}

View File

@ -16,11 +16,9 @@ package js
import (
"syscall/js"
"time"
"unicode"
"github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/gamepad"
)
var (
@ -83,61 +81,6 @@ func (i *Input) CursorPosition() (x, y int) {
return int(xf), int(yf)
}
func (i *Input) GamepadSDLID(id driver.GamepadID) string {
g := gamepad.Get(id)
if g == nil {
return ""
}
return g.SDLID()
}
// GamepadName returns a string containing some information about the controller.
// A PS2 controller returned "810-3-USB Gamepad" on Firefox
// A Xbox 360 controller returned "xinput" on Firefox and "Xbox 360 Controller (XInput STANDARD GAMEPAD)" on Chrome
func (i *Input) GamepadName(id driver.GamepadID) string {
g := gamepad.Get(id)
if g == nil {
return ""
}
return g.Name()
}
func (i *Input) AppendGamepadIDs(gamepadIDs []driver.GamepadID) []driver.GamepadID {
return gamepad.AppendGamepadIDs(gamepadIDs)
}
func (i *Input) GamepadAxisNum(id driver.GamepadID) int {
g := gamepad.Get(id)
if g == nil {
return 0
}
return g.AxisCount()
}
func (i *Input) GamepadAxisValue(id driver.GamepadID, axis int) float64 {
g := gamepad.Get(id)
if g == nil {
return 0
}
return g.Axis(axis)
}
func (i *Input) GamepadButtonNum(id driver.GamepadID) int {
g := gamepad.Get(id)
if g == nil {
return 0
}
return g.ButtonCount()
}
func (i *Input) IsGamepadButtonPressed(id driver.GamepadID, button driver.GamepadButton) bool {
g := gamepad.Get(id)
if g == nil {
return false
}
return g.Button(int(button))
}
func (i *Input) AppendTouchIDs(touchIDs []driver.TouchID) []driver.TouchID {
for id := range i.touches {
touchIDs = append(touchIDs, id)
@ -252,10 +195,6 @@ func (i *Input) mouseUp(code int) {
i.mouseButtonPressed[code] = false
}
func (i *Input) updateGamepads() {
gamepad.Update()
}
func (i *Input) updateFromEvent(e js.Value) {
// Avoid using js.Value.String() as String creates a Uint8Array via a TextEncoder and causes a heavy
// overhead (#1437).
@ -376,43 +315,3 @@ func (i *Input) updateForGo2Cpp() {
}
}
}
func (i *Input) IsStandardGamepadLayoutAvailable(id driver.GamepadID) bool {
g := gamepad.Get(id)
if g == nil {
return false
}
return g.IsStandardLayoutAvailable()
}
func (i *Input) StandardGamepadAxisValue(id driver.GamepadID, axis driver.StandardGamepadAxis) float64 {
g := gamepad.Get(id)
if g == nil {
return 0
}
return g.StandardAxisValue(axis)
}
func (i *Input) StandardGamepadButtonValue(id driver.GamepadID, button driver.StandardGamepadButton) float64 {
g := gamepad.Get(id)
if g == nil {
return 0
}
return g.StandardButtonValue(button)
}
func (i *Input) IsStandardGamepadButtonPressed(id driver.GamepadID, button driver.StandardGamepadButton) bool {
g := gamepad.Get(id)
if g == nil {
return false
}
return g.IsStandardButtonPressed(button)
}
func (i *Input) VibrateGamepad(id driver.GamepadID, duration time.Duration, strongMagnitude float64, weakMagnitude float64) {
g := gamepad.Get(id)
if g == nil {
return
}
g.Vibrate(duration, strongMagnitude, weakMagnitude)
}

View File

@ -20,6 +20,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/devicescale"
"github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/gamepad"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl"
"github.com/hajimehoshi/ebiten/v2/internal/hooks"
)
@ -290,7 +291,7 @@ func (u *UserInterface) updateImpl(force bool) error {
return nil
}
u.input.updateGamepads()
gamepad.Update()
u.input.updateForGo2Cpp()
u.updateSize()
if force {

View File

@ -1,117 +0,0 @@
// 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 android || ios
// +build android ios
package mobile
import (
"time"
"github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/gamepad"
)
func (i *Input) AppendGamepadIDs(gamepadIDs []driver.GamepadID) []driver.GamepadID {
return gamepad.AppendGamepadIDs(gamepadIDs)
}
func (i *Input) GamepadSDLID(id driver.GamepadID) string {
g := gamepad.Get(id)
if g == nil {
return ""
}
return g.SDLID()
}
func (i *Input) GamepadName(id driver.GamepadID) string {
g := gamepad.Get(id)
if g == nil {
return ""
}
return g.Name()
}
func (i *Input) GamepadAxisNum(id driver.GamepadID) int {
g := gamepad.Get(id)
if g == nil {
return 0
}
return g.AxisCount()
}
func (i *Input) GamepadAxisValue(id driver.GamepadID, axis int) float64 {
g := gamepad.Get(id)
if g == nil {
return 0
}
return g.Axis(axis)
}
func (i *Input) GamepadButtonNum(id driver.GamepadID) int {
g := gamepad.Get(id)
if g == nil {
return 0
}
return g.ButtonCount()
}
func (i *Input) IsGamepadButtonPressed(id driver.GamepadID, button driver.GamepadButton) bool {
g := gamepad.Get(id)
if g == nil {
return false
}
return g.Button(int(button))
}
func (i *Input) IsStandardGamepadLayoutAvailable(id driver.GamepadID) bool {
g := gamepad.Get(id)
if g == nil {
return false
}
return g.IsStandardLayoutAvailable()
}
func (i *Input) IsStandardGamepadButtonPressed(id driver.GamepadID, button driver.StandardGamepadButton) bool {
g := gamepad.Get(id)
if g == nil {
return false
}
return g.IsStandardButtonPressed(button)
}
func (i *Input) StandardGamepadButtonValue(id driver.GamepadID, button driver.StandardGamepadButton) float64 {
g := gamepad.Get(id)
if g == nil {
return 0
}
return g.StandardButtonValue(button)
}
func (i *Input) StandardGamepadAxisValue(id driver.GamepadID, axis driver.StandardGamepadAxis) float64 {
g := gamepad.Get(id)
if g == nil {
return 0
}
return g.StandardAxisValue(axis)
}
func (i *Input) VibrateGamepad(id driver.GamepadID, duration time.Duration, strongMagnitude float64, weakMagnitude float64) {
g := gamepad.Get(id)
if g == nil {
return
}
g.Vibrate(duration, strongMagnitude, weakMagnitude)
}

View File

@ -16,6 +16,8 @@ package ebiten
import (
"time"
"github.com/hajimehoshi/ebiten/v2/internal/gamepad"
)
// VibrateOptions represents the options for device vibration.
@ -69,5 +71,9 @@ type VibrateGamepadOptions struct {
//
// VibrateGamepad is concurrent-safe.
func VibrateGamepad(gamepadID GamepadID, options *VibrateGamepadOptions) {
uiDriver().Input().VibrateGamepad(gamepadID, options.Duration, options.StrongMagnitude, options.WeakMagnitude)
g := gamepad.Get(gamepadID)
if g == nil {
return
}
g.Vibrate(options.Duration, options.StrongMagnitude, options.WeakMagnitude)
}