55 lines
1.5 KiB
Go
55 lines
1.5 KiB
Go
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}
|
|
}
|
|
|
|
func (b *Button) DesiredSize(ctx Context) geom.PointF {
|
|
var fonts = ctx.Fonts()
|
|
var fnt = fonts.Get("default")
|
|
var w = fnt.TextWidth(b.Text)
|
|
var fntH = fnt.Height()
|
|
return geom.PtF(float64(w+fntH), float64(2*fntH))
|
|
}
|
|
|
|
func (b *Button) Render(ctx Context) {
|
|
var fonts = ctx.Fonts()
|
|
|
|
var min = b.Bounds.Min.To32()
|
|
var max = b.Bounds.Max.To32()
|
|
|
|
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)
|
|
}
|