allg5/alui/button.go

38 lines
791 B
Go

package alui
import (
"opslag.de/schobers/allg5"
"opslag.de/schobers/geom"
)
var _ Control = &Button{}
type Button struct {
ControlBase
Text string
TextAlign allg5.HorizontalAlignment
}
func NewButton(text string, onClick func()) *Button {
b := &Button{Text: text}
b.OnClick = onClick
return b
}
func (b *Button) DesiredSize(ctx *Context) geom.PointF32 {
font := ctx.Fonts.Get(b.Font)
w := font.TextWidth(b.Text)
return geom.PtF32(w+8, font.Height()+8)
}
func (b *Button) Render(ctx *Context, bounds geom.RectangleF32) {
fore := ctx.Palette.Primary
if b.Over {
fore = ctx.Palette.Dark
ctx.Cursor = allg5.MouseCursorLink
}
font := ctx.Fonts.Get(b.Font)
ctx.Fonts.DrawAlignFont(font, bounds.Min.X+4, bounds.Min.Y+4, bounds.Max.X-4, fore, b.TextAlign, b.Text)
}