-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathConnection.cs
More file actions
1231 lines (1076 loc) · 34.5 KB
/
Connection.cs
File metadata and controls
1231 lines (1076 loc) · 34.5 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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using System.Security;
using System.Security.Permissions;
using System.ComponentModel.Design;
using System.Drawing.Design;
using Netron.GraphLib.Interfaces;
using Netron.GraphLib.Configuration;
using Netron.GraphLib.Attributes;
namespace Netron.GraphLib
{
/// <summary>
/// The connection class captures a connection between two connectors and is derived from an entity.
/// </summary>
[Netron.GraphLib.Attributes.NetronGraphConnection("Default","A0BDE8D6-A95C-45ec-8081-A28E487F38A6", "Netron.GraphLib.Connection")]
[Serializable] public class Connection : Entity, IConnection, ISerializable
{
#region Fields
private bool mBoxedLabel;
/// <summary>
/// Holds the polyline data
/// </summary>
private ArrayList mInsertionPoints;
/// <summary>
/// default line width
/// </summary>
private float mLineWidth = 1F;
/// <summary>
/// default rest length of the connection
/// </summary>
private int mRestLength = 100;
/// <summary>
/// This is a public floating point assigned by the canvascontrol in the MouseMove and
/// MouseDown events. It makes it possible mTo show a drawn line before there is an actual link between
/// two connectors.
/// </summary>
private PointF mToPoint;
/// <summary>
/// The starting connector
/// </summary>
private Connector mFrom ;
/// <summary>
/// the destination connector
/// </summary>
private Connector mTo;
/// <summary>
/// The line color
/// </summary>
private Color mLineColor=Color.Black;
/// <summary>
/// The line style (Solid, Dashed...)
/// </summary>
private DashStyle mLineStyle=DashStyle.Solid;
/// <summary>
/// The line weight; thin, medium or fat. Could be set mTo arbitrary size.
/// </summary>
private ConnectionWeight mLineWeight = ConnectionWeight.Thin;
/// <summary>
/// The type of arrow or line end
/// </summary>
private ConnectionEnd mLineEnd=ConnectionEnd.NoEnds;
/// <summary>
/// the shape of the connection
/// </summary>
private string mLinePath = "Default";
/// <summary>
/// the painter class used to paint the connection
/// </summary>
private ConnectionPainter mPainter;
/// <summary>
/// Tracker used for the connection
/// </summary>
private Tracker mTracker;
/// <summary>
/// the z-order of the connection
/// </summary>
private int mZOrder;
#endregion
#region Properties
/// <summary>
/// Gets or sets the z-order of the shape
/// </summary>
[GraphMLData]public virtual int ZOrder
{
get{return mZOrder;}
set
{
mZOrder = value;
Site.Abstract.SortPaintables();
this.Invalidate();
}
}
/// <summary>
/// Gets the connection painter
/// </summary>
protected internal ConnectionPainter ConnectionPainter
{
get{return mPainter;}
}
/// <summary>
/// Gets or sets the additional set of points along the connection
/// </summary>
internal ArrayList InsertionPoints
{
get{return mInsertionPoints;}
set{mInsertionPoints = value;}
}
/// <summary>
/// Gets or sets the font to be used for drawing text
///
/// </summary>
/// <remarks>Redefines the Font property of the Entity class as public <see cref="Netron.GraphLib.Entity"/></remarks>
public new Font Font
{
get{return base.Font;}
set{base.Font = value;}
}
/// <summary>
/// Gets or sets whether the label is shown as a tooltip
/// </summary>
public bool BoxedLabel
{
get{return mBoxedLabel;}
set{mBoxedLabel = value;}
}
/// <summary>
/// Puts the connection in a layer
/// </summary>
/// <param name="layer"></param>
protected override void SetLayer(GraphLayer layer)
{
base.SetLayer (layer);
if(layer==Site.Abstract.DefaultLayer)
{
LineColor = Color.FromArgb(255,mLineColor);
}
else
{
if(Layer.UseColor)
LineColor = Color.FromArgb((int) (Layer.Opacity*255f/100),Layer.LayerColor);
else
LineColor = Color.FromArgb((int) (Layer.Opacity*255f/100),mLineColor);
}
}
/// <summary>
/// Gets the ConnectionPainter object for this connection
/// </summary>
protected internal ConnectionPainter Painter
{
get{return mPainter;}
}
/// <summary>
/// Gets or sets whether the connection is selected
/// </summary>
public override bool IsSelected
{
get { return base.IsSelected; }
set
{
base.IsSelected = value;
if(this.mLinePath == "Bezier")
{
mPainter.Selected = value;
mTracker = new BezierTracker(this);
}
else
mTracker = new ConnectionTracker(mInsertionPoints,true);
Invalidate();
}
}
/// <summary>
/// Gets or set the tracker associated with the connection
/// </summary>
public override Tracker Tracker
{
get { return mTracker; }
set { mTracker = value; }
}
/// <summary>
/// Gets or sets the type of connection or shape of the path.
/// </summary>
[GraphMLData]public string LinePath
{
get{return mLinePath;}
[Browsable(false)] set
{
mLinePath = value;
switch(value)
{
case "Bezier":
//this now creates a Bezier curve with the given points and a default set of tangent handles
mPainter = new BezierPainter(this);
mTracker = new BezierTracker(this);
break;
case "Default":
mPainter = new DefaultPainter(this);
mTracker = new ConnectionTracker(mInsertionPoints,true);
break;
case "Rectangular":
mPainter = new RectangularPainter(this);
mTracker = new ConnectionTracker(mInsertionPoints,true);
break;
default:
mTracker = new ConnectionTracker(mInsertionPoints,true);
//we have a custom connection
ConnectionSummary consum = this.Site.Libraries.GetConnectionSummary(value);
if(consum==null)
{
Trace.WriteLine("Couldn't find the custom connection called '" + value +"'", "Connection.LinePath");
}
else
{
try
{
//TODO: add some .Net security checks here to make sure no malicious code will harm your computer
Directory.SetCurrentDirectory(Path.GetDirectoryName(Application.ExecutablePath));//reset the dir since the Dialogs can change the CurrentDirectory!
Assembly ass = Assembly.LoadFrom(consum.LibPath);
//Directory.SetCurrentDirectory(Path.GetDirectoryName(Application.ExecutablePath));
//handle = ass.CreateInstance(consum.ReflectionName,true,BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance , null, new object[]{this}, null, new object[]{}) as ObjectHandle;
mPainter = ass.CreateInstance(consum.ReflectionName,true,BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance , null, new object[]{this}, null, new object[]{}) as ConnectionPainter;
if(mPainter==null)
{
mPainter = new DefaultPainter(this);
Trace.WriteLine("Couldn't instantiate the painter called '" + value + "'", "Connection.LinePath");
}
//Trace.WriteLine(o.ToString());
//handle = Activator.CreateInstance(consum.LibPath,consum.ReflectionName,ins);
//mPainter = handle.Unwrap() as ConnectionPainter;
}
catch(Exception exc)
{
//TODO: catch a more specific exception here
Trace.WriteLine(exc.Message, "Connection.LinePath");
}
//shape.Site = this;
//mTracker = new ConnectionTracker(mInsertionPoints,true);
}
break;
}
Invalidate();
}
}
/// <summary>
/// Gets or sets the rest length of the connection (used by the layour algorithms)
/// </summary>
public int RestLength
{
get{return mRestLength;}
[Browsable(false)] set{mRestLength = value;}
}
/// <summary>
/// Returns the length of the connection
/// </summary>
public double Length
{
get
{
PointF s = From.BelongsTo.ConnectionPoint(From); //start point
PointF e = (To != null) ? To.BelongsTo.ConnectionPoint(To) : mToPoint; //endpoint
return Math.Sqrt((s.X-e.X)*(s.X-e.X) + (s.Y-e.Y)*(s.Y-e.Y));
}
}
/// <summary>
/// Gets the rectangle corresponding mTo or embedding the connection
/// </summary>
public SizeF ConnectionSize
{
get
{
SizeF ret=new SizeF(0,0);
try
{
PointF s = From.BelongsTo.ConnectionPoint(From); //start point
PointF e = (To != null) ? To.BelongsTo.ConnectionPoint(To) : mToPoint; //endpoint
RectangleF sr=new RectangleF(s,new SizeF(0,0));
RectangleF er=new RectangleF(e,new SizeF(0,0));
RectangleF rec= RectangleF.Union(sr,er);
ret= rec.Size;
}
catch (Exception exc)
{
//TODO: catch a more specific excpetion here
Trace.WriteLine(exc.Message, "Connection.ConnectionSize");
}
catch
{
Trace.WriteLine("Non-CLS exception caught.", "Connection.ConnectionSize");
}
return ret;
}
}
/// <summary>
/// Gets or sets the line style
/// </summary>
public DashStyle LineStyle
{
get{return mLineStyle;}
[Browsable(false)] set
{
mLineStyle = value;
Pen.Color = mLineColor;
Pen.Width = mLineWidth;
Pen.DashStyle = mLineStyle;
this.Invalidate();
}
}
/// <summary>
/// Gets or sets the line end
/// </summary>
public ConnectionEnd LineEnd
{
get{return mLineEnd;}
set{mLineEnd=value;}
}
/// <summary>
/// Gets or sets the temporary To point when drawing and connecting mTo a To connector.
/// Holds normally the mouse coordinate.
/// </summary>
public PointF ToPoint
{
get{return mToPoint;}
set{mToPoint =value;}
}
/// <summary>
/// Gets or sets the line color
/// </summary>
[GraphMLData] public Color LineColor
{
get{return mLineColor;}
set
{
mLineColor = value;
Pen.Color = value;
mPainter.Pen = Pen;
this.Invalidate();
}
}
/// <summary>
/// Gets or sets the line weight
/// </summary>
[GraphMLData]public ConnectionWeight LineWeight
{
get{return mLineWeight;}
set
{
mLineWeight = value;
switch (mLineWeight)
{
case ConnectionWeight.Thin:
mLineWidth= 1F;break;
case ConnectionWeight.Medium:
mLineWidth= 2F;break;
case ConnectionWeight.Fat:
mLineWidth= 3F;break;
}
Pen.Width = mLineWidth;
this.Invalidate();
}
}
/// <summary>
/// Gets or sets where the connection originates
/// </summary>
public Connector From
{
get{return mFrom;}
set{mFrom = value;}
}
/// <summary>
/// Gets or sets where the connection ends
/// </summary>
public Connector To
{
get{return mTo;}
set
{
mTo = value;
}
}
#endregion
#region Constructors
/// <summary>
/// Default constructor, assigns null connectors and so a null connection
/// </summary>
public Connection()
{
InitConnection();
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="site"></param>
public Connection(IGraphSite site): base(site)
{
InitConnection();
}
/// <summary>
/// Deserialization constructor
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
protected Connection(SerializationInfo info, StreamingContext context) : base(info, context)
{
this.InitConnection();
//attaching the connection to shape connector occurs in the BinarySerializer class;
//instantiate the connectors to keep the UID references until then
this.mTo = new Connector(info.GetString("mTo.UID"));
this.mFrom = new Connector(info.GetString("mFrom.UID"));
this.mLineColor = (Color) info.GetValue("mLineColor", typeof(Color));
this.mLineEnd = (ConnectionEnd) info.GetValue("mLineEnd",typeof(ConnectionEnd));
this.mLineWidth = info.GetInt32("mLineWidth");
mLinePath = info.GetString("mLinePath");
if(mLinePath=="Bezier")
{
mPainter = info.GetValue("mPainter", typeof(BezierPainter)) as BezierPainter;
}
mInsertionPoints = (ArrayList) info.GetValue("mInsertionPoints", typeof(ArrayList));
this.Tag = info.GetString("mLayer"); //layer is set in the post-deserialization
try
{
this.mZOrder = info.GetInt32("mZOrder");
}
catch
{
this.mZOrder = 0;
}
}
/// <summary>
/// Additional actions after deserialization
/// </summary>
public override void PostDeserialization()
{
base.PostDeserialization ();
/* not the easiest thing to do, but anyway; here's the (de)serialization of Bezier connections.
*This is different than the other connections because the this connection depends on additional objects for its shape and drawing:
* handles and tangent to manipulate the curvature.
*/
if(mLinePath=="Bezier")
{
//the painter is deserialized in the constructor
if(mPainter==null)//making the best of it if things fail
{
LinePath = "Default";
}
mPainter.Connection = this;
//the postdeserialization is necessary because some stuff requires the mPainter.Connection to be set
(mPainter as BezierPainter).PostDeserialization();
mTracker = new BezierTracker(this);
}
else
LinePath = mLinePath;//weird, but that's how to combine the ISerializable mechanism with custom conections
if(Tag!=null && typeof(string).IsInstanceOfType(Tag))
{
SetLayer((string) Tag);
Tag = null; //be nice to the host/user
}
}
#endregion
#region Methods
/// <summary>
/// Adds an intermediate connection point to the connection
/// </summary>
/// <param name="point"></param>
public void AddConnectionPoint(PointF point)
{
this.mInsertionPoints.Add(point);
//the test is necessary if the developer doesn't implement a separate painter but paints in this class (which is bad bad)
if(mPainter!=null) mPainter.AddConnectionPoint(point);
}
/// <summary>
/// Removes a connection point from the connection
/// </summary>
/// <param name="point"></param>
public void RemoveConnectionPoint(PointF point)
{
RectangleF r = new RectangleF(point.X,point.Y,0,0);
RectangleF s;
for(int m=0;m<this.mInsertionPoints.Count;m++)
{
s = new RectangleF((PointF) mInsertionPoints[m],new SizeF(50,50));
s.Offset(-25,-25);//center around the point
if(s.Contains(r))
{
mInsertionPoints.RemoveAt(m);
mPainter.RemoveConnectionPoint(point);
return;
}
}
}
/// <summary>
/// Common constructors initialization
/// </summary>
public void InitConnection()
{
//From = null;
//To = null;
this.ShowLabel = false;
Pen = BlackPen;
mPainter = new DefaultPainter(this);
mInsertionPoints = new ArrayList();
mTracker = new ConnectionTracker(mInsertionPoints,true);
}
/// <summary>
/// Returns wether or not the given rectangle is contained in the object
/// </summary>
/// <param name="r"></param>
/// <returns></returns>
public override bool Hit(RectangleF r)
{
if ((From == null) || (To == null)) return false;
PointF p1 = From.AdjacentPoint;
PointF p2 = To.AdjacentPoint;
if (((int)r.Width == 0) && ((int)r.Height == 0))
{
PointF p = r.Location;
if(mPainter.Selected)
{
return mTracker.Hit(p)!=Point.Empty || mPainter.Hit(p);
}
return mPainter.Hit(p);
}
return (r.Contains(p1) && r.Contains(p2));
}
/// <summary>
/// Returns the points of the connection
/// </summary>
/// <returns>An array of PointF structs</returns>
public virtual PointF[] GetConnectionPoints()
{
if(From==null) return null;
PointF[] points = new PointF[4+mInsertionPoints.Count];
PointF s = From.Location;
PointF e = (To != null) ? To.Location : mToPoint;
points[0] = s; //the From connector
//if there an adjacent point, use it
if(mFrom.ConnectorLocation == ConnectorLocation.Unknown)
points[1] = s;
else
points[1] = mFrom.AdjacentPoint;
//loop over the intermediate points
for(int k=0; k<mInsertionPoints.Count; k++)
{
points[2+k] =(PointF) mInsertionPoints[k];
}
//use the adjacent point of the To connector
if(mTo==null || mTo.ConnectorLocation == ConnectorLocation.Unknown)
points[4+mInsertionPoints.Count-2] = e;
else
points[4+mInsertionPoints.Count-2] = mTo.AdjacentPoint;
//the To connector
points[4+mInsertionPoints.Count-1] = e;
return points;
}
/// <summary>
/// The painting of the connection
/// </summary>
/// <param name="g">The graphics object</param>
public override void Paint(Graphics g)
{
if(!From.BelongsTo.Layer.Visible || !To.BelongsTo.Layer.Visible) return;
PaintPolyLine(g);
if (!IsHovered) //the mouse is NOT over this connection
{
if(this.ShowLabel) PaintLabel(g);
}
}
/// <summary>
/// Paints the label
/// </summary>
/// <param name="g"></param>
public virtual void PaintLabel(Graphics g)
{
try
{
RectangleF r = RectangleF.Union(this.mFrom.ConnectionGrip(),this.mTo.ConnectionGrip());
Size s = g.MeasureString(this.Text, Font).ToSize();
RectangleF a = RectangleF.Empty;
switch(this.mLinePath)
{
case "Default":
a = new RectangleF(r.X+r.Width/2 , r.Y + r.Height/2 + 6, s.Width, s.Height + 1);
break;
case "Rectangular":
switch(this.mFrom.ConnectorLocation)
{
case ConnectorLocation.South: case ConnectorLocation.North:
a=new RectangleF(mFrom.BelongsTo.ConnectionPoint(mFrom).X+5,mTo.BelongsTo.ConnectionPoint(mTo).Y+5,s.Width,s.Height+1);
break;
case ConnectorLocation.East: case ConnectorLocation.West:
a=new RectangleF(mTo.BelongsTo.ConnectionPoint(mTo).X+5,mFrom.BelongsTo.ConnectionPoint(mFrom).Y+5,s.Width,s.Height+1);
break;
}
break;
}
RectangleF b = a;
a.Inflate(+3, +2);
if(mBoxedLabel)
{
g.FillRectangle(new SolidBrush(Color.FromArgb(255, 255, 231)), a);
g.DrawRectangle(new Pen(Color.Black, 1), Rectangle.Round(a));
}
g.DrawString(this.Text, Font, new SolidBrush(Color.Black), b.Location);
}
catch(Exception exc)
{
//TODO: catch a more specific excpetion here
Trace.WriteLine(exc.Message, "Connection.PaintLabel");
}
catch
{
Trace.WriteLine("Non-CLS exception caught.","Connection.PantLabel");
}
}
/// <summary>
/// Paints the tracker object
/// </summary>
/// <param name="g"></param>
protected internal void PaintTrack(Graphics g)
{
//pen = blackpen;
// p.DashStyle = DashStyle.Dash;
PaintPolyLine(g);
}
/// <summary>
/// Draws a line between the To and From connectors
/// </summary>
/// <param name="g">The graphics</param>
protected void PaintPolyLine(Graphics g)
{
//style
if (IsSelected)
Pen.DashStyle = DashStyle.Dash;
else
{
if(mLineStyle != DashStyle.Custom)
Pen.DashStyle=mLineStyle;
else
Pen.DashStyle=DashStyle.Solid;
}
if (From == null) return;
PointF s = From.BelongsTo.ConnectionPoint(From);
PointF e = (To != null) ? To.BelongsTo.ConnectionPoint(To) : mToPoint;
//this is for the Omni connector, a central connector
double len = Math.Sqrt((e.X - s.X) * (e.X - s.X) + (e.Y - s.Y) * (e.Y - s.Y));
if (len > 0 && To != null && this.To.ConnectorLocation == ConnectorLocation.Omni)
{
e.X -= Convert.ToSingle((e.X - s.X) * To.ConnectionShift / len);
e.Y -= Convert.ToSingle((e.Y - s.Y) * To.ConnectionShift / len);
}
if (len > 0 && From != null && this.From.ConnectorLocation == ConnectorLocation.Omni)
{
s.X -= Convert.ToSingle((e.X - s.X) * 10 / len);
s.Y -= Convert.ToSingle((e.Y - s.Y) * 10 / len);
}
//now, for the normal connectors
if ((s.X != e.X) || (s.Y != e.Y))
{
mPainter.IsHovered = IsHovered;
switch(this.mLinePath)
{
case "Default":
{
mPainter.Points = this.GetConnectionPoints(); //update points to reflect the user actions/motions
break;
}
case "Bezier":
{
if(mFrom.ConnectorLocation == ConnectorLocation.Unknown)
(mPainter as BezierPainter).Handles[0].ChangeLocation(s);
else
(mPainter as BezierPainter).Handles[0].ChangeLocation(mFrom.AdjacentPoint);
if(mTo==null || mTo.ConnectorLocation == ConnectorLocation.Unknown)
(mPainter as BezierPainter).Handles[(mPainter as BezierPainter).Handles.Count-1].ChangeLocation(e);
else
(mPainter as BezierPainter).Handles[(mPainter as BezierPainter).Handles.Count-1].ChangeLocation(mTo.AdjacentPoint);
break;
}
case "Rectangular":
{
mPainter.Points = GetConnectionPoints(); //update points to reflect the user actions/motions
break;
}
default:
mPainter.Points = GetConnectionPoints();
break;
}
mPainter.Paint(g);
if(IsHovered || IsSelected) this.mTracker.Paint(g);
}
PointF left = PointF.Empty, right = PointF.Empty;
#region the end arrow
if (LineEnd==ConnectionEnd.BothFilledArrow ||
LineEnd==ConnectionEnd.BothOpenArrow ||
LineEnd==ConnectionEnd.RightFilledArrow ||
LineEnd==ConnectionEnd.RightOpenArrow)
{
switch(this.mTo.ConnectorLocation)
{
case ConnectorLocation.North:
left= new PointF(e.X+4,e.Y-7);
right = new PointF(e.X-4, e.Y-7); break;
case ConnectorLocation.South:
left= new PointF(e.X-4,e.Y+7);
right = new PointF(e.X+4, e.Y+7); break;
case ConnectorLocation.West:
left= new PointF(e.X-7,e.Y-4);
right = new PointF(e.X-7, e.Y+4); break;
case ConnectorLocation.East:
left= new PointF(e.X+7,e.Y+4);
right = new PointF(e.X+7, e.Y-4); break;
case ConnectorLocation.Unknown:
return;
}
if(LineEnd==ConnectionEnd.RightFilledArrow || LineEnd==ConnectionEnd.BothFilledArrow)
PaintArrow(g,e,left,right,true);
else
PaintArrow(g,e,left,right,false);
//the omni arrow is a bit more difficult
if(this.mTo.ConnectorLocation==ConnectorLocation.Omni)
if(LineEnd==ConnectionEnd.RightFilledArrow || LineEnd==ConnectionEnd.BothFilledArrow)
PaintArrow(g,s,e,mLineColor,true,false);
else
PaintArrow(g,s,e,mLineColor,false,false);
}
#endregion
#region the start or From arrow
if (LineEnd==ConnectionEnd.BothFilledArrow ||
LineEnd==ConnectionEnd.BothOpenArrow ||
LineEnd==ConnectionEnd.LeftFilledArrow ||
LineEnd==ConnectionEnd.LeftOpenArrow)
{
switch(this.mFrom.ConnectorLocation)
{
case ConnectorLocation.North:
left= new PointF(s.X+4,s.Y-7);
right = new PointF(s.X-4, s.Y-7); break;
case ConnectorLocation.South:
left= new PointF(s.X-4,s.Y+7);
right = new PointF(s.X+4, s.Y+7); break;
case ConnectorLocation.West:
left= new PointF(s.X-7,s.Y-4);
right = new PointF(s.X-7, s.Y+4); break;
case ConnectorLocation.East:
left= new PointF(s.X+7,s.Y+4);
right = new PointF(s.X+7, s.Y-4); break;
case ConnectorLocation.Unknown:
return;
}
if(LineEnd==ConnectionEnd.LeftFilledArrow || LineEnd==ConnectionEnd.BothFilledArrow)
PaintArrow(g,s,left,right,true);
else
PaintArrow(g,s,left,right,false);
//the omni arrow is a bit more difficult
if(this.mTo.ConnectorLocation==ConnectorLocation.Omni)
if(LineEnd==ConnectionEnd.LeftFilledArrow || LineEnd==ConnectionEnd.BothFilledArrow)
PaintArrow(g,e,s,mLineColor,true,false);
else
PaintArrow(g,e,s,mLineColor,false,false);
}
#endregion
}
/// <summary>
/// Paints an arrow
/// </summary>
/// <param name="g"></param>
/// <param name="tip"></param>
/// <param name="left"></param>
/// <param name="right"></param>
/// <param name="filled"></param>
internal protected void PaintArrow(Graphics g, PointF tip, PointF left,PointF right,bool filled)
{
try
{
SolidBrush brush=new SolidBrush(mLineColor);
PointF[] points={left, tip, right};
if (filled)
g.FillPolygon(brush,points);
else
{
Pen p=new Pen(brush);
switch(mLineWeight)
{
case ConnectionWeight.Thin:
p.Width=1F;break;
case ConnectionWeight.Medium:
p.Width=1.5F;break;
case ConnectionWeight.Fat:
p.Width=2F;break;
}
g.DrawLines(p,points);
}
}
catch(Exception exc)
{
//TODO: catch a more specific excpetion here
Trace.WriteLine(exc.Message,"Connection.PaintArrow");
}
catch
{
Trace.WriteLine("Non-CLS exception caught.","Connection.PaintArrow");
}
}
/// <summary>
/// Paints an arrow
/// </summary>
/// <param name="endPoint"></param>
/// <param name="filled"></param>
/// <param name="g"></param>
/// <param name="lineColor"></param>
/// <param name="showLabel"></param>
/// <param name="startPoint"></param>
protected void PaintArrow(Graphics g, PointF startPoint, PointF endPoint,Color lineColor, bool filled, bool showLabel)
{
try
{
//g.DrawLine(new Pen(lineColor,1F),startPoint,endPoint);
SolidBrush brush=new SolidBrush(lineColor);
double angle = Math.Atan2(endPoint.Y - startPoint.Y,endPoint.X-startPoint.X);
double length = Math.Sqrt((endPoint.X - startPoint.X)*(endPoint.X - startPoint.X)+(endPoint.Y - startPoint.Y)*(endPoint.Y - startPoint.Y))-10;
double delta = Math.Atan2(7,length);
PointF left = new PointF(Convert.ToSingle(startPoint.X + length*Math.Cos(angle-delta)),Convert.ToSingle(startPoint.Y+length*Math.Sin(angle-delta)));
PointF right = new PointF(Convert.ToSingle(startPoint.X+length*Math.Cos(angle+delta)),Convert.ToSingle(startPoint.Y+length*Math.Sin(angle+delta)));
PointF[] points={left, endPoint, right};
if (filled)
g.FillPolygon(brush,points);
else
{
Pen p=new Pen(brush,1F);
g.DrawLines(p,points);
}
}
catch(Exception exc)
{
//TODO: catch a more specific excpetion here
Trace.WriteLine(exc.Message, "Connection.PaintArrow");
}
catch
{
Trace.WriteLine("Non-CLS exception caught.","Connection.PaintArrow");
}
}
/// <summary>
/// Overrides the invalidate (refresh)
/// </summary>
/// <remarks>
/// Note the smart way to invalidate the region arround the start and end points
/// by inflating the endpoints and then to make a union rectangle
/// </remarks>
public override void Invalidate()
{
if (From != null && From.BelongsTo!= null && To !=null && To.BelongsTo!=null)
{
PointF s = From.BelongsTo.ConnectionPoint(From); //start point
PointF e = (To != null) ? To.BelongsTo.ConnectionPoint(To) : mToPoint; //endpoint
RectangleF sr = new RectangleF(s.X, s.Y, 0, 0);
RectangleF er = new RectangleF(e.X, e.Y, 0, 0);
sr.Inflate(+4, +4);
er.Inflate(+4, +4);
RectangleF sum = RectangleF.Union(sr, er);
foreach( PointF p in mInsertionPoints )
{
RectangleF pr = new RectangleF(p.X, p.Y, 0, 0);
// Point size is determined by tracker object.
//pr.Inflate(ShapeTracker.HandleWidth/2+1, ShapeTracker.HandleHeight/2+1);
sum = RectangleF.Union(pr, sum);
}
// Invalidate the bounding rectangle
if ((From.BelongsTo != null) && (From.BelongsTo.Site != null))
{
Rectangle r = Rectangle.Round(sum);