func (b *Button) Draw(self Widget, ctx *nanovgo.Context) { b.WidgetImplement.Draw(self, ctx) bx := float32(b.x) by := float32(b.y) bw := float32(b.w) bh := float32(b.h) var gradTop nanovgo.Color var gradBot nanovgo.Color if b.pushed { gradTop = b.theme.ButtonGradientTopPushed gradBot = b.theme.ButtonGradientBotPushed } else if b.mouseFocus && b.enabled { gradTop = b.theme.ButtonGradientTopFocused gradBot = b.theme.ButtonGradientBotFocused } else { gradTop = b.theme.ButtonGradientTopUnfocused gradBot = b.theme.ButtonGradientBotUnfocused } ctx.BeginPath() ctx.RoundedRect(bx+1.0, by+1.0, bw-2.0, bh-2.0, float32(b.theme.ButtonCornerRadius-1)) if b.backgroundColor.A != 0.0 { bgColor := b.backgroundColor bgColor.A = 1.0 ctx.SetFillColor(bgColor) ctx.Fill() if b.pushed { gradTop.A = 0.8 gradBot.A = 0.8 } else { a := 1 - b.backgroundColor.A if !b.enabled { a = a*0.5 + 0.5 } gradTop.A = a gradBot.A = a } } bg := nanovgo.LinearGradient(bx, by, bx, by+bh, gradTop, gradBot) ctx.SetFillPaint(bg) ctx.Fill() ctx.BeginPath() var pOff float32 = 0.0 if b.pushed { pOff = 1.0 } ctx.RoundedRect(bx+0.5, by+1.5-pOff, bw-1.0, bh-2+pOff, float32(b.theme.ButtonCornerRadius)) ctx.SetStrokeColor(b.theme.BorderLight) ctx.Stroke() ctx.BeginPath() ctx.RoundedRect(bx+0.5, by+0.5, bw-1.0, bh-2, float32(b.theme.ButtonCornerRadius)) ctx.SetStrokeColor(b.theme.BorderDark) ctx.Stroke() fontSize := float32(b.FontSize()) ctx.SetFontSize(fontSize) ctx.SetFontFace(b.theme.FontBold) caption := b.caption tw, _ := ctx.TextBounds(0, 0, caption) centerX := bx + bw*0.5 centerY := by + bh*0.5 textPosX := centerX - tw*0.5 textPosY := centerY - 1.0 textColor := b.TextColor() if b.icon > 0 || b.imageIcon > 0 { var iw, ih float32 if b.icon > 0 { ih = fontSize * 1.5 / 2 ctx.SetFontSize(ih) ctx.SetFontFace(b.theme.FontIcons) iw, _ = ctx.TextBounds(0, 0, string([]rune{rune(b.icon)})) } else if b.imageIcon > 0 { ih = fontSize * 0.9 w, h, _ := ctx.ImageSize(b.imageIcon) iw = float32(w) * ih / float32(h) } if b.caption != "" { iw += float32(b.h) * 0.15 } ctx.SetFillColor(textColor) ctx.SetTextAlign(nanovgo.AlignLeft | nanovgo.AlignMiddle) iconPosX := centerX iconPosY := centerY - 1 switch b.iconPosition { case ButtonIconLeftCentered: iconPosX -= (tw + iw) * 0.5 textPosX += iw * 0.5 case ButtonIconRightCentered: iconPosX -= iw * 0.5 textPosX += tw * 0.5 case ButtonIconLeft: iconPosX = bx + 8.0 case ButtonIconRight: iconPosX = bx + bw - iw - 8 } if b.icon > 0 { ctx.TextRune(iconPosX, iconPosY, []rune{rune(b.icon)}) } else { var eOff float32 = 0.25 if b.enabled { eOff = 0.5 } imgPaint := nanovgo.ImagePattern(iconPosX, iconPosY-ih*0.5, iw, ih, 0, b.imageIcon, eOff) ctx.SetFillPaint(imgPaint) ctx.Fill() } } ctx.SetFontSize(fontSize) ctx.SetFontFace(b.theme.FontBold) ctx.SetTextAlign(nanovgo.AlignLeft | nanovgo.AlignMiddle) ctx.SetFillColor(b.theme.TextColorShadow) ctx.Text(textPosX, textPosY, caption) ctx.SetFillColor(textColor) ctx.Text(textPosX, textPosY+1.0, caption) }