From ae0748819072e3adeb9a6a72ed01e25749eeaf38 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 4 Feb 2018 23:35:09 +0900 Subject: [PATCH] examples/blocks: Remove keyStates and use inpututil instead (#415) --- examples/blocks/blocks/gamepadscene.go | 3 ++- examples/blocks/blocks/gamescene.go | 3 ++- examples/blocks/blocks/input.go | 34 +++++--------------------- examples/blocks/blocks/titlescene.go | 3 ++- 4 files changed, 12 insertions(+), 31 deletions(-) diff --git a/examples/blocks/blocks/gamepadscene.go b/examples/blocks/blocks/gamepadscene.go index 400f7b039..64407e8c5 100644 --- a/examples/blocks/blocks/gamepadscene.go +++ b/examples/blocks/blocks/gamepadscene.go @@ -22,6 +22,7 @@ import ( "strings" "github.com/hajimehoshi/ebiten" + "github.com/hajimehoshi/ebiten/inpututil" ) type GamepadScene struct { @@ -34,7 +35,7 @@ func (s *GamepadScene) Update(state *GameState) error { if s.currentIndex == 0 { state.Input.gamepadConfig.Reset() } - if state.Input.StateForKey(ebiten.KeyEscape) == 1 { + if inpututil.IsKeyJustPressed(ebiten.KeyEscape) { state.Input.gamepadConfig.Reset() state.SceneManager.GoTo(&TitleScene{}) } diff --git a/examples/blocks/blocks/gamescene.go b/examples/blocks/blocks/gamescene.go index a22661c0b..7120d92a8 100644 --- a/examples/blocks/blocks/gamescene.go +++ b/examples/blocks/blocks/gamescene.go @@ -25,6 +25,7 @@ import ( "github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten/ebitenutil" + "github.com/hajimehoshi/ebiten/inpututil" ) var ( @@ -219,7 +220,7 @@ func (s *GameScene) Update(state *GameState) error { if s.gameover { // TODO: Gamepad key? - if state.Input.StateForKey(ebiten.KeySpace) == 1 { + if inpututil.IsKeyJustPressed(ebiten.KeySpace) { state.SceneManager.GoTo(&TitleScene{}) } return nil diff --git a/examples/blocks/blocks/input.go b/examples/blocks/blocks/input.go index 6e487df49..f20d13796 100644 --- a/examples/blocks/blocks/input.go +++ b/examples/blocks/blocks/input.go @@ -18,24 +18,16 @@ package blocks import ( "github.com/hajimehoshi/ebiten" + "github.com/hajimehoshi/ebiten/inpututil" ) // Input manages the input state including gamepads and keyboards. type Input struct { - keyStates map[ebiten.Key]int anyGamepadButtonPressed bool virtualGamepadButtonStates map[virtualGamepadButton]int gamepadConfig gamepadConfig } -// StateForKey returns time length indicating how long the key is pressed. -func (i *Input) StateForKey(key ebiten.Key) int { - if i.keyStates == nil { - return 0 - } - return i.keyStates[key] -} - // IsAnyGamepadButtonPressed returns a boolean value indicating // whether any gamepad button is pressed. func (i *Input) IsAnyGamepadButtonPressed() bool { @@ -50,17 +42,6 @@ func (i *Input) stateForVirtualGamepadButton(b virtualGamepadButton) int { } func (i *Input) Update() { - if i.keyStates == nil { - i.keyStates = map[ebiten.Key]int{} - } - for key := ebiten.Key(0); key <= ebiten.KeyMax; key++ { - if !ebiten.IsKeyPressed(ebiten.Key(key)) { - i.keyStates[key] = 0 - continue - } - i.keyStates[key]++ - } - const gamepadID = 0 i.anyGamepadButtonPressed = false for b := ebiten.GamepadButton(0); b <= ebiten.GamepadButtonMax; b++ { @@ -83,38 +64,35 @@ func (i *Input) Update() { } func (i *Input) IsRotateRightJustPressed() bool { - if i.StateForKey(ebiten.KeySpace) == 1 || i.StateForKey(ebiten.KeyX) == 1 { + if inpututil.IsKeyJustPressed(ebiten.KeySpace) || inpututil.IsKeyJustPressed(ebiten.KeyX) { return true } return i.stateForVirtualGamepadButton(virtualGamepadButtonButtonB) == 1 } func (i *Input) IsRotateLeftJustPressed() bool { - if i.StateForKey(ebiten.KeyZ) == 1 { + if inpututil.IsKeyJustPressed(ebiten.KeyZ) { return true } return i.stateForVirtualGamepadButton(virtualGamepadButtonButtonA) == 1 } func (i *Input) StateForLeft() int { - v := i.StateForKey(ebiten.KeyLeft) - if 0 < v { + if v := inpututil.KeyPressDuration(ebiten.KeyLeft); 0 < v { return v } return i.stateForVirtualGamepadButton(virtualGamepadButtonLeft) } func (i *Input) StateForRight() int { - v := i.StateForKey(ebiten.KeyRight) - if 0 < v { + if v := inpututil.KeyPressDuration(ebiten.KeyRight); 0 < v { return v } return i.stateForVirtualGamepadButton(virtualGamepadButtonRight) } func (i *Input) StateForDown() int { - v := i.StateForKey(ebiten.KeyDown) - if 0 < v { + if v := inpututil.KeyPressDuration(ebiten.KeyDown); 0 < v { return v } return i.stateForVirtualGamepadButton(virtualGamepadButtonDown) diff --git a/examples/blocks/blocks/titlescene.go b/examples/blocks/blocks/titlescene.go index b0ebb5ad7..7d3510076 100644 --- a/examples/blocks/blocks/titlescene.go +++ b/examples/blocks/blocks/titlescene.go @@ -21,6 +21,7 @@ import ( "github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten/ebitenutil" + "github.com/hajimehoshi/ebiten/inpututil" ) var imageBackground *ebiten.Image @@ -48,7 +49,7 @@ func anyGamepadAbstractButtonPressed(i *Input) bool { func (s *TitleScene) Update(state *GameState) error { s.count++ - if state.Input.StateForKey(ebiten.KeySpace) == 1 { + if inpututil.IsKeyJustPressed(ebiten.KeySpace) { state.SceneManager.GoTo(NewGameScene()) return nil }