2015-01-06 13:52:36 +01:00
|
|
|
// Copyright 2015 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2021-06-10 18:03:35 +02:00
|
|
|
//go:build (darwin || freebsd || linux || windows) && !android && !ios
|
2017-05-11 12:09:13 +02:00
|
|
|
// +build darwin freebsd linux windows
|
2016-05-18 20:17:50 +02:00
|
|
|
// +build !android
|
2016-06-15 17:49:22 +02:00
|
|
|
// +build !ios
|
2015-01-06 13:52:36 +01:00
|
|
|
|
2021-07-21 08:08:35 +02:00
|
|
|
//go:generate file2byteslice -package glfw -input=./gamecontrollerdb.txt -output=./gamecontrollerdb.txt.go -var=gamecontrollerdbTxt
|
|
|
|
|
2019-04-07 11:55:56 +02:00
|
|
|
package glfw
|
2015-01-06 13:52:36 +01:00
|
|
|
|
|
|
|
import (
|
2021-07-21 08:08:35 +02:00
|
|
|
"fmt"
|
2021-03-27 10:40:39 +01:00
|
|
|
"math"
|
2017-04-11 04:56:05 +02:00
|
|
|
"sync"
|
2017-08-14 21:11:51 +02:00
|
|
|
"unicode"
|
2017-04-11 04:56:05 +02:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/driver"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
|
2015-01-06 13:52:36 +01:00
|
|
|
)
|
|
|
|
|
2021-07-19 15:25:43 +02:00
|
|
|
type gamepad struct {
|
2019-04-07 11:55:56 +02:00
|
|
|
valid bool
|
2020-01-09 14:11:32 +01:00
|
|
|
guid string
|
|
|
|
name string
|
2019-04-07 11:55:56 +02:00
|
|
|
axisNum int
|
|
|
|
axes [16]float64
|
|
|
|
buttonNum int
|
|
|
|
buttonPressed [256]bool
|
2021-07-21 08:08:35 +02:00
|
|
|
state *glfw.GamepadState
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
|
2017-04-28 16:45:01 +02:00
|
|
|
type Input struct {
|
2019-03-31 13:20:52 +02:00
|
|
|
keyPressed map[glfw.Key]bool
|
|
|
|
mouseButtonPressed map[glfw.MouseButton]bool
|
|
|
|
onceCallback sync.Once
|
|
|
|
scrollX float64
|
|
|
|
scrollY float64
|
|
|
|
cursorX int
|
|
|
|
cursorY int
|
2021-07-19 15:25:43 +02:00
|
|
|
gamepads [16]gamepad
|
2020-10-09 20:36:14 +02:00
|
|
|
touches map[driver.TouchID]pos // TODO: Implement this (#417)
|
2019-03-31 13:20:52 +02:00
|
|
|
runeBuffer []rune
|
2019-04-07 12:27:30 +02:00
|
|
|
ui *UserInterface
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
|
|
|
|
2019-04-07 11:55:56 +02:00
|
|
|
type pos struct {
|
|
|
|
X int
|
|
|
|
Y int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Input) CursorPosition() (x, y int) {
|
2019-12-11 19:20:20 +01:00
|
|
|
if !i.ui.isRunning() {
|
|
|
|
return 0, 0
|
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
|
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
|
|
|
return i.cursorX, i.cursorY
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
|
2021-07-09 18:15:47 +02:00
|
|
|
func (i *Input) AppendGamepadIDs(gamepadIDs []driver.GamepadID) []driver.GamepadID {
|
2019-12-11 19:20:20 +01:00
|
|
|
if !i.ui.isRunning() {
|
2019-04-07 11:55:56 +02:00
|
|
|
return nil
|
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
|
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
|
|
|
for id, g := range i.gamepads {
|
|
|
|
if g.valid {
|
2021-07-09 18:15:47 +02:00
|
|
|
gamepadIDs = append(gamepadIDs, driver.GamepadID(id))
|
2019-12-11 19:20:20 +01:00
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
}
|
2021-07-09 18:15:47 +02:00
|
|
|
return gamepadIDs
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-07 18:08:30 +02:00
|
|
|
func (i *Input) GamepadSDLID(id driver.GamepadID) string {
|
2020-01-09 14:11:32 +01:00
|
|
|
if !i.ui.isRunning() {
|
|
|
|
return ""
|
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
|
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
|
|
|
if len(i.gamepads) <= int(id) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return i.gamepads[id].guid
|
2020-01-09 14:11:32 +01:00
|
|
|
}
|
|
|
|
|
2020-10-07 18:08:30 +02:00
|
|
|
func (i *Input) GamepadName(id driver.GamepadID) string {
|
2020-01-09 14:11:32 +01:00
|
|
|
if !i.ui.isRunning() {
|
|
|
|
return ""
|
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
|
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
|
|
|
if len(i.gamepads) <= int(id) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return i.gamepads[id].name
|
2020-01-09 14:11:32 +01:00
|
|
|
}
|
|
|
|
|
2020-10-07 18:08:30 +02:00
|
|
|
func (i *Input) GamepadAxisNum(id driver.GamepadID) int {
|
2019-12-11 19:20:20 +01:00
|
|
|
if !i.ui.isRunning() {
|
2019-04-07 11:55:56 +02:00
|
|
|
return 0
|
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
|
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
|
|
|
if len(i.gamepads) <= int(id) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return i.gamepads[id].axisNum
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
|
2021-07-18 18:22:31 +02:00
|
|
|
func (i *Input) GamepadAxisValue(id driver.GamepadID, axis int) float64 {
|
2019-12-11 19:20:20 +01:00
|
|
|
if !i.ui.isRunning() {
|
2019-04-07 11:55:56 +02:00
|
|
|
return 0
|
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
|
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
|
|
|
if len(i.gamepads) <= int(id) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return i.gamepads[id].axes[axis]
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-07 18:08:30 +02:00
|
|
|
func (i *Input) GamepadButtonNum(id driver.GamepadID) int {
|
2019-12-11 19:20:20 +01:00
|
|
|
if !i.ui.isRunning() {
|
2019-04-07 11:55:56 +02:00
|
|
|
return 0
|
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
|
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
|
|
|
if len(i.gamepads) <= int(id) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return i.gamepads[id].buttonNum
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-07 18:08:30 +02:00
|
|
|
func (i *Input) IsGamepadButtonPressed(id driver.GamepadID, button driver.GamepadButton) bool {
|
2019-12-11 19:20:20 +01:00
|
|
|
if !i.ui.isRunning() {
|
2019-04-07 11:55:56 +02:00
|
|
|
return false
|
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
|
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
|
|
|
if len(i.gamepads) <= int(id) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return i.gamepads[id].buttonPressed[button]
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
|
2021-07-09 18:15:47 +02:00
|
|
|
func (i *Input) AppendTouchIDs(touchIDs []driver.TouchID) []driver.TouchID {
|
2019-12-11 19:20:20 +01:00
|
|
|
if !i.ui.isRunning() {
|
2019-04-07 11:55:56 +02:00
|
|
|
return nil
|
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
|
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
|
|
|
for id := range i.touches {
|
2021-07-09 18:15:47 +02:00
|
|
|
touchIDs = append(touchIDs, id)
|
2020-11-18 11:33:20 +01:00
|
|
|
}
|
2021-07-09 18:15:47 +02:00
|
|
|
return touchIDs
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 20:36:14 +02:00
|
|
|
func (i *Input) TouchPosition(id driver.TouchID) (x, y int) {
|
2019-12-11 19:20:20 +01:00
|
|
|
if !i.ui.isRunning() {
|
|
|
|
return 0, 0
|
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
|
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
|
|
|
for tid, pos := range i.touches {
|
|
|
|
if id == tid {
|
|
|
|
return pos.X, pos.Y
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
2019-08-18 18:01:43 +02:00
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
return 0, 0
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
|
2021-07-09 18:15:47 +02:00
|
|
|
func (i *Input) AppendInputChars(runes []rune) []rune {
|
2019-12-11 19:20:20 +01:00
|
|
|
if !i.ui.isRunning() {
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
|
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
2021-07-09 18:15:47 +02:00
|
|
|
return append(runes, i.runeBuffer...)
|
2017-08-14 21:11:51 +02:00
|
|
|
}
|
|
|
|
|
2020-04-02 17:06:42 +02:00
|
|
|
func (i *Input) resetForFrame() {
|
2019-12-11 19:20:20 +01:00
|
|
|
if !i.ui.isRunning() {
|
|
|
|
return
|
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
|
|
|
|
i.ui.m.Lock()
|
|
|
|
defer i.ui.m.Unlock()
|
|
|
|
i.runeBuffer = i.runeBuffer[:0]
|
|
|
|
i.scrollX, i.scrollY = 0, 0
|
2018-06-17 17:38:30 +02:00
|
|
|
}
|
|
|
|
|
2019-03-30 15:28:07 +01:00
|
|
|
func (i *Input) IsKeyPressed(key driver.Key) bool {
|
2019-12-11 19:20:20 +01:00
|
|
|
if !i.ui.isRunning() {
|
|
|
|
return false
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
|
|
|
|
i.ui.m.Lock()
|
|
|
|
defer i.ui.m.Unlock()
|
|
|
|
if i.keyPressed == nil {
|
|
|
|
i.keyPressed = map[glfw.Key]bool{}
|
|
|
|
}
|
|
|
|
gk, ok := driverKeyToGLFWKey[key]
|
|
|
|
return ok && i.keyPressed[gk]
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
|
|
|
|
2019-03-30 15:19:52 +01:00
|
|
|
func (i *Input) IsMouseButtonPressed(button driver.MouseButton) bool {
|
2019-12-11 19:20:20 +01:00
|
|
|
if !i.ui.isRunning() {
|
|
|
|
return false
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
|
|
|
|
i.ui.m.Lock()
|
|
|
|
defer i.ui.m.Unlock()
|
|
|
|
if i.mouseButtonPressed == nil {
|
|
|
|
i.mouseButtonPressed = map[glfw.MouseButton]bool{}
|
|
|
|
}
|
|
|
|
for gb, b := range glfwMouseButtonToMouseButton {
|
|
|
|
if b != button {
|
|
|
|
continue
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
if i.mouseButtonPressed[gb] {
|
|
|
|
return true
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
}
|
|
|
|
return false
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
|
|
|
|
2018-09-30 10:01:45 +02:00
|
|
|
func (i *Input) Wheel() (xoff, yoff float64) {
|
2019-12-11 19:20:20 +01:00
|
|
|
if !i.ui.isRunning() {
|
|
|
|
return 0, 0
|
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
|
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
|
|
|
return i.scrollX, i.scrollY
|
2018-06-17 17:38:30 +02:00
|
|
|
}
|
|
|
|
|
2019-03-30 15:19:52 +01:00
|
|
|
var glfwMouseButtonToMouseButton = map[glfw.MouseButton]driver.MouseButton{
|
|
|
|
glfw.MouseButtonLeft: driver.MouseButtonLeft,
|
|
|
|
glfw.MouseButtonRight: driver.MouseButtonRight,
|
|
|
|
glfw.MouseButtonMiddle: driver.MouseButtonMiddle,
|
2015-01-06 20:04:04 +01:00
|
|
|
}
|
|
|
|
|
2020-10-16 22:16:02 +02:00
|
|
|
// update must be called from the main thread.
|
2019-12-09 18:37:10 +01:00
|
|
|
func (i *Input) update(window *glfw.Window, context driver.UIContext) {
|
2020-11-18 11:33:20 +01:00
|
|
|
i.ui.m.Lock()
|
|
|
|
defer i.ui.m.Unlock()
|
|
|
|
|
2020-10-16 22:16:02 +02:00
|
|
|
i.onceCallback.Do(func() {
|
2021-06-13 14:43:23 +02:00
|
|
|
window.SetCharModsCallback(glfw.ToCharModsCallback(func(w *glfw.Window, char rune, mods glfw.ModifierKey) {
|
2020-11-18 11:33:20 +01:00
|
|
|
// As this function is called from GLFW callbacks, the current thread is main.
|
|
|
|
if !unicode.IsPrint(char) {
|
|
|
|
return
|
|
|
|
}
|
2020-11-18 12:43:29 +01:00
|
|
|
|
|
|
|
i.ui.m.Lock()
|
|
|
|
defer i.ui.m.Unlock()
|
2020-11-18 11:33:20 +01:00
|
|
|
i.runeBuffer = append(i.runeBuffer, char)
|
2021-06-13 14:43:23 +02:00
|
|
|
}))
|
|
|
|
window.SetScrollCallback(glfw.ToScrollCallback(func(w *glfw.Window, xoff float64, yoff float64) {
|
2020-11-18 11:33:20 +01:00
|
|
|
// As this function is called from GLFW callbacks, the current thread is main.
|
2020-11-18 12:43:29 +01:00
|
|
|
i.ui.m.Lock()
|
|
|
|
defer i.ui.m.Unlock()
|
2020-11-18 11:33:20 +01:00
|
|
|
i.scrollX = xoff
|
|
|
|
i.scrollY = yoff
|
2021-06-13 14:43:23 +02:00
|
|
|
}))
|
2019-12-09 18:37:10 +01:00
|
|
|
})
|
2020-10-16 22:16:02 +02:00
|
|
|
if i.keyPressed == nil {
|
|
|
|
i.keyPressed = map[glfw.Key]bool{}
|
|
|
|
}
|
|
|
|
for gk := range glfwKeyToDriverKey {
|
|
|
|
i.keyPressed[gk] = window.GetKey(gk) == glfw.Press
|
|
|
|
}
|
|
|
|
if i.mouseButtonPressed == nil {
|
|
|
|
i.mouseButtonPressed = map[glfw.MouseButton]bool{}
|
|
|
|
}
|
|
|
|
for gb := range glfwMouseButtonToMouseButton {
|
|
|
|
i.mouseButtonPressed[gb] = window.GetMouseButton(gb) == glfw.Press
|
|
|
|
}
|
|
|
|
cx, cy := window.GetCursorPos()
|
|
|
|
// TODO: This is tricky. Rename the function?
|
2021-02-10 19:45:35 +01:00
|
|
|
s := i.ui.deviceScaleFactor()
|
|
|
|
cx = fromGLFWMonitorPixel(cx, s)
|
|
|
|
cy = fromGLFWMonitorPixel(cy, s)
|
2021-03-27 10:35:31 +01:00
|
|
|
cx, cy = context.AdjustPosition(cx, cy, s)
|
2021-03-27 10:40:39 +01:00
|
|
|
|
|
|
|
// AdjustPosition can return NaN at the initialization.
|
|
|
|
if !math.IsNaN(cx) && !math.IsNaN(cy) {
|
|
|
|
i.cursorX, i.cursorY = int(cx), int(cy)
|
|
|
|
}
|
2020-10-16 22:16:02 +02:00
|
|
|
|
|
|
|
for id := glfw.Joystick(0); id < glfw.Joystick(len(i.gamepads)); id++ {
|
|
|
|
i.gamepads[id].valid = false
|
|
|
|
if !id.Present() {
|
|
|
|
continue
|
|
|
|
}
|
2019-12-09 18:37:10 +01:00
|
|
|
|
2021-07-21 08:08:35 +02:00
|
|
|
i.gamepads[id].state = id.GetGamepadState()
|
|
|
|
|
|
|
|
// Note that GLFW's gamepad GUID follows SDL's GUID.
|
|
|
|
i.gamepads[id].guid = id.GetGUID()
|
|
|
|
i.gamepads[id].name = id.GetName()
|
|
|
|
|
2020-10-16 22:16:02 +02:00
|
|
|
buttons := id.GetButtons()
|
2019-12-09 18:37:10 +01:00
|
|
|
|
2020-10-16 22:16:02 +02:00
|
|
|
// A gamepad can be detected even though there are not. Apparently, some special devices are
|
|
|
|
// recognized as gamepads by GLFW. In this case, the number of the 'buttons' can exceeds the
|
|
|
|
// maximum. Skip such devices as a tentative solution (#1173).
|
|
|
|
if len(buttons) > driver.GamepadButtonNum {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-03 15:58:46 +02:00
|
|
|
|
2020-10-16 22:16:02 +02:00
|
|
|
i.gamepads[id].valid = true
|
2020-07-03 15:58:46 +02:00
|
|
|
|
2020-10-16 22:16:02 +02:00
|
|
|
i.gamepads[id].buttonNum = len(buttons)
|
|
|
|
for b := 0; b < len(i.gamepads[id].buttonPressed); b++ {
|
|
|
|
if len(buttons) <= b {
|
|
|
|
i.gamepads[id].buttonPressed[b] = false
|
2020-07-03 15:58:46 +02:00
|
|
|
continue
|
|
|
|
}
|
2020-10-16 22:16:02 +02:00
|
|
|
i.gamepads[id].buttonPressed[b] = glfw.Action(buttons[b]) == glfw.Press
|
|
|
|
}
|
2020-07-03 15:58:46 +02:00
|
|
|
|
2020-10-16 22:16:02 +02:00
|
|
|
axes32 := id.GetAxes()
|
|
|
|
i.gamepads[id].axisNum = len(axes32)
|
|
|
|
for a := 0; a < len(i.gamepads[id].axes); a++ {
|
|
|
|
if len(axes32) <= a {
|
|
|
|
i.gamepads[id].axes[a] = 0
|
|
|
|
continue
|
2019-12-11 19:20:20 +01:00
|
|
|
}
|
2020-10-16 22:16:02 +02:00
|
|
|
i.gamepads[id].axes[a] = float64(axes32[a])
|
2015-01-11 17:54:18 +01:00
|
|
|
}
|
2020-10-16 22:16:02 +02:00
|
|
|
}
|
2015-01-06 13:52:36 +01:00
|
|
|
}
|
2021-07-18 16:28:26 +02:00
|
|
|
|
2021-07-19 19:46:09 +02:00
|
|
|
func (i *Input) IsStandardGamepadLayoutAvailable(id driver.GamepadID) bool {
|
2021-07-18 16:28:26 +02:00
|
|
|
i.ui.m.Lock()
|
|
|
|
defer i.ui.m.Unlock()
|
|
|
|
|
|
|
|
if len(i.gamepads) <= int(id) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
g := i.gamepads[int(id)]
|
2021-07-21 08:08:35 +02:00
|
|
|
return g.state != nil
|
2021-07-18 16:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Input) StandardGamepadAxisValue(id driver.GamepadID, axis driver.StandardGamepadAxis) float64 {
|
|
|
|
i.ui.m.Lock()
|
|
|
|
defer i.ui.m.Unlock()
|
|
|
|
|
|
|
|
if len(i.gamepads) <= int(id) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
g := i.gamepads[int(id)]
|
2021-07-21 08:08:35 +02:00
|
|
|
if g.state == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return float64(g.state.Axes[standardAxisToGLFWAxis(axis)])
|
2021-07-18 16:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Input) IsStandardGamepadButtonPressed(id driver.GamepadID, button driver.StandardGamepadButton) bool {
|
|
|
|
i.ui.m.Lock()
|
|
|
|
defer i.ui.m.Unlock()
|
|
|
|
|
|
|
|
if len(i.gamepads) <= int(id) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
g := i.gamepads[int(id)]
|
2021-07-21 08:08:35 +02:00
|
|
|
if g.state == nil {
|
|
|
|
return false
|
2021-07-18 16:28:26 +02:00
|
|
|
}
|
2021-07-21 08:08:35 +02:00
|
|
|
switch button {
|
|
|
|
case driver.StandardGamepadButtonFrontBottomLeft:
|
|
|
|
return g.state.Axes[glfw.AxisLeftTrigger] > 0
|
|
|
|
case driver.StandardGamepadButtonFrontBottomRight:
|
|
|
|
return g.state.Axes[glfw.AxisRightTrigger] > 0
|
2021-07-18 16:28:26 +02:00
|
|
|
}
|
2021-07-21 08:08:35 +02:00
|
|
|
return g.state.Buttons[standardButtonToGLFWButton(button)] == glfw.Press
|
2021-07-18 16:28:26 +02:00
|
|
|
}
|
|
|
|
|
2021-07-21 08:08:35 +02:00
|
|
|
func standardAxisToGLFWAxis(axis driver.StandardGamepadAxis) glfw.GamepadAxis {
|
|
|
|
switch axis {
|
|
|
|
case driver.StandardGamepadAxisLeftStickHorizontal:
|
|
|
|
return glfw.AxisLeftX
|
|
|
|
case driver.StandardGamepadAxisLeftStickVertical:
|
|
|
|
return glfw.AxisLeftY
|
|
|
|
case driver.StandardGamepadAxisRightStickHorizontal:
|
|
|
|
return glfw.AxisRightX
|
|
|
|
case driver.StandardGamepadAxisRightStickVertical:
|
|
|
|
return glfw.AxisRightY
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("glfw: invalid or inconvertible StandardGamepadAxis: %d", axis))
|
|
|
|
}
|
2021-07-18 16:28:26 +02:00
|
|
|
}
|
|
|
|
|
2021-07-21 08:08:35 +02:00
|
|
|
func standardButtonToGLFWButton(button driver.StandardGamepadButton) glfw.GamepadButton {
|
|
|
|
switch button {
|
|
|
|
case driver.StandardGamepadButtonRightBottom:
|
|
|
|
return glfw.ButtonA
|
|
|
|
case driver.StandardGamepadButtonRightRight:
|
|
|
|
return glfw.ButtonB
|
|
|
|
case driver.StandardGamepadButtonRightLeft:
|
|
|
|
return glfw.ButtonX
|
|
|
|
case driver.StandardGamepadButtonRightTop:
|
|
|
|
return glfw.ButtonY
|
|
|
|
case driver.StandardGamepadButtonFrontTopLeft:
|
|
|
|
return glfw.ButtonLeftBumper
|
|
|
|
case driver.StandardGamepadButtonFrontTopRight:
|
|
|
|
return glfw.ButtonRightBumper
|
|
|
|
case driver.StandardGamepadButtonCenterLeft:
|
|
|
|
return glfw.ButtonBack
|
|
|
|
case driver.StandardGamepadButtonCenterRight:
|
|
|
|
return glfw.ButtonStart
|
|
|
|
case driver.StandardGamepadButtonLeftStick:
|
|
|
|
return glfw.ButtonLeftThumb
|
|
|
|
case driver.StandardGamepadButtonRightStick:
|
|
|
|
return glfw.ButtonRightThumb
|
|
|
|
case driver.StandardGamepadButtonLeftTop:
|
|
|
|
return glfw.ButtonDpadUp
|
|
|
|
case driver.StandardGamepadButtonLeftBottom:
|
|
|
|
return glfw.ButtonDpadDown
|
|
|
|
case driver.StandardGamepadButtonLeftLeft:
|
|
|
|
return glfw.ButtonDpadLeft
|
|
|
|
case driver.StandardGamepadButtonLeftRight:
|
|
|
|
return glfw.ButtonDpadRight
|
|
|
|
case driver.StandardGamepadButtonCenterCenter:
|
|
|
|
return glfw.ButtonGuide
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("glfw: invalid or inconvertible StandardGamepadButton: %d", button))
|
|
|
|
}
|
2021-07-18 16:28:26 +02:00
|
|
|
}
|