fixing rows creation

This commit is contained in:
Gavin Nishizawa 2023-04-04 13:42:25 -07:00
parent a16924752d
commit 7ac72140c7
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD
2 changed files with 85 additions and 18 deletions

View file

@ -123,27 +123,62 @@ func layoutGrid(g *d2graph.Graph, obj *d2graph.Object) (*grid, error) {
targetWidth := totalWidth / float64(grid.rows) targetWidth := totalWidth / float64(grid.rows)
rowWidth := 0. rowWidth := 0.
rowIndex := 0 rowIndex := 0
for i, n := range grid.nodes { addRow := func() {
layout = append(layout, []int{})
rowIndex++
rowWidth = 0
}
addNode := func(i int, n *d2graph.Object) {
layout[rowIndex] = append(layout[rowIndex], i) layout[rowIndex] = append(layout[rowIndex], i)
rowWidth += n.Width + HORIZONTAL_PAD rowWidth += n.Width + HORIZONTAL_PAD
// add a new row if we pass the target width and there are more nodes }
if rowWidth > targetWidth && i < len(grid.nodes)-1 {
layout = append(layout, []int{}) for i, n := range grid.nodes {
rowIndex++ // if the next node will be past the target, start a new row
rowWidth = 0 if rowWidth+n.Width+HORIZONTAL_PAD > targetWidth {
// if the node is mostly past the target, put it on the next row
if rowWidth+n.Width/2 > targetWidth {
addRow()
addNode(i, n)
} else {
addNode(i, n)
if i < len(grid.nodes)-1 {
addRow()
}
}
} else {
addNode(i, n)
} }
} }
} else { } else {
targetHeight := totalHeight / float64(grid.columns) targetHeight := totalHeight / float64(grid.columns)
colHeight := 0. colHeight := 0.
colIndex := 0 colIndex := 0
for i, n := range grid.nodes { addCol := func() {
layout = append(layout, []int{})
colIndex++
colHeight = 0
}
addNode := func(i int, n *d2graph.Object) {
layout[colIndex] = append(layout[colIndex], i) layout[colIndex] = append(layout[colIndex], i)
colHeight += n.Height + VERTICAL_PAD colHeight += n.Height + VERTICAL_PAD
if colHeight > targetHeight && i < len(grid.nodes)-1 { }
layout = append(layout, []int{})
colIndex++ for i, n := range grid.nodes {
colHeight = 0 // if the next node will be past the target, start a new row
if colHeight+n.Height+VERTICAL_PAD > targetHeight {
// if the node is mostly past the target, put it on the next row
if colHeight+n.Height/2 > targetHeight {
addCol()
addNode(i, n)
} else {
addNode(i, n)
if i < len(grid.nodes)-1 {
addCol()
}
}
} else {
addNode(i, n)
} }
} }
} }

View file

@ -1,12 +1,44 @@
rows: 4 rows: 5
style.fill: black style.fill: black
flow: "" { flow1: "" {
width: 800 width: 120
height: 200 style: {fill: white; stroke: cornflowerblue; stroke-width: 10}
style: { }
fill: cornflowerblue flow2: "" {
} width: 120
style: {fill: white; stroke: cornflowerblue; stroke-width: 10}
}
flow3: "" {
width: 120
style: {fill: white; stroke: cornflowerblue; stroke-width: 10}
}
flow4: "" {
width: 120
style: {fill: white; stroke: cornflowerblue; stroke-width: 10}
}
flow5: "" {
width: 120
style: {fill: white; stroke: cornflowerblue; stroke-width: 10}
}
flow6: "" {
width: 240
style: {fill: white; stroke: cornflowerblue; stroke-width: 10}
}
flow7: "" {
width: 100
style: {fill: white; stroke: cornflowerblue; stroke-width: 10}
}
flow8: "" {
style: {fill: white; stroke: cornflowerblue; stroke-width: 10}
}
flow9: "" {
width: 120
style: {fill: white; stroke: cornflowerblue; stroke-width: 10}
}
flow10: "" {
width: 120
style: {fill: white; stroke: cornflowerblue; stroke-width: 10}
} }
DAGGER ENGINE: { DAGGER ENGINE: {