diff --git a/example/blocks/game.go b/example/blocks/game.go index 9798ba325..738461dfc 100644 --- a/example/blocks/game.go +++ b/example/blocks/game.go @@ -2,7 +2,6 @@ package blocks import ( "github.com/hajimehoshi/ebiten/graphics" - "github.com/hajimehoshi/ebiten/ui" _ "image/png" ) @@ -61,11 +60,11 @@ func (game *Game) isInitialized() bool { return true } -func (game *Game) Update(state ui.InputState) { +func (game *Game) Update() { if !game.isInitialized() { return } - game.input.Update(state) + game.input.Update() game.sceneManager.Update(&GameState{ SceneManager: game.sceneManager, Input: game.input, diff --git a/example/blocks/input.go b/example/blocks/input.go index 17536379b..626e129ec 100644 --- a/example/blocks/input.go +++ b/example/blocks/input.go @@ -22,9 +22,9 @@ func (i *Input) StateForKey(key ui.Key) int { return i.states[key] } -func (i *Input) Update(inputState ui.InputState) { +func (i *Input) Update() { for key := range i.states { - if !inputState.IsPressedKey(key) { + if !ui.IsKeyPressed(key) { i.states[key] = 0 continue } diff --git a/ui/glfw/canvas.go b/ui/glfw/canvas.go index 9790d88e5..afdb85cf7 100644 --- a/ui/glfw/canvas.go +++ b/ui/glfw/canvas.go @@ -10,11 +10,11 @@ import ( ) type Canvas struct { - window *glfw.Window - inputState *InputState - context *opengl.Context - funcs chan func() - funcsDone chan struct{} + window *glfw.Window + context *opengl.Context + keyboard *Keyboard + funcs chan func() + funcsDone chan struct{} } func NewCanvas(width, height, scale int, title string) *Canvas { @@ -23,12 +23,15 @@ func NewCanvas(width, height, scale int, title string) *Canvas { panic(err) } canvas := &Canvas{ - window: window, - inputState: newInputState(), - funcs: make(chan func()), - funcsDone: make(chan struct{}), + window: window, + keyboard: NewKeyboard(), + funcs: make(chan func()), + funcsDone: make(chan struct{}), } + ui.SetKeyboard(canvas.keyboard) + graphics.SetTextureFactory(canvas) + // For retina displays, recalculate the scale with the framebuffer size. windowWidth, _ := window.GetFramebufferSize() realScale := windowWidth / width @@ -51,10 +54,6 @@ func (c *Canvas) IsClosed() bool { return c.window.ShouldClose() } -func (c *Canvas) InputState() ui.InputState { - return c.inputState -} - func (c *Canvas) CreateTexture(img image.Image, filter graphics.Filter) (graphics.TextureId, error) { var id graphics.TextureId var err error @@ -92,5 +91,5 @@ func (c *Canvas) use(f func()) { } func (c *Canvas) update() { - c.inputState.update(c.window) + c.keyboard.update(c.window) } diff --git a/ui/glfw/inputstate.go b/ui/glfw/inputstate.go deleted file mode 100644 index 9b447cdb7..000000000 --- a/ui/glfw/inputstate.go +++ /dev/null @@ -1,70 +0,0 @@ -package glfw - -import ( - glfw "github.com/go-gl/glfw3" - "github.com/hajimehoshi/ebiten/ui" -) - -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 InputState struct { - pressedKeys Keys - mouseX int - mouseY int -} - -func newInputState() *InputState { - return &InputState{ - pressedKeys: newKeys(), - mouseX: -1, - mouseY: -1, - } -} - -func (i *InputState) IsPressedKey(key ui.Key) bool { - return i.pressedKeys.Includes(key) -} - -func (i *InputState) MouseX() int { - // TODO: Update - return i.mouseX -} - -func (i *InputState) MouseY() int { - return i.mouseY -} - -var glfwKeyCodeToKey = map[glfw.Key]ui.Key{ - glfw.KeySpace: ui.KeySpace, - glfw.KeyLeft: ui.KeyLeft, - glfw.KeyRight: ui.KeyRight, - glfw.KeyUp: ui.KeyUp, - glfw.KeyDown: ui.KeyDown, -} - -func (i *InputState) update(window *glfw.Window) { - for g, u := range glfwKeyCodeToKey { - if window.GetKey(g) == glfw.Press { - i.pressedKeys.add(u) - } else { - i.pressedKeys.remove(u) - } - } -} diff --git a/ui/glfw/keyboard.go b/ui/glfw/keyboard.go new file mode 100644 index 000000000..6f6f915a4 --- /dev/null +++ b/ui/glfw/keyboard.go @@ -0,0 +1,39 @@ +package glfw + +import ( + glfw "github.com/go-gl/glfw3" + "github.com/hajimehoshi/ebiten/ui" +) + +type Keyboard struct { + pressedKeys map[ui.Key]struct{} +} + +func NewKeyboard() *Keyboard { + return &Keyboard{ + pressedKeys: map[ui.Key]struct{}{}, + } +} + +func (k *Keyboard) IsKeyPressed(key ui.Key) bool { + _, ok := k.pressedKeys[key] + return ok +} + +var glfwKeyCodeToKey = map[glfw.Key]ui.Key{ + glfw.KeySpace: ui.KeySpace, + glfw.KeyLeft: ui.KeyLeft, + glfw.KeyRight: ui.KeyRight, + glfw.KeyUp: ui.KeyUp, + glfw.KeyDown: ui.KeyDown, +} + +func (k *Keyboard) update(window *glfw.Window) { + for g, u := range glfwKeyCodeToKey { + if window.GetKey(g) == glfw.Press { + k.pressedKeys[u] = struct{}{} + } else { + delete(k.pressedKeys, u) + } + } +} diff --git a/ui/input.go b/ui/input.go new file mode 100644 index 000000000..62e98e05e --- /dev/null +++ b/ui/input.go @@ -0,0 +1,29 @@ +package ui + +type Key int + +const ( + KeyUp Key = iota + KeyDown + KeyLeft + KeyRight + KeySpace + KeyMax +) + +var currentKeyboard Keyboard + +type Keyboard interface { + IsKeyPressed(key Key) bool +} + +func SetKeyboard(keyboard Keyboard) { + currentKeyboard = keyboard +} + +func IsKeyPressed(key Key) bool { + if currentKeyboard == nil { + panic("ui.IsKeyPressed: currentKeyboard is not set") + } + return currentKeyboard.IsKeyPressed(key) +} diff --git a/ui/run.go b/ui/run.go index 3028e97d5..ac54930e9 100644 --- a/ui/run.go +++ b/ui/run.go @@ -10,7 +10,7 @@ import ( type Game interface { Draw(context graphics.Context) - Update(inputState InputState) + Update() SetTextureFactory(textureFactory graphics.TextureFactory) } @@ -30,7 +30,7 @@ func Run(u UI, game Game, width, height, scale int, title string, fps int) { default: canvas.Draw(game.Draw) case <-tick: - game.Update(canvas.InputState()) + game.Update() if canvas.IsClosed() { return } diff --git a/ui/ui.go b/ui/ui.go index 4fdea5892..f5397ebbb 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -4,32 +4,14 @@ import ( "github.com/hajimehoshi/ebiten/graphics" ) -type Key int - -const ( - KeyUp Key = iota - KeyDown - KeyLeft - KeyRight - KeySpace - KeyMax -) - type UI interface { Start(widht, height, scale int, title string) Canvas DoEvents() Terminate() } -type InputState interface { - IsPressedKey(key Key) bool - MouseX() int - MouseY() int -} - type Canvas interface { graphics.TextureFactory Draw(func(graphics.Context)) IsClosed() bool - InputState() InputState }