zntg/ui/textures.go

140 lines
2.9 KiB
Go
Raw Normal View History

2020-05-12 21:03:43 +00:00
package ui
import (
"image"
"github.com/nfnt/resize"
"opslag.de/schobers/geom"
)
func ScaleTexture(render Renderer, texture Texture, scale float32) Texture {
w := uint(texture.Width() * scale)
if w == 0 {
return nil
}
source, ok := texture.(ImageSource)
if !ok {
return nil
}
image, err := source.CreateImage()
if err != nil {
return nil
}
scaled := resize.Resize(w, 0, image, resize.Bilinear)
res, err := render.CreateTextureGo(scaled, false)
2020-05-12 21:03:43 +00:00
if err != nil {
return nil
}
return res
}
type Textures struct {
render Renderer
textures map[string]Texture
scaled map[Texture]ScaledTextures
}
func NewTextures(render Renderer) *Textures {
return &Textures{render, map[string]Texture{}, map[Texture]ScaledTextures{}}
}
func (t *Textures) AddTexture(name string, texture Texture) {
curr := t.textures[name]
if curr != nil {
curr.Destroy()
}
t.textures[name] = texture
}
func (t *Textures) createTexture(name string, create func() (Texture, error)) (Texture, error) {
texture, err := create()
2020-05-12 21:03:43 +00:00
if err != nil {
return nil, err
2020-05-12 21:03:43 +00:00
}
t.AddTexture(name, texture)
return texture, nil
2020-05-12 21:03:43 +00:00
}
func (t *Textures) CreateTexture(name string, source ImageSource) (Texture, error) {
return t.createTexture(name, func() (Texture, error) {
return t.render.CreateTexture(source)
})
}
func (t *Textures) CreateTextureGo(name string, im image.Image, source bool) (Texture, error) {
return t.createTexture(name, func() (Texture, error) {
return t.render.CreateTextureGo(im, source)
})
}
func (t *Textures) CreateTexturePath(name string, path string, source bool) (Texture, error) {
return t.createTexture(name, func() (Texture, error) {
return t.render.CreateTexturePath(path, source)
})
2020-05-12 21:03:43 +00:00
}
func (t *Textures) Destroy() {
for _, texture := range t.textures {
texture.Destroy()
}
t.textures = nil
for _, texture := range t.scaled {
texture.Destroy()
2020-05-12 21:03:43 +00:00
}
t.scaled = nil
}
func (t *Textures) Texture(name string) Texture {
texture, ok := t.textures[name]
if ok {
return texture
}
return nil
}
func (t *Textures) Scaled(texture Texture, scale float32) Texture {
if scale <= 0 {
return nil
}
if scale == 1 {
return texture
}
textures := t.scaled[texture]
if textures == nil {
textures = make(ScaledTextures)
} else {
scaled := textures[scale]
if scaled != nil {
return scaled
}
}
scaled := ScaleTexture(t.render, texture, scale)
textures[scale] = scaled
t.scaled[texture] = textures
return scaled
}
func (t *Textures) ScaledHeight(texture Texture, height float32) (Texture, float32) {
scale := height / texture.Height()
2020-05-12 21:03:43 +00:00
if geom.IsNaN32(scale) {
return nil, 0
}
return t.Scaled(texture, scale), scale
2020-05-12 21:03:43 +00:00
}
func (t *Textures) ScaledByName(name string, scale float32) Texture {
texture := t.Texture(name)
if texture == nil {
2020-05-12 21:03:43 +00:00
return nil
}
return t.Scaled(texture, scale)
2020-05-12 21:03:43 +00:00
}
type ScaledTextures map[float32]Texture
func (t ScaledTextures) Destroy() {
for _, texture := range t {
texture.Destroy()
2020-05-12 21:03:43 +00:00
}
}