examples/blocks: Bug fix: record axes default values

Fixes #483
This commit is contained in:
Hajime Hoshi 2018-01-22 23:06:27 +09:00
parent fb4343af21
commit 13d3918ec6

View File

@ -40,7 +40,7 @@ var virtualGamepadButtons = []virtualGamepadButton{
virtualGamepadButtonButtonB, virtualGamepadButtonButtonB,
} }
const threshold = 0.75 const axisThreshold = 0.75
type axis struct { type axis struct {
id int id int
@ -53,6 +53,8 @@ type gamepadConfig struct {
axes map[virtualGamepadButton]axis axes map[virtualGamepadButton]axis
assignedButtons map[ebiten.GamepadButton]struct{} assignedButtons map[ebiten.GamepadButton]struct{}
assignedAxes map[axis]struct{} assignedAxes map[axis]struct{}
defaultAxesValues map[int]float64
} }
func (c *gamepadConfig) initializeIfNeeded() { func (c *gamepadConfig) initializeIfNeeded() {
@ -68,6 +70,20 @@ func (c *gamepadConfig) initializeIfNeeded() {
if c.assignedAxes == nil { if c.assignedAxes == nil {
c.assignedAxes = map[axis]struct{}{} c.assignedAxes = map[axis]struct{}{}
} }
// 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.
// For example, on PS4 controllers, L2/R2's axes valuse can be -1.0.
if c.defaultAxesValues == nil {
c.defaultAxesValues = map[int]float64{}
const gamepadID = 0
na := ebiten.GamepadAxisNum(gamepadID)
for a := 0; a < na; a++ {
c.defaultAxesValues[a] = ebiten.GamepadAxis(gamepadID, a)
}
}
} }
func (c *gamepadConfig) Reset() { func (c *gamepadConfig) Reset() {
@ -97,22 +113,19 @@ func (c *gamepadConfig) Scan(gamepadID int, b virtualGamepadButton) bool {
} }
} }
an := ebiten.GamepadAxisNum(gamepadID) na := ebiten.GamepadAxisNum(gamepadID)
for a := 0; a < an; a++ { for a := 0; a < na; a++ {
v := ebiten.GamepadAxis(gamepadID, a) v := ebiten.GamepadAxis(gamepadID, a)
// Check |v| < 1.0 because // Check |v| < 1.0 because there is a bug that a button returns
// 1) there is a bug that a button returns an axis value wrongly // an axis value wrongly and the value may be over 1 on some platforms.
// and the value may be over 1. if axisThreshold <= v && v <= 1.0 && v != c.defaultAxesValues[a] {
// 2) just 1.0 or -1.0 values are ignored since PS4's L2/R2 keys take
// -1.0 by default.
if threshold <= v && v < 1.0 {
if _, ok := c.assignedAxes[axis{a, true}]; !ok { if _, ok := c.assignedAxes[axis{a, true}]; !ok {
c.axes[b] = axis{a, true} c.axes[b] = axis{a, true}
c.assignedAxes[axis{a, true}] = struct{}{} c.assignedAxes[axis{a, true}] = struct{}{}
return true return true
} }
} }
if -1.0 < v && v <= -threshold { if -1.0 <= v && v <= -axisThreshold && v != c.defaultAxesValues[a] {
if _, ok := c.assignedAxes[axis{a, false}]; !ok { if _, ok := c.assignedAxes[axis{a, false}]; !ok {
c.axes[b] = axis{a, false} c.axes[b] = axis{a, false}
c.assignedAxes[axis{a, false}] = struct{}{} c.assignedAxes[axis{a, false}] = struct{}{}
@ -138,9 +151,9 @@ func (c *gamepadConfig) IsButtonPressed(b virtualGamepadButton) bool {
if ok { if ok {
v := ebiten.GamepadAxis(0, a.id) v := ebiten.GamepadAxis(0, a.id)
if a.positive { if a.positive {
return threshold <= v && v <= 1.0 return axisThreshold <= v && v <= 1.0
} else { } else {
return -1.0 <= v && v <= -threshold return -1.0 <= v && v <= -axisThreshold
} }
} }
return false return false