package ui import ( "opslag.de/schobers/galleg/allegro5" "opslag.de/schobers/geom" ) type Button struct { ControlBase Text string HorizontalAlignment allegro5.HorizontalAlignment } func NewButton(text string, click MouseClickFn) *Button { return &Button{ControlBase: ControlBase{OnClick: click}, Text: text} } func NewButtonAlign(text string, click MouseClickFn, align allegro5.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() } allegro5.DrawFilledRectangle(min.X, min.Y, max.X, max.Y, back) switch b.HorizontalAlignment { case allegro5.AlignLeft: fnt.Draw(min.X+.5*fntH, min.Y+.5*fntH, ctx.Palette().Lightest(), allegro5.AlignLeft, b.Text) case allegro5.AlignCenter: fnt.Draw(.5*(min.X+max.X), min.Y+.5*fntH, ctx.Palette().Lightest(), allegro5.AlignCenter, b.Text) case allegro5.AlignRight: fnt.Draw(min.X-.5*fntH, min.Y+.5*fntH, ctx.Palette().Lightest(), allegro5.AlignRight, b.Text) } b.ControlBase.Render(ctx) }