Refactor canvas into seperate repo

This commit is contained in:
Caleb Bassi 2019-01-26 05:19:45 -08:00
parent ad36843630
commit 067b2c5514
5 changed files with 25 additions and 68 deletions

View File

@ -17,8 +17,7 @@ func main() {
c := ui.NewCanvas()
c.SetRect(0, 0, 50, 50)
c.Line(image.Pt(0, 0), image.Pt(80, 50), ui.ColorClear)
c.Line(image.Pt(0, 5), image.Pt(3, 10), ui.ColorClear)
c.SetLine(image.Pt(0, 0), image.Pt(10, 20), ui.ColorWhite)
ui.Render(c)

View File

@ -2,70 +2,42 @@ package termui
import (
"image"
drawille "github.com/cjbassi/drawille-go"
)
type Canvas struct {
CellMap map[image.Point]Cell
Block
drawille.Canvas
}
func NewCanvas() *Canvas {
return &Canvas{
Block: *NewBlock(),
CellMap: make(map[image.Point]Cell),
Canvas: *drawille.NewCanvas(),
}
}
// 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
func (self *Canvas) SetPoint(p image.Point, color Color) {
self.Canvas.SetPoint(p, drawille.Color(color))
}
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
}
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
}
}
}
func (self *Canvas) Point(p image.Point, color Color) {
point := image.Pt(p.X/2, p.Y/4)
self.CellMap[point] = Cell{
self.CellMap[point].Rune | BRAILLE[p.X%4][p.Y%2],
NewStyle(color),
}
func (self *Canvas) SetLine(p0, p1 image.Point, color Color) {
self.Canvas.SetLine(p0, p1, drawille.Color(color))
}
func (self *Canvas) Draw(buf *Buffer) {
for point, cell := range self.CellMap {
for point, cell := range self.Canvas.GetCells() {
if point.In(self.Rectangle) {
buf.SetCell(Cell{cell.Rune + BRAILLE_OFFSET, cell.Style}, point)
convertedCell := Cell{
cell.Rune,
Style{
Color(cell.Color),
ColorClear,
ModifierClear,
},
}
buf.SetCell(convertedCell, point)
}
}
}

6
go.mod
View File

@ -1,12 +1,8 @@
module github.com/gizak/termui
require (
github.com/google/pprof v0.0.0-20190109223431-e84dfd68c163 // indirect
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6 // indirect
github.com/cjbassi/drawille-go v0.0.0-20190126131713-27dc511fe6fd
github.com/mattn/go-runewidth v0.0.2
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d
golang.org/x/arch v0.0.0-20181203225421-5a4828bb7045 // indirect
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc // indirect
golang.org/x/sys v0.0.0-20190116161447-11f53e031339 // indirect
)

14
go.sum
View File

@ -1,18 +1,8 @@
github.com/google/pprof v0.0.0-20190109223431-e84dfd68c163 h1:beB+Da4k9B1zmgag78k3k1Bx4L/fdWr5FwNa0f8RxmY=
github.com/google/pprof v0.0.0-20190109223431-e84dfd68c163/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6 h1:UDMh68UUwekSh5iP2OMhRRZJiiBccgV7axzUG8vi56c=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/cjbassi/drawille-go v0.0.0-20190126131713-27dc511fe6fd h1:XtfPmj9tQRilnrEmI1HjQhxXWRhEM+m8CACtaMJE/kM=
github.com/cjbassi/drawille-go v0.0.0-20190126131713-27dc511fe6fd/go.mod h1:vjcQJUZJYD3MeVGhtZXSMnCHfUNZxsyYzJt90eCYxK4=
github.com/mattn/go-runewidth v0.0.2 h1:UnlwIPBGaTZfPQ6T1IGzPI0EkYAQmT9fAEJ/poFC63o=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/nsf/termbox-go v0.0.0-20180613055208-5c94acc5e6eb h1:YahEjAGkJtCrkqgVHhX6n8ZX+CZ3hDRL9fjLYugLfSs=
github.com/nsf/termbox-go v0.0.0-20180613055208-5c94acc5e6eb/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ=
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d h1:x3S6kxmy49zXVVyhcnrFqxvNVCBPb2KZ9hV2RBdS840=
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ=
golang.org/x/arch v0.0.0-20181203225421-5a4828bb7045 h1:Pn8fQdvx+z1avAi7fdM2kRYWQNxGlavNDSyzrQg2SsU=
golang.org/x/arch v0.0.0-20181203225421-5a4828bb7045/go.mod h1:cYlCBUl1MsqxdiKgmc4uh7TxZfWSFLOGSRR090WDxt8=
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc h1:F5tKCVGp+MUAHhKp5MZtGqAlGX3+oCsiL1Q629FL90M=
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/sys v0.0.0-20190116161447-11f53e031339 h1:g/Jesu8+QLnA0CPzF3E1pURg0Byr7i6jLoX5sqjcAh0=
golang.org/x/sys v0.0.0-20190116161447-11f53e031339/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

View File

@ -85,7 +85,7 @@ func (self *Plot) renderBraille(buf *Buffer, drawArea image.Rectangle, maxVal fl
for i, line := range self.Data {
for j, val := range line {
height := int((val / maxVal) * float64(drawArea.Dy()-1))
canvas.Point(
canvas.SetPoint(
image.Pt(
(drawArea.Min.X+(j*self.HorizontalScale))*2,
(drawArea.Max.Y-height-1)*4,
@ -99,7 +99,7 @@ func (self *Plot) renderBraille(buf *Buffer, drawArea image.Rectangle, maxVal fl
previousHeight := int((line[1] / maxVal) * float64(drawArea.Dy()-1))
for j, val := range line[1:] {
height := int((val / maxVal) * float64(drawArea.Dy()-1))
canvas.Line(
canvas.SetLine(
image.Pt(
(drawArea.Min.X+(j*self.HorizontalScale))*2,
(drawArea.Max.Y-previousHeight-1)*4,