2019-03-05 20:52:18 +00:00
|
|
|
package ui
|
|
|
|
|
2019-04-11 21:38:32 +00:00
|
|
|
import (
|
|
|
|
"opslag.de/schobers/geom"
|
|
|
|
)
|
2019-03-05 20:52:18 +00:00
|
|
|
|
|
|
|
type StackPanel struct {
|
|
|
|
ContainerBase
|
|
|
|
Orientation Orientation
|
|
|
|
}
|
|
|
|
|
2019-04-10 19:23:56 +00:00
|
|
|
func BuildStackPanel(o Orientation, fn func(*StackPanel)) *StackPanel {
|
|
|
|
var p = &StackPanel{Orientation: o}
|
|
|
|
if fn != nil {
|
|
|
|
fn(p)
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2019-04-11 21:38:32 +00:00
|
|
|
func (p *StackPanel) Arrange(ctx Context, bounds geom.RectangleF32, offset geom.PointF32, parent Control) {
|
2019-03-05 20:52:18 +00:00
|
|
|
bounds = p.Orientation.FlipRect(bounds)
|
|
|
|
var length float32
|
|
|
|
var stretch int
|
|
|
|
var desired = make([]geom.PointF32, len(p.Children))
|
|
|
|
for i, child := range p.Children {
|
2020-05-16 13:37:53 +00:00
|
|
|
var size = p.Orientation.FlipPt(child.DesiredSize(ctx, bounds.Size()))
|
2019-03-05 20:52:18 +00:00
|
|
|
if geom.IsNaN32(size.Y) {
|
|
|
|
stretch++
|
|
|
|
} else {
|
|
|
|
length += size.Y
|
|
|
|
}
|
|
|
|
desired[i] = size
|
|
|
|
}
|
|
|
|
var remainder = bounds.Dy() - length
|
|
|
|
var childOffset float32
|
|
|
|
for i, size := range desired {
|
|
|
|
var height = size.Y
|
|
|
|
if geom.IsNaN32(size.Y) {
|
|
|
|
if remainder < 0 {
|
|
|
|
height = 0
|
|
|
|
} else {
|
|
|
|
height = remainder / float32(stretch)
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 15:55:43 +00:00
|
|
|
minY := geom.Min32(bounds.Max.Y, bounds.Min.Y+childOffset)
|
|
|
|
maxY := geom.Min32(bounds.Max.Y, bounds.Min.Y+childOffset+height)
|
|
|
|
var child = geom.RectF32(bounds.Min.X, minY, bounds.Max.X, maxY)
|
2019-04-11 21:38:32 +00:00
|
|
|
p.Children[i].Arrange(ctx, p.Orientation.FlipRect(child), offset, p)
|
2019-03-05 20:52:18 +00:00
|
|
|
childOffset += height
|
|
|
|
}
|
2019-04-11 21:38:32 +00:00
|
|
|
p.ControlBase.Arrange(ctx, p.Orientation.FlipRect(bounds), offset, parent)
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 13:37:53 +00:00
|
|
|
func (p *StackPanel) DesiredSize(ctx Context, size geom.PointF32) geom.PointF32 {
|
2019-03-05 20:52:18 +00:00
|
|
|
var length float32
|
|
|
|
var width float32
|
|
|
|
for _, child := range p.Children {
|
2020-05-16 13:37:53 +00:00
|
|
|
var size = child.DesiredSize(ctx, size)
|
2019-03-05 20:52:18 +00:00
|
|
|
var l, w = p.Orientation.LengthParallel(size), p.Orientation.LengthPerpendicular(size)
|
|
|
|
if geom.IsNaN32(l) {
|
|
|
|
length = l
|
|
|
|
} else {
|
|
|
|
if !geom.IsNaN32(length) {
|
|
|
|
length += l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if geom.IsNaN32(w) {
|
|
|
|
width = w
|
|
|
|
} else {
|
|
|
|
if !geom.IsNaN32(width) {
|
|
|
|
width = geom.Max32(width, w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p.Orientation.Pt(length, width)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *StackPanel) Render(ctx Context) {
|
|
|
|
p.RenderBackground(ctx)
|
|
|
|
var bounds = p.Bounds()
|
|
|
|
for _, child := range p.Children {
|
|
|
|
var childB = child.Bounds()
|
2019-07-04 20:06:48 +00:00
|
|
|
if childB.Min.X >= bounds.Max.X || childB.Min.Y >= bounds.Max.Y || childB.Max.X < bounds.Min.X || childB.Max.Y < bounds.Min.Y || childB.Max.X < 0 || childB.Max.Y < 0 {
|
2019-03-05 20:52:18 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
child.Render(ctx)
|
|
|
|
}
|
|
|
|
}
|