examples/blocks: Skip the gamepad configuration if a standard layout is available

This commit is contained in:
Hajime Hoshi 2021-07-20 03:48:01 +09:00
parent d32b58d050
commit 984275d0a0
4 changed files with 52 additions and 9 deletions

View File

@ -39,6 +39,23 @@ var virtualGamepadButtons = []virtualGamepadButton{
virtualGamepadButtonButtonB,
}
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")
}
}
const axisThreshold = 0.75
type axis struct {
@ -64,15 +81,28 @@ func (c *gamepadConfig) SetGamepadID(id ebiten.GamepadID) {
c.gamepadIDInitialized = true
}
func (c *gamepadConfig) IsInitialized() bool {
func (c *gamepadConfig) ResetGamepadID() {
c.gamepadID = 0
c.gamepadIDInitialized = false
}
func (c *gamepadConfig) IsGamepadIDInitialized() bool {
return c.gamepadIDInitialized
}
func (c *gamepadConfig) NeedsConfiguration() bool {
return !ebiten.IsStandardGamepadLayoutAvailable(c.gamepadID)
}
func (c *gamepadConfig) initializeIfNeeded() {
if !c.gamepadIDInitialized {
panic("not reached")
}
if ebiten.IsStandardGamepadLayoutAvailable(c.gamepadID) {
return
}
if c.buttons == nil {
c.buttons = map[virtualGamepadButton]ebiten.GamepadButton{}
}
@ -101,9 +131,6 @@ func (c *gamepadConfig) initializeIfNeeded() {
}
func (c *gamepadConfig) Reset() {
c.gamepadID = 0
c.gamepadIDInitialized = false
c.buttons = nil
c.axes = nil
c.assignedButtons = nil
@ -168,6 +195,10 @@ func (c *gamepadConfig) IsButtonPressed(b virtualGamepadButton) bool {
panic("not reached")
}
if ebiten.IsStandardGamepadLayoutAvailable(c.gamepadID) {
return ebiten.IsStandardGamepadButtonPressed(c.gamepadID, b.StandardGamepadButton())
}
c.initializeIfNeeded()
bb, ok := c.buttons[b]
@ -193,6 +224,10 @@ func (c *gamepadConfig) IsButtonJustPressed(b virtualGamepadButton) bool {
panic("not reached")
}
if ebiten.IsStandardGamepadLayoutAvailable(c.gamepadID) {
return inpututil.IsStandardGamepadButtonJustPressed(c.gamepadID, b.StandardGamepadButton())
}
c.initializeIfNeeded()
bb, ok := c.buttons[b]

View File

@ -33,10 +33,10 @@ type GamepadScene struct {
func (s *GamepadScene) Update(state *GameState) error {
if s.currentIndex == 0 {
state.Input.gamepadConfig.Reset()
state.Input.gamepadConfig.SetGamepadID(s.gamepadID)
}
if inpututil.IsKeyJustPressed(ebiten.KeyEscape) {
state.Input.gamepadConfig.Reset()
state.Input.gamepadConfig.ResetGamepadID()
state.SceneManager.GoTo(&TitleScene{})
return nil
}

View File

@ -48,7 +48,7 @@ func (i *Input) stateForVirtualGamepadButton(b virtualGamepadButton) int {
}
func (i *Input) Update() {
if !i.gamepadConfig.IsInitialized() {
if !i.gamepadConfig.IsGamepadIDInitialized() {
return
}

View File

@ -40,7 +40,7 @@ type TitleScene struct {
}
func anyGamepadVirtualButtonJustPressed(i *Input) bool {
if !i.gamepadConfig.IsInitialized() {
if !i.gamepadConfig.IsGamepadIDInitialized() {
return false
}
@ -64,13 +64,21 @@ func (s *TitleScene) Update(state *GameState) error {
return nil
}
if state.Input.gamepadConfig.IsGamepadIDInitialized() {
return nil
}
// If 'virtual' gamepad buttons are not set and any gamepad buttons are pressed,
// go to the gamepad configuration scene.
if id := state.Input.GamepadIDButtonPressed(); id >= 0 {
id := state.Input.GamepadIDButtonPressed()
if id < 0 {
return nil
}
state.Input.gamepadConfig.SetGamepadID(id)
if state.Input.gamepadConfig.NeedsConfiguration() {
g := &GamepadScene{}
g.gamepadID = id
state.SceneManager.GoTo(g)
return nil
}
return nil
}