2015-01-19 14:21:24 +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.
|
|
|
|
|
2016-09-03 07:03:41 +02:00
|
|
|
package blocks
|
2015-01-19 14:21:24 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-09-03 07:03:41 +02:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
2015-01-19 14:21:24 +01:00
|
|
|
)
|
|
|
|
|
2018-01-22 07:12:17 +01:00
|
|
|
type virtualGamepadButton int
|
2015-01-19 14:21:24 +01:00
|
|
|
|
|
|
|
const (
|
2018-01-22 07:12:17 +01:00
|
|
|
virtualGamepadButtonLeft virtualGamepadButton = iota
|
|
|
|
virtualGamepadButtonRight
|
|
|
|
virtualGamepadButtonDown
|
|
|
|
virtualGamepadButtonButtonA
|
|
|
|
virtualGamepadButtonButtonB
|
2015-01-19 14:21:24 +01:00
|
|
|
)
|
|
|
|
|
2018-01-22 07:12:17 +01:00
|
|
|
var virtualGamepadButtons = []virtualGamepadButton{
|
|
|
|
virtualGamepadButtonLeft,
|
|
|
|
virtualGamepadButtonRight,
|
|
|
|
virtualGamepadButtonDown,
|
|
|
|
virtualGamepadButtonButtonA,
|
|
|
|
virtualGamepadButtonButtonB,
|
|
|
|
}
|
|
|
|
|
2021-07-19 20:48:01 +02:00
|
|
|
func (v virtualGamepadButton) StandardGamepadButton() ebiten.StandardGamepadButton {
|
|
|
|
switch v {
|
|
|
|
case virtualGamepadButtonLeft:
|
|
|
|
return ebiten.StandardGamepadButtonLeftLeft
|
|
|
|
case virtualGamepadButtonRight:
|
|
|
|
return ebiten.StandardGamepadButtonLeftRight
|
|
|
|
case virtualGamepadButtonDown:
|
|
|
|
return ebiten.StandardGamepadButtonLeftBottom
|
|
|
|
case virtualGamepadButtonButtonA:
|
|
|
|
return ebiten.StandardGamepadButtonRightBottom
|
|
|
|
case virtualGamepadButtonButtonB:
|
|
|
|
return ebiten.StandardGamepadButtonRightRight
|
|
|
|
default:
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 15:06:27 +01:00
|
|
|
const axisThreshold = 0.75
|
2015-01-19 14:21:24 +01:00
|
|
|
|
|
|
|
type axis struct {
|
2023-12-24 17:50:20 +01:00
|
|
|
id ebiten.GamepadAxisType
|
2015-01-19 14:21:24 +01:00
|
|
|
positive bool
|
|
|
|
}
|
|
|
|
|
2016-09-03 07:33:45 +02:00
|
|
|
type gamepadConfig struct {
|
2020-10-07 18:08:30 +02:00
|
|
|
gamepadID ebiten.GamepadID
|
2018-10-13 09:25:33 +02:00
|
|
|
gamepadIDInitialized bool
|
|
|
|
|
2018-01-22 07:12:17 +01:00
|
|
|
current virtualGamepadButton
|
|
|
|
buttons map[virtualGamepadButton]ebiten.GamepadButton
|
|
|
|
axes map[virtualGamepadButton]axis
|
2015-01-19 14:21:24 +01:00
|
|
|
assignedButtons map[ebiten.GamepadButton]struct{}
|
|
|
|
assignedAxes map[axis]struct{}
|
2018-01-22 15:06:27 +01:00
|
|
|
|
2023-12-24 17:50:20 +01:00
|
|
|
defaultAxesValues map[ebiten.GamepadAxisType]float64
|
2015-01-19 14:21:24 +01:00
|
|
|
}
|
|
|
|
|
2020-10-07 18:08:30 +02:00
|
|
|
func (c *gamepadConfig) SetGamepadID(id ebiten.GamepadID) {
|
2018-10-13 09:25:33 +02:00
|
|
|
c.gamepadID = id
|
|
|
|
c.gamepadIDInitialized = true
|
|
|
|
}
|
|
|
|
|
2021-07-19 20:48:01 +02:00
|
|
|
func (c *gamepadConfig) ResetGamepadID() {
|
|
|
|
c.gamepadID = 0
|
|
|
|
c.gamepadIDInitialized = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *gamepadConfig) IsGamepadIDInitialized() bool {
|
2018-10-13 09:25:33 +02:00
|
|
|
return c.gamepadIDInitialized
|
|
|
|
}
|
|
|
|
|
2021-07-19 20:48:01 +02:00
|
|
|
func (c *gamepadConfig) NeedsConfiguration() bool {
|
|
|
|
return !ebiten.IsStandardGamepadLayoutAvailable(c.gamepadID)
|
|
|
|
}
|
|
|
|
|
2016-09-03 07:33:45 +02:00
|
|
|
func (c *gamepadConfig) initializeIfNeeded() {
|
2018-10-13 09:25:33 +02:00
|
|
|
if !c.gamepadIDInitialized {
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
|
2021-07-19 20:48:01 +02:00
|
|
|
if ebiten.IsStandardGamepadLayoutAvailable(c.gamepadID) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-19 14:21:24 +01:00
|
|
|
if c.buttons == nil {
|
2018-01-22 07:12:17 +01:00
|
|
|
c.buttons = map[virtualGamepadButton]ebiten.GamepadButton{}
|
2015-01-19 14:21:24 +01:00
|
|
|
}
|
|
|
|
if c.axes == nil {
|
2018-01-22 07:12:17 +01:00
|
|
|
c.axes = map[virtualGamepadButton]axis{}
|
2015-01-19 14:21:24 +01:00
|
|
|
}
|
|
|
|
if c.assignedButtons == nil {
|
|
|
|
c.assignedButtons = map[ebiten.GamepadButton]struct{}{}
|
|
|
|
}
|
|
|
|
if c.assignedAxes == nil {
|
|
|
|
c.assignedAxes = map[axis]struct{}{}
|
|
|
|
}
|
2018-01-22 15:06:27 +01:00
|
|
|
|
|
|
|
// Set default values.
|
|
|
|
// It is assumed that all axes are not pressed here.
|
|
|
|
//
|
|
|
|
// These default values are used to detect if an axis is actually pressed.
|
2023-12-17 14:42:34 +01:00
|
|
|
// For example, on PS4 controllers, L2/R2's axes value can be -1.0.
|
2018-01-22 15:06:27 +01:00
|
|
|
if c.defaultAxesValues == nil {
|
2023-12-24 17:50:20 +01:00
|
|
|
c.defaultAxesValues = map[ebiten.GamepadAxisType]float64{}
|
|
|
|
na := ebiten.GamepadAxisType(ebiten.GamepadAxisCount(c.gamepadID))
|
|
|
|
for a := ebiten.GamepadAxisType(0); a < na; a++ {
|
2021-07-18 18:31:17 +02:00
|
|
|
c.defaultAxesValues[a] = ebiten.GamepadAxisValue(c.gamepadID, a)
|
2018-01-22 15:06:27 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-19 14:21:24 +01:00
|
|
|
}
|
|
|
|
|
2016-09-03 07:33:45 +02:00
|
|
|
func (c *gamepadConfig) Reset() {
|
2015-01-19 14:21:24 +01:00
|
|
|
c.buttons = nil
|
|
|
|
c.axes = nil
|
|
|
|
c.assignedButtons = nil
|
|
|
|
c.assignedAxes = nil
|
|
|
|
}
|
|
|
|
|
2018-01-22 07:12:17 +01:00
|
|
|
// Scan scans the current input state and assigns the given virtual gamepad button b
|
2023-12-17 14:42:34 +01:00
|
|
|
// to the current (physical) pressed buttons of the gamepad.
|
2018-10-13 09:25:33 +02:00
|
|
|
func (c *gamepadConfig) Scan(b virtualGamepadButton) bool {
|
|
|
|
if !c.gamepadIDInitialized {
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
|
2015-01-19 14:21:24 +01:00
|
|
|
c.initializeIfNeeded()
|
|
|
|
|
|
|
|
delete(c.buttons, b)
|
|
|
|
delete(c.axes, b)
|
|
|
|
|
2022-07-12 18:27:14 +02:00
|
|
|
ebn := ebiten.GamepadButton(ebiten.GamepadButtonCount(c.gamepadID))
|
2015-01-19 14:21:24 +01:00
|
|
|
for eb := ebiten.GamepadButton(0); eb < ebn; eb++ {
|
|
|
|
if _, ok := c.assignedButtons[eb]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-13 09:25:33 +02:00
|
|
|
if inpututil.IsGamepadButtonJustPressed(c.gamepadID, eb) {
|
2015-01-19 14:21:24 +01:00
|
|
|
c.buttons[b] = eb
|
|
|
|
c.assignedButtons[eb] = struct{}{}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-24 17:50:20 +01:00
|
|
|
na := ebiten.GamepadAxisType(ebiten.GamepadAxisCount(c.gamepadID))
|
|
|
|
for a := ebiten.GamepadAxisType(0); a < na; a++ {
|
2021-07-18 18:31:17 +02:00
|
|
|
v := ebiten.GamepadAxisValue(c.gamepadID, a)
|
2018-10-13 09:25:33 +02:00
|
|
|
const delta = 0.25
|
|
|
|
|
2018-01-22 15:06:27 +01:00
|
|
|
// Check |v| < 1.0 because there is a bug that a button returns
|
|
|
|
// an axis value wrongly and the value may be over 1 on some platforms.
|
2018-10-13 09:25:33 +02:00
|
|
|
if axisThreshold <= v && v <= 1.0 &&
|
|
|
|
(v < c.defaultAxesValues[a]-delta || c.defaultAxesValues[a]+delta < v) {
|
2015-01-19 14:21:24 +01:00
|
|
|
if _, ok := c.assignedAxes[axis{a, true}]; !ok {
|
|
|
|
c.axes[b] = axis{a, true}
|
|
|
|
c.assignedAxes[axis{a, true}] = struct{}{}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2018-10-13 09:25:33 +02:00
|
|
|
if -1.0 <= v && v <= -axisThreshold &&
|
|
|
|
(v < c.defaultAxesValues[a]-delta || c.defaultAxesValues[a]+delta < v) {
|
2015-01-19 14:21:24 +01:00
|
|
|
if _, ok := c.assignedAxes[axis{a, false}]; !ok {
|
|
|
|
c.axes[b] = axis{a, false}
|
|
|
|
c.assignedAxes[axis{a, false}] = struct{}{}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-04-17 14:55:58 +02:00
|
|
|
// IsButtonPressed reports whether the given virtual button b is pressed.
|
2018-01-22 07:12:17 +01:00
|
|
|
func (c *gamepadConfig) IsButtonPressed(b virtualGamepadButton) bool {
|
2018-10-13 09:25:33 +02:00
|
|
|
if !c.gamepadIDInitialized {
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
|
2021-07-19 20:48:01 +02:00
|
|
|
if ebiten.IsStandardGamepadLayoutAvailable(c.gamepadID) {
|
2021-09-24 16:35:01 +02:00
|
|
|
if ebiten.IsStandardGamepadButtonPressed(c.gamepadID, b.StandardGamepadButton()) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
const threshold = 0.7
|
|
|
|
switch b {
|
|
|
|
case virtualGamepadButtonLeft:
|
|
|
|
return ebiten.StandardGamepadAxisValue(c.gamepadID, ebiten.StandardGamepadAxisLeftStickHorizontal) < -threshold
|
|
|
|
case virtualGamepadButtonRight:
|
|
|
|
return ebiten.StandardGamepadAxisValue(c.gamepadID, ebiten.StandardGamepadAxisLeftStickHorizontal) > threshold
|
|
|
|
case virtualGamepadButtonDown:
|
|
|
|
return ebiten.StandardGamepadAxisValue(c.gamepadID, ebiten.StandardGamepadAxisLeftStickVertical) > threshold
|
|
|
|
}
|
|
|
|
return false
|
2021-07-19 20:48:01 +02:00
|
|
|
}
|
|
|
|
|
2015-01-19 14:21:24 +01:00
|
|
|
c.initializeIfNeeded()
|
|
|
|
|
|
|
|
bb, ok := c.buttons[b]
|
|
|
|
if ok {
|
2018-10-13 09:25:33 +02:00
|
|
|
return ebiten.IsGamepadButtonPressed(c.gamepadID, bb)
|
2015-01-19 14:21:24 +01:00
|
|
|
}
|
2018-01-22 12:57:54 +01:00
|
|
|
|
2015-01-19 14:21:24 +01:00
|
|
|
a, ok := c.axes[b]
|
|
|
|
if ok {
|
2021-07-18 18:31:17 +02:00
|
|
|
v := ebiten.GamepadAxisValue(c.gamepadID, a.id)
|
2015-01-19 14:21:24 +01:00
|
|
|
if a.positive {
|
2018-01-22 15:06:27 +01:00
|
|
|
return axisThreshold <= v && v <= 1.0
|
2015-01-19 14:21:24 +01:00
|
|
|
}
|
2021-09-03 17:22:37 +02:00
|
|
|
return -1.0 <= v && v <= -axisThreshold
|
2015-01-19 14:21:24 +01:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-04-17 14:55:58 +02:00
|
|
|
// IsButtonJustPressed reports whether the given virtual button b started to be pressed now.
|
|
|
|
func (c *gamepadConfig) IsButtonJustPressed(b virtualGamepadButton) bool {
|
|
|
|
if !c.gamepadIDInitialized {
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
|
2021-07-19 20:48:01 +02:00
|
|
|
if ebiten.IsStandardGamepadLayoutAvailable(c.gamepadID) {
|
|
|
|
return inpututil.IsStandardGamepadButtonJustPressed(c.gamepadID, b.StandardGamepadButton())
|
|
|
|
}
|
|
|
|
|
2021-04-17 14:55:58 +02:00
|
|
|
c.initializeIfNeeded()
|
|
|
|
|
|
|
|
bb, ok := c.buttons[b]
|
|
|
|
if ok {
|
|
|
|
return inpututil.IsGamepadButtonJustPressed(c.gamepadID, bb)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-12-17 14:42:34 +01:00
|
|
|
// Name returns the physical button's name for the given virtual button.
|
2018-01-22 07:12:17 +01:00
|
|
|
func (c *gamepadConfig) ButtonName(b virtualGamepadButton) string {
|
2018-10-13 09:25:33 +02:00
|
|
|
if !c.gamepadIDInitialized {
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
|
2015-01-19 14:21:24 +01:00
|
|
|
c.initializeIfNeeded()
|
|
|
|
|
|
|
|
bb, ok := c.buttons[b]
|
|
|
|
if ok {
|
|
|
|
return fmt.Sprintf("Button %d", bb)
|
|
|
|
}
|
|
|
|
|
|
|
|
a, ok := c.axes[b]
|
|
|
|
if ok {
|
|
|
|
if a.positive {
|
|
|
|
return fmt.Sprintf("Axis %d+", a.id)
|
|
|
|
}
|
2021-09-03 17:22:37 +02:00
|
|
|
return fmt.Sprintf("Axis %d-", a.id)
|
2015-01-19 14:21:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|