Files
ordoor/internal/ui/window.go

62 lines
1.2 KiB
Go
Raw Normal View History

package ui
import (
"flag"
2019-12-29 15:38:49 +00:00
"github.com/hajimehoshi/ebiten"
)
var (
winX = flag.Int("win-x", 1280, "width of the view-map window")
winY = flag.Int("win-y", 1024, "height of the view-map window")
)
type Window struct {
2019-12-29 15:38:49 +00:00
Title string
KeyEventHandlers map[ebiten.Key][]func()
// User-provided update actions
updateFn func() error
drawFn func(*ebiten.Image) error
}
2019-12-29 15:38:49 +00:00
// 0,0 is the *top left* of the window
//
2019-12-29 15:38:49 +00:00
// ebiten assumes a single window, so only call this once...
func NewWindow(title string) (*Window, error) {
2019-12-29 15:38:49 +00:00
return &Window{
Title: title,
KeyEventHandlers: make(map[ebiten.Key][]func()),
}, nil
}
func (w *Window) OnKeyUp(key ebiten.Key, f func()) {
}
func (w *Window) run(screen *ebiten.Image) error {
if err := w.updateFn(); err != nil {
return err
}
2019-12-29 15:38:49 +00:00
// Process keys
if !ebiten.IsDrawingSkipped() {
if err := w.drawFn(screen); err != nil {
return err
}
}
2019-12-29 15:38:49 +00:00
return nil
}
// TODO: a stop or other cancellation mechanism
2019-12-29 15:38:49 +00:00
//
// Note that this must be called on the main OS thread
func (w *Window) Run(updateFn func() error, drawFn func(*ebiten.Image) error) error {
w.updateFn = updateFn
w.drawFn = drawFn
return ebiten.Run(w.run, *winX, *winY, 1, w.Title)
}