krampus19/alui/button.go
Sander Schobers 11ab3fca0f Added settings in-game.
Added video settings.
Added and improved reusable controls.
Separated drawing of sprites.
Fixed bug that text was right aligned instead of left aligned.
2019-12-28 16:03:57 +01:00

38 lines
791 B
Go

package alui
import (
"opslag.de/schobers/allg5"
"opslag.de/schobers/geom"
)
var _ Control = &Button{}
type Button struct {
ControlBase
Text string
TextAlign allg5.HorizontalAlignment
}
func NewButton(text string, onClick func()) *Button {
b := &Button{Text: text}
b.OnClick = onClick
return b
}
func (b *Button) DesiredSize(ctx *Context) geom.PointF32 {
font := ctx.Fonts.Get(b.Font)
w := font.TextWidth(b.Text)
return geom.PtF32(w+8, font.Height()+8)
}
func (b *Button) Render(ctx *Context, bounds geom.RectangleF32) {
fore := ctx.Palette.Primary
if b.Over {
fore = ctx.Palette.Dark
ctx.Cursor = allg5.MouseCursorLink
}
font := ctx.Fonts.Get(b.Font)
ctx.Fonts.DrawAlignFont(font, bounds.Min.X+4, bounds.Min.Y+4, bounds.Max.X-4, fore, b.TextAlign, b.Text)
}