2019-03-05 20:52:18 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2020-12-12 13:46:52 +00:00
|
|
|
"image/color"
|
|
|
|
|
2019-03-05 20:52:18 +00:00
|
|
|
"opslag.de/schobers/geom"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Label struct {
|
|
|
|
ControlBase
|
|
|
|
|
2020-12-12 13:46:52 +00:00
|
|
|
Text string
|
|
|
|
DropShadow color.Color
|
2019-07-04 20:15:32 +00:00
|
|
|
|
2020-05-16 13:37:53 +00:00
|
|
|
desired CachedValue
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 20:11:43 +00:00
|
|
|
func BuildLabel(text string, fn func(*Label)) *Label {
|
2019-03-05 20:52:18 +00:00
|
|
|
var l = &Label{Text: text}
|
2019-03-12 20:11:43 +00:00
|
|
|
if fn != nil {
|
|
|
|
fn(l)
|
|
|
|
}
|
2019-03-05 20:52:18 +00:00
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2020-05-16 13:37:53 +00:00
|
|
|
func (l *Label) hashDesiredSize(ctx Context) string {
|
2020-05-12 18:58:42 +00:00
|
|
|
return l.FontName(ctx) + l.Text
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Label) desiredSize(ctx Context) interface{} {
|
2021-07-19 05:58:46 +00:00
|
|
|
font := l.ActualFont(ctx)
|
2020-05-12 18:58:42 +00:00
|
|
|
width := font.WidthOf(l.Text)
|
|
|
|
height := font.Height()
|
2021-07-19 05:58:46 +00:00
|
|
|
pad := l.ActualTextPadding(ctx)
|
|
|
|
return geom.PtF32(pad.Left+width+pad.Right, pad.Top+height+pad.Bottom)
|
2020-05-12 18:58:42 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 13:37:53 +00:00
|
|
|
func (l *Label) DesiredSize(ctx Context, _ geom.PointF32) geom.PointF32 {
|
|
|
|
return l.desired.GetContext(ctx, l.desiredSize, l.hashDesiredSize).(geom.PointF32)
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:46:52 +00:00
|
|
|
func (l *Label) getLabelTopLeft(ctx Context) geom.PointF32 {
|
2021-07-19 05:58:46 +00:00
|
|
|
pad := l.ActualTextPadding(ctx)
|
|
|
|
bounds := pad.InsetRect(l.bounds)
|
2019-07-09 17:07:40 +00:00
|
|
|
switch l.TextAlignment {
|
|
|
|
case AlignRight:
|
2020-12-12 13:46:52 +00:00
|
|
|
return geom.PtF32(bounds.Max.X, bounds.Min.Y)
|
2019-07-09 17:07:40 +00:00
|
|
|
case AlignCenter:
|
2020-12-12 13:46:52 +00:00
|
|
|
return geom.PtF32(.5*(bounds.Min.X+bounds.Max.X), bounds.Min.Y)
|
|
|
|
default:
|
|
|
|
return bounds.Min
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Label) Render(ctx Context) {
|
|
|
|
l.RenderBackground(ctx)
|
|
|
|
fontColor := l.TextColor(ctx)
|
|
|
|
fontName := l.FontName(ctx)
|
|
|
|
topLeft := l.getLabelTopLeft(ctx)
|
|
|
|
if l.DropShadow != nil {
|
|
|
|
ctx.Fonts().TextAlign(fontName, topLeft.Add2D(1, 1), l.DropShadow, l.Text, l.TextAlignment)
|
2019-07-09 17:07:40 +00:00
|
|
|
}
|
2020-12-12 13:46:52 +00:00
|
|
|
ctx.Fonts().TextAlign(fontName, topLeft, fontColor, l.Text, l.TextAlignment)
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|