Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
So far you can just fill them in with solid colours and draw homogenous rectangles within it
- Loading branch information
Chris Saunders
committed
Jun 19, 2014
1 parent
f2d78ec
commit d761076
Showing
4 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package windeau | ||
|
||
import ( | ||
"github.com/nsf/termbox-go" | ||
) | ||
|
||
type Cell struct { | ||
Char rune | ||
Fg, Bg termbox.Attribute | ||
} | ||
|
||
var BlankCell Cell = Cell{' ', termbox.ColorDefault, termbox.ColorDefault} | ||
|
||
type Canvas struct { | ||
X, Y, Width, Height int | ||
Parent *Drawable | ||
cells [][]Cell | ||
} | ||
|
||
func MakeCanvas(x, y, w, h int) *Canvas { | ||
canvas := &Canvas{X: x, Y: y, Width: w, Height: h} | ||
canvas.initialize() | ||
return canvas | ||
} | ||
|
||
func (c *Canvas) GetRect() Rect { | ||
return Rect{c.X, c.Y, c.Width, c.Height} | ||
} | ||
|
||
func (c *Canvas) Fill(char rune, fg, bg termbox.Attribute) { | ||
c.FilledRect(char, fg, bg, c.GetRect()) | ||
} | ||
|
||
func (c *Canvas) FilledRect(char rune, fg, bg termbox.Attribute, rect Rect) { | ||
if c.GetRect().DoesNotContain(rect) { | ||
return | ||
} | ||
|
||
offsetX := rect.X - c.X | ||
offsetY := rect.Y - c.Y | ||
|
||
for i := 0; i < rect.Width; i++ { | ||
for j := 0; j < rect.Height; j++ { | ||
c.cells[i+offsetX][j+offsetY] = Cell{char, fg, bg} | ||
} | ||
} | ||
} | ||
|
||
func (c *Canvas) Draw() { | ||
for x, row := range c.cells { | ||
for y, cell := range row { | ||
termbox.SetCell(x+c.X, y+c.Y, cell.Char, cell.Fg, cell.Bg) | ||
} | ||
} | ||
} | ||
|
||
func (c *Canvas) Cells() [][]Cell { | ||
return c.cells | ||
} | ||
|
||
func (c *Canvas) initialize() { | ||
c.cells = make([][]Cell, c.Width) | ||
for i := range c.cells { | ||
c.cells[i] = make([]Cell, c.Height) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package windeau | ||
|
||
import ( | ||
"fmt" | ||
"github.com/nsf/termbox-go" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func assert_canvas_cells(t *testing.T, c rune, fg, bg termbox.Attribute, cells [][]Cell) { | ||
for _, row := range cells { | ||
for _, cell := range row { | ||
assert_canvas_cell(t, c, fg, bg, cell) | ||
} | ||
} | ||
} | ||
|
||
func assert_canvas_cell(t *testing.T, c rune, fg, bg termbox.Attribute, cell Cell) { | ||
assert.Equal(t, c, cell.Char, fmt.Sprintf("Cell should've been %v but was %v instead", c, cell.Char)) | ||
assert.Equal(t, fg, cell.Fg, "Cell foreground colour should be Red") | ||
assert.Equal(t, bg, cell.Bg, "Cell background colour should be Default") | ||
} | ||
|
||
func TestFillingCanvas(t *testing.T) { | ||
canvas := MakeCanvas(0, 0, 2, 2) | ||
canvas.Fill('x', termbox.ColorRed, termbox.ColorDefault) | ||
cells := canvas.Cells() | ||
assert_canvas_cells(t, 'x', termbox.ColorRed, termbox.ColorDefault, cells) | ||
} | ||
|
||
func TestFillingARegionInsideTheCanvas(t *testing.T) { | ||
canvas := MakeCanvas(0, 0, 5, 5) | ||
canvas.Fill('x', termbox.ColorRed, termbox.ColorDefault) | ||
canvas.FilledRect('y', termbox.ColorGreen, termbox.ColorDefault, Rect{2, 2, 1, 1}) | ||
cells := canvas.Cells() | ||
assert_canvas_cell(t, 'y', termbox.ColorGreen, termbox.ColorDefault, cells[2][2]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters