|
| 1 | +package sequence |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/fogleman/gg" |
| 6 | + "image/color" |
| 7 | + "log" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + participantBoxWidth = 100.0 |
| 12 | + participantBoxHeight = 50.0 |
| 13 | + participantsPadding = 32 |
| 14 | + |
| 15 | + rectangleStrokeWidth = 2.0 |
| 16 | + lineStrokeWidth = 1.0 |
| 17 | + |
| 18 | + verticalSpaceBetweenEdges = 50 |
| 19 | + |
| 20 | + width = 1000 |
| 21 | + height = 1000 |
| 22 | +) |
| 23 | + |
| 24 | +type Diagram struct { |
| 25 | + participants []participant |
| 26 | + edges []edge |
| 27 | + renderedParticipants []*participant |
| 28 | + participantsCoordMap map[string]participantCoord |
| 29 | + |
| 30 | + dc *gg.Context |
| 31 | + title string |
| 32 | +} |
| 33 | + |
| 34 | +func NewDiagram() *Diagram { |
| 35 | + coordMap := make(map[string]participantCoord) |
| 36 | + |
| 37 | + return &Diagram{ |
| 38 | + participantsCoordMap: coordMap, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func (d *Diagram) Render() { |
| 43 | + d.dc = gg.NewContext(width, height) |
| 44 | + d.dc.DrawRectangle(0, 0, width, height) |
| 45 | + d.dc.SetColor(color.White) |
| 46 | + d.dc.Fill() |
| 47 | + |
| 48 | + d.renderTitle() |
| 49 | + d.renderParticipants() |
| 50 | + d.renderEdges() |
| 51 | + |
| 52 | + d.dc.SavePNG("out.png") |
| 53 | +} |
| 54 | + |
| 55 | +func (d *Diagram) renderTitle() { |
| 56 | + s := d.title |
| 57 | + textWidth, _ := d.dc.MeasureString(s) |
| 58 | + centerX := float64(d.dc.Width())/2.0 - float64(textWidth)/2.0 |
| 59 | + log.Printf("title: %s", d.title) |
| 60 | + d.dc.SetColor(color.Black) |
| 61 | + d.dc.DrawString(s, centerX, height*0.05) |
| 62 | + d.dc.Stroke() |
| 63 | +} |
| 64 | + |
| 65 | +func (d *Diagram) renderParticipants() { |
| 66 | + for idx := range d.participants { |
| 67 | + p := &d.participants[idx] |
| 68 | + |
| 69 | + for rIdx := range d.renderedParticipants { |
| 70 | + if d.renderedParticipants[rIdx].Name == p.Name { |
| 71 | + return |
| 72 | + } |
| 73 | + } |
| 74 | + spacePerBlock := float64(d.dc.Width() / len(d.participants)) |
| 75 | + startX := spacePerBlock*float64(len(d.renderedParticipants)+1) - spacePerBlock/2 - participantsPadding |
| 76 | + // startX := float64(participantsPadding + (len(d.renderedParticipants) * (participantBoxWidth + 1000/(len(d.participants))))) |
| 77 | + endX := startX + participantBoxWidth |
| 78 | + startY := 1000 * 0.1 // 10% from the top |
| 79 | + endY := startY + participantBoxHeight |
| 80 | + // draw the border |
| 81 | + d.dc.SetColor(color.Black) |
| 82 | + d.dc.SetLineWidth(rectangleStrokeWidth) |
| 83 | + d.dc.SetFillRule(gg.FillRuleEvenOdd) |
| 84 | + |
| 85 | + d.dc.DrawLine(startX, startY, endX, startY) |
| 86 | + d.dc.Stroke() |
| 87 | + |
| 88 | + d.dc.DrawLine(startX, endY, endX, endY) |
| 89 | + d.dc.Stroke() |
| 90 | + |
| 91 | + d.dc.DrawLine(startX, startY, startX, endY) |
| 92 | + d.dc.Stroke() |
| 93 | + |
| 94 | + d.dc.DrawLine(endX, startY, endX, endY) |
| 95 | + d.dc.Stroke() |
| 96 | + |
| 97 | + d.dc.SetColor(color.Gray{Y: 230}) |
| 98 | + d.dc.DrawRectangle( |
| 99 | + startX, |
| 100 | + startY, |
| 101 | + participantBoxWidth, |
| 102 | + participantBoxHeight, |
| 103 | + ) |
| 104 | + d.dc.SetColor(color.Black) |
| 105 | + strWidth, strHeight := d.dc.MeasureString(p.Name) |
| 106 | + centerStrWidth := startX + ((endX - startX) / 2) - strWidth/2 |
| 107 | + centerStrHeight := (endY-startY)/2 + startY + (strHeight / 2) |
| 108 | + |
| 109 | + d.dc.DrawString( |
| 110 | + p.Name, |
| 111 | + centerStrWidth, |
| 112 | + centerStrHeight, |
| 113 | + ) |
| 114 | + d.dc.Stroke() |
| 115 | + |
| 116 | + // render vertical action line for each participant |
| 117 | + centerX := startX + (endX-startX)/2 - 2.5 |
| 118 | + lineStartY := endY + 2.5 |
| 119 | + lineEndY := float64(len(d.edges)*(verticalSpaceBetweenEdges)) + lineStartY + verticalSpaceBetweenEdges // padding |
| 120 | + |
| 121 | + d.dc.SetLineWidth(lineStrokeWidth) |
| 122 | + d.dc.DrawLine(centerX, lineStartY, centerX, lineEndY) |
| 123 | + d.dc.Stroke() |
| 124 | + d.renderedParticipants = append(d.renderedParticipants, p) |
| 125 | + |
| 126 | + d.participantsCoordMap[p.Name] = participantCoord{ |
| 127 | + X: startX, |
| 128 | + Y: startY, |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func (d *Diagram) renderEdges() { |
| 134 | + renderedEdges := 0 |
| 135 | + |
| 136 | + for idx := range d.edges { |
| 137 | + e := &d.edges[idx] |
| 138 | + fromCords := d.participantsCoordMap[e.from.Name] |
| 139 | + toCords := d.participantsCoordMap[e.to.Name] |
| 140 | + startX := fromCords.X + participantBoxWidth/2 - 2.5 // 2.5 = half of stroke width |
| 141 | + startY := fromCords.Y + participantBoxHeight + 2.5 + float64((1+renderedEdges)*verticalSpaceBetweenEdges) |
| 142 | + endX := toCords.X + participantBoxWidth/2 - 2.5 |
| 143 | + isReverseEdge := endX < startX |
| 144 | + |
| 145 | + d.dc.SetDash(6) |
| 146 | + d.dc.DrawLine( |
| 147 | + startX, |
| 148 | + startY, |
| 149 | + endX, |
| 150 | + startY) |
| 151 | + d.dc.Stroke() |
| 152 | + |
| 153 | + d.dc.SetDash() |
| 154 | + |
| 155 | + if e.directional { |
| 156 | + arrowTipStartX := endX |
| 157 | + var arrowTipEndX float64 |
| 158 | + |
| 159 | + if isReverseEdge { |
| 160 | + arrowTipEndX = arrowTipStartX + 10 |
| 161 | + } else { |
| 162 | + arrowTipEndX = arrowTipStartX - 10 |
| 163 | + } |
| 164 | + d.dc.DrawLine(arrowTipStartX, startY, arrowTipEndX, startY-10) |
| 165 | + d.dc.DrawLine(arrowTipStartX, startY, arrowTipEndX, startY+10) |
| 166 | + d.dc.Stroke() |
| 167 | + } |
| 168 | + |
| 169 | + if e.Label != "" { |
| 170 | + textWidth, textHeight := d.dc.MeasureString(e.Label) |
| 171 | + textY := startY + textHeight |
| 172 | + textX := startX |
| 173 | + if isReverseEdge { |
| 174 | + textX -= participantsPadding / 2 |
| 175 | + textX -= textWidth |
| 176 | + } else { |
| 177 | + textX += participantsPadding / 2 |
| 178 | + } |
| 179 | + |
| 180 | + d.dc.DrawString(e.Label, textX, textY) |
| 181 | + } |
| 182 | + |
| 183 | + renderedEdges++ |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +func (d *Diagram) AddParticipant(name string) { |
| 188 | + for i := range d.participants { |
| 189 | + if d.participants[i].Name == name { |
| 190 | + return |
| 191 | + } |
| 192 | + } |
| 193 | + d.participants = append(d.participants, participant{Name: name}) |
| 194 | +} |
| 195 | + |
| 196 | +func (d *Diagram) AddDirectionalEdge(from, to string, label string) error { |
| 197 | + var fromPar *participant |
| 198 | + var toPar *participant |
| 199 | + for i := range d.participants { |
| 200 | + if d.participants[i].Name == from { |
| 201 | + fromPar = &d.participants[i] |
| 202 | + } |
| 203 | + if d.participants[i].Name == to { |
| 204 | + toPar = &d.participants[i] |
| 205 | + } |
| 206 | + } |
| 207 | + if fromPar == nil { |
| 208 | + panic(fmt.Sprintf("participant \"%s\" not found", from)) |
| 209 | + } |
| 210 | + if toPar == nil { |
| 211 | + panic(fmt.Sprintf("participant \"%s not found", to)) |
| 212 | + } |
| 213 | + |
| 214 | + d.edges = append(d.edges, edge{from: *fromPar, to: *toPar, Label: label, directional: true}) |
| 215 | + return nil |
| 216 | +} |
| 217 | + |
| 218 | +func (d *Diagram) AddUndirectionalEdge(from, to string, label string) error { |
| 219 | + var fromPar *participant |
| 220 | + var toPar *participant |
| 221 | + for i := range d.participants { |
| 222 | + if d.participants[i].Name == from { |
| 223 | + fromPar = &d.participants[i] |
| 224 | + } |
| 225 | + if d.participants[i].Name == to { |
| 226 | + toPar = &d.participants[i] |
| 227 | + } |
| 228 | + } |
| 229 | + if fromPar == nil || toPar == nil { |
| 230 | + return fmt.Errorf("participant not found") |
| 231 | + } |
| 232 | + |
| 233 | + d.edges = append(d.edges, edge{from: *fromPar, to: *toPar, Label: label, directional: false}) |
| 234 | + return nil |
| 235 | +} |
| 236 | + |
| 237 | +func (d *Diagram) SetTitle(s string) { |
| 238 | + d.title = s |
| 239 | +} |
0 commit comments