Add ui.Keys

This commit is contained in:
Hajime Hoshi 2014-05-12 02:16:23 +09:00
parent fb4751e8cd
commit 0b9f27287b
4 changed files with 54 additions and 50 deletions

View File

@ -70,7 +70,7 @@ func (game *Game) Update(state ui.CanvasState) {
if !game.isInitialized() {
return
}
game.input.Update(state.Keys)
game.input.Update(state.PressedKeys)
game.sceneManager.Update(&GameState{
SceneManager: game.sceneManager,
Input: game.input,

View File

@ -22,14 +22,9 @@ func (i *Input) StateForKey(key ui.Key) int {
return i.states[key]
}
func (i *Input) Update(keys []ui.Key) {
pressedKeys := map[ui.Key]struct{}{}
for _, key := range keys {
pressedKeys[key] = struct{}{}
}
func (i *Input) Update(keys ui.Keys) {
for key, _ := range i.states {
if _, ok := pressedKeys[key]; !ok {
if !keys.Includes(key) {
i.states[key] = 0
continue
}

View File

@ -26,11 +26,29 @@ import (
"unsafe"
)
type Keys map[ui.Key]struct{}
func newKeys() Keys {
return Keys(map[ui.Key]struct{}{})
}
func (k Keys) add(key ui.Key) {
k[key] = struct{}{}
}
func (k Keys) remove(key ui.Key) {
delete(k, key)
}
func (k Keys) Includes(key ui.Key) bool {
_, ok := k[key]
return ok
}
type GameWindow struct {
state ui.CanvasState
title string
native *C.EbitenGameWindow
pressedKeys map[ui.Key]struct{}
funcs chan func(*opengl.Context)
funcsDone chan struct{}
closed chan struct{}
@ -44,7 +62,7 @@ func newGameWindow(width, height, scale int, title string) *GameWindow {
Width: width,
Height: height,
Scale: scale,
Keys: []ui.Key{},
PressedKeys: newKeys(),
MouseX: -1,
MouseY: -1,
IsClosed: false,
@ -52,7 +70,6 @@ func newGameWindow(width, height, scale int, title string) *GameWindow {
return &GameWindow{
state: state,
title: title,
pressedKeys: map[ui.Key]struct{}{},
funcs: make(chan func(*opengl.Context)),
funcsDone: make(chan struct{}),
closed: make(chan struct{}),
@ -145,16 +162,10 @@ func ebiten_KeyDown(nativeWindow C.EbitenGameWindowPtr, keyCode int) {
return
}
w := windows[nativeWindow]
w.pressedKeys[key] = struct{}{}
keys := []ui.Key{}
for key, _ := range w.pressedKeys {
keys = append(keys, key)
}
w.Lock()
defer w.Unlock()
w.state.Keys = keys
w.state.PressedKeys.(Keys).add(key)
}
//export ebiten_KeyUp
@ -164,16 +175,10 @@ func ebiten_KeyUp(nativeWindow C.EbitenGameWindowPtr, keyCode int) {
return
}
w := windows[nativeWindow]
delete(w.pressedKeys, key)
keys := []ui.Key{}
for key, _ := range w.pressedKeys {
keys = append(keys, key)
}
w.Lock()
defer w.Unlock()
w.state.Keys = keys
w.state.PressedKeys.(Keys).remove(key)
}
//export ebiten_MouseStateUpdated

View File

@ -22,11 +22,15 @@ type UI interface {
Terminate()
}
type Keys interface {
Includes(key Key) bool
}
type CanvasState struct {
Width int
Height int
Scale int
Keys []Key
PressedKeys Keys
MouseX int
MouseY int
IsClosed bool