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
|
|
|
|
}
|
2020-05-15 07:20:44 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-15 07:20:44 +00:00
|
|
|
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 {
|
2020-05-15 07:20:44 +00:00
|
|
|
return nil, err
|
2020-05-12 21:03:43 +00:00
|
|
|
}
|
2020-05-15 07:20:44 +00:00
|
|
|
t.AddTexture(name, texture)
|
|
|
|
return texture, nil
|
2020-05-12 21:03:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 07:20:44 +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
|
2020-05-15 12:44:55 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-15 12:44:55 +00:00
|
|
|
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
|
|
|
|
}
|
2020-05-15 12:44:55 +00:00
|
|
|
return t.Scaled(texture, scale), scale
|
2020-05-12 21:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Textures) ScaledByName(name string, scale float32) Texture {
|
2020-05-15 12:44:55 +00:00
|
|
|
texture := t.Texture(name)
|
|
|
|
if texture == nil {
|
2020-05-12 21:03:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-05-15 12:44:55 +00:00
|
|
|
return t.Scaled(texture, scale)
|
2020-05-12 21:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ScaledTextures map[float32]Texture
|
|
|
|
|
|
|
|
func (t ScaledTextures) Destroy() {
|
2020-05-15 12:44:55 +00:00
|
|
|
for _, texture := range t {
|
|
|
|
texture.Destroy()
|
2020-05-12 21:03:43 +00:00
|
|
|
}
|
|
|
|
}
|