Moved scaling by height to Images.

This commit is contained in:
Sander Schobers 2019-04-11 20:00:26 +02:00
parent 3acb3f09af
commit 3390d46f34
2 changed files with 13 additions and 28 deletions

View File

@ -14,9 +14,6 @@ type Button struct {
Icon Image Icon Image
IconScale float32 IconScale float32
scale float32
icon Image
} }
type ButtonType int type ButtonType int
@ -91,29 +88,6 @@ func (b *Button) fillColor(p *Palette) color.Color {
return nil return nil
} }
func (b *Button) scaledIcon(ctx Context, height float32) Image {
scale := height / b.Icon.Height()
if b.IconScale > 0 {
scale *= b.IconScale
}
if geom.IsNaN32(scale) {
return nil
}
if scale == 1 {
b.scale = 1
return b.Icon
}
if b.icon == nil || b.scale != scale {
icon := ctx.Images().Scaled(b.Icon, scale)
if icon == nil {
return nil
}
b.icon = icon
b.scale = scale
}
return b.icon
}
func (b *Button) textColor(p *Palette) color.Color { func (b *Button) textColor(p *Palette) color.Color {
if b.Font.Color != nil { if b.Font.Color != nil {
return b.Font.Color return b.Font.Color
@ -149,7 +123,7 @@ func (b *Button) Render(ctx Context) {
bounds = bounds.Inset(pad) bounds = bounds.Inset(pad)
pos := bounds.Min pos := bounds.Min
if b.Icon != nil && b.Icon.Height() > 0 { if b.Icon != nil && b.Icon.Height() > 0 {
icon := b.scaledIcon(ctx, bounds.Dy()) icon, _ := ctx.Images().ScaledHeight(b.Icon, b.IconScale*bounds.Dy())
if icon != nil { if icon != nil {
ctx.Renderer().DrawImageOptions(icon, geom.PtF32(pos.X, pos.Y+.5*(bounds.Dy()-icon.Height())), DrawOptions{Tint: textColor}) ctx.Renderer().DrawImageOptions(icon, geom.PtF32(pos.X, pos.Y+.5*(bounds.Dy()-icon.Height())), DrawOptions{Tint: textColor})
pos.X += icon.Width() + pad pos.X += icon.Width() + pad

View File

@ -1,6 +1,9 @@
package ui package ui
import "github.com/nfnt/resize" import (
"github.com/nfnt/resize"
"opslag.de/schobers/geom"
)
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)
@ -74,6 +77,14 @@ func (i *Images) Scaled(im Image, scale float32) Image {
return scaled return scaled
} }
func (i *Images) ScaledHeight(im Image, height float32) (Image, float32) {
scale := height / im.Height()
if geom.IsNaN32(scale) {
return nil, 0
}
return i.Scaled(im, scale), scale
}
func (i *Images) ScaledByName(name string, scale float32) Image { func (i *Images) ScaledByName(name string, scale float32) Image {
im := i.Image(name) im := i.Image(name)
if im == nil { if im == nil {