Try casting to PhysicalResources first when setting resource provider.

SetResourceProvider accepts Resources instead of factory method to it.
This commit is contained in:
Sander Schobers 2020-07-07 18:19:34 +02:00
parent 67e73a8671
commit de87c5d3aa
4 changed files with 22 additions and 12 deletions

View File

@ -331,15 +331,20 @@ func (r *Renderer) SetMouseCursor(c ui.MouseCursor) {
func (r *Renderer) SetPosition(p geom.PointF32) { r.disp.SetPosition(int(p.X), int(p.Y)) }
func (r *Renderer) SetResourceProvider(factory func() ui.Resources) {
func (r *Renderer) SetResourceProvider(res ui.Resources) {
if r.res != nil {
r.res.Destroy()
}
res, err := ui.NewCopyResources("allg5ui", factory(), false)
if err != nil {
return
if phys, ok := res.(ui.PhysicalResources); ok {
r.res = phys
} else {
copy, err := ui.NewCopyResources("allg5ui", res, false)
if err != nil {
return
}
r.res = copy
}
r.res = res
}
func (r *Renderer) SetUnhandledEventHandler(handler func(allg5.Event)) {

View File

@ -473,15 +473,20 @@ func (r *Renderer) TextTexture(font ui.Font, color color.Color, text string) (ui
func (r *Renderer) Resources() ui.Resources { return r.resources }
func (r *Renderer) SetResourceProvider(factory func() ui.Resources) {
func (r *Renderer) SetResourceProvider(resources ui.Resources) {
if r.resources != nil {
r.resources.Destroy()
}
resources, err := ui.NewCopyResources("sdlui", factory(), false)
if err != nil {
return
if physical, ok := resources.(ui.PhysicalResources); ok {
r.resources = physical
} else {
copy, err := ui.NewCopyResources("sdlui", resources, false)
if err != nil {
return
}
r.resources = copy
}
r.resources = resources
}
// Texture

View File

@ -41,7 +41,7 @@ type Renderer interface {
// Resources
Resources() Resources
SetResourceProvider(factory func() Resources)
SetResourceProvider(resources Resources)
}
// TextTexture renders specified text to a new texture.

View File

@ -22,7 +22,7 @@ func NewRenderer(title string, width, height int, opts NewRendererOptions) (Rend
if err != nil {
return nil, err
}
renderer.SetResourceProvider(func() Resources { return DefaultResources() })
renderer.SetResourceProvider(DefaultResources())
return renderer, nil
}