-
Notifications
You must be signed in to change notification settings - Fork 104
/
stack.go
66 lines (58 loc) · 1.29 KB
/
stack.go
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
61
62
63
64
65
66
package render
import (
"image"
"github.com/tidbyt/gg"
)
// Stack draws its children on top of each other.
//
// Just like a stack of pancakes, except with Widgets instead of
// pancakes. The Stack will be given a width and height sufficient to
// fit all its children.
//
// DOC(Children): Widgets to stack
//
// EXAMPLE BEGIN
// render.Stack(
//
// children=[
// render.Box(width=50, height=25, color="#911"),
// render.Text("hello there"),
// render.Box(width=4, height=32, color="#119"),
// ],
//
// )
// EXAMPLE END
type Stack struct {
Widget
Children []Widget `starlark:"children,required"`
}
func (s Stack) PaintBounds(bounds image.Rectangle, frameIdx int) image.Rectangle {
width, height := 0, 0
for _, child := range s.Children {
cb := child.PaintBounds(bounds, frameIdx)
imW, imH := cb.Dx(), cb.Dy()
if imW > width {
width = imW
}
if imH > height {
height = imH
}
}
if width > bounds.Dx() {
width = bounds.Dx()
}
if height > bounds.Dy() {
height = bounds.Dy()
}
return image.Rect(0, 0, width, height)
}
func (s Stack) Paint(dc *gg.Context, bounds image.Rectangle, frameIdx int) {
for _, child := range s.Children {
dc.Push()
child.Paint(dc, bounds, frameIdx)
dc.Pop()
}
}
func (s Stack) FrameCount() int {
return MaxFrameCount(s.Children)
}