Added IconScale for Button.

This commit is contained in:
Sander Schobers 2019-03-13 20:16:54 +01:00
parent ff51378aff
commit 4d518849e5

View File

@ -14,6 +14,8 @@ type Button struct {
Text string Text string
Icon Image Icon Image
IconScale float32
scale float32 scale float32
icon Image icon Image
} }
@ -42,15 +44,19 @@ func (b *Button) desiredSize(ctx Context) geom.PointF32 {
var font = ctx.Renderer().Font(b.FontName(ctx)) var font = ctx.Renderer().Font(b.FontName(ctx))
var w, h float32 = 0, font.Height() var w, h float32 = 0, font.Height()
if len(b.Text) != 0 { 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 { 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 { if w == 0 {
return geom.ZeroPtF32 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 { 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 { func (b *Button) scaledIcon(ctx Context, height float32) Image {
scale := height / b.Icon.Height() scale := height / b.Icon.Height()
if b.IconScale > 0 {
scale *= b.IconScale
}
if geom.IsNaN32(scale) {
return nil
}
if scale == 1 { if scale == 1 {
b.scale = 1 b.scale = 1
return b.Icon return b.Icon
@ -97,7 +109,11 @@ func (b *Button) scaledIcon(ctx Context, height float32) Image {
b.icon.Destroy() b.icon.Destroy()
b.icon = nil 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) icon, err := ctx.Renderer().CreateImage(im)
if err != nil { if err != nil {
return nil return nil
@ -142,10 +158,11 @@ func (b *Button) Render(ctx Context) {
var pad = style.Dimensions.TextPadding var pad = style.Dimensions.TextPadding
bounds = bounds.Inset(pad) bounds = bounds.Inset(pad)
pos := bounds.Min pos := bounds.Min
pos.X += pad
if b.Icon != nil && b.Icon.Height() > 0 { if b.Icon != nil && b.Icon.Height() > 0 {
icon := b.scaledIcon(ctx, bounds.Dy()) icon := b.scaledIcon(ctx, bounds.Dy())
if icon != nil { 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 pos.X += icon.Width() + pad
} }
} }