2018-09-09 19:07:53 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"opslag.de/schobers/geom"
|
2018-09-22 14:23:26 +00:00
|
|
|
"opslag.de/schobers/zntg/allg5"
|
2018-09-09 19:07:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Button struct {
|
|
|
|
ControlBase
|
|
|
|
Text string
|
2018-09-22 14:23:26 +00:00
|
|
|
HorizontalAlignment allg5.HorizontalAlignment
|
2018-09-09 19:07:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewButton(text string, click MouseClickFn) *Button {
|
|
|
|
return &Button{ControlBase: ControlBase{OnClick: click}, Text: text}
|
|
|
|
}
|
|
|
|
|
2018-09-22 14:23:26 +00:00
|
|
|
func NewButtonAlign(text string, click MouseClickFn, align allg5.HorizontalAlignment) *Button {
|
2018-09-09 19:07:53 +00:00
|
|
|
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()
|
|
|
|
}
|
2018-09-22 14:23:26 +00:00
|
|
|
allg5.DrawFilledRectangle(min.X, min.Y, max.X, max.Y, back)
|
2018-09-09 19:07:53 +00:00
|
|
|
switch b.HorizontalAlignment {
|
2018-09-22 14:23:26 +00:00
|
|
|
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)
|
2018-09-09 19:07:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.ControlBase.Render(ctx)
|
|
|
|
}
|