From 4d518849e57ad3f1a6168f5b7ba8ea9c539453e2 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Wed, 13 Mar 2019 20:16:54 +0100 Subject: [PATCH] Added IconScale for Button. --- ui/button.go | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/ui/button.go b/ui/button.go index 1607a8e..80fc77b 100644 --- a/ui/button.go +++ b/ui/button.go @@ -14,6 +14,8 @@ type Button struct { Text string Icon Image + IconScale float32 + scale float32 icon Image } @@ -42,15 +44,19 @@ func (b *Button) desiredSize(ctx Context) geom.PointF32 { var font = ctx.Renderer().Font(b.FontName(ctx)) var w, h float32 = 0, font.Height() if len(b.Text) != 0 { - w += font.WidthOf(b.Text) + pad + w += pad + font.WidthOf(b.Text) } if b.Icon != nil && b.Icon.Height() > 0 { - w += b.Icon.Width()*h/b.Icon.Height() + pad + iconW := b.Icon.Width() * h / b.Icon.Height() + if b.IconScale > 0 { + iconW *= b.IconScale + } + w += pad + iconW } if w == 0 { return geom.ZeroPtF32 } - return geom.PtF32(pad+w, pad+h+pad) + return geom.PtF32(w+pad, pad+h+pad) } func (b *Button) DesiredSize(ctx Context) geom.PointF32 { @@ -88,6 +94,12 @@ func (b *Button) fillColor(p *Palette) color.Color { 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 @@ -97,7 +109,11 @@ func (b *Button) scaledIcon(ctx Context, height float32) Image { b.icon.Destroy() b.icon = nil } - im := resize.Resize(uint(b.Icon.Width()*scale), 0, b.Icon.Image(), resize.Bilinear) + w := uint(b.Icon.Width() * scale) + if w == 0 { + return nil + } + im := resize.Resize(w, 0, b.Icon.Image(), resize.Bilinear) icon, err := ctx.Renderer().CreateImage(im) if err != nil { return nil @@ -142,10 +158,11 @@ func (b *Button) Render(ctx Context) { var pad = style.Dimensions.TextPadding bounds = bounds.Inset(pad) pos := bounds.Min + pos.X += pad if b.Icon != nil && b.Icon.Height() > 0 { icon := b.scaledIcon(ctx, bounds.Dy()) if icon != nil { - ctx.Renderer().DrawImageOptions(icon, pos, 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 } }