Skip to content

Commit

Permalink
Adds a Canvas
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
7 changes: 7 additions & 0 deletions _demos/windows.go
Expand Up @@ -8,6 +8,7 @@ import (

var window, fancyWindow *windeau.Window
var focusableWindow *windeau.FocusableWindow
var canvas *windeau.Canvas

func main() {
termbox.Init()
Expand Down Expand Up @@ -62,6 +63,7 @@ func draw() {
window.Draw()
fancyWindow.Draw()
focusableWindow.Draw()
canvas.Draw()
termbox.Flush()
}

Expand All @@ -84,4 +86,9 @@ func prepareWindows() {
unfocusColor := windeau.WindowState{FgColor: termbox.ColorWhite, BgColor: termbox.ColorBlack}
focusableWindow = &windeau.FocusableWindow{FocusOn: focusColor, FocusOff: unfocusColor, Focused: false}
focusableWindow.SetParent(underlyingWindow)

canvas = windeau.MakeCanvas(70, 5, 10, 10)
canvas.Fill('x', termbox.ColorYellow, termbox.ColorYellow)
canvas.FilledRect('y', termbox.ColorBlack, termbox.ColorYellow, windeau.Rect{75, 7, 3, 3})
canvas.FilledRect('z', termbox.ColorBlue, termbox.ColorDefault, windeau.Rect{72, 12, 2, 3})
}
66 changes: 66 additions & 0 deletions canvas.go
@@ -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)
}
}
37 changes: 37 additions & 0 deletions canvas_test.go
@@ -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])
}
13 changes: 13 additions & 0 deletions rect.go
Expand Up @@ -12,3 +12,16 @@ func (r Rect) WithinRect(x, y int) bool {
}
return false
}

func (r Rect) Contains(other Rect) bool {
if r.X <= other.X && r.X+r.Width >= other.X+other.Width {
if r.Y <= other.Y && r.Y+r.Height >= other.Y+other.Height {
return true
}
}
return false
}

func (r Rect) DoesNotContain(other Rect) bool {
return !r.Contains(other)
}

0 comments on commit d761076

Please sign in to comment.