Added (horizontal) text alignment.

This commit is contained in:
Sander Schobers 2019-06-24 20:58:14 +02:00
parent ff064bbf1f
commit e867925de8
4 changed files with 22 additions and 5 deletions

View File

@ -237,13 +237,28 @@ func (r *Renderer) Target() ui.Image {
return &uiImage{allg5.CurrentTarget()} 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] var f = r.ft[font]
if f == nil { if f == nil {
return return
} }
x, y := snap(p) 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 // Utility functions

View File

@ -30,6 +30,7 @@ type ControlBase struct {
Background color.Color Background color.Color
Font FontStyle Font FontStyle
TextAlignment HorizontalAlignment
} }
func (c *ControlBase) Arrange(ctx Context, bounds geom.RectangleF32, offset geom.PointF32, parent Control) { func (c *ControlBase) Arrange(ctx Context, bounds geom.RectangleF32, offset geom.PointF32, parent Control) {

View File

@ -32,5 +32,5 @@ func (l *Label) Render(ctx Context) {
var c = l.FontColor(ctx) var c = l.FontColor(ctx)
var f = l.FontName(ctx) var f = l.FontName(ctx)
var pad = ctx.Style().Dimensions.TextPadding 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)
} }

View File

@ -32,4 +32,5 @@ type Renderer interface {
Size() geom.PointF32 Size() geom.PointF32
Target() Image Target() Image
Text(p geom.PointF32, font string, color color.Color, text string) Text(p geom.PointF32, font string, color color.Color, text string)
TextAlign(p geom.PointF32, font string, color color.Color, text string, align HorizontalAlignment)
} }