summaryrefslogtreecommitdiffstats
path: root/termui/grid.go
blob: b0eb7e4f8d286f42efdbae9a97dc75da69b5d794 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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)
}

type Grid struct {
	Widgets []GridBufferer
	Width   int
	Height  int
	Cols    int
	Rows    int
	BgColor Color
}

func NewGrid() *Grid {
	return &Grid{}
}

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)
}

func (g *Grid) Resize() {
	for _, w := range g.Widgets {
		w.Resize(g.Width, g.Height, g.Cols, g.Rows)
	}
}

// Buffer implements Bufferer interface.
func (g *Grid) Buffer() *Buffer {
	buf := NewFilledBuffer(0, 0, g.Width, g.Height, Cell{' ', ColorDefault, Theme.Bg})
	for _, w := range g.Widgets {
		buf.MergeWithOffset(w.Buffer(), w.GetXOffset(), w.GetYOffset())
	}
	return buf
}

func (g *Grid) GetXOffset() int {
	return 0
}

func (g *Grid) GetYOffset() int {
	return 0
}