2018-02-19 15: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 16:42:39 +08:00
|
|
|
// Grid holds widgets and information about terminal dimensions.
|
|
|
|
// Widgets are adjusted and rendered through the grid.
|
2018-02-19 15:25:02 +08:00
|
|
|
type Grid struct {
|
|
|
|
Widgets []GridBufferer
|
|
|
|
Width int
|
|
|
|
Height int
|
|
|
|
Cols int
|
|
|
|
Rows int
|
|
|
|
}
|
|
|
|
|
2018-03-04 09:05:52 +08:00
|
|
|
// NewGrid creates an empty Grid.
|
2018-02-19 15:25:02 +08:00
|
|
|
func NewGrid() *Grid {
|
|
|
|
return &Grid{}
|
|
|
|
}
|
|
|
|
|
2018-03-04 09:05:52 +08:00
|
|
|
// Set assigns a widget and its grid dimensions to Grid.
|
2018-02-19 15:25:02 +08:00
|
|
|
func (g *Grid) Set(x0, y0, x1, y1 int, widget GridBufferer) {
|
|
|
|
if widget == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if x1 <= x0 || y1 <= y0 {
|
|
|
|
panic("Invalid widget coordinates")
|
|
|
|
}
|
|
|
|
|
|
|
|
widget.SetGrid(x0, y0, x1, y1)
|
|
|
|
widget.Resize(g.Width, g.Height, g.Cols, g.Rows)
|
|
|
|
|
|
|
|
g.Widgets = append(g.Widgets, widget)
|
|
|
|
}
|
|
|
|
|
2018-03-04 09:05:52 +08:00
|
|
|
// Resize resizes each widget in the grid.
|
2018-02-19 15:25:02 +08:00
|
|
|
func (g *Grid) Resize() {
|
|
|
|
for _, w := range g.Widgets {
|
|
|
|
w.Resize(g.Width, g.Height, g.Cols, g.Rows)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-04 09:05:52 +08:00
|
|
|
// Buffer implements the Bufferer interface by merging each widget in Grid into one buffer.
|
2018-02-19 15:25:02 +08:00
|
|
|
func (g *Grid) Buffer() *Buffer {
|
2018-02-22 02:39:19 +08:00
|
|
|
buf := NewFilledBuffer(0, 0, g.Width, g.Height, Cell{' ', ColorDefault, Theme.Bg})
|
2018-02-19 15:25:02 +08:00
|
|
|
for _, w := range g.Widgets {
|
|
|
|
buf.MergeWithOffset(w.Buffer(), w.GetXOffset(), w.GetYOffset())
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2018-03-04 09:05:52 +08:00
|
|
|
// GetXOffset implements Bufferer interface.
|
2018-02-19 15:25:02 +08:00
|
|
|
func (g *Grid) GetXOffset() int {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2018-03-04 09:05:52 +08:00
|
|
|
// GetYOffset implements Bufferer interface.
|
2018-02-19 15:25:02 +08:00
|
|
|
func (g *Grid) GetYOffset() int {
|
|
|
|
return 0
|
|
|
|
}
|