aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--gui/text/text.go20
-rw-r--r--gui/widget/input.go4
-rw-r--r--gui/widget/label.go2
-rw-r--r--gui/widget/output.go2
4 files changed, 20 insertions, 8 deletions
diff --git a/gui/text/text.go b/gui/text/text.go
index dfe60e1..942dce2 100644
--- a/gui/text/text.go
+++ b/gui/text/text.go
@@ -38,12 +38,19 @@ func init() {
face = &concurrentFace{sync.Mutex{}, fce}
}
+type Align int
+
+const (
+ ALIGN_LEFT Align = iota
+ ALIGN_RIGHT
+)
+
func Size(text string) image.Point {
bounds := textBounds([]byte(text), font.Drawer{Face: face})
return image.Point{bounds.Max.X - bounds.Min.X + 2*PAD, bounds.Max.Y - bounds.Min.Y + 2*PAD}
}
-func Draw(text []byte, dst draw.Image, r image.Rectangle, fg, bg color.Color) {
+func Draw(text []byte, dst draw.Image, r image.Rectangle, fg, bg color.Color, align Align) {
drawer := font.Drawer{
Src: &image.Uniform{fg},
Face: face,
@@ -60,11 +67,16 @@ func Draw(text []byte, dst draw.Image, r image.Rectangle, fg, bg color.Color) {
drawer.Dst = textImg
drawer.DrawBytes(text)
- // draw text image over background
leftCentre := image.Pt(bounds.Min.X, (bounds.Min.Y+bounds.Max.Y)/2)
- target := image.Pt(r.Max.X-bounds.Max.X-PAD, (r.Min.Y+r.Max.Y)/2)
+ var target image.Point
+ switch align {
+ case ALIGN_LEFT:
+ target = image.Pt(r.Min.X+PAD, (r.Min.Y+r.Max.Y)/2)
+ case ALIGN_RIGHT:
+ target = image.Pt(r.Max.X-bounds.Max.X-PAD, (r.Min.Y+r.Max.Y)/2)
+ }
delta := target.Sub(leftCentre)
- draw.Draw(dst, bounds.Add(delta).Intersect(r), drawer.Dst, bounds.Min, draw.Src)
+ draw.Draw(dst, bounds.Add(delta).Intersect(r), textImg, bounds.Min, draw.Src)
}
func textBounds(text []byte, drawer font.Drawer) image.Rectangle {
diff --git a/gui/widget/input.go b/gui/widget/input.go
index 954e7db..f47f587 100644
--- a/gui/widget/input.go
+++ b/gui/widget/input.go
@@ -64,9 +64,9 @@ Loop:
func inputDraw(str []byte, focused bool, r image.Rectangle) func(draw.Image) image.Rectangle {
return func(drw draw.Image) image.Rectangle {
if focused {
- text.Draw(str, drw, r, GREEN, FOCUS_COLOR)
+ text.Draw(str, drw, r, GREEN, FOCUS_COLOR, text.ALIGN_RIGHT)
} else {
- text.Draw(str, drw, r, GREEN, WHITE)
+ text.Draw(str, drw, r, GREEN, WHITE, text.ALIGN_RIGHT)
}
return r
}
diff --git a/gui/widget/label.go b/gui/widget/label.go
index ed2d24c..fe451f8 100644
--- a/gui/widget/label.go
+++ b/gui/widget/label.go
@@ -15,7 +15,7 @@ func Label(str string, r image.Rectangle, env gui.Env, wg *sync.WaitGroup) {
defer close(env.Draw())
redraw := func(drw draw.Image) image.Rectangle {
- text.Draw([]byte(str), drw, r, BLACK, WHITE)
+ text.Draw([]byte(str), drw, r, BLACK, WHITE, text.ALIGN_LEFT)
return r
}
diff --git a/gui/widget/output.go b/gui/widget/output.go
index cfbdade..ecca18c 100644
--- a/gui/widget/output.go
+++ b/gui/widget/output.go
@@ -35,7 +35,7 @@ Loop:
func outputDraw(v float64, r image.Rectangle) func(draw.Image) image.Rectangle {
return func(drw draw.Image) image.Rectangle {
- text.Draw([]byte(fmt.Sprintf("%.3f", v)), drw, r, BLACK, WHITE)
+ text.Draw([]byte(fmt.Sprintf("%.3f", v)), drw, r, BLACK, WHITE, text.ALIGN_RIGHT)
return r
}
}