2020-10-01 13:14:36 +02:00
|
|
|
package buf2d
|
|
|
|
|
|
|
|
import (
|
2020-10-01 13:56:05 +02:00
|
|
|
"fmt"
|
2020-10-01 13:14:36 +02:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Buffer is a 2-dimensional rune buffer
|
|
|
|
type Buffer struct {
|
|
|
|
data [][]rune
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
parent *Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBuffer makes a new buffer with the given dimensions
|
|
|
|
func NewBuffer(width, height int) *Buffer {
|
2020-10-01 13:37:58 +02:00
|
|
|
b := make([][]rune, height)
|
|
|
|
for y := range b {
|
|
|
|
b[y] = make([]rune, width)
|
|
|
|
for x := range b[y] {
|
|
|
|
b[y][x] = ' '
|
2020-10-01 13:14:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Buffer{
|
|
|
|
data: b,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
parent: nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-01 13:56:05 +02:00
|
|
|
// Set sets the rune at position (x,y) to c
|
2020-10-01 13:14:36 +02:00
|
|
|
func (b *Buffer) Set(x, y int, c rune) {
|
2020-10-01 13:37:58 +02:00
|
|
|
b.data[y][x] = c
|
2020-10-01 13:14:36 +02:00
|
|
|
}
|
|
|
|
|
2020-10-01 13:56:05 +02:00
|
|
|
// Get returns the rune at position (x,y)
|
2020-10-01 13:14:36 +02:00
|
|
|
func (b *Buffer) Get(x, y int) rune {
|
2020-10-01 13:37:58 +02:00
|
|
|
return b.data[y][x]
|
2020-10-01 13:14:36 +02:00
|
|
|
}
|
|
|
|
|
2020-10-01 13:56:05 +02:00
|
|
|
// Size returns width and height of b
|
2020-10-01 13:14:36 +02:00
|
|
|
func (b *Buffer) Size() (w, h int) {
|
|
|
|
return b.width, b.height
|
|
|
|
}
|
|
|
|
|
2020-10-01 13:56:05 +02:00
|
|
|
// Width returns the width of b
|
2020-10-01 13:14:36 +02:00
|
|
|
func (b *Buffer) Width() int {
|
|
|
|
return b.width
|
|
|
|
}
|
|
|
|
|
2020-10-01 13:56:05 +02:00
|
|
|
// Height returns the height of b
|
2020-10-01 13:14:36 +02:00
|
|
|
func (b *Buffer) Height() int {
|
|
|
|
return b.height
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw calls drawFunc for every rune in this buffer
|
|
|
|
func (b *Buffer) Draw(drawFunc func(x, y int, c rune)) {
|
2020-10-01 13:37:58 +02:00
|
|
|
for y, col := range b.data {
|
|
|
|
for x, char := range col {
|
2020-10-01 13:14:36 +02:00
|
|
|
drawFunc(x, y, char)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Buffer) String() string {
|
|
|
|
s := new(strings.Builder)
|
2020-10-01 13:37:58 +02:00
|
|
|
for ci, col := range b.data {
|
|
|
|
for _, char := range col {
|
2020-10-01 13:14:36 +02:00
|
|
|
s.WriteRune(char)
|
|
|
|
}
|
2020-10-01 13:37:58 +02:00
|
|
|
if ci != len(b.data)-1 {
|
2020-10-01 13:14:36 +02:00
|
|
|
s.WriteRune('\n')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sub returns a buffer which is completely contained in this buffer
|
|
|
|
// Modifying the main buffer or the sub buffer will modify the other one as well
|
|
|
|
// This method can be used recursively
|
|
|
|
// If the given dimensions don't fit in the parent buffer, it will be truncated
|
|
|
|
func (b *Buffer) Sub(x, y, w, h int) *Buffer {
|
|
|
|
// sanitize inputs
|
|
|
|
x = limit(x, 0, b.width-1)
|
|
|
|
y = limit(y, 0, b.height-1)
|
|
|
|
w = limit(w, 1, b.width-x)
|
|
|
|
h = limit(h, 1, b.height-y)
|
|
|
|
|
|
|
|
// make slice references
|
2020-10-01 13:37:58 +02:00
|
|
|
data := make([][]rune, h)
|
2020-10-01 13:56:05 +02:00
|
|
|
for dy := 0; dy < h; dy++ {
|
|
|
|
col := b.data[y+dy]
|
2020-10-01 13:37:58 +02:00
|
|
|
data[dy] = col[x : x+w]
|
2020-10-01 13:14:36 +02:00
|
|
|
}
|
|
|
|
|
2020-10-01 13:56:05 +02:00
|
|
|
fmt.Println(data)
|
|
|
|
|
2020-10-01 13:14:36 +02:00
|
|
|
// make buffer
|
|
|
|
return &Buffer{
|
|
|
|
data: data,
|
|
|
|
width: w,
|
|
|
|
height: h,
|
|
|
|
parent: b,
|
|
|
|
}
|
|
|
|
}
|