mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 11:48:55 +01:00
Remove ui.InputState; Add ui.Keyboard
This commit is contained in:
parent
71d9bb8958
commit
18141ecb6f
@ -2,7 +2,6 @@ package blocks
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hajimehoshi/ebiten/graphics"
|
"github.com/hajimehoshi/ebiten/graphics"
|
||||||
"github.com/hajimehoshi/ebiten/ui"
|
|
||||||
_ "image/png"
|
_ "image/png"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -61,11 +60,11 @@ func (game *Game) isInitialized() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (game *Game) Update(state ui.InputState) {
|
func (game *Game) Update() {
|
||||||
if !game.isInitialized() {
|
if !game.isInitialized() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
game.input.Update(state)
|
game.input.Update()
|
||||||
game.sceneManager.Update(&GameState{
|
game.sceneManager.Update(&GameState{
|
||||||
SceneManager: game.sceneManager,
|
SceneManager: game.sceneManager,
|
||||||
Input: game.input,
|
Input: game.input,
|
||||||
|
@ -22,9 +22,9 @@ func (i *Input) StateForKey(key ui.Key) int {
|
|||||||
return i.states[key]
|
return i.states[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) Update(inputState ui.InputState) {
|
func (i *Input) Update() {
|
||||||
for key := range i.states {
|
for key := range i.states {
|
||||||
if !inputState.IsPressedKey(key) {
|
if !ui.IsKeyPressed(key) {
|
||||||
i.states[key] = 0
|
i.states[key] = 0
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -10,11 +10,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Canvas struct {
|
type Canvas struct {
|
||||||
window *glfw.Window
|
window *glfw.Window
|
||||||
inputState *InputState
|
context *opengl.Context
|
||||||
context *opengl.Context
|
keyboard *Keyboard
|
||||||
funcs chan func()
|
funcs chan func()
|
||||||
funcsDone chan struct{}
|
funcsDone chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCanvas(width, height, scale int, title string) *Canvas {
|
func NewCanvas(width, height, scale int, title string) *Canvas {
|
||||||
@ -23,12 +23,15 @@ func NewCanvas(width, height, scale int, title string) *Canvas {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
canvas := &Canvas{
|
canvas := &Canvas{
|
||||||
window: window,
|
window: window,
|
||||||
inputState: newInputState(),
|
keyboard: NewKeyboard(),
|
||||||
funcs: make(chan func()),
|
funcs: make(chan func()),
|
||||||
funcsDone: make(chan struct{}),
|
funcsDone: make(chan struct{}),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ui.SetKeyboard(canvas.keyboard)
|
||||||
|
graphics.SetTextureFactory(canvas)
|
||||||
|
|
||||||
// For retina displays, recalculate the scale with the framebuffer size.
|
// For retina displays, recalculate the scale with the framebuffer size.
|
||||||
windowWidth, _ := window.GetFramebufferSize()
|
windowWidth, _ := window.GetFramebufferSize()
|
||||||
realScale := windowWidth / width
|
realScale := windowWidth / width
|
||||||
@ -51,10 +54,6 @@ func (c *Canvas) IsClosed() bool {
|
|||||||
return c.window.ShouldClose()
|
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) {
|
func (c *Canvas) CreateTexture(img image.Image, filter graphics.Filter) (graphics.TextureId, error) {
|
||||||
var id graphics.TextureId
|
var id graphics.TextureId
|
||||||
var err error
|
var err error
|
||||||
@ -92,5 +91,5 @@ func (c *Canvas) use(f func()) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Canvas) update() {
|
func (c *Canvas) update() {
|
||||||
c.inputState.update(c.window)
|
c.keyboard.update(c.window)
|
||||||
}
|
}
|
||||||
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
39
ui/glfw/keyboard.go
Normal file
39
ui/glfw/keyboard.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
29
ui/input.go
Normal file
29
ui/input.go
Normal file
@ -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)
|
||||||
|
}
|
@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
type Game interface {
|
type Game interface {
|
||||||
Draw(context graphics.Context)
|
Draw(context graphics.Context)
|
||||||
Update(inputState InputState)
|
Update()
|
||||||
SetTextureFactory(textureFactory graphics.TextureFactory)
|
SetTextureFactory(textureFactory graphics.TextureFactory)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ func Run(u UI, game Game, width, height, scale int, title string, fps int) {
|
|||||||
default:
|
default:
|
||||||
canvas.Draw(game.Draw)
|
canvas.Draw(game.Draw)
|
||||||
case <-tick:
|
case <-tick:
|
||||||
game.Update(canvas.InputState())
|
game.Update()
|
||||||
if canvas.IsClosed() {
|
if canvas.IsClosed() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
18
ui/ui.go
18
ui/ui.go
@ -4,32 +4,14 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/graphics"
|
"github.com/hajimehoshi/ebiten/graphics"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Key int
|
|
||||||
|
|
||||||
const (
|
|
||||||
KeyUp Key = iota
|
|
||||||
KeyDown
|
|
||||||
KeyLeft
|
|
||||||
KeyRight
|
|
||||||
KeySpace
|
|
||||||
KeyMax
|
|
||||||
)
|
|
||||||
|
|
||||||
type UI interface {
|
type UI interface {
|
||||||
Start(widht, height, scale int, title string) Canvas
|
Start(widht, height, scale int, title string) Canvas
|
||||||
DoEvents()
|
DoEvents()
|
||||||
Terminate()
|
Terminate()
|
||||||
}
|
}
|
||||||
|
|
||||||
type InputState interface {
|
|
||||||
IsPressedKey(key Key) bool
|
|
||||||
MouseX() int
|
|
||||||
MouseY() int
|
|
||||||
}
|
|
||||||
|
|
||||||
type Canvas interface {
|
type Canvas interface {
|
||||||
graphics.TextureFactory
|
graphics.TextureFactory
|
||||||
Draw(func(graphics.Context))
|
Draw(func(graphics.Context))
|
||||||
IsClosed() bool
|
IsClosed() bool
|
||||||
InputState() InputState
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user