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
|
package layout
import (
"image"
"image/color"
"image/draw"
)
type Grid struct {
// Rows represents the number of childs of each row.
Rows []int
// Background represents the background of the grid as a uniform color.
Background color.Color
// Gap represents the grid gap, equal on all sides.
Gap int
// SplitX represents the way the space is divided among the columns in each row.
SplitX SplitFunc
// SplitY represents the way the space is divided among the rows.
SplitY SplitFunc
}
func (g Grid) Redraw(drw draw.Image, bounds image.Rectangle) {
col := g.Background
if col == nil {
col = image.Black
}
draw.Draw(drw, bounds, image.NewUniform(col), image.ZP, draw.Src)
}
func (g Grid) Lay(bounds image.Rectangle) []image.Rectangle {
gap := g.Gap
rows := g.Rows
splitX := g.SplitX
if splitX == nil {
splitX = EvenSplit
}
splitY := g.SplitY
if splitY == nil {
splitY = EvenSplit
}
ret := make([]image.Rectangle, 0)
rowsH := splitY(len(rows), bounds.Dy()-(gap*(len(rows)+1)))
X := gap + bounds.Min.X
Y := gap + bounds.Min.Y
for y, cols := range rows {
h := rowsH[y]
colsW := splitX(cols, bounds.Dx()-(gap*(cols+1)))
X = gap + bounds.Min.X
for _, w := range colsW {
ret = append(ret, image.Rect(X, Y, X+w, Y+h))
X += gap + w
}
Y += gap + h
}
return ret
}
|