Added overloads to add an image to the Images container.

This commit is contained in:
Sander Schobers 2019-05-01 09:42:09 +02:00
parent fe4a2b7b73
commit eee707c4ec

View File

@ -1,10 +1,14 @@
package ui package ui
import ( import (
"image"
"github.com/nfnt/resize" "github.com/nfnt/resize"
"opslag.de/schobers/geom" "opslag.de/schobers/geom"
) )
type CreateImageFn func() (image.Image, error)
func ScaleImage(render Renderer, im Image, scale float32) Image { func ScaleImage(render Renderer, im Image, scale float32) Image {
w := uint(im.Width() * scale) w := uint(im.Width() * scale)
if w == 0 { if w == 0 {
@ -36,6 +40,23 @@ func (i *Images) AddImage(name string, im Image) {
i.ims[name] = im i.ims[name] = im
} }
func (i *Images) AddImageFn(name string, create CreateImageFn) error {
im, err := create()
if err != nil {
return err
}
return i.AddImageNative(name, im)
}
func (i *Images) AddImageNative(name string, im image.Image) error {
m, err := i.render.CreateImage(im)
if err != nil {
return err
}
i.AddImage(name, m)
return nil
}
func (i *Images) Destroy() { func (i *Images) Destroy() {
for _, im := range i.ims { for _, im := range i.ims {
im.Destroy() im.Destroy()