handle non-vertical directions when shifting nodes
This commit is contained in:
parent
ead2850fc4
commit
5844c54bcf
1 changed files with 17 additions and 8 deletions
|
|
@ -332,32 +332,43 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Downshift descendents and edges that have one endpoint connected to a descendant
|
|
||||||
q := []*d2graph.Object{obj}
|
q := []*d2graph.Object{obj}
|
||||||
|
// Downshift descendants and edges that have one endpoint connected to a descendant
|
||||||
for len(q) > 0 {
|
for len(q) > 0 {
|
||||||
curr := q[0]
|
curr := q[0]
|
||||||
q = q[1:]
|
q = q[1:]
|
||||||
|
|
||||||
stepSize := subtract
|
stepSize := subtract
|
||||||
// The object itself needs to move down the height it was just subtracted
|
// The object itself needs to move down the height it was just subtracted
|
||||||
// all descendents move half, to maintain vertical padding
|
// all descendants move half, to maintain vertical padding
|
||||||
if curr != obj {
|
if curr != obj {
|
||||||
stepSize /= 2.
|
stepSize /= 2.
|
||||||
}
|
}
|
||||||
curr.TopLeft.Y += stepSize
|
curr.TopLeft.Y += stepSize
|
||||||
|
almostEqual := func(a, b float64) bool {
|
||||||
|
return b-1 <= a && a <= b+1
|
||||||
|
}
|
||||||
shouldMove := func(p *geo.Point) bool {
|
shouldMove := func(p *geo.Point) bool {
|
||||||
if curr != obj {
|
if curr != obj {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
// Edge should only move if it's not connected to the bottom side of the shrinking container
|
if isHorizontal {
|
||||||
// Give some margin for error
|
// Only move horizontal edges if they are connected to the top side of the shrinking container
|
||||||
return !(obj.TopLeft.Y+obj.Height-1 <= p.Y && obj.TopLeft.Y+obj.Height+1 >= p.Y && p.X != obj.TopLeft.X && p.X != (obj.TopLeft.X+obj.Width))
|
return almostEqual(p.Y, obj.TopLeft.Y-stepSize)
|
||||||
|
} else {
|
||||||
|
// Edge should only move if it's not connected to the bottom side of the shrinking container
|
||||||
|
return !almostEqual(p.Y, obj.TopLeft.Y+obj.Height)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for _, e := range g.Edges {
|
for _, e := range g.Edges {
|
||||||
if _, ok := movedEdges[e]; ok {
|
if _, ok := movedEdges[e]; ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if e.Src == curr {
|
if e.Src == curr {
|
||||||
|
// Don't move src points on side of container
|
||||||
|
if almostEqual(e.Route[0].X, obj.TopLeft.X) || almostEqual(e.Route[0].X, obj.TopLeft.X+obj.Width) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if shouldMove(e.Route[0]) {
|
if shouldMove(e.Route[0]) {
|
||||||
e.Route[0].Y += stepSize
|
e.Route[0].Y += stepSize
|
||||||
}
|
}
|
||||||
|
|
@ -368,9 +379,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, c := range curr.ChildrenArray {
|
q = append(q, curr.ChildrenArray...)
|
||||||
q = append(q, c)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue