-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiagramRenderer.cs
295 lines (257 loc) · 7.33 KB
/
DiagramRenderer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
using System;
using System.Xml;
using System.Collections.Generic;
using Cairo;
public class DiagramRenderer
{
Context cr;
public double width;
public double height;
Diagram diagram;
public DiagramRenderer(
Context cr,
double width,
double height,
Diagram diagram)
{
this.cr = cr;
this.width = width;
this.height = height;
this.diagram = diagram;
}
private double y_current;
public double YCurrent {
get { return y_current; }
set { y_current = value; }
}
class LifeLine
{
public string label;
private double x;
public double X {
get { return x; }
set { x = value; }
}
double y0, y1;
DiagramRenderer dr;
List<CanvasItem> boxes;
Stack<Box> activeBoxes;
private double x_left_before;
public double XLeftBefore {
get { return x_left_before; }
}
private double x_left_after;
public double XLeftAfter {
get { return x_left_after; }
}
private double x_right_before;
public double XRightBefore {
get { return x_right_before; }
}
private double x_right_after;
public double XRightAfter {
get { return x_right_after; }
}
double GetXLeft()
{
if(activeBoxes.Count != 0)
{
return activeBoxes.Peek().X0;
}
else
{
return x;
}
}
double GetXRight()
{
if(activeBoxes.Count != 0)
{
return activeBoxes.Peek().X1;
}
else
{
return x;
}
}
public double XClearLeft {
get { return x - 3; }
}
public double XClearRight {
get { return GetXRight(); }
}
public LifeLine(string label, DiagramRenderer dr, double x, double y0)
{
this.label = label;
this.dr = dr;
this.x = x;
this.y0 = y0;
boxes = new List<CanvasItem>();
activeBoxes = new Stack<Box>();
}
public void Activate()
{
Box box = new Box();
box.Y0 = dr.YCurrent;
box.X0 = GetXRight() - 3;
box.X1 = GetXRight() + 3;
activeBoxes.Push(box);
}
public void Deactivate()
{
if(activeBoxes.Count != 0)
{
Box box = activeBoxes.Pop();
box.Y1 = dr.YCurrent;
boxes.Add(box);
}
}
public void BeforeActivations()
{
x_right_before = GetXRight();
x_left_before = GetXLeft();
}
public void AfterActivations()
{
x_right_after = GetXRight();
x_left_after = GetXLeft();
}
public void End()
{
y1 = dr.YCurrent;
boxes.Reverse();
// Console.WriteLine("ll {0} {1}",
// label, y1);
}
public void Draw()
{
dr.cr.Save();
double[] llDash = new double[]{ 10.0, 5.0 };
dr.cr.SetDash(llDash, 0);
dr.cr.MoveTo(x, y1);
dr.cr.LineTo(x, y0);
dr.cr.Stroke();
dr.cr.Restore();
foreach(CanvasItem item in boxes)
{
item.Draw(dr.cr);
}
}
}
List<CanvasItem> items;
List<LifeLine> lifeLines;
public void Render()
{
items = new List<CanvasItem>();
lifeLines = new List<LifeLine>();
Dictionary<string, LifeLine> llDict = new Dictionary<string, LifeLine>();
double x_where = 50.5;
double y_step = 25;
y_current = 0;
width = 0;
height = 0;
foreach(ElemObj elem in diagram.elements)
{
TextBox tb = new TextBox();
tb.YTop = 10.5;
tb.XCenter = x_where;
x_where += 100;
width += 100;
tb.Width = 70;
tb.BorderWidth = 3;
tb.Text = elem.text;
tb.Layout(cr);
items.Add(tb);
LifeLine ll = new LifeLine(elem.label, this, tb.XCenter, tb.Y1);
lifeLines.Add(ll);
llDict.Add(elem.label, ll);
if(tb.Y1 > YCurrent)
{
YCurrent = tb.Y1;
}
}
foreach(Step step in diagram.sequence)
{
if(step.amount == 0)
{
step.amount = (int)y_step;
}
YCurrent += step.amount;
foreach(LifeLine ll in lifeLines)
{
ll.BeforeActivations();
}
foreach(Activation a in step.activations)
{
LifeLine ll = llDict[a.label];
if(a.on)
{
ll.Activate();
}
else
{
ll.Deactivate();
}
}
foreach(LifeLine ll in lifeLines)
{
ll.AfterActivations();
}
foreach(Arrow arrow in step.arrows)
{
TextArrow ta;
LifeLine to = llDict[arrow.to];
LifeLine from = llDict[arrow.from];
if(to != from)
{
ta = new TextArrow();
bool right = to.X > from.X;
ta.Y0 = YCurrent;
if(right)
{
ta.X0 = from.XRightAfter;
ta.X1 = to.XLeftAfter;
ta.XText = from.XClearRight;
}
else
{
ta.X0 = from.XLeftAfter;
ta.X1 = to.XRightAfter;
ta.XText = from.XClearLeft;
}
}
else
{
SelfTextArrow sta = new SelfTextArrow();
sta.Y0 = YCurrent - 10;
sta.Y1 = YCurrent;
sta.X0 = from.XRightBefore;
sta.X0b = from.XRightAfter;
sta.X1 = from.XRightBefore + 30;
sta.XText = from.XClearRight;
ta = sta;
}
ta.Text = arrow.text;
ta.ArrowKind = arrow.type;
ta.Layout(cr);
items.Add(ta);
}
}
foreach(LifeLine ll in lifeLines)
{
ll.End();
}
height = YCurrent;
}
public void Draw()
{
foreach(LifeLine ll in lifeLines)
{
ll.Draw();
}
foreach(CanvasItem item in items)
{
item.Draw(cr);
}
}
}