Add padding to edges

This commit is contained in:
Júlio César Batista 2022-11-29 11:53:30 -08:00
parent 9c0df4ebdb
commit a0c0546059
No known key found for this signature in database
GPG key ID: 10C4B861BF314878
3 changed files with 37 additions and 13 deletions

View file

@ -14,6 +14,9 @@ const MIN_EDGE_DISTANCE = 100.
// default size // default size
const ACTIVATION_BOX_WIDTH = 20. const ACTIVATION_BOX_WIDTH = 20.
// small pad so that edges don't touch lifelines and activation boxes
const ACTIVATION_BOX_EDGE_PAD = 2.
// as the activation boxes start getting nested, their size grows // as the activation boxes start getting nested, their size grows
const ACTIVATION_BOX_DEPTH_GROW_FACTOR = 10. const ACTIVATION_BOX_DEPTH_GROW_FACTOR = 10.

View file

@ -238,6 +238,15 @@ func (sd *sequenceDiagram) routeEdges() {
} else { } else {
endX = edge.Dst.TopLeft.X + edge.Dst.Width endX = edge.Dst.TopLeft.X + edge.Dst.Width
} }
if isLeftToRight {
startX += ACTIVATION_BOX_EDGE_PAD
endX -= ACTIVATION_BOX_EDGE_PAD
} else {
startX -= ACTIVATION_BOX_EDGE_PAD
endX += ACTIVATION_BOX_EDGE_PAD
}
edgeY := sd.getEdgeY(rank) edgeY := sd.getEdgeY(rank)
edge.Route = []*geo.Point{ edge.Route = []*geo.Point{
geo.NewPoint(startX, edgeY), geo.NewPoint(startX, edgeY),

View file

@ -90,11 +90,23 @@ func TestBasicSequenceDiagram(t *testing.T) {
if edge.Route[0].Y != edge.Route[1].Y { if edge.Route[0].Y != edge.Route[1].Y {
t.Fatalf("expected edge[%d] to be a horizontal line", i) t.Fatalf("expected edge[%d] to be a horizontal line", i)
} }
if edge.Route[0].X != edge.Src.Center().X { if edge.Src.TopLeft.X < edge.Dst.TopLeft.X {
t.Fatalf("expected edge[%d] source endpoint to be at the middle of the source actor", i) // left to right
if edge.Route[0].X != edge.Src.Center().X+ACTIVATION_BOX_EDGE_PAD {
t.Fatalf("expected edge[%d] x to be at the actor center", i)
}
if edge.Route[1].X != edge.Dst.Center().X-ACTIVATION_BOX_EDGE_PAD {
t.Fatalf("expected edge[%d] x to be at the actor center", i)
}
} else {
if edge.Route[0].X != edge.Src.Center().X-ACTIVATION_BOX_EDGE_PAD {
t.Fatalf("expected edge[%d] x to be at the actor center", i)
}
if edge.Route[1].X != edge.Dst.Center().X+ACTIVATION_BOX_EDGE_PAD {
t.Fatalf("expected edge[%d] x to be at the actor center", i)
} }
if edge.Route[1].X != edge.Dst.Center().X {
t.Fatalf("expected edge[%d] target endpoint to be at the middle of the target actor", i)
} }
if i > 0 { if i > 0 {
prevEdge := g.Edges[i-1] prevEdge := g.Edges[i-1]
@ -108,19 +120,19 @@ func TestBasicSequenceDiagram(t *testing.T) {
for i := nEdges; i < nExpectedEdges; i++ { for i := nEdges; i < nExpectedEdges; i++ {
edge := g.Edges[i] edge := g.Edges[i]
if len(edge.Route) != 2 { if len(edge.Route) != 2 {
t.Fatalf("expected edge[%d] to have only 2 points", i) t.Fatalf("expected lifeline edge[%d] to have only 2 points", i)
} }
if edge.Route[0].X != edge.Route[1].X { if edge.Route[0].X != edge.Route[1].X {
t.Fatalf("expected edge[%d] to be a vertical line", i) t.Fatalf("expected lifeline edge[%d] to be a vertical line", i)
} }
if edge.Route[0].X != edge.Src.Center().X { if edge.Route[0].X != edge.Src.Center().X {
t.Fatalf("expected edge[%d] x to be at the actor center", i) t.Fatalf("expected lifeline edge[%d] x to be at the actor center", i)
} }
if edge.Route[0].Y != edge.Src.Height+edge.Src.TopLeft.Y { if edge.Route[0].Y != edge.Src.Height+edge.Src.TopLeft.Y {
t.Fatalf("expected edge[%d] to start at the bottom of the source actor", i) t.Fatalf("expected lifeline edge[%d] to start at the bottom of the source actor", i)
} }
if edge.Route[1].Y < lastSequenceEdge.Route[0].Y { if edge.Route[1].Y < lastSequenceEdge.Route[0].Y {
t.Fatalf("expected edge[%d] to end after the last sequence edge", i) t.Fatalf("expected lifeline edge[%d] to end after the last sequence edge", i)
} }
} }
@ -224,15 +236,15 @@ func TestActivationBoxesSequenceDiagram(t *testing.T) {
} }
// check routes // check routes
if g.Edges[0].Route[0].X != a_t1.TopLeft.X+a_t1.Width { if g.Edges[0].Route[0].X != a_t1.TopLeft.X+a_t1.Width+ACTIVATION_BOX_EDGE_PAD {
t.Fatal("expected the first edge to start on a.t1 top right X") t.Fatal("expected the first edge to start on a.t1 top right X")
} }
if g.Edges[0].Route[1].X != b_t1.TopLeft.X { if g.Edges[0].Route[1].X != b_t1.TopLeft.X-ACTIVATION_BOX_EDGE_PAD {
t.Fatal("expected the first edge to end on b.t1 top left X") t.Fatal("expected the first edge to end on b.t1 top left X")
} }
if g.Edges[2].Route[1].X != b.Center().X { if g.Edges[2].Route[1].X != b.Center().X-ACTIVATION_BOX_EDGE_PAD {
t.Fatal("expected the third edge to end on b.t1 center X") t.Fatal("expected the third edge to end on b.t1 center X")
} }
} }