2019-03-05 20:52:18 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"opslag.de/schobers/geom"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Button struct {
|
|
|
|
ControlBase
|
|
|
|
|
|
|
|
Text string
|
|
|
|
}
|
|
|
|
|
2019-03-12 20:11:43 +00:00
|
|
|
func BuildButton(text string, fn func(b *Button)) *Button {
|
2019-03-05 20:52:18 +00:00
|
|
|
var b = &Button{Text: text}
|
2019-03-12 20:11:43 +00:00
|
|
|
if fn != nil {
|
|
|
|
fn(b)
|
|
|
|
}
|
2019-03-05 20:52:18 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Button) DesiredSize(ctx Context) geom.PointF32 {
|
2019-03-12 18:43:52 +00:00
|
|
|
var fontName = b.FontName(ctx)
|
2019-03-05 20:52:18 +00:00
|
|
|
var font = ctx.Renderer().Font(fontName)
|
|
|
|
var width = font.WidthOf(b.Text)
|
|
|
|
var height = font.Height()
|
|
|
|
var pad = ctx.Style().Dimensions.TextPadding
|
|
|
|
return geom.PtF32(width+pad*2, height+pad*2)
|
|
|
|
}
|
|
|
|
|
2019-03-11 16:30:41 +00:00
|
|
|
func (b *Button) Handle(ctx Context, e Event) {
|
|
|
|
b.ControlBase.Handle(ctx, e)
|
|
|
|
if b.over {
|
|
|
|
ctx.Renderer().SetMouseCursor(MouseCursorPointer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:52:18 +00:00
|
|
|
func (b *Button) Render(ctx Context) {
|
2019-03-12 18:43:52 +00:00
|
|
|
var fore = b.Font.Color
|
2019-03-05 20:52:18 +00:00
|
|
|
var style = ctx.Style()
|
|
|
|
if fore == nil {
|
|
|
|
fore = style.Palette.TextOnPrimary
|
|
|
|
}
|
|
|
|
var fill = style.Palette.Primary
|
|
|
|
if b.over {
|
|
|
|
fill = style.Palette.PrimaryHighlight
|
|
|
|
}
|
|
|
|
var pad = style.Dimensions.TextPadding
|
2019-03-12 18:43:52 +00:00
|
|
|
var font = b.FontName(ctx)
|
2019-03-05 20:52:18 +00:00
|
|
|
ctx.Renderer().FillRectangle(b.bounds, fill)
|
|
|
|
ctx.Renderer().Text(b.bounds.Min.Add(geom.PtF32(pad, pad)), font, fore, b.Text)
|
|
|
|
}
|