From dcf1c50b5c3985b044ab339baddbf135c27aa092 Mon Sep 17 00:00:00 2001 From: Vladimir Markelov Date: Mon, 19 Oct 2015 14:51:18 -0700 Subject: [PATCH] closes #3 - label text alignment and direction --- canvas.go | 23 +++++++++++++++++++++++ interface.go | 3 +++ label.go | 17 +++++++++++++++-- window.go | 4 ++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/canvas.go b/canvas.go index da26b95..e3959f4 100644 --- a/canvas.go +++ b/canvas.go @@ -128,6 +128,29 @@ func (fb *FrameBuffer) PutText(x, y int, text string, fg, bg term.Attribute) { } } +// Draws vertical text line on buffer +func (fb *FrameBuffer) PutVerticalText(x, y int, text string, fg, bg term.Attribute) { + height := fb.h + + if (y < 0 && xs.Len(text) <= -y) || x < 0 || y < 0 || x >= fb.w { + return + } + + if y < 0 { + yy := -y + y = 0 + text = xs.Slice(text, yy, -1) + } + text = CutText(text, height) + + dy := 0 + for _, char := range text { + s := term.Cell{Ch: char, Fg: fg, Bg: bg} + fb.buffer[y+dy][x] = s + dy++ + } +} + // Draws vertical text line on buffer func (fb *FrameBuffer) PutTextVertical(x, y int, text string, fg, bg term.Attribute) { height := fb.h diff --git a/interface.go b/interface.go index 483970f..1f12d3a 100644 --- a/interface.go +++ b/interface.go @@ -17,6 +17,7 @@ type Canvas interface { Size() (int, int) PutSymbol(int, int, term.Cell) bool PutText(int, int, string, term.Attribute, term.Attribute) + PutVerticalText(int, int, string, term.Attribute, term.Attribute) Symbol(int, int) (term.Cell, bool) Clear(term.Attribute) FillRect(int, int, int, int, term.Cell) @@ -50,6 +51,7 @@ type View interface { HitTest(int, int) HitResult Paddings() (int, int, int, int) + SetPaddings(int, int, int, int) AddChild(Control, int) SetPack(PackType) Pack() PackType @@ -80,6 +82,7 @@ type Control interface { SetScale(int) Constraints() (int, int) Paddings() (int, int, int, int) + SetPaddings(int, int, int, int) Repaint() AddChild(Control, int) SetPack(PackType) diff --git a/label.go b/label.go index 8265b44..f4ca3fe 100644 --- a/label.go +++ b/label.go @@ -29,6 +29,14 @@ func NewLabel(view View, parent Control, w, h int, title string, scale int) *Lab return c } +func (l *Label) Direction() Direction { + return l.direction +} + +func (l *Label) SetDirection(dir Direction) { + l.direction = dir +} + func (l *Label) Repaint() { canvas := l.view.Canvas() tm := l.view.Screen().Theme() @@ -40,6 +48,11 @@ func (l *Label) Repaint() { canvas.FillRect(l.x, l.y, l.width, l.height, term.Cell{Ch: ' ', Fg: fg, Bg: bg}) - shift, text := AlignText(l.title, l.width, l.align) - canvas.PutText(l.x+shift, l.y, text, fg, bg) + if l.direction == Horizontal { + shift, text := AlignText(l.title, l.width, l.align) + canvas.PutText(l.x+shift, l.y, text, fg, bg) + } else { + shift, text := AlignText(l.title, l.height, l.align) + canvas.PutVerticalText(l.x, l.y+shift, text, fg, bg) + } } diff --git a/window.go b/window.go index 0e04c77..d04842b 100644 --- a/window.go +++ b/window.go @@ -185,6 +185,10 @@ func (w *Window) Paddings() (int, int, int, int) { return w.padSide, w.padTop, w.padX, w.padY } +func (w *Window) SetPaddings(side, top, dx, dy int) { + w.padSide, w.padTop, w.padX, w.padY = side, top, dx, dy +} + func (w *Window) Canvas() Canvas { return w.canvas }