-
Notifications
You must be signed in to change notification settings - Fork 103
/
vector.go
240 lines (206 loc) · 5.34 KB
/
vector.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package render
import (
"image"
"github.com/tidbyt/gg"
)
// A vector draws its children either vertically or horizontally (like
// a row or a column).
//
// A vector has a main axis along which children are draw. The main
// axis is either horizontal or vertical (i.e. a row or a
// column). MainAlign controls how children are placed along this
// axis. CrossAlign controls placement orthogonally to the main axis.
type Vector struct {
Widget
Children []Widget
MainAlign string `starlark: "main_align"`
CrossAlign string `starlark: "cross_align"`
Expanded bool
Vertical bool
}
func (v Vector) PaintBounds(bounds image.Rectangle, frameIdx int) image.Rectangle {
// (dx, dy) determines the orientation of this Vector
dx, dy := 1, 0
if v.Vertical {
dx, dy = 0, 1
}
boundsW := bounds.Dx()
boundsH := bounds.Dy()
// Paint as many children as we can fit. Compute their max and
// total width and height as we go along.
maxW, maxH := 0, 0
sumW, sumH := 0, 0
for _, child := range v.Children {
cb := child.PaintBounds(image.Rect(0, 0, boundsW-dx*sumW, boundsH-dy*sumH), frameIdx)
imW := cb.Dx()
imH := cb.Dy()
sumW += imW
if imW > maxW {
maxW = imW
}
sumH += imH
if imH > maxH {
maxH = imH
}
// This checks if we've overflowed the main axis
if sumW*dx >= boundsW || sumH*dy >= boundsH {
break
}
}
// Compute the final dimensions of the vector. If the vector
// is expanded, then it will span the full bounds along the
// main axis. Otherwise, it will be the size of its children.
// Along the cross axis, size will be the max of the
// children. However, in both cases, total size can never
// exceed the available bounds.
width := dx*sumW + dy*maxW
height := dx*maxH + dy*sumH
if v.Expanded {
width = dx*boundsW + dy*maxW
height = dx*maxH + dy*boundsH
}
if height > boundsH {
height = boundsH
}
if width > boundsW {
width = boundsW
}
return image.Rect(0, 0, width, height)
}
func (v Vector) Paint(dc *gg.Context, bounds image.Rectangle, frameIdx int) {
// (dx, dy) determines the orientation of this Vector
dx, dy := 1, 0
if v.Vertical {
dx, dy = 0, 1
}
boundsW := bounds.Dx()
boundsH := bounds.Dy()
// Paint as many children as we can fit. Compute their max and
// total width and height as we go along.
maxW, maxH := 0, 0
sumW, sumH := 0, 0
childrenBounds := make([]image.Rectangle, 0, len(v.Children))
for _, child := range v.Children {
cb := child.PaintBounds(image.Rect(0, 0, boundsW-dx*sumW, boundsH-dy*sumH), frameIdx)
imW := cb.Dx()
imH := cb.Dy()
sumW += imW
if imW > maxW {
maxW = imW
}
sumH += imH
if imH > maxH {
maxH = imH
}
childrenBounds = append(childrenBounds, cb)
// This checks if we've overflowed the main axis
if sumW*dx >= boundsW || sumH*dy >= boundsH {
break
}
}
// Compute the final dimensions of the vector. If the vector
// is expanded, then it will span the full bounds along the
// main axis. Otherwise, it will be the size of its children.
// Along the cross axis, size will be the max of the
// children. However, in both cases, total size can never
// exceed the available bounds.
width := dx*sumW + dy*maxW
height := dx*maxH + dy*sumH
if v.Expanded {
width = dx*boundsW + dy*maxW
height = dx*maxH + dy*boundsH
}
if height > boundsH {
height = boundsH
}
if width > boundsW {
width = boundsW
}
// These control position and spacing across main axis
offset := 0
spacing := 0
spacingResidual := 0
// The amount of space we have to play with
remaining := (dx*(width-sumW) + dy*(height-sumH))
if remaining < 0 {
remaining = 0
}
switch v.MainAlign {
case "start":
// all = 0
case "end":
offset = dx*(width-sumW) + dy*(height-sumH)
if offset < 0 {
offset = 0
}
case "space_evenly":
spacing = remaining / (len(childrenBounds) + 1)
spacingResidual = remaining % (len(childrenBounds) + 1)
offset = spacing
case "space_around":
spacing = remaining / len(childrenBounds)
spacingResidual = remaining % len(childrenBounds)
offset = spacing / 2
case "center":
offset = remaining / 2
case "space_between":
n := len(childrenBounds)
if n > 1 {
spacing = remaining / (n - 1)
spacingResidual = remaining % (n - 1)
if spacingResidual > 0 {
offset = -1
spacingResidual += 1
}
}
}
maxW, maxH = 0, 0
sumW, sumH = 0, 0
// Draw the children
for i, cb := range childrenBounds {
imW := cb.Dx()
imH := cb.Dy()
child := v.Children[i]
// Residual space gets distributed 1 pixel at a time
if spacingResidual > 0 {
offset += 1
spacingResidual -= 1
}
// Cross axis position depends on cross axis alignment
crossOffset := 0
switch v.CrossAlign {
case "start":
// crossOffset = 0
case "center":
crossOffset = (dx*(height-imH) + dy*(width-imW)) / 2
case "end":
crossOffset = dx*(height-imH) + dy*(width-imW)
}
dc.Push()
dc.Translate(float64(dx*offset+dy*crossOffset), float64(dx*crossOffset+dy*offset))
dc.DrawRectangle(
float64(0),
float64(0),
float64(cb.Dx()),
float64(cb.Dy()),
)
dc.Clip()
child.Paint(dc, image.Rect(0, 0, boundsW-dx*sumW, boundsH-dy*sumH), frameIdx)
dc.Pop()
sumW += imW
if imW > maxW {
maxW = imW
}
sumH += imH
if imH > maxH {
maxH = imH
}
offset += dx*imW + dy*imH + spacing
if offset >= dx*boundsW+dy*boundsH {
break
}
}
}
func (v Vector) FrameCount() int {
return MaxFrameCount(v.Children)
}