Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Diagram elements/Connections/Connection.cs @ 4019

Last change on this file since 4019 was 3175, checked in by mkommend, 14 years ago

corrected bug in connection hit test (ticket #867)

File size: 11.8 KB
Line 
1using System;
2using System.Drawing;
3using System.Drawing.Drawing2D;
4namespace Netron.Diagramming.Core
5{
6  /// <summary>
7  /// Represents the connection between two connectors
8  /// </summary>
9  public sealed partial class Connection : ConnectionBase
10    {
11
12
13        // ------------------------------------------------------------------
14        /// <summary>
15        /// Implementation of IVersion - the current version of
16        /// Connection.
17        /// </summary>
18        // ------------------------------------------------------------------
19        private const double connectionVersion = 1.0;
20
21        #region Hack for the caps
22        private const float capslength = 0.01F;
23        private const float standardsshift = 7F;
24        private const float arrowshift = 0.1F;
25        /// <summary>
26        /// the ration between the arrow width to the line width
27        /// </summary>
28        private const float capsratio = 5.5F;
29        private const float generalizationration = 2.2F;
30        private float capsshift;       
31        private Pen leftPen;
32        private Pen rightPen;
33        private PointF unitvector;
34       
35        #endregion
36
37        #region Properties
38
39        // ------------------------------------------------------------------
40        /// <summary>
41        /// Gets the current version.
42        /// </summary>
43        // ------------------------------------------------------------------
44        public override double Version
45        {
46            get
47            {
48                return connectionVersion;
49            }
50        }
51
52        /// <summary>
53        /// Gets the friendly name of the entity to be displayed in the UI
54        /// </summary>
55        /// <value></value>
56        public override string EntityName
57        {
58            get { return "Default Connection"; }
59        }
60        /// <summary>
61        /// The bounds of the paintable entity
62        /// </summary>
63        /// <value></value>
64        public override Rectangle Rectangle
65        {
66            get
67            {
68                if ( (From == null) || (To == null) )
69                {
70                    return Rectangle.Empty;
71                }
72                return Rectangle.FromLTRB(
73                    Math.Min(From.Point.X, To.Point.X),
74                    Math.Min(From.Point.Y, To.Point.Y),
75                    Math.Max(From.Point.X, To.Point.X),
76                    Math.Max(From.Point.Y, To.Point.Y));
77            }
78        }
79       
80        #endregion
81
82    #region Constructor
83
84        /// <summary>
85        /// Constructs a connection between the two given points
86        /// </summary>
87        /// <param name="mFrom">the starting point of the connection</param>
88        /// <param name="mTo">the end-point of the connection</param>
89        /// <param name="model">The model.</param>
90    public Connection(Point mFrom, Point mTo, IModel model):base(model)
91    {
92      this.From = new Connector(mFrom, model);
93      this.From.Name = "From";
94      this.From.Parent = this;
95      this.To = new Connector(mTo, model);
96      this.To.Name = "To";
97      this.To.Parent = this;
98            PenStyle = ArtPalette.GetDefaultPenStyle();
99    }
100
101        /// <summary>
102        /// Initializes a new instance of the <see cref="T:Connection"/> class.
103        /// </summary>
104        /// <param name="from">From.</param>
105        /// <param name="to">To.</param>
106        public Connection(Point from, Point to)
107            : base(from, to)
108        {
109        }
110
111        public Connection() : base(new Point(10,10), new Point(20,20)) {
112       
113        }
114    #endregion
115
116    #region Methods
117
118    /// <summary>
119    /// Paints the connection on the canvas.
120    /// </summary>
121    /// <param name="g"></param>
122    public override void Paint(Graphics g)
123    { 
124            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
125            base.Paint(g);
126
127            if (Hovered)
128            {
129                g.DrawLine(ArtPalette.HighlightPen, From.Point, To.Point);
130            }
131            else
132            {
133                g.DrawLine(mPenStyle.DrawingPen(), From.Point, To.Point);
134            }
135
136            if(ArtPalette.EnableShadows)
137                g.DrawLine(ArtPalette.ConnectionShadow, From.Point.X + 5, From.Point.Y + 5, To.Point.X + 5, To.Point.Y + 5);
138
139            if (leftPen != null)
140            {
141                g.DrawLine(leftPen, From.Point.X + capsshift * unitvector.X, From.Point.Y + capsshift * unitvector.Y, From.Point.X + (capsshift + capslength) * unitvector.X, From.Point.Y + (capsshift + capslength) * unitvector.Y);
142            }
143            if (rightPen != null)
144            {
145                g.DrawLine(rightPen, To.Point.X - (capsshift + capslength) * unitvector.X, To.Point.Y - (capsshift + capslength) * unitvector.Y, To.Point.X - capsshift * unitvector.X, To.Point.Y - capsshift * unitvector.Y);
146            }
147    }
148    /// <summary>
149    /// Invalidates the connection
150    /// </summary>
151    public override void Invalidate()
152    {
153
154            float x = 0, y = 0;
155            try
156            {
157                if (To == null || From == null) return;
158                double length = Math.Sqrt((To.Point.X - From.Point.X) * (To.Point.X - From.Point.X) + (To.Point.Y - From.Point.Y) * (To.Point.Y - From.Point.Y));
159                x = Convert.ToSingle(Convert.ToDouble(To.Point.X - From.Point.X) / length);
160                y = Convert.ToSingle(Convert.ToDouble(To.Point.Y - From.Point.Y) / length);
161            }
162            catch (OverflowException exc)
163            {
164                throw new InconsistencyException("So, you tried to shrink the connection too much...", exc);
165            }
166            unitvector = new PointF(x, y);
167            /* the old way
168      Rectangle f = new Rectangle(From.Point,new Size(10,10));
169      Rectangle t = new Rectangle(To.Point,new Size(10,10));
170      this.Invalidate(Rectangle.Union(f,t));
171             */
172            base.Invalidate();
173
174    }
175
176    /// <summary>
177    /// Tests if the mouse hits this connection
178    /// </summary>
179    /// <param name="p"></param>
180    /// <returns></returns>
181    public override bool Hit(Point p)
182    {
183      Point p1,p2, s;
184      RectangleF r1, r2;
185      float o,u;
186      p1 = From.Point; p2 = To.Point;
187 
188      // p1 must be the leftmost point.
189      if (p1.X > p2.X) { s = p2; p2 = p1; p1 = s; }
190
191      r1 = new RectangleF(p1.X, p1.Y, 0, 0);
192      r2 = new RectangleF(p2.X, p2.Y, 0, 0);
193      r1.Inflate(3, 3);
194      r2.Inflate(3, 3);
195      if (RectangleF.Union(r1, r2).Contains(p))
196      {
197        PointF connectionVector = new PointF(p2.X - p1.X, p2.Y - p1.Y);
198        PointF normalVector = new PointF(connectionVector.Y, connectionVector.X * -1);
199        PointF pointVector = new PointF(p.X - p1.X, p.Y - p1.Y);
200
201        double normalVectorLength = Math.Sqrt(normalVector.X * normalVector.X + normalVector.Y * normalVector.Y);
202        double distance = Math.Abs(pointVector.X * normalVector.X + pointVector.Y * normalVector.Y) / normalVectorLength;
203
204        return distance < 5;
205      }
206      return false;
207    }
208
209    /// <summary>
210    /// Moves the connection with the given shift
211    /// </summary>
212    /// <param name="p"></param>
213    public override void MoveBy(Point p)
214    {
215            if (From.AttachedTo != null || To.AttachedTo != null) return;
216
217            Rectangle rec = this.Rectangle;
218            rec.Inflate(20, 20);
219            this.From.MoveBy(p);
220            this.To.MoveBy(p);           
221            this.Invalidate();
222            this.Invalidate(rec);
223    }
224        /// <summary>
225        /// Updates pens and brushes.
226        /// <remarks>The .Net API allows you to simply set caps but the visual results are less than satisfactory, to say the least. So, there is
227        /// a hack here (unfortunately) which amounts to use two pen for one connection; one pen for the line and one for the (custom) cap. This is
228        /// the easiest way to have visible arrow which otherwise is miniaturistic. There is also a custom shift of the caps since the location is sometime
229        /// inappropriate; the arrow is drawn with the tip at the end of the connection while the diamond or circle caps are drawn with their center at the connection
230        /// end. So, altogether a lot of tweaking and I really find it regrettable that the out-of-the-box caps are not what they should be (besides some obvious bugs like
231        /// the 'not implemented' one if you try to fill a custom cap...).
232        /// </remarks>
233        /// </summary>
234        protected override void UpdatePaintingMaterial()
235        {
236            base.UpdatePaintingMaterial();
237            #region Hack
238            //see the code comments of the LinePenStyle to understand the problem and this hack
239            if (this.PenStyle is LinePenStyle)
240            {
241               
242                LinePenStyle lp = PenStyle as LinePenStyle;
243                if (lp.StartCap == System.Drawing.Drawing2D.LineCap.NoAnchor)
244                {                     
245                    leftPen = null;
246                }
247                else
248                {
249                   
250
251                    if (lp.StartCap == System.Drawing.Drawing2D.LineCap.Custom)
252                    {
253                        //leftPen.StartCap = System.Drawing.Drawing2D.LineCap.Custom;
254                        //AdjustableArrowCap ccap = new AdjustableArrowCap(lp.Width+2, lp.Width+2, true);
255                        //leftPen.CustomStartCap = ccap;
256                        leftPen = new Pen(lp.Color, lp.Width * generalizationration);
257                        leftPen.CustomStartCap = LinePenStyle.GenerallizationCap; //change to something like lp.CustomStartCap if you have more than one custom cap
258                        capsshift = standardsshift;
259                    }
260                    else if (lp.StartCap == LineCap.ArrowAnchor)
261                    {
262                        leftPen = new Pen(lp.Color, lp.Width * capsratio);
263                        leftPen.StartCap = lp.StartCap;
264                        capsshift = arrowshift;
265                    }
266                    else
267                    {
268                        leftPen = new Pen(lp.Color, lp.Width * capsratio);
269                        leftPen.StartCap = lp.StartCap;
270                        capsshift = standardsshift;
271                    }
272                }
273
274                if (lp.EndCap == System.Drawing.Drawing2D.LineCap.NoAnchor)
275                {
276                    rightPen = null;
277                }
278                else
279                {
280                   
281                    if (lp.EndCap == System.Drawing.Drawing2D.LineCap.Custom)
282                    {
283                        //leftPen.StartCap = System.Drawing.Drawing2D.LineCap.Custom;
284                        //AdjustableArrowCap ccap = new AdjustableArrowCap(lp.Width+2, lp.Width+2, true);
285                        //leftPen.CustomStartCap = ccap;
286                        //rightPen = new Pen(lp.Color, lp.Width * generalizationration);                       
287                        //rightPen.CustomEndCap = lp.CustomEndCap;
288                        //capsshift = standardsshift;
289                        Pen.CustomEndCap = LinePenStyle.GenerallizationCap;
290                    }
291
292                    else if (lp.EndCap == LineCap.ArrowAnchor)
293                    {
294                        rightPen = new Pen(lp.Color, lp.Width * capsratio);
295                        rightPen.EndCap = lp.EndCap;
296                        capsshift = arrowshift;
297                    }
298                    else
299                    {
300                        rightPen = new Pen(lp.Color, lp.Width * capsratio);
301                        rightPen.EndCap = lp.EndCap;
302                        capsshift = standardsshift;
303                    }
304                }
305
306            }
307            #endregion
308        }         
309
310         
311       
312    #endregion
313
314  }
315}
Note: See TracBrowser for help on using the repository browser.