closes #3 - label text alignment and direction

This commit is contained in:
Vladimir Markelov 2015-10-19 14:51:18 -07:00
parent 6addbfe158
commit dcf1c50b5c
4 changed files with 45 additions and 2 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)
}
}

View File

@ -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
}