47 lines
927 B
Go
47 lines
927 B
Go
|
package ui
|
||
|
|
||
|
import (
|
||
|
"flag"
|
||
|
|
||
|
"github.com/faiface/pixel"
|
||
|
"github.com/faiface/pixel/pixelgl"
|
||
|
)
|
||
|
|
||
|
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 {
|
||
|
PixelWindow *pixelgl.Window
|
||
|
ButtonEventHandlers map[pixelgl.Button][]func()
|
||
|
}
|
||
|
|
||
|
// WARE! 0,0 is the *bottom left* of the window
|
||
|
//
|
||
|
// To invert things so 0,0 is the *top left*, apply the InvertY` matrix
|
||
|
func NewWindow(title string) (*Window, error) {
|
||
|
cfg := pixelgl.WindowConfig{
|
||
|
Title: title,
|
||
|
Bounds: pixel.R(0, 0, float64(*winX), float64(*winY)),
|
||
|
VSync: true,
|
||
|
}
|
||
|
|
||
|
pixelWindow, err := pixelgl.NewWindow(cfg)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return &Window{
|
||
|
PixelWindow: pixelWindow,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
// TODO: a stop or other cancellation mechanism
|
||
|
func (w *Window) Run(f func()) {
|
||
|
for !w.PixelWindow.Closed() {
|
||
|
f()
|
||
|
w.PixelWindow.Update()
|
||
|
}
|
||
|
}
|