forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxEdgeStyle.cs
More file actions
471 lines (409 loc) · 15.8 KB
/
Copy pathmxEdgeStyle.cs
File metadata and controls
471 lines (409 loc) · 15.8 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Copyright (c) 2007-2008, Gaudenz Alder
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace com.mxgraph
{
/// <summary>
/// Defines the requirements for an edge style function. At the time
/// the function is called, the result array contains a placeholder (null)
/// for the first absolute point, that is, the point where the edge and
/// source terminal are connected. The implementation of the style then
/// adds all intermediate waypoints except for the last point, that is,
/// the connection point between the edge and the target terminal. The
/// first ant the last point in the result array are then replaced with
/// mxPoints that take into account the terminal's perimeter and next point
/// on the edge.
/// </summary>
/// <param name="state">Cell state that represents the edge to be updated.</param>
/// <param name="source">Cell state that represents the source terminal.</param>
/// <param name="target">Cell state that represents the target terminal.</param>
/// <param name="points">List of relative control points.</param>
/// <param name="result">Array of points that represent the actual points of the
/// edge.</param>
public delegate void mxEdgeStyleFunction(mxCellState state, mxCellState source,
mxCellState target, List<mxPoint> points, List<mxPoint> result);
/// <summary>
/// Provides various edge styles to be used as the values for
/// mxConstants.STYLE_EDGE in a cell style.
/// </summary>
public class mxEdgeStyle
{
/// <summary>
/// Implements an entity relation style for edges (as used in database
/// schema diagrams).
/// </summary>
public static mxEdgeStyleFunction EntityRelation = delegate(
mxCellState state, mxCellState source,
mxCellState target, List<mxPoint> points,
List<mxPoint> result)
{
mxGraphView view = state.View;
mxIGraphModel model = view.Graph.Model;
double segment = mxUtils.GetDouble(state.Style,
mxConstants.STYLE_SEGMENT, mxConstants.ENTITY_SEGMENT) *
state.View.Scale;
mxPoint p0 = state.AbsolutePoints[0];
mxPoint pe = state.AbsolutePoints[state.AbsolutePointCount() - 1];
bool isSourceLeft = false;
if (p0 != null)
{
source = new mxCellState();
source.X = p0.X;
source.Y = p0.Y;
}
else if (source != null)
{
mxGeometry sourceGeometry = model.GetGeometry(source.Cell);
if (sourceGeometry.Relative)
{
isSourceLeft = sourceGeometry.X <= 0.5;
}
else if (target != null)
{
isSourceLeft = target.X + target.Width < source
.X;
}
}
bool isTargetLeft = true;
if (pe != null)
{
target = new mxCellState();
target.X = pe.X;
target.Y = pe.Y;
}
else if (target != null)
{
mxGeometry targetGeometry = model.GetGeometry(target.Cell);
if (targetGeometry.Relative)
{
isTargetLeft = targetGeometry.X <= 0.5;
}
else if (source != null)
{
isTargetLeft = source.X + source.Width < target
.X;
}
}
if (source != null && target != null)
{
double x0 = (isSourceLeft) ? source.X : source.X
+ source.Width;
double y0 = view.GetRoutingCenterY(source);
double xe = (isTargetLeft) ? target.X : target.X
+ target.Width;
double ye = view.GetRoutingCenterY(target);
double seg = segment;
double dx = (isSourceLeft) ? -seg : seg;
mxPoint dep = new mxPoint(x0 + dx, y0);
result.Add(dep);
dx = (isTargetLeft) ? -seg : seg;
mxPoint arr = new mxPoint(xe + dx, ye);
// Adds intermediate points if both go out on same side
if (isSourceLeft == isTargetLeft)
{
double x = (isSourceLeft) ? Math.Min(x0, xe) - segment : Math
.Max(x0, xe)
+ segment;
result.Add(new mxPoint(x, y0));
result.Add(new mxPoint(x, ye));
}
else if ((dep.X < arr.X) == isSourceLeft)
{
double midY = y0 + (ye - y0) / 2;
result.Add(new mxPoint(dep.X, midY));
result.Add(new mxPoint(arr.X, midY));
}
result.Add(arr);
}
};
/// <summary>
/// Implements a self-reference, aka. loop.
/// </summary>
public static mxEdgeStyleFunction Loop = delegate(
mxCellState state, mxCellState source,
mxCellState target, List<mxPoint> points,
List<mxPoint> result)
{
if (source != null)
{
mxGraphView view = state.View;
mxGraph graph = view.Graph;
mxPoint pt = (points != null && points.Count > 0) ? points[0] : null;
if (pt != null)
{
pt = view.TransformControlPoint(state, pt);
if (source.Contains(pt.X, pt.Y))
{
pt = null;
}
}
double x = 0;
double dx = 0;
double y = 0;
double dy = 0;
double seg = mxUtils.GetDouble(state.Style,
mxConstants.STYLE_SEGMENT, graph.GridSize)
* view.Scale;
String dir = mxUtils.GetString(state.Style,
mxConstants.STYLE_DIRECTION,
mxConstants.DIRECTION_WEST);
if (dir.Equals(mxConstants.DIRECTION_NORTH)
|| dir.Equals(mxConstants.DIRECTION_SOUTH))
{
x = view.GetRoutingCenterX(source);
dx = seg;
}
else
{
y = view.GetRoutingCenterY(source);
dy = seg;
}
if (pt == null ||
pt.X < source.X ||
pt.X > source.X + source.Width)
{
if (pt != null)
{
x = pt.X;
dy = Math.Max(Math.Abs(y - pt.Y), dy);
}
else
{
if (dir.Equals(mxConstants.DIRECTION_NORTH))
{
y = source.Y - 2 * dx;
}
else if (dir.Equals(mxConstants.DIRECTION_SOUTH))
{
y = source.Y + source.Height + 2 * dx;
}
else if (dir.Equals(mxConstants.DIRECTION_EAST))
{
x = source.X - 2 * dy;
}
else
{
x = source.X + source.Width + 2 * dy;
}
}
}
else if (pt != null)
{
x = view.GetRoutingCenterX(source);
dx = Math.Max(Math.Abs(x - pt.X), dy);
y = pt.Y;
dy = 0;
}
result.Add(new mxPoint(x - dx, y - dy));
result.Add(new mxPoint(x + dx, y + dy));
}
};
/// <summary>
/// Uses either SideToSide or TopToBottom depending on the horizontal
/// flag in the cell style. SideToSide is used if horizontal is true or
/// unspecified.
/// </summary>
public static mxEdgeStyleFunction ElbowConnector = delegate(
mxCellState state, mxCellState source,
mxCellState target, List<mxPoint> points,
List<mxPoint> result)
{
mxPoint pt = (points != null && points.Count > 0) ? points[0] : null;
bool vertical = false;
bool horizontal = false;
if (source != null && target != null)
{
if (pt != null)
{
double left = Math.Min(source.X, target.X);
double right = Math.Max(source.X + source.Width,
target.X + target.Width);
double top = Math.Min(source.Y, target.Y);
double bottom = Math.Max(source.Y + source.Height,
target.Y + target.Height);
pt = state.View.TransformControlPoint(state, pt);
vertical = pt.Y < top || pt.Y > bottom;
horizontal = pt.X < left || pt.X > right;
}
else
{
double left = Math.Max(source.X, target.X);
double right = Math.Min(source.X + source.Width,
target.X + target.Width);
vertical = left == right;
if (!vertical)
{
double top = Math.Max(source.Y, target.Y);
double bottom = Math.Min(source.Y
+ source.Height, target.Y
+ target.Height);
horizontal = top == bottom;
}
}
}
if (!horizontal && (vertical ||
mxUtils.GetString(state.Style, mxConstants.STYLE_ELBOW, "").Equals(mxConstants.ELBOW_VERTICAL)))
{
mxEdgeStyle.TopToBottom(state, source, target, points, result);
}
else
{
mxEdgeStyle.SideToSide(state, source, target, points, result);
}
};
/// <summary>
/// Implements a vertical elbow edge.
/// </summary>
public static mxEdgeStyleFunction SideToSide = delegate(
mxCellState state, mxCellState source,
mxCellState target, List<mxPoint> points,
List<mxPoint> result)
{
mxGraphView view = state.View;
mxPoint pt = (points != null && points.Count > 0) ? points[0] : null;
mxPoint p0 = state.AbsolutePoints[0];
mxPoint pe = state.AbsolutePoints[state.AbsolutePointCount() - 1];
if (pt != null)
{
pt = view.TransformControlPoint(state, pt);
}
if (p0 != null)
{
source = new mxCellState();
source.X = p0.X;
source.Y = p0.Y;
}
if (pe != null)
{
target = new mxCellState();
target.X = pe.X;
target.Y = pe.Y;
}
if (source != null && target != null)
{
double l = Math.Max(source.X, target.X);
double r = Math.Min(source.X + source.Width,
target.X + target.Width);
double x = (pt != null) ? pt.X : r + (l - r) / 2;
double y1 = view.GetRoutingCenterY(source);
double y2 = view.GetRoutingCenterY(target);
if (pt != null)
{
if (pt.Y >= source.Y &&
pt.Y <= source.Y + source.Height)
{
y1 = pt.Y;
}
if (pt.Y >= target.Y &&
pt.Y <= target.Y + target.Height)
{
y2 = pt.Y;
}
}
if (!target.Contains(x, y1) &&
!source.Contains(x, y1))
{
result.Add(new mxPoint(x, y1));
}
if (!target.Contains(x, y2) &&
!source.Contains(x, y2))
{
result.Add(new mxPoint(x, y2));
}
if (result.Count == 1)
{
if (pt != null)
{
result.Add(new mxPoint(x, pt.Y));
}
else
{
double t = Math.Max(source.Y, target.Y);
double b = Math.Min(source.Y + source.Height,
target.Y + target.Height);
result.Add(new mxPoint(x, t + (b - t) / 2));
}
}
}
};
/// <summary>
/// Implements a horizontal elbow edge.
/// </summary>
public static mxEdgeStyleFunction TopToBottom = delegate(
mxCellState state, mxCellState source,
mxCellState target, List<mxPoint> points,
List<mxPoint> result)
{
mxGraphView view = state.View;
mxPoint pt = (points != null && points.Count > 0) ? points[0] : null;
mxPoint p0 = state.AbsolutePoints[0];
mxPoint pe = state.AbsolutePoints[state.AbsolutePointCount() - 1];
if (pt != null)
{
pt = view.TransformControlPoint(state, pt);
}
if (p0 != null)
{
source = new mxCellState();
source.X = p0.X;
source.Y = p0.Y;
}
if (pe != null)
{
target = new mxCellState();
target.X = pe.X;
target.Y = pe.Y;
}
if (source != null && target != null)
{
double t = Math.Max(source.Y, target.Y);
double b = Math.Min(source.Y + source.Height,
target.Y + target.Height);
double x = view.GetRoutingCenterX(source);
if (pt != null &&
pt.X >= source.X &&
pt.X <= source.X + source.Width)
{
x = pt.X;
}
double y = (pt != null) ? pt.Y : b + (t - b) / 2;
if (!target.Contains(x, y) &&
!source.Contains(x, y))
{
result.Add(new mxPoint(x, y));
}
if (pt != null &&
pt.X >= target.X &&
pt.X <= target.X + target.Width)
{
x = pt.X;
}
else
{
x = view.GetRoutingCenterX(target);
}
if (!target.Contains(x, y) &&
!source.Contains(x, y))
{
result.Add(new mxPoint(x, y));
}
if (result.Count == 1)
{
if (pt != null)
{
result.Add(new mxPoint(pt.X, y));
}
else
{
double l = Math.Max(source.X, target.X);
double r = Math.Min(source.X + source.Width,
target.X + target.Width);
result.Add(new mxPoint(l + (r - l) / 2, y));
}
}
}
};
}
}