From 3390d46f3420534ae274c179f245a37532c29556 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Thu, 11 Apr 2019 20:00:26 +0200 Subject: [PATCH] Moved scaling by height to Images. --- ui/button.go | 28 +--------------------------- ui/images.go | 13 ++++++++++++- 2 files changed, 13 insertions(+), 28 deletions(-) diff --git a/ui/button.go b/ui/button.go index b59d5e0..efe6273 100644 --- a/ui/button.go +++ b/ui/button.go @@ -14,9 +14,6 @@ type Button struct { Icon Image IconScale float32 - - scale float32 - icon Image } type ButtonType int @@ -91,29 +88,6 @@ func (b *Button) fillColor(p *Palette) color.Color { 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 { if b.Font.Color != nil { return b.Font.Color @@ -149,7 +123,7 @@ func (b *Button) Render(ctx Context) { bounds = bounds.Inset(pad) pos := bounds.Min 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 { ctx.Renderer().DrawImageOptions(icon, geom.PtF32(pos.X, pos.Y+.5*(bounds.Dy()-icon.Height())), DrawOptions{Tint: textColor}) pos.X += icon.Width() + pad diff --git a/ui/images.go b/ui/images.go index d38fced..ddd8317 100644 --- a/ui/images.go +++ b/ui/images.go @@ -1,6 +1,9 @@ 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 { w := uint(im.Width() * scale) @@ -74,6 +77,14 @@ func (i *Images) Scaled(im Image, scale float32) Image { 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 { im := i.Image(name) if im == nil {