diff --git a/elements/grid.go b/elements/grid.go new file mode 100644 index 0000000..4664938 --- /dev/null +++ b/elements/grid.go @@ -0,0 +1,134 @@ +package elements + +import "image" +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/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" + +type gridCell struct { + rune + tomo.FontStyle + background tomo.Color + foreground tomo.Color + clean bool +} + +type gridBuffer struct { + cells []gridCell + stride int +} + +// Grid is an array of monospaced character cells. Each one has a foreground and +// background color. It satisfies io.Writer and can be fed text with ANSI escape +// codes. +type Grid struct { + *core.Core + *core.FocusableCore + core core.CoreControl + + cells []gridCell + stride int + cellWidth int + cellHeight int + + face font.Face + config config.Wrapped + theme theme.Wrapped + + onResize func () +} + +func NewGrid () (element *Grid) { + element = &Grid { } + element.theme.Case = tomo.C("tomo", "grid") + element.Core, element.core = core.NewCore(element, element.drawAndPush) + element.updateFont() + element.updateMinimumSize() + return +} + +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) { + +} + +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.updateFont() + 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() +} + +func (element *Grid) alloc () bool { + bounds := element.Bounds() + width := bounds.Dx() / element.cellWidth + height := bounds.Dy() / element.cellHeight + unchanged := + width == element.stride && + height == len(element.cells) / element.stride + if unchanged { return false } + + // TODO: attempt to wrap text + element.stride = width + element.cells = make([]gridCell, width * height) + for index := range element.cells { + element.cells[index].background = tomo.ColorBackground + element.cells[index].foreground = tomo.ColorForeground + } + return true +} + +func (element *Grid) updateFont () { + 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() +} + +func (element *Grid) updateMinimumSize () { + element.core.SetMinimumSize(element.cellWidth, element.cellHeight) +} + +func (element *Grid) state () tomo.State { + return tomo.State { + + } +} + +func (element *Grid) drawAndPush () { + if element.core.HasImage () { + element.core.DamageRegion(element.draw(true)) + } +} + +func (element *Grid) draw (force bool) image.Rectangle { + +} diff --git a/go.mod b/go.mod index 8884df5..0b1ff5f 100644 --- a/go.mod +++ b/go.mod @@ -22,3 +22,4 @@ require ( github.com/BurntSushi/graphics-go v0.0.0-20160129215708-b43f31a4a966 // indirect github.com/jezek/xgb v1.1.0 ) + diff --git a/theme.go b/theme.go index 63b5218..73af8b8 100644 --- a/theme.go +++ b/theme.go @@ -276,12 +276,13 @@ type FontSize int; const ( // about. FontSizeSmall ) - + // FontStyle specifies stylistic alterations to a font face. type FontStyle int; const ( FontStyleRegular FontStyle = 0 FontStyleBold FontStyle = 1 FontStyleItalic FontStyle = 2 + FontStyleMonospace FontStyle = 4 FontStyleBoldItalic FontStyle = 1 | 2 )