diff --git a/allg5ui/renderer.go b/allg5ui/renderer.go index b64cd4d..bc52fcc 100644 --- a/allg5ui/renderer.go +++ b/allg5ui/renderer.go @@ -202,8 +202,8 @@ func (r *Renderer) Rectangle(rect geom.RectangleF32, c color.Color, thickness fl allg5.DrawRectangle(minX, minY, maxX, maxY, newColor(c), thickness) } -func (r *Renderer) RegisterFont(path, name string, size int) error { - var f, err = allg5.LoadTTFFont(path, int(size)) +func (r *Renderer) RegisterFont(name, path string, size int) error { + var f, err = allg5.LoadTTFFont(path, size) if err != nil { return err } diff --git a/allg5ui/rendererfactory.go b/allg5ui/rendererfactory.go new file mode 100644 index 0000000..2be3eca --- /dev/null +++ b/allg5ui/rendererfactory.go @@ -0,0 +1,27 @@ +package allg5ui + +import ( + "opslag.de/schobers/allg5" + "opslag.de/schobers/zntg/ui" +) + +func init() { + ui.SetRendererFactory(&rendererFactory{}) +} + +type rendererFactory struct{} + +func (f rendererFactory) New(title string, width, height int) (ui.Renderer, error) { + return f.NewOptions(title, width, height, ui.NewRendererOptions{Resizable: true}) +} + +func (f rendererFactory) NewOptions(title string, width, height int, opts ui.NewRendererOptions) (ui.Renderer, error) { + renderer, err := NewRenderer(width, height, allg5.NewDisplayOptions{ + Resizable: opts.Resizable, + }) + if err != nil { + return nil, err + } + renderer.SetWindowTitle(title) + return renderer, nil +} diff --git a/ui/examples/01_basic/basic.go b/ui/examples/01_basic/basic.go index 7e8cd7c..98a1693 100644 --- a/ui/examples/01_basic/basic.go +++ b/ui/examples/01_basic/basic.go @@ -4,9 +4,9 @@ import ( "image/color" "log" - "opslag.de/schobers/allg5" + _ "opslag.de/schobers/zntg/allg5ui" // import the renderer for the UI + "opslag.de/schobers/geom" - "opslag.de/schobers/zntg/allg5ui" "opslag.de/schobers/zntg/ui" ) @@ -63,13 +63,13 @@ func (b *basic) Init(ctx ui.Context) error { } func run() error { - var render, err = allg5ui.NewRenderer(800, 600, allg5.NewDisplayOptions{}) + var render, err = ui.NewRenderer("Basic Example", 800, 600) if err != nil { return err } defer render.Destroy() - err = render.RegisterFont("../resources/font/OpenSans-Regular.ttf", "default", 14) + err = render.RegisterFont("default", "../resources/font/OpenSans-Regular.ttf", 14) if err != nil { return err } diff --git a/ui/renderer.go b/ui/renderer.go index 5d646e0..4ac34fd 100644 --- a/ui/renderer.go +++ b/ui/renderer.go @@ -24,8 +24,9 @@ type Renderer interface { DrawImage(im Image, p geom.PointF32) DrawImageOptions(im Image, p geom.PointF32, opts DrawOptions) FillRectangle(r geom.RectangleF32, c color.Color) - Font(font string) Font + Font(name string) Font Rectangle(r geom.RectangleF32, c color.Color, thickness float32) + RegisterFont(name, path string, size int) error RenderTo(Image) RenderToDisplay() SetMouseCursor(c MouseCursor) diff --git a/ui/rendererfactory.go b/ui/rendererfactory.go new file mode 100644 index 0000000..d8bccb1 --- /dev/null +++ b/ui/rendererfactory.go @@ -0,0 +1,31 @@ +package ui + +import ( + "errors" + + "opslag.de/schobers/geom" +) + +type RendererFactory interface { + New(title string, width, height int) (Renderer, error) + NewOptions(title string, width, height int, opts NewRendererOptions) (Renderer, error) +} + +var rendererFactory RendererFactory + +func NewRenderer(title string, width, height int) (Renderer, error) { + if rendererFactory == nil { + return nil, errors.New("no renderer factory registered") + } + return rendererFactory.New(title, width, height) +} + +// SetRendererFactory sets the new factory that is used to create a new renderer. +func SetRendererFactory(factory RendererFactory) { + rendererFactory = factory +} + +type NewRendererOptions struct { + Location *geom.PointF32 + Resizable bool +}