add scroll draw method to canvas

This commit is contained in:
Vladimir Markelov 2015-11-04 18:09:45 -08:00
parent f17984c4d8
commit c599674c3a
3 changed files with 77 additions and 25 deletions

View File

@ -274,3 +274,55 @@ if you want to hide the caret. Used by controls like EditField
func (fb *FrameBuffer) SetCursorPos(x, y int) {
term.SetCursor(x, y)
}
/*
DrawScroll paints a scroll bar inside FrameBuffer.
x, y - start position.
w, h - width and height (if h equals 1 then horizontal scroll is drawn
and vertical otherwise).
pos - thumb position.
fgScroll, bgScroll - scroll bar main attributes.
fgThumb, bgThumb - thumb colors.
scrollChars - rune set(by default, in case of is is empty string, the
rune set equals "░■▲▼")
*/
func (fb *FrameBuffer) DrawScroll(x, y, w, h, pos int, fgScroll, bgScroll, fgThumb, bgThumb term.Attribute, scrollChars string) {
if w < 1 || h < 1 {
return
}
if scrollChars == "" {
scrollChars = "░■▲▼"
}
parts := []rune(scrollChars)
chLine, chCursor, chUp, chDown := parts[0], parts[1], parts[2], parts[3]
if h == 1 {
fb.PutChar(x, y, chUp, fgScroll, bgScroll)
fb.PutChar(x+w-1, y, chDown, fgScroll, bgScroll)
if w > 2 {
for xx := 1; xx < w-1; xx++ {
fb.PutChar(x+xx, y, chLine, fgScroll, bgScroll)
}
}
if pos != -1 {
fb.PutChar(x+pos, y, chCursor, fgThumb, bgThumb)
}
} else {
fb.PutChar(x, y, chUp, fgScroll, bgScroll)
fb.PutChar(x, y+h-1, chDown, fgScroll, bgScroll)
if h > 2 {
for yy := 1; yy < h-1; yy++ {
fb.PutChar(x, y+yy, chLine, fgScroll, bgScroll)
}
}
if pos != -1 {
fb.PutChar(x, y+pos, chCursor, fgThumb, bgThumb)
}
}
}

View File

@ -49,6 +49,18 @@ type Canvas interface {
FillRect(x int, y int, width int, height int, symbol term.Cell)
// DrawFrame paints a frame inside Canvas with optional border rune set(by default, in case of border is empty string, the rune set equals "─│┌┐└┘" - single border). The inner area of frame is not filled - in other words it is transparent
DrawFrame(x int, y int, width int, height int, fg term.Attribute, bg term.Attribute, border string)
/*
DrawScroll paints a scroll bar inside FrameBuffer.
x, y - start position.
w, h - width and height (if h equals 1 then horizontal scroll is drawn
and vertical otherwise).
pos - thumb position.
fgScroll, bgScroll - scroll bar main attributes.
fgThumb, bgThumb - thumb colors.
scrollChars - rune set(by default, in case of is is empty string, the
rune set equals "░■▲▼")
*/
DrawScroll(x, y, w, h, pos int, fgScroll, bgScroll, fgThumb, bgThumb term.Attribute, scrollChars string)
// SetCursorPos sets text caret position. Used by controls like EditField
SetCursorPos(x int, y int)
}

View File

@ -68,36 +68,24 @@ func NewListBox(view View, parent Control, width, height int, scale int) *ListBo
}
func (l *ListBox) redrawScroll(canvas Canvas, tm Theme) {
parts := []rune(tm.SysObject(ObjScrollBar))
chLine, chCursor, chUp, chDown := parts[0], parts[1], parts[2], parts[3]
fg, bg := RealColor(tm, l.fg, ColorScrollText), RealColor(tm, l.bg, ColorScrollBack)
fgThumb, bgThumb := RealColor(tm, l.fg, ColorThumbText), RealColor(tm, l.bg, ColorThumbBack)
canvas.PutSymbol(l.x+l.width-1, l.y, term.Cell{Ch: chUp, Fg: fg, Bg: bg})
canvas.PutSymbol(l.x+l.width-1, l.y+l.height-1, term.Cell{Ch: chDown, Fg: fg, Bg: bg})
if l.height > 2 {
for yy := 1; yy < l.height-1; yy++ {
canvas.PutSymbol(l.x+l.width-1, l.y+yy, term.Cell{Ch: chLine, Fg: fg, Bg: bg})
pos := -1
if l.currSelection != -1 {
if len(l.items) > 1 {
ydiff := int(float32(l.currSelection) / float32(len(l.items)-1.0) * float32(l.height-3))
l.buttonPos = ydiff + 1
if l.height == 3 {
pos = 1
} else {
pos = l.buttonPos
}
} else {
pos = 1
}
}
if l.currSelection == -1 {
return
}
if l.height == 3 || l.currSelection <= 0 {
canvas.PutSymbol(l.x+l.width-1, l.y+1, term.Cell{Ch: chCursor, Fg: fgThumb, Bg: bgThumb})
return
}
// if l.pressY == -1 {
ydiff := int(float32(l.currSelection) / float32(len(l.items)-1.0) * float32(l.height-3))
l.buttonPos = ydiff + 1
// }
canvas.PutSymbol(l.x+l.width-1, l.y+l.buttonPos, term.Cell{Ch: chCursor, Fg: fgThumb, Bg: bgThumb})
canvas.DrawScroll(l.x+l.width-1, l.y, 1, l.height, pos, fg, bg, fgThumb, bgThumb, tm.SysObject(ObjScrollBar))
}
func (l *ListBox) redrawItems(canvas Canvas, tm Theme) {