Files
ordoor/internal/ui/window.go
2019-12-29 17:30:21 +00:00

83 lines
1.7 KiB
Go

package ui
import (
"flag"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/inpututil"
)
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 {
Title string
KeyUpHandlers map[ebiten.Key]func()
MouseWheelHandler func(float64, float64)
// User-provided update actions
updateFn func() error
drawFn func(*ebiten.Image) error
}
// 0,0 is the *top left* of the window
//
// ebiten assumes a single window, so only call this once...
func NewWindow(title string) (*Window, error) {
return &Window{
Title: title,
KeyUpHandlers: make(map[ebiten.Key]func()),
}, nil
}
// TODO: multiple handlers for the same key?
func (w *Window) OnKeyUp(key ebiten.Key, f func()) {
w.KeyUpHandlers[key] = f
}
func (w *Window) OnMouseWheel(f func(x, y float64)) {
w.MouseWheelHandler = f
}
func (w *Window) run(screen *ebiten.Image) error {
if err := w.updateFn(); err != nil {
return err
}
// Process keys
// TODO: efficient set operations
for key, cb := range w.KeyUpHandlers {
if inpututil.IsKeyJustReleased(key) {
cb()
}
}
if w.MouseWheelHandler != nil {
x, y := ebiten.Wheel()
if x != 0 || y != 0 {
w.MouseWheelHandler(x, y)
}
}
if !ebiten.IsDrawingSkipped() {
if err := w.drawFn(screen); err != nil {
return err
}
}
return nil
}
// TODO: a stop or other cancellation mechanism
//
// 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)
}