Added documentation for Grid

This commit is contained in:
Sasha Koshka 2023-04-08 19:43:23 -04:00
parent 643bbf1116
commit 7ca2622c1e
1 changed files with 16 additions and 5 deletions

View File

@ -13,9 +13,15 @@ import "git.tebibyte.media/sashakoshka/tomo/default/config"
// import "git.tebibyte.media/sashakoshka/tomo/textdraw" // import "git.tebibyte.media/sashakoshka/tomo/textdraw"
import "git.tebibyte.media/sashakoshka/tomo/elements/core" import "git.tebibyte.media/sashakoshka/tomo/elements/core"
// Cell represents a single cell on the grid.
type Cell struct { type Cell struct {
// TODO: we should probably store more information than just a rune
// (because emojis and combining characters exist).
Rune rune Rune rune
// Monospace is assumed here.
Style tomo.FontStyle Style tomo.FontStyle
Background tomo.Color Background tomo.Color
Foreground tomo.Color Foreground tomo.Color
} }
@ -31,7 +37,8 @@ func (cell *Cell) initColor () {
} }
// Grid is an array of monospaced character cells. Each one has a foreground and // Grid is an array of monospaced character cells. Each one has a foreground and
// background color. // background color. The width and height of the grid is determined by the size
// of its canvas.
type Grid struct { type Grid struct {
*core.Core *core.Core
*core.FocusableCore *core.FocusableCore
@ -44,8 +51,6 @@ type Grid struct {
cellHeight int cellHeight int
gridBounds image.Rectangle gridBounds image.Rectangle
cursor image.Point
ignorePush bool ignorePush bool
ascent int ascent int
@ -57,6 +62,7 @@ type Grid struct {
onResize func () onResize func ()
} }
// NewGrid creates a new grid element.
func NewGrid () (element *Grid) { func NewGrid () (element *Grid) {
element = &Grid { } element = &Grid { }
element.theme.Case = tomo.C("tomo", "grid") element.theme.Case = tomo.C("tomo", "grid")
@ -68,6 +74,7 @@ func NewGrid () (element *Grid) {
return return
} }
// Size returns the width in height (in cells) of the grid.
func (element *Grid) Size () (columns, rows int) { func (element *Grid) Size () (columns, rows int) {
columns = element.stride columns = element.stride
if element.stride > 0 { if element.stride > 0 {
@ -76,23 +83,27 @@ func (element *Grid) Size () (columns, rows int) {
return return
} }
// At returns the cell located at the given point, starting at (0, 0).
func (element *Grid) At (point image.Point) Cell { func (element *Grid) At (point image.Point) Cell {
if !element.inBounds(point) { return Cell { } } if !element.inBounds(point) { return Cell { } }
return element.cells[element.index(point)].Cell return element.cells[element.index(point)].Cell
} }
// Set sets the cell located at the given point, starting at (0, 0).
func (element *Grid) Set (point image.Point, cell Cell) { func (element *Grid) Set (point image.Point, cell Cell) {
if !element.inBounds(point) { return } if !element.inBounds(point) { return }
element.cells[element.index(point)].Cell = cell element.cells[element.index(point)].Cell = cell
element.cells[element.index(point)].clean = false element.cells[element.index(point)].clean = false
} }
// Push pushes whatever changes were made to the grid to the screen.
func (element *Grid) Push () { func (element *Grid) Push () {
if !element.ignorePush { if !element.ignorePush {
element.drawAndPush() element.drawAndPush()
} }
} }
// OnResize sets a function to be called when the grid is resized.
func (element *Grid) OnResize (callback func ()) { func (element *Grid) OnResize (callback func ()) {
element.onResize = callback element.onResize = callback
} }