zntg/ui/rendererfactory.go
Sander Schobers de87c5d3aa Try casting to PhysicalResources first when setting resource provider.
SetResourceProvider accepts Resources instead of factory method to it.
2020-07-07 18:19:34 +02:00

47 lines
1.3 KiB
Go

package ui
import (
"errors"
"opslag.de/schobers/geom"
)
// RendererFactory can be used to inject a new factory for creating renderers.
type RendererFactory interface {
New(title string, width, height int, opts NewRendererOptions) (Renderer, error)
}
var rendererFactory RendererFactory
// NewRenderer creates a new renderer based on the registered renderer factory.
func NewRenderer(title string, width, height int, opts NewRendererOptions) (Renderer, error) {
if rendererFactory == nil {
return nil, errors.New("no renderer factory registered")
}
renderer, err := rendererFactory.New(title, width, height, opts)
if err != nil {
return nil, err
}
renderer.SetResourceProvider(DefaultResources())
return renderer, nil
}
// NewRendererDefault creates a new renderer with default options set based on the registered renderer factory.
func NewRendererDefault(title string, width, height int) (Renderer, error) {
return NewRenderer(title, width, height, NewRendererOptions{
Resizable: true,
})
}
// SetRendererFactory sets the new factory that is used to create a new renderer.
func SetRendererFactory(factory RendererFactory) {
rendererFactory = factory
}
// NewRendererOptions provides options when creating a new renderer.
type NewRendererOptions struct {
Location *geom.PointF32
Resizable bool
VSync bool
}