zntg/ui/rendererfactory.go
Sander Schobers 67e73a8671 Resources only exposes OpenResource (and Destroy).
PhysicalResources derives from Resources and exposes FetchResource.
Made dependency specific resource addons.
Extended the available resource options (fallback, path, refactored copy).
NewRenderer provides DefaultResources to the created renderer.
2020-05-25 22:24:06 +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(func() Resources { return 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
}