zntg/ui/button.go

55 lines
1.5 KiB
Go
Raw Normal View History

package ui
import (
"opslag.de/schobers/geom"
"opslag.de/schobers/zntg/allg5"
)
type Button struct {
ControlBase
Text string
HorizontalAlignment allg5.HorizontalAlignment
}
func NewButton(text string, click MouseClickFn) *Button {
return &Button{ControlBase: ControlBase{OnClick: click}, Text: text}
}
func NewButtonAlign(text string, click MouseClickFn, align allg5.HorizontalAlignment) *Button {
return &Button{ControlBase: ControlBase{OnClick: click}, Text: text, HorizontalAlignment: align}
}
2018-09-23 07:42:23 +00:00
func (b *Button) DesiredSize(ctx Context) geom.PointF32 {
var fonts = ctx.Fonts()
var fnt = fonts.Get("default")
var w = fnt.TextWidth(b.Text)
var fntH = fnt.Height()
2018-09-23 07:42:23 +00:00
return geom.PtF32(w+fntH, 2*fntH)
}
func (b *Button) Render(ctx Context) {
var fonts = ctx.Fonts()
2018-09-23 07:42:23 +00:00
var min = b.Bounds.Min
var max = b.Bounds.Max
var fnt = fonts.Get("default")
var fntH = fnt.Height()
var back = ctx.Palette().Primary()
if b.IsOver && !b.IsPressed {
back = ctx.Palette().PrimaryHighlight()
}
allg5.DrawFilledRectangle(min.X, min.Y, max.X, max.Y, back)
switch b.HorizontalAlignment {
case allg5.AlignLeft:
fnt.Draw(min.X+.5*fntH, min.Y+.5*fntH, ctx.Palette().Lightest(), allg5.AlignLeft, b.Text)
case allg5.AlignCenter:
fnt.Draw(.5*(min.X+max.X), min.Y+.5*fntH, ctx.Palette().Lightest(), allg5.AlignCenter, b.Text)
case allg5.AlignRight:
fnt.Draw(min.X-.5*fntH, min.Y+.5*fntH, ctx.Palette().Lightest(), allg5.AlignRight, b.Text)
}
b.ControlBase.Render(ctx)
}