termui/canvas.go

64 lines
1.6 KiB
Go
Raw Normal View History

2015-03-26 16:11:41 -06:00
package termui
2019-01-23 21:12:10 -07:00
import (
"image"
)
2015-03-26 16:11:41 -06:00
2019-01-23 21:12:10 -07:00
type Canvas struct {
CellMap map[image.Point]Cell
Block
2015-03-26 16:11:41 -06:00
}
2019-01-23 21:12:10 -07:00
func NewCanvas() *Canvas {
return &Canvas{
Block: *NewBlock(),
CellMap: make(map[image.Point]Cell),
2015-03-26 16:11:41 -06:00
}
}
2019-01-23 21:12:10 -07:00
// points given as arguments correspond to dots within a braille character
// and therefore have 2x4 times the resolution of a normal cell
func (self *Canvas) Line(p0, p1 image.Point, color Color) {
leftPoint, rightPoint := p0, p1
if leftPoint.X > rightPoint.X {
leftPoint, rightPoint = rightPoint, leftPoint
}
2015-03-26 16:11:41 -06:00
2019-01-23 21:12:10 -07:00
xDistance := AbsInt(leftPoint.X - rightPoint.X)
yDistance := AbsInt(leftPoint.Y - rightPoint.Y)
slope := float64(yDistance) / float64(xDistance)
slopeDirection := 1
if rightPoint.Y < leftPoint.Y {
slopeDirection = -1
}
2015-03-26 16:11:41 -06:00
2019-01-23 21:12:10 -07:00
targetYCoordinate := float64(leftPoint.Y)
currentYCoordinate := leftPoint.Y
for i := leftPoint.X; i < rightPoint.X; i++ {
targetYCoordinate += (slope * float64(slopeDirection))
if currentYCoordinate == int(targetYCoordinate) {
point := image.Pt(i/2, currentYCoordinate/4)
self.CellMap[point] = Cell{
self.CellMap[point].Rune | BRAILLE[currentYCoordinate%4][i%2],
NewStyle(color),
}
}
for currentYCoordinate != int(targetYCoordinate) {
point := image.Pt(i/2, currentYCoordinate/4)
self.CellMap[point] = Cell{
self.CellMap[point].Rune | BRAILLE[currentYCoordinate%4][i%2],
NewStyle(color),
}
currentYCoordinate += slopeDirection
}
}
2015-03-26 16:11:41 -06:00
}
2019-01-23 21:12:10 -07:00
func (self *Canvas) Draw(buf *Buffer) {
for point, cell := range self.CellMap {
if point.In(self.Rectangle) {
buf.SetCell(Cell{cell.Rune + BRAILLE_OFFSET, cell.Style}, point)
}
2015-03-26 16:11:41 -06:00
}
}