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,28 +123,63 @@ func layoutGrid(g *d2graph.Graph, obj *d2graph.Object) (*grid, error) {
targetWidth := totalWidth / float64(grid.rows)
rowWidth := 0.
rowIndex := 0
for i, n := range grid.nodes {
layout[rowIndex] = append(layout[rowIndex], i)
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 {
addRow := func() {
layout = append(layout, []int{})
rowIndex++
rowWidth = 0
}
addNode := func(i int, n *d2graph.Object) {
layout[rowIndex] = append(layout[rowIndex], i)
rowWidth += n.Width + HORIZONTAL_PAD
}
for i, n := range grid.nodes {
// if the next node will be past the target, start a new row
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 {
targetHeight := totalHeight / float64(grid.columns)
colHeight := 0.
colIndex := 0
for i, n := range grid.nodes {
layout[colIndex] = append(layout[colIndex], i)
colHeight += n.Height + VERTICAL_PAD
if colHeight > targetHeight && i < len(grid.nodes)-1 {
addCol := func() {
layout = append(layout, []int{})
colIndex++
colHeight = 0
}
addNode := func(i int, n *d2graph.Object) {
layout[colIndex] = append(layout[colIndex], i)
colHeight += n.Height + VERTICAL_PAD
}
for i, n := range grid.nodes {
// 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
flow: "" {
width: 800
height: 200
style: {
fill: cornflowerblue
flow1: "" {
width: 120
style: {fill: white; stroke: cornflowerblue; stroke-width: 10}
}
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: {