From e867925de81b318b00204c0941c286f48b445fad Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Mon, 24 Jun 2019 20:58:14 +0200 Subject: [PATCH] Added (horizontal) text alignment. --- ui/allg5ui/renderer.go | 19 +++++++++++++++++-- ui/controlbase.go | 5 +++-- ui/label.go | 2 +- ui/renderer.go | 1 + 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/ui/allg5ui/renderer.go b/ui/allg5ui/renderer.go index eb5c770..9f19b9a 100644 --- a/ui/allg5ui/renderer.go +++ b/ui/allg5ui/renderer.go @@ -237,13 +237,28 @@ func (r *Renderer) Target() ui.Image { return &uiImage{allg5.CurrentTarget()} } -func (r *Renderer) Text(p geom.PointF32, font string, c color.Color, t string) { +func (r *Renderer) text(p geom.PointF32, font string, c color.Color, t string, align allg5.HorizontalAlignment) { var f = r.ft[font] if f == nil { return } x, y := snap(p) - f.f.Draw(x, y, newColor(c), allg5.AlignLeft, t) + f.f.Draw(x, y, newColor(c), align, t) +} + +func (r *Renderer) Text(p geom.PointF32, font string, c color.Color, t string) { + r.text(p, font, c, t, allg5.AlignLeft) +} + +func (r *Renderer) TextAlign(p geom.PointF32, font string, c color.Color, t string, align ui.HorizontalAlignment) { + var alignment = allg5.AlignLeft + switch align { + case ui.AlignCenter: + alignment = allg5.AlignCenter + case ui.AlignRight: + alignment = allg5.AlignRight + } + r.text(p, font, c, t, alignment) } // Utility functions diff --git a/ui/controlbase.go b/ui/controlbase.go index e18cd31..6f0e536 100644 --- a/ui/controlbase.go +++ b/ui/controlbase.go @@ -28,8 +28,9 @@ type ControlBase struct { onDragMove DragMoveFn onDragStart DragStartFn - Background color.Color - Font FontStyle + Background color.Color + Font FontStyle + TextAlignment HorizontalAlignment } func (c *ControlBase) Arrange(ctx Context, bounds geom.RectangleF32, offset geom.PointF32, parent Control) { diff --git a/ui/label.go b/ui/label.go index 8d2cdf7..8328fe5 100644 --- a/ui/label.go +++ b/ui/label.go @@ -32,5 +32,5 @@ func (l *Label) Render(ctx Context) { var c = l.FontColor(ctx) var f = l.FontName(ctx) var pad = ctx.Style().Dimensions.TextPadding - ctx.Renderer().Text(l.bounds.Min.Add(geom.PtF32(pad, pad)), f, c, l.Text) + ctx.Renderer().TextAlign(l.bounds.Min.Add(geom.PtF32(pad, pad)), f, c, l.Text, l.TextAlignment) } diff --git a/ui/renderer.go b/ui/renderer.go index ecbbb2a..5d646e0 100644 --- a/ui/renderer.go +++ b/ui/renderer.go @@ -32,4 +32,5 @@ type Renderer interface { Size() geom.PointF32 Target() Image Text(p geom.PointF32, font string, color color.Color, text string) + TextAlign(p geom.PointF32, font string, color color.Color, text string, align HorizontalAlignment) }