Skip to content

Commit

Permalink
examples/blocks: Skip the gamepad configuration if a standard layout …
Browse files Browse the repository at this point in the history
…is available
  • Loading branch information
hajimehoshi committed Jul 19, 2021
1 parent d32b58d commit 984275d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
43 changes: 39 additions & 4 deletions examples/blocks/blocks/gamepad.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion examples/blocks/blocks/gamepadscene.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion examples/blocks/blocks/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (i *Input) stateForVirtualGamepadButton(b virtualGamepadButton) int {
}

func (i *Input) Update() {
if !i.gamepadConfig.IsInitialized() {
if !i.gamepadConfig.IsGamepadIDInitialized() {
return
}

Expand Down
14 changes: 11 additions & 3 deletions examples/blocks/blocks/titlescene.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type TitleScene struct {
}

func anyGamepadVirtualButtonJustPressed(i *Input) bool {
if !i.gamepadConfig.IsInitialized() {
if !i.gamepadConfig.IsGamepadIDInitialized() {
return false
}

Expand All @@ -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
}
Expand Down

0 comments on commit 984275d

Please sign in to comment.