input: Add KeyMax

This commit is contained in:
Hajime Hoshi 2016-09-03 15:28:37 +09:00
parent d62a90dfca
commit 730b0d5ff9
3 changed files with 11 additions and 3 deletions

View File

@ -27,13 +27,16 @@ var gamepadAbstractButtons = []abstractButton{
} }
type Input struct { type Input struct {
keyStates [256]int keyStates map[ebiten.Key]int
gamepadButtonStates map[ebiten.GamepadButton]int gamepadButtonStates map[ebiten.GamepadButton]int
gamepadAbstractButtonStates map[abstractButton]int gamepadAbstractButtonStates map[abstractButton]int
gamepadConfig gamepadConfig gamepadConfig gamepadConfig
} }
func (i *Input) StateForKey(key ebiten.Key) int { func (i *Input) StateForKey(key ebiten.Key) int {
if i.keyStates == nil {
return 0
}
return i.keyStates[key] return i.keyStates[key]
} }
@ -52,7 +55,10 @@ func (i *Input) stateForGamepadAbstractButton(b abstractButton) int {
} }
func (i *Input) Update() { func (i *Input) Update() {
for key := range i.keyStates { 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)) { if !ebiten.IsKeyPressed(ebiten.Key(key)) {
i.keyStates[key] = 0 i.keyStates[key] = 0
continue continue

View File

@ -88,7 +88,7 @@ type Key int
// Keys // Keys
const ( const (
{{range $index, $name := .KeyNames}}Key{{$name}} = Key(ui.Key{{$name}}) {{range $index, $name := .KeyNames}}Key{{$name}} = Key(ui.Key{{$name}})
{{end}} {{end}} KeyMax = Key{{.LastKeyName}}
) )
` `
@ -279,6 +279,7 @@ func main() {
"KeyCodeToName": keyCodeToName, "KeyCodeToName": keyCodeToName,
"Codes": codes, "Codes": codes,
"KeyNames": names, "KeyNames": names,
"LastKeyName": names[len(names)-1],
"KeyNamesWithoutMods": namesWithoutMods, "KeyNamesWithoutMods": namesWithoutMods,
}); err != nil { }); err != nil {
log.Fatal(err) log.Fatal(err)

View File

@ -94,4 +94,5 @@ const (
KeySpace = Key(ui.KeySpace) KeySpace = Key(ui.KeySpace)
KeyTab = Key(ui.KeyTab) KeyTab = Key(ui.KeyTab)
KeyUp = Key(ui.KeyUp) KeyUp = Key(ui.KeyUp)
KeyMax = KeyUp
) )