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