zntg/ui/button.go
Sander Schobers c83a6d5aad Added FontStyle on ControlBase.
Forced putting text on integer coordinates.
2019-03-12 19:43:52 +01:00

50 lines
1.1 KiB
Go

package ui
import (
"opslag.de/schobers/geom"
)
type Button struct {
ControlBase
Text string
}
func BuildButton(text string, factory func(b *Button)) *Button {
var b = &Button{Text: text}
factory(b)
return b
}
func (b *Button) DesiredSize(ctx Context) geom.PointF32 {
var fontName = b.FontName(ctx)
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)
}
func (b *Button) Handle(ctx Context, e Event) {
b.ControlBase.Handle(ctx, e)
if b.over {
ctx.Renderer().SetMouseCursor(MouseCursorPointer)
}
}
func (b *Button) Render(ctx Context) {
var fore = b.Font.Color
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
var font = b.FontName(ctx)
ctx.Renderer().FillRectangle(b.bounds, fill)
ctx.Renderer().Text(b.bounds.Min.Add(geom.PtF32(pad, pad)), font, fore, b.Text)
}