Added renderer factory.

- Removes dependency on the specific backend from an application point-of-view.
This commit is contained in:
Sander Schobers 2020-05-12 22:46:58 +02:00
parent 5ecfd10754
commit 2c9007ce9b
5 changed files with 66 additions and 7 deletions

View File

@ -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) allg5.DrawRectangle(minX, minY, maxX, maxY, newColor(c), thickness)
} }
func (r *Renderer) RegisterFont(path, name string, size int) error { func (r *Renderer) RegisterFont(name, path string, size int) error {
var f, err = allg5.LoadTTFFont(path, int(size)) var f, err = allg5.LoadTTFFont(path, size)
if err != nil { if err != nil {
return err return err
} }

View File

@ -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
}

View File

@ -4,9 +4,9 @@ import (
"image/color" "image/color"
"log" "log"
"opslag.de/schobers/allg5" _ "opslag.de/schobers/zntg/allg5ui" // import the renderer for the UI
"opslag.de/schobers/geom" "opslag.de/schobers/geom"
"opslag.de/schobers/zntg/allg5ui"
"opslag.de/schobers/zntg/ui" "opslag.de/schobers/zntg/ui"
) )
@ -63,13 +63,13 @@ func (b *basic) Init(ctx ui.Context) error {
} }
func run() 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 { if err != nil {
return err return err
} }
defer render.Destroy() 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 { if err != nil {
return err return err
} }

View File

@ -24,8 +24,9 @@ type Renderer interface {
DrawImage(im Image, p geom.PointF32) DrawImage(im Image, p geom.PointF32)
DrawImageOptions(im Image, p geom.PointF32, opts DrawOptions) DrawImageOptions(im Image, p geom.PointF32, opts DrawOptions)
FillRectangle(r geom.RectangleF32, c color.Color) FillRectangle(r geom.RectangleF32, c color.Color)
Font(font string) Font Font(name string) Font
Rectangle(r geom.RectangleF32, c color.Color, thickness float32) Rectangle(r geom.RectangleF32, c color.Color, thickness float32)
RegisterFont(name, path string, size int) error
RenderTo(Image) RenderTo(Image)
RenderToDisplay() RenderToDisplay()
SetMouseCursor(c MouseCursor) SetMouseCursor(c MouseCursor)

31
ui/rendererfactory.go Normal file
View File

@ -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
}