67 lines
1.5 KiB
Go
Raw Normal View History

2018-02-18 23:25:02 -08:00
package termui
var Body *Grid
// GridBufferer introduces a Bufferer that can be manipulated by Grid.
type GridBufferer interface {
Bufferer
Resize(int, int, int, int)
SetGrid(int, int, int, int)
}
2018-02-23 00:42:39 -08:00
// Grid holds widgets and information about terminal dimensions.
// Widgets are adjusted and rendered through the grid.
2018-02-18 23:25:02 -08:00
type Grid struct {
Widgets []GridBufferer
Width int
Height int
Cols int
Rows int
}
2018-03-03 17:05:52 -08:00
// NewGrid creates an empty Grid.
2018-02-18 23:25:02 -08:00
func NewGrid() *Grid {
return &Grid{}
}
2018-03-03 17:05:52 -08:00
// Set assigns a widget and its grid dimensions to Grid.
2018-03-27 14:27:23 -07:00
func (self *Grid) Set(x0, y0, x1, y1 int, widget GridBufferer) {
2018-02-18 23:25:02 -08:00
if widget == nil {
return
}
if x1 <= x0 || y1 <= y0 {
panic("Invalid widget coordinates")
}
widget.SetGrid(x0, y0, x1, y1)
2018-03-27 14:27:23 -07:00
widget.Resize(self.Width, self.Height, self.Cols, self.Rows)
2018-02-18 23:25:02 -08:00
2018-03-27 14:27:23 -07:00
self.Widgets = append(self.Widgets, widget)
2018-02-18 23:25:02 -08:00
}
2018-03-03 17:05:52 -08:00
// Resize resizes each widget in the grid.
2018-03-27 14:27:23 -07:00
func (self *Grid) Resize() {
for _, w := range self.Widgets {
w.Resize(self.Width, self.Height, self.Cols, self.Rows)
2018-02-18 23:25:02 -08:00
}
}
2018-03-03 17:05:52 -08:00
// Buffer implements the Bufferer interface by merging each widget in Grid into one buffer.
2018-03-27 14:27:23 -07:00
func (self *Grid) Buffer() *Buffer {
buf := NewFilledBuffer(0, 0, self.Width, self.Height, Cell{' ', ColorDefault, Theme.Bg})
for _, w := range self.Widgets {
2018-02-18 23:25:02 -08:00
buf.MergeWithOffset(w.Buffer(), w.GetXOffset(), w.GetYOffset())
}
return buf
}
2018-03-03 17:05:52 -08:00
// GetXOffset implements Bufferer interface.
2018-03-27 14:27:23 -07:00
func (self *Grid) GetXOffset() int {
2018-02-18 23:25:02 -08:00
return 0
}
2018-03-03 17:05:52 -08:00
// GetYOffset implements Bufferer interface.
2018-03-27 14:27:23 -07:00
func (self *Grid) GetYOffset() int {
2018-02-18 23:25:02 -08:00
return 0
}