Sander Schobers
cdfb863ab0
Added Action{,s}. List of actions that can be used to defer cleanup code (see NewRenderer implementations). Added TextInputEvent (replaces the old KeyPressEvent) and added to new events KeyDown & KeyUp. Added VSync to NewRendererOptions. Removed IconScale from button. Added ImageSource interface that replaces the Image/Texture method on the Texture interface. This makes converting back a texture to an image optional (since this is atypical for a hardware texture for instance). Added new KeyModifier: OSCommand (Windows/Command key). Added KeyState that can keep the state of keys (pressed or not). Added KeyEnter, representing the Enter key. Changed signatures of CreateTexture methods in Renderer. Changed signatures of icon related method (removed factories). Basic example now depends on sdlgui.
140 lines
2.9 KiB
Go
140 lines
2.9 KiB
Go
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)
|
|
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()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
t.AddTexture(name, texture)
|
|
return texture, nil
|
|
}
|
|
|
|
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)
|
|
})
|
|
}
|
|
|
|
func (t *Textures) Destroy() {
|
|
for _, texture := range t.textures {
|
|
texture.Destroy()
|
|
}
|
|
t.textures = nil
|
|
for _, textures := range t.scaled {
|
|
textures.Destroy()
|
|
}
|
|
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(textures Texture, height float32) (Texture, float32) {
|
|
scale := height / textures.Height()
|
|
if geom.IsNaN32(scale) {
|
|
return nil, 0
|
|
}
|
|
return t.Scaled(textures, scale), scale
|
|
}
|
|
|
|
func (t *Textures) ScaledByName(name string, scale float32) Texture {
|
|
textures := t.Texture(name)
|
|
if textures == nil {
|
|
return nil
|
|
}
|
|
return t.Scaled(textures, scale)
|
|
}
|
|
|
|
type ScaledTextures map[float32]Texture
|
|
|
|
func (t ScaledTextures) Destroy() {
|
|
for _, textures := range t {
|
|
textures.Destroy()
|
|
}
|
|
}
|