141 lines
4.4 KiB
Go
141 lines
4.4 KiB
Go
/*
|
|
Copyright © Brian Buller <brian@bullercodeworks.com>
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
package widgets
|
|
|
|
import (
|
|
h "git.bullercodeworks.com/brian/tcell-widgets/helpers"
|
|
"github.com/gdamore/tcell"
|
|
)
|
|
|
|
type Buffer struct {
|
|
// cells [][]Cell
|
|
cells map[Coord]Cell
|
|
maxX, maxY int
|
|
minX, minY int
|
|
}
|
|
|
|
func NewBuffer() Buffer {
|
|
return Buffer{cells: make(map[Coord]Cell)}
|
|
}
|
|
|
|
func (b *Buffer) Width() int { return b.maxX - b.minX }
|
|
func (b *Buffer) Height() int { return b.maxY - b.minY }
|
|
|
|
func (b *Buffer) Draw(x, y int, screen tcell.Screen) {
|
|
for crd, cell := range b.cells {
|
|
cell.Draw(x+crd.X, y+crd.Y, screen)
|
|
}
|
|
}
|
|
|
|
func (b *Buffer) SetCell(x, y int, c Cell) {
|
|
b.cells[Coord{X: x, Y: y}] = c
|
|
b.maxX, b.maxY = h.Max(b.maxX, x), h.Max(b.maxY, y)
|
|
b.minX, b.minY = h.Min(b.minX, x), h.Min(b.minY, y)
|
|
}
|
|
|
|
func (b *Buffer) SetCells(x, y int, c [][]Cell) {
|
|
for i := range c {
|
|
for j := range c[i] {
|
|
b.SetCell(x+j, y+i, c[i][j])
|
|
}
|
|
}
|
|
}
|
|
|
|
// Buffer Helpers
|
|
func (b *Buffer) FillText(x, y int, txt string, style tcell.Style) {
|
|
for i := range txt {
|
|
b.SetCell(x+i, y, *NewCell(rune(txt[i]), style))
|
|
}
|
|
}
|
|
|
|
func (b *Buffer) Fill(x1, y1, x2, y2 int, r rune, style tcell.Style) {
|
|
if x1 > x2 {
|
|
x1, x2 = x2, x1
|
|
}
|
|
if y1 > y2 {
|
|
y1, y2 = y2, y1
|
|
}
|
|
for x := x1; x <= x2; x++ {
|
|
for y := y1; y <= y2; y++ {
|
|
b.SetCell(x, y, *NewCell(r, style))
|
|
}
|
|
}
|
|
}
|
|
|
|
func (b *Buffer) HRule(x1, x2, y int, rule []rune, s tcell.Style) {
|
|
b.Fill(x1, y, x2, y, rule[h.RULE_FILL], s)
|
|
b.SetCell(x1, y, *NewCell(rule[h.RULE_START], s))
|
|
b.SetCell(x2, y, *NewCell(rule[h.RULE_END], s))
|
|
}
|
|
|
|
func (b *Buffer) VRule(x, y1, y2 int, rule []rune, s tcell.Style) {
|
|
b.Fill(x, y1, x, y2, rule[h.RULE_FILL], s)
|
|
b.SetCell(x, y1, *NewCell(rule[h.RULE_START], s))
|
|
b.SetCell(x, y2, *NewCell(rule[h.RULE_END], s))
|
|
}
|
|
|
|
func (b *Buffer) BorderFilled(x1, y1, x2, y2 int, border []rune, s tcell.Style) {
|
|
b.Border(x1, y1, x2, y2, border, s)
|
|
b.Fill(x1+1, y1+1, x2-1, y2-1, ' ', s)
|
|
}
|
|
|
|
func (b *Buffer) Border(x1, y1, x2, y2 int, border []rune, s tcell.Style) {
|
|
border = h.ValidateBorder(border)
|
|
b.Fill(x1+1, y1, x2-1, y1, border[h.BRD_N], s)
|
|
b.Fill(x2, y1+1, x2, y2-1, border[h.BRD_E], s)
|
|
b.Fill(x1+1, y2, x2-1, y2, border[h.BRD_S], s)
|
|
b.Fill(x1, y1+1, x1, y2-1, border[h.BRD_W], s)
|
|
b.SetCell(x1, y1, *NewCell(border[h.BRD_NW], s))
|
|
b.SetCell(x2, y1, *NewCell(border[h.BRD_NE], s))
|
|
b.SetCell(x1, y2, *NewCell(border[h.BRD_SW], s))
|
|
b.SetCell(x2, y2, *NewCell(border[h.BRD_SE], s))
|
|
}
|
|
|
|
func (b *Buffer) TitledBorderFilled(x1, y1, x2, y2 int, ttl string, border []rune, s tcell.Style) {
|
|
b.TitledBorder(x1, y1, x2, y2, ttl, border, s)
|
|
b.Fill(x1+1, y1+1, x2-1, y2-1, ' ', s)
|
|
}
|
|
|
|
func (b *Buffer) TitledBorder(x1, y1, x2, y2 int, ttl string, border []rune, s tcell.Style) {
|
|
if ttl == "" {
|
|
b.Border(x1, y1, x2, y2, border, s)
|
|
return
|
|
}
|
|
border = h.ValidateBorder(border)
|
|
ttlLength, maxTtlLength := len(ttl), (x2 - x1 - 2)
|
|
if ttlLength > maxTtlLength {
|
|
if maxTtlLength-3 > 0 {
|
|
ttlLength = maxTtlLength
|
|
ttl = ttl[0:maxTtlLength-3] + "..."
|
|
}
|
|
}
|
|
b.FillText(x1+1, y1, ttl, s)
|
|
b.Fill(x1+1+ttlLength, y1, x2-1, y1, border[h.BRD_N], s)
|
|
b.Fill(x2, y1+1, x2, y2-1, border[h.BRD_E], s)
|
|
b.Fill(x1+1, y2, x2-1, y2, border[h.BRD_S], s)
|
|
b.Fill(x1, y1+1, x1, y2-1, border[h.BRD_W], s)
|
|
b.SetCell(x1, y1, *NewCell(border[h.BRD_NW], s))
|
|
b.SetCell(x2, y1, *NewCell(border[h.BRD_NE], s))
|
|
b.SetCell(x1, y2, *NewCell(border[h.BRD_SW], s))
|
|
b.SetCell(x2, y2, *NewCell(border[h.BRD_SE], s))
|
|
}
|