mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 10:48:53 +01:00
examples/blocks: Rename enums and functions
This commit is contained in:
parent
15a523c42b
commit
581fbc4406
@ -20,36 +20,14 @@ import (
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
)
|
||||
|
||||
// A StdButton represents a standard gamepad button.
|
||||
// See also: http://www.w3.org/TR/gamepad/
|
||||
// [UL0] [UR0]
|
||||
// [UL1] [UR1]
|
||||
//
|
||||
// [LU] [CC] [RU]
|
||||
// [LL][LR] [CL][CR] [RL][RR]
|
||||
// [LD] [RD]
|
||||
// [AL] [AR]
|
||||
type StdButton int
|
||||
type abstractButton int
|
||||
|
||||
const (
|
||||
StdButtonNone StdButton = iota
|
||||
StdButtonLL
|
||||
StdButtonLR
|
||||
StdButtonLU
|
||||
StdButtonLD
|
||||
StdButtonCL
|
||||
StdButtonCC
|
||||
StdButtonCR
|
||||
StdButtonRL
|
||||
StdButtonRR
|
||||
StdButtonRU
|
||||
StdButtonRD
|
||||
StdButtonUL0
|
||||
StdButtonUL1
|
||||
StdButtonUR0
|
||||
StdButtonUR1
|
||||
StdButtonAL
|
||||
StdButtonAR
|
||||
abstractButtonLeft abstractButton = iota
|
||||
abstractButtonRight
|
||||
abstractButtonDown
|
||||
abstractButtonButtonA
|
||||
abstractButtonButtonB
|
||||
)
|
||||
|
||||
const threshold = 0.75
|
||||
@ -59,20 +37,20 @@ type axis struct {
|
||||
positive bool
|
||||
}
|
||||
|
||||
type Configuration struct {
|
||||
current StdButton
|
||||
buttons map[StdButton]ebiten.GamepadButton
|
||||
axes map[StdButton]axis
|
||||
type gamepadConfig struct {
|
||||
current abstractButton
|
||||
buttons map[abstractButton]ebiten.GamepadButton
|
||||
axes map[abstractButton]axis
|
||||
assignedButtons map[ebiten.GamepadButton]struct{}
|
||||
assignedAxes map[axis]struct{}
|
||||
}
|
||||
|
||||
func (c *Configuration) initializeIfNeeded() {
|
||||
func (c *gamepadConfig) initializeIfNeeded() {
|
||||
if c.buttons == nil {
|
||||
c.buttons = map[StdButton]ebiten.GamepadButton{}
|
||||
c.buttons = map[abstractButton]ebiten.GamepadButton{}
|
||||
}
|
||||
if c.axes == nil {
|
||||
c.axes = map[StdButton]axis{}
|
||||
c.axes = map[abstractButton]axis{}
|
||||
}
|
||||
if c.assignedButtons == nil {
|
||||
c.assignedButtons = map[ebiten.GamepadButton]struct{}{}
|
||||
@ -82,14 +60,14 @@ func (c *Configuration) initializeIfNeeded() {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Configuration) Reset() {
|
||||
func (c *gamepadConfig) Reset() {
|
||||
c.buttons = nil
|
||||
c.axes = nil
|
||||
c.assignedButtons = nil
|
||||
c.assignedAxes = nil
|
||||
}
|
||||
|
||||
func (c *Configuration) Scan(index int, b StdButton) bool {
|
||||
func (c *gamepadConfig) Scan(index int, b abstractButton) bool {
|
||||
c.initializeIfNeeded()
|
||||
|
||||
delete(c.buttons, b)
|
||||
@ -130,7 +108,7 @@ func (c *Configuration) Scan(index int, b StdButton) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *Configuration) IsButtonPressed(id int, b StdButton) bool {
|
||||
func (c *gamepadConfig) IsButtonPressed(id int, b abstractButton) bool {
|
||||
c.initializeIfNeeded()
|
||||
|
||||
bb, ok := c.buttons[b]
|
||||
@ -149,7 +127,7 @@ func (c *Configuration) IsButtonPressed(id int, b StdButton) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *Configuration) Name(b StdButton) string {
|
||||
func (c *gamepadConfig) Name(b abstractButton) string {
|
||||
c.initializeIfNeeded()
|
||||
|
||||
bb, ok := c.buttons[b]
|
||||
|
@ -43,9 +43,9 @@ func (s *GamepadScene) Update(state *GameState) error {
|
||||
}
|
||||
|
||||
if s.buttonStates == nil {
|
||||
s.buttonStates = make([]string, len(gamepadStdButtons))
|
||||
s.buttonStates = make([]string, len(gamepadAbstractButtons))
|
||||
}
|
||||
for i, b := range gamepadStdButtons {
|
||||
for i, b := range gamepadAbstractButtons {
|
||||
if i < s.currentIndex {
|
||||
s.buttonStates[i] = strings.ToUpper(state.Input.gamepadConfig.Name(b))
|
||||
continue
|
||||
@ -65,10 +65,10 @@ func (s *GamepadScene) Update(state *GameState) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
b := gamepadStdButtons[s.currentIndex]
|
||||
b := gamepadAbstractButtons[s.currentIndex]
|
||||
if state.Input.gamepadConfig.Scan(0, b) {
|
||||
s.currentIndex++
|
||||
if s.currentIndex == len(gamepadStdButtons) {
|
||||
if s.currentIndex == len(gamepadAbstractButtons) {
|
||||
s.countAfterSetting = ebiten.FPS
|
||||
}
|
||||
}
|
||||
@ -102,7 +102,7 @@ ROTATE RIGHT: %s
|
||||
|
||||
%s`
|
||||
msg := ""
|
||||
if s.currentIndex == len(gamepadStdButtons) {
|
||||
if s.currentIndex == len(gamepadAbstractButtons) {
|
||||
msg = "OK!"
|
||||
}
|
||||
str := fmt.Sprintf(f, s.buttonStates[0], s.buttonStates[1], s.buttonStates[2], s.buttonStates[3], s.buttonStates[4], msg)
|
||||
|
@ -18,19 +18,19 @@ import (
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
)
|
||||
|
||||
var gamepadStdButtons = []StdButton{
|
||||
StdButtonLL,
|
||||
StdButtonLR,
|
||||
StdButtonLD,
|
||||
StdButtonRD,
|
||||
StdButtonRR,
|
||||
var gamepadAbstractButtons = []abstractButton{
|
||||
abstractButtonLeft,
|
||||
abstractButtonRight,
|
||||
abstractButtonDown,
|
||||
abstractButtonButtonA,
|
||||
abstractButtonButtonB,
|
||||
}
|
||||
|
||||
type Input struct {
|
||||
keyStates [256]int
|
||||
gamepadButtonStates [256]int
|
||||
gamepadStdButtonStates [16]int
|
||||
gamepadConfig Configuration
|
||||
gamepadAbstractButtonStates [16]int
|
||||
gamepadConfig gamepadConfig
|
||||
}
|
||||
|
||||
func (i *Input) StateForKey(key ebiten.Key) int {
|
||||
@ -41,8 +41,8 @@ func (i *Input) StateForGamepadButton(b ebiten.GamepadButton) int {
|
||||
return i.gamepadButtonStates[b]
|
||||
}
|
||||
|
||||
func (i *Input) stateForGamepadStdButton(b StdButton) int {
|
||||
return i.gamepadStdButtonStates[b]
|
||||
func (i *Input) stateForGamepadAbstractButton(b abstractButton) int {
|
||||
return i.gamepadAbstractButtonStates[b]
|
||||
}
|
||||
|
||||
func (i *Input) Update() {
|
||||
@ -63,12 +63,12 @@ func (i *Input) Update() {
|
||||
i.gamepadButtonStates[b]++
|
||||
}
|
||||
|
||||
for _, b := range gamepadStdButtons {
|
||||
for _, b := range gamepadAbstractButtons {
|
||||
if !i.gamepadConfig.IsButtonPressed(gamepadID, b) {
|
||||
i.gamepadStdButtonStates[b] = 0
|
||||
i.gamepadAbstractButtonStates[b] = 0
|
||||
continue
|
||||
}
|
||||
i.gamepadStdButtonStates[b]++
|
||||
i.gamepadAbstractButtonStates[b]++
|
||||
}
|
||||
}
|
||||
|
||||
@ -76,14 +76,14 @@ func (i *Input) IsRotateRightTrigger() bool {
|
||||
if i.StateForKey(ebiten.KeySpace) == 1 || i.StateForKey(ebiten.KeyX) == 1 {
|
||||
return true
|
||||
}
|
||||
return i.stateForGamepadStdButton(StdButtonRR) == 1
|
||||
return i.stateForGamepadAbstractButton(abstractButtonButtonB) == 1
|
||||
}
|
||||
|
||||
func (i *Input) IsRotateLeftTrigger() bool {
|
||||
if i.StateForKey(ebiten.KeyZ) == 1 {
|
||||
return true
|
||||
}
|
||||
return i.stateForGamepadStdButton(StdButtonRD) == 1
|
||||
return i.stateForGamepadAbstractButton(abstractButtonButtonA) == 1
|
||||
}
|
||||
|
||||
func (i *Input) StateForLeft() int {
|
||||
@ -91,7 +91,7 @@ func (i *Input) StateForLeft() int {
|
||||
if 0 < v {
|
||||
return v
|
||||
}
|
||||
return i.stateForGamepadStdButton(StdButtonLL)
|
||||
return i.stateForGamepadAbstractButton(abstractButtonLeft)
|
||||
}
|
||||
|
||||
func (i *Input) StateForRight() int {
|
||||
@ -99,7 +99,7 @@ func (i *Input) StateForRight() int {
|
||||
if 0 < v {
|
||||
return v
|
||||
}
|
||||
return i.stateForGamepadStdButton(StdButtonLR)
|
||||
return i.stateForGamepadAbstractButton(abstractButtonRight)
|
||||
}
|
||||
|
||||
func (i *Input) StateForDown() int {
|
||||
@ -107,5 +107,5 @@ func (i *Input) StateForDown() int {
|
||||
if 0 < v {
|
||||
return v
|
||||
}
|
||||
return i.stateForGamepadStdButton(StdButtonLD)
|
||||
return i.stateForGamepadAbstractButton(abstractButtonDown)
|
||||
}
|
||||
|
@ -68,8 +68,8 @@ func NewTitleScene() *TitleScene {
|
||||
}
|
||||
}
|
||||
|
||||
func anyGamepadStdButtonPressed(i *Input) bool {
|
||||
for _, b := range gamepadStdButtons {
|
||||
func anyGamepadAbstractButtonPressed(i *Input) bool {
|
||||
for _, b := range gamepadAbstractButtons {
|
||||
if i.gamepadConfig.IsButtonPressed(0, b) {
|
||||
return true
|
||||
}
|
||||
@ -93,7 +93,7 @@ func (s *TitleScene) Update(state *GameState) error {
|
||||
state.SceneManager.GoTo(NewGameScene())
|
||||
return nil
|
||||
}
|
||||
if anyGamepadStdButtonPressed(state.Input) {
|
||||
if anyGamepadAbstractButtonPressed(state.Input) {
|
||||
state.SceneManager.GoTo(NewGameScene())
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user