examples/piano: Use inpututil (#415)

This commit is contained in:
Hajime Hoshi 2018-02-04 23:48:19 +09:00
parent 2772f8d990
commit 67ebf848e7

View File

@ -29,6 +29,7 @@ import (
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/audio" "github.com/hajimehoshi/ebiten/audio"
"github.com/hajimehoshi/ebiten/ebitenutil" "github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/inpututil"
"github.com/hajimehoshi/ebiten/text" "github.com/hajimehoshi/ebiten/text"
) )
@ -209,20 +210,8 @@ var (
ebiten.KeyO, ebiten.KeyO,
ebiten.KeyL, ebiten.KeyL,
} }
keyStates = map[ebiten.Key]int{}
) )
// updateInput updates the input state.
func updateInput() {
for _, key := range keys {
if !ebiten.IsKeyPressed(key) {
keyStates[key] = 0
continue
}
keyStates[key]++
}
}
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
// The piano data is still being initialized. // The piano data is still being initialized.
// Get the progress if available. // Get the progress if available.
@ -235,9 +224,8 @@ func update(screen *ebiten.Image) error {
} }
if pianoNoteSamplesInited { if pianoNoteSamplesInited {
updateInput()
for i, key := range keys { for i, key := range keys {
if keyStates[key] != 1 { if !inpututil.IsKeyJustPressed(key) {
continue continue
} }
playNote(baseFreq * math.Exp2(float64(i-1)/12.0)) playNote(baseFreq * math.Exp2(float64(i-1)/12.0))