piss/elements/grid.go

346 lines
8.8 KiB
Go

package elements
import "image"
import "unicode"
import "image/color"
import "golang.org/x/image/font"
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/input"
import "git.tebibyte.media/sashakoshka/tomo/fixedutil"
import "git.tebibyte.media/sashakoshka/tomo/artist/shapes"
import "git.tebibyte.media/sashakoshka/tomo/default/theme"
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 {
// 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
}
type gridCell struct {
Cell
clean bool
}
func (cell *Cell) initColor () {
cell.Background = tomo.ColorBackground
cell.Foreground = tomo.ColorForeground
}
// Grid is an array of monospaced character cells. Each one has a foreground and
// background color. The width and height of the grid is determined by the size
// of its canvas.
type Grid struct {
*core.Core
*core.FocusableCore
core core.CoreControl
focusableControl core.FocusableCoreControl
cells []gridCell
stride int
cellWidth int
cellHeight int
gridBounds image.Rectangle
ignorePush bool
ascent int
face font.Face
config config.Wrapped
theme theme.Wrapped
colors [19]color.RGBA
onResize func ()
}
// NewGrid creates a new grid element.
func NewGrid () (element *Grid) {
element = &Grid { }
element.theme.Case = tomo.C("tomo", "grid")
element.Core, element.core = core.NewCore(element, element.drawAllAndPush)
element.FocusableCore,
element.focusableControl = core.NewFocusableCore(element.core, element.drawAndPush)
element.updateFontAndColors()
element.updateMinimumSize()
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 {
rows = len(element.cells) / element.stride
}
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
}
// LineFeed pushes all cells up by one line.
func (element *Grid) LineFeed () {
if element.cells == nil { return }
element.cells = element.cells[element.stride:]
var index int
for index = 0; index < len(element.cells) - element.stride; index ++ {
element.cells[index] = element.cells[index + element.stride]
element.cells[index].clean = false
}
for ; index < len(element.cells); index ++ {
element.cells[index] = gridCell { }
element.cells[index].initColor()
}
}
// 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
}
func (element *Grid) HandleMouseDown (x, y int, button input.Button) {
}
func (element *Grid) HandleMouseUp (x, y int, button input.Button) {
}
func (element *Grid) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
// TODO we need to grab shift ctrl c for copying text
}
func (element *Grid) HandleKeyUp(key input.Key, modifiers input.Modifiers) { }
// SetTheme sets the element's theme.
func (element *Grid) SetTheme (new tomo.Theme) {
if new == element.theme.Theme { return }
element.theme.Theme = new
element.updateFontAndColors()
element.updateMinimumSize()
element.drawAndPush()
}
// SetConfig sets the element's configuration.
func (element *Grid) SetConfig (new tomo.Config) {
if new == element.config.Config { return }
element.config.Config = new
element.updateMinimumSize()
element.drawAndPush()
}
// -------- private methods -------- //
func (element *Grid) inBounds (point image.Point) bool {
if point.X < 0 { return false }
if point.Y < 0 { return false }
width, height := element.Size()
if point.X >= width { return false }
if point.Y >= height { return false }
return true
}
func (element *Grid) index (point image.Point) int {
return point.X + element.stride * point.Y
}
func (element *Grid) alloc () bool {
bounds := element.Bounds()
width := bounds.Dx() / element.cellWidth
height := bounds.Dy() / element.cellHeight
oldWidth, oldHeight := element.Size()
if width == oldWidth && height == oldHeight { return false }
element.gridBounds = image.Rect (
0, 0,
width * element.cellWidth,
height * element.cellHeight).Add(bounds.Min)
oldCells := element.cells
heightLarger := height > oldHeight
element.stride = width
element.cells = make([]gridCell, width * height)
// TODO: attempt to wrap text?
if heightLarger {
newRowsStart := oldHeight * width
for index := range element.cells[newRowsStart:] {
element.cells[index + newRowsStart].initColor()
}
}
commonHeight := height
if heightLarger { commonHeight = oldHeight }
for index := range element.cells[:commonHeight * width] {
x := index % width
if x < oldWidth {
row := index / width
element.cells[index] = oldCells[x + row * oldWidth]
} else {
element.cells[index].initColor()
}
}
element.ignorePush = true
if element.onResize != nil { element.onResize() }
element.ignorePush = false
return true
}
func (element *Grid) updateFontAndColors () {
element.face = element.theme.FontFace (
tomo.FontStyleMonospace,
tomo.FontSizeNormal)
emSpace, _ := element.face.GlyphAdvance('M')
metrics := element.face.Metrics()
element.cellWidth = emSpace.Round()
element.cellHeight = metrics.Height.Round()
element.ascent = metrics.Ascent.Round()
for index := range element.colors {
element.colors[index] = element.theme.Color (
tomo.Color(index),
element.state())
}
}
func (element *Grid) updateMinimumSize () {
element.core.SetMinimumSize(element.cellWidth, element.cellHeight)
}
func (element *Grid) state () tomo.State {
return tomo.State {
Focused: element.Focused(),
}
}
func (element *Grid) drawAndPush () {
if element.core.HasImage () {
element.core.DamageRegion(element.draw(element.alloc()))
}
}
func (element *Grid) drawAllAndPush () {
element.alloc()
if element.core.HasImage () {
element.core.DamageRegion(element.draw(true))
}
}
func (element *Grid) corner (index int) image.Point {
return image.Point {
X: (index % element.stride) * element.cellWidth,
Y: (index / element.stride) * element.cellHeight,
}.Add(element.Bounds().Min)
}
func (element *Grid) bound (index int) image.Rectangle {
corner := element.corner(index)
return image.Rectangle {
Min: corner,
Max: image.Pt (
corner.X + element.cellWidth,
corner.Y + element.cellHeight),
}
}
func (element *Grid) draw (force bool) (updatedRegion image.Rectangle) {
bounds := element.Bounds()
for index, cell := range element.cells {
if force || !cell.clean {
element.cells[index].clean = true
cellBounds := element.bound(index)
updatedRegion = updatedRegion.Union(cellBounds)
shapes.FillColorRectangle (
element.core,
element.colors[cell.Background],
cellBounds)
element.drawCellGlyph(cellBounds, cell.Cell)
}}
if force {
shapes.FillColorRectangleShatter (
element.core,
element.theme.Color(tomo.ColorBackground, element.state()),
bounds, element.gridBounds)
}
return
}
func (element *Grid) drawCellGlyph (cellBounds image.Rectangle, cell Cell) {
if cell.Rune < 32 || unicode.IsSpace(cell.Rune) { return }
glyphColor := element.colors[cell.Foreground]
destinationRectangle, mask, maskPoint, _, ok := element.face.Glyph (
fixedutil.Pt(cellBounds.Min),
cell.Rune)
if !ok {
// tofu
shapes.StrokeColorRectangle (
element.core, glyphColor, cellBounds.Inset(1), 1)
return
}
maxX := destinationRectangle.Dx()
maxY := destinationRectangle.Dy()
data, stride := element.core.Buffer()
for y := 0; y < maxY; y ++ {
for x := 0; x < maxX; x ++ {
_, _, _,
alpha := mask.At(x + maskPoint.X, y + maskPoint.Y).RGBA()
dstX := x + destinationRectangle.Min.X
dstY := y + destinationRectangle.Min.Y + element.ascent
dstIndex := dstX + dstY * stride
data[dstIndex] = blend (
data[dstIndex],
glyphColor,
float64(alpha) / 0xFFFF)
}}
}
func blend (bottom, top color.RGBA, fac float64) color.RGBA {
return color.RGBA {
R: uint8(float64(bottom.R) * (1 - fac) + float64(top.R) * fac),
G: uint8(float64(bottom.G) * (1 - fac) + float64(top.G) * fac),
B: uint8(float64(bottom.B) * (1 - fac) + float64(top.B) * fac),
A: 0xFF,
}
}