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() { if !game.isInitialized() {
return return
} }
game.input.Update(state.Keys) game.input.Update(state.PressedKeys)
game.sceneManager.Update(&GameState{ game.sceneManager.Update(&GameState{
SceneManager: game.sceneManager, SceneManager: game.sceneManager,
Input: game.input, Input: game.input,

View File

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

View File

@ -26,14 +26,32 @@ import (
"unsafe" "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 { type GameWindow struct {
state ui.CanvasState state ui.CanvasState
title string title string
native *C.EbitenGameWindow native *C.EbitenGameWindow
pressedKeys map[ui.Key]struct{} funcs chan func(*opengl.Context)
funcs chan func(*opengl.Context) funcsDone chan struct{}
funcsDone chan struct{} closed chan struct{}
closed chan struct{}
sync.RWMutex sync.RWMutex
} }
@ -41,21 +59,20 @@ var windows = map[*C.EbitenGameWindow]*GameWindow{}
func newGameWindow(width, height, scale int, title string) *GameWindow { func newGameWindow(width, height, scale int, title string) *GameWindow {
state := ui.CanvasState{ state := ui.CanvasState{
Width: width, Width: width,
Height: height, Height: height,
Scale: scale, Scale: scale,
Keys: []ui.Key{}, PressedKeys: newKeys(),
MouseX: -1, MouseX: -1,
MouseY: -1, MouseY: -1,
IsClosed: false, IsClosed: false,
} }
return &GameWindow{ return &GameWindow{
state: state, state: state,
title: title, title: title,
pressedKeys: map[ui.Key]struct{}{}, funcs: make(chan func(*opengl.Context)),
funcs: make(chan func(*opengl.Context)), funcsDone: make(chan struct{}),
funcsDone: make(chan struct{}), closed: make(chan struct{}),
closed: make(chan struct{}),
} }
} }
@ -145,16 +162,10 @@ func ebiten_KeyDown(nativeWindow C.EbitenGameWindowPtr, keyCode int) {
return return
} }
w := windows[nativeWindow] w := windows[nativeWindow]
w.pressedKeys[key] = struct{}{}
keys := []ui.Key{}
for key, _ := range w.pressedKeys {
keys = append(keys, key)
}
w.Lock() w.Lock()
defer w.Unlock() defer w.Unlock()
w.state.Keys = keys w.state.PressedKeys.(Keys).add(key)
} }
//export ebiten_KeyUp //export ebiten_KeyUp
@ -164,16 +175,10 @@ func ebiten_KeyUp(nativeWindow C.EbitenGameWindowPtr, keyCode int) {
return return
} }
w := windows[nativeWindow] w := windows[nativeWindow]
delete(w.pressedKeys, key)
keys := []ui.Key{}
for key, _ := range w.pressedKeys {
keys = append(keys, key)
}
w.Lock() w.Lock()
defer w.Unlock() defer w.Unlock()
w.state.Keys = keys w.state.PressedKeys.(Keys).remove(key)
} }
//export ebiten_MouseStateUpdated //export ebiten_MouseStateUpdated
@ -216,6 +221,6 @@ func ebiten_WindowClosed(nativeWindow C.EbitenGameWindowPtr) {
w.Lock() w.Lock()
defer w.Unlock() defer w.Unlock()
w.state.IsClosed = true w.state.IsClosed = true
delete(windows, nativeWindow) delete(windows, nativeWindow)
} }

View File

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