2022-02-05 10:30:17 +01:00
|
|
|
// 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.
|
|
|
|
|
2022-08-12 05:40:23 +02:00
|
|
|
//go:build nintendosdk
|
2022-02-05 10:30:17 +01:00
|
|
|
|
|
|
|
package gamepad
|
|
|
|
|
2022-12-31 07:27:04 +01:00
|
|
|
// #cgo !darwin LDFLAGS: -Wl,-unresolved-symbols=ignore-all
|
|
|
|
// #cgo darwin LDFLAGS: -Wl,-undefined,dynamic_lookup
|
|
|
|
//
|
|
|
|
// #include "gamepad_nintendosdk.h"
|
|
|
|
import "C"
|
|
|
|
|
2022-02-05 10:30:17 +01:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2022-08-11 05:35:20 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/gamepaddb"
|
2022-02-05 10:30:17 +01:00
|
|
|
)
|
|
|
|
|
2022-06-24 13:03:56 +02:00
|
|
|
type nativeGamepadsImpl struct {
|
2022-12-31 07:27:04 +01:00
|
|
|
gamepads []C.struct_Gamepad
|
2022-02-05 10:30:17 +01:00
|
|
|
ids map[int]struct{}
|
|
|
|
}
|
|
|
|
|
2022-06-24 13:03:56 +02:00
|
|
|
func newNativeGamepadsImpl() nativeGamepads {
|
|
|
|
return &nativeGamepadsImpl{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*nativeGamepadsImpl) init(gamepads *gamepads) error {
|
2022-02-05 10:30:17 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-24 13:03:56 +02:00
|
|
|
func (g *nativeGamepadsImpl) update(gamepads *gamepads) error {
|
2022-12-31 07:27:04 +01:00
|
|
|
C.ebitengine_UpdateGamepads()
|
|
|
|
|
2022-02-05 10:30:17 +01:00
|
|
|
g.gamepads = g.gamepads[:0]
|
2022-12-31 07:27:04 +01:00
|
|
|
if n := int(C.ebitengine_GetGamepadCount()); n > 0 {
|
|
|
|
if cap(g.gamepads) < n {
|
|
|
|
g.gamepads = make([]C.struct_Gamepad, n)
|
|
|
|
} else {
|
|
|
|
g.gamepads = g.gamepads[:n]
|
|
|
|
}
|
|
|
|
C.ebitengine_GetGamepads(&g.gamepads[0])
|
|
|
|
}
|
2022-02-05 10:30:17 +01:00
|
|
|
|
|
|
|
for id := range g.ids {
|
|
|
|
delete(g.ids, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, gp := range g.gamepads {
|
|
|
|
if g.ids == nil {
|
|
|
|
g.ids = map[int]struct{}{}
|
|
|
|
}
|
2022-12-31 07:27:04 +01:00
|
|
|
g.ids[int(gp.id)] = struct{}{}
|
2022-02-05 10:30:17 +01:00
|
|
|
|
|
|
|
gamepad := gamepads.find(func(gamepad *Gamepad) bool {
|
2022-12-31 07:27:04 +01:00
|
|
|
return gamepad.native.(*nativeGamepadImpl).id == int(gp.id)
|
2022-02-05 10:30:17 +01:00
|
|
|
})
|
|
|
|
if gamepad == nil {
|
|
|
|
gamepad = gamepads.add("", "")
|
2022-06-24 13:03:56 +02:00
|
|
|
gamepad.native = &nativeGamepadImpl{
|
2022-12-31 07:27:04 +01:00
|
|
|
id: int(gp.id),
|
|
|
|
standard: bool(gp.standard != 0),
|
|
|
|
axisValues: make([]float64, gp.axis_count),
|
|
|
|
buttonPressed: make([]bool, gp.button_count),
|
|
|
|
buttonValues: make([]float64, gp.button_count),
|
2022-06-24 13:03:56 +02:00
|
|
|
}
|
2022-02-05 10:30:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
gamepad.m.Lock()
|
2022-06-24 13:03:56 +02:00
|
|
|
n := gamepad.native.(*nativeGamepadImpl)
|
2022-12-31 07:27:04 +01:00
|
|
|
for i := range n.axisValues {
|
|
|
|
n.axisValues[i] = float64(gp.axis_values[i])
|
|
|
|
}
|
|
|
|
for i := range n.buttonValues {
|
|
|
|
n.buttonValues[i] = float64(gp.button_values[i])
|
|
|
|
}
|
|
|
|
for i := range n.buttonPressed {
|
|
|
|
n.buttonPressed[i] = gp.button_pressed[i] != 0
|
|
|
|
}
|
2022-02-05 10:30:17 +01:00
|
|
|
gamepad.m.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove an unused gamepads.
|
|
|
|
gamepads.remove(func(gamepad *Gamepad) bool {
|
2022-06-24 13:03:56 +02:00
|
|
|
_, ok := g.ids[gamepad.native.(*nativeGamepadImpl).id]
|
2022-02-05 10:30:17 +01:00
|
|
|
return !ok
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-24 13:03:56 +02:00
|
|
|
type nativeGamepadImpl struct {
|
2022-02-05 10:30:17 +01:00
|
|
|
id int
|
|
|
|
standard bool
|
|
|
|
|
|
|
|
axisValues []float64
|
|
|
|
buttonPressed []bool
|
|
|
|
buttonValues []float64
|
|
|
|
}
|
|
|
|
|
2022-06-24 13:03:56 +02:00
|
|
|
func (*nativeGamepadImpl) update(gamepad *gamepads) error {
|
2022-02-05 10:30:17 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-24 13:03:56 +02:00
|
|
|
func (g *nativeGamepadImpl) hasOwnStandardLayoutMapping() bool {
|
2022-02-05 10:30:17 +01:00
|
|
|
return g.standard
|
|
|
|
}
|
|
|
|
|
2023-03-03 15:29:04 +01:00
|
|
|
func (g *nativeGamepadImpl) standardAxisInOwnMapping(axis gamepaddb.StandardAxis) mappingInput {
|
2022-08-11 05:35:20 +02:00
|
|
|
// TODO: Implement this on the C side.
|
2023-03-03 15:29:04 +01:00
|
|
|
if axis < 0 || int(axis) >= len(g.axisValues) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return axisMappingInput{g: g, axis: int(axis)}
|
2022-08-11 05:35:20 +02:00
|
|
|
}
|
|
|
|
|
2023-03-03 15:29:04 +01:00
|
|
|
func (g *nativeGamepadImpl) standardButtonInOwnMapping(button gamepaddb.StandardButton) mappingInput {
|
2022-08-11 05:35:20 +02:00
|
|
|
// TODO: Implement this on the C side.
|
2023-03-03 15:29:04 +01:00
|
|
|
if button < 0 || int(button) >= len(g.buttonValues) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return buttonMappingInput{g: g, button: int(button)}
|
2022-08-11 05:35:20 +02:00
|
|
|
}
|
|
|
|
|
2022-06-24 13:03:56 +02:00
|
|
|
func (g *nativeGamepadImpl) axisCount() int {
|
2022-02-05 10:30:17 +01:00
|
|
|
return len(g.axisValues)
|
|
|
|
}
|
|
|
|
|
2022-06-24 13:03:56 +02:00
|
|
|
func (g *nativeGamepadImpl) buttonCount() int {
|
2022-02-05 10:30:17 +01:00
|
|
|
return len(g.buttonValues)
|
|
|
|
}
|
|
|
|
|
2022-06-24 13:03:56 +02:00
|
|
|
func (g *nativeGamepadImpl) hatCount() int {
|
2022-02-05 10:30:17 +01:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2022-06-24 13:03:56 +02:00
|
|
|
func (g *nativeGamepadImpl) axisValue(axis int) float64 {
|
2022-02-05 10:30:17 +01:00
|
|
|
if axis < 0 || axis >= len(g.axisValues) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return g.axisValues[axis]
|
|
|
|
}
|
|
|
|
|
2022-06-24 13:03:56 +02:00
|
|
|
func (g *nativeGamepadImpl) isButtonPressed(button int) bool {
|
2022-02-05 10:30:17 +01:00
|
|
|
if button < 0 || button >= len(g.buttonPressed) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return g.buttonPressed[button]
|
|
|
|
}
|
|
|
|
|
2022-06-24 13:03:56 +02:00
|
|
|
func (g *nativeGamepadImpl) buttonValue(button int) float64 {
|
2022-02-05 10:30:17 +01:00
|
|
|
if button < 0 || button >= len(g.buttonValues) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return g.buttonValues[button]
|
|
|
|
}
|
|
|
|
|
2022-06-24 13:03:56 +02:00
|
|
|
func (*nativeGamepadImpl) hatState(hat int) int {
|
2022-02-05 10:30:17 +01:00
|
|
|
return hatCentered
|
|
|
|
}
|
|
|
|
|
2022-06-24 13:03:56 +02:00
|
|
|
func (g *nativeGamepadImpl) vibrate(duration time.Duration, strongMagnitude float64, weakMagnitude float64) {
|
2022-12-31 07:27:04 +01:00
|
|
|
C.ebitengine_VibrateGamepad(C.int(g.id), C.double(float64(duration)/float64(time.Second)), C.double(strongMagnitude), C.double(weakMagnitude))
|
2022-02-05 10:30:17 +01:00
|
|
|
}
|