ebiten/example/blocks/input.go

39 lines
621 B
Go
Raw Normal View History

2013-12-18 19:21:25 +01:00
package blocks
import (
"github.com/hajimehoshi/go-ebiten/ui"
)
type Input struct {
2014-05-11 14:24:37 +02:00
states map[ui.Key]int
2013-12-18 19:21:25 +01:00
}
func NewInput() *Input {
states := map[ui.Key]int{}
for key := ui.Key(0); key < ui.KeyMax; key++ {
states[key] = 0
}
return &Input{
states: states,
}
}
func (i *Input) StateForKey(key ui.Key) int {
return i.states[key]
}
2014-05-11 14:24:37 +02:00
func (i *Input) Update(keys []ui.Key) {
pressedKeys := map[ui.Key]struct{}{}
for _, key := range keys {
pressedKeys[key] = struct{}{}
}
2013-12-18 19:21:25 +01:00
for key, _ := range i.states {
2014-05-11 14:24:37 +02:00
if _, ok := pressedKeys[key]; !ok {
2013-12-18 19:21:25 +01:00
i.states[key] = 0
continue
}
i.states[key] += 1
}
}