[2768] | 1 | using System;
|
---|
| 2 | using System.Drawing;
|
---|
| 3 | using System.Drawing.Drawing2D;
|
---|
| 4 | namespace 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 | // ------------------------------------------------------------------
|
---|
[3038] | 19 | private const double connectionVersion = 1.0;
|
---|
[2768] | 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 | //this is like a topological neighborhood
|
---|
| 196 | //the connection is shifted left and right
|
---|
| 197 | //and the point under consideration has to be in between.
|
---|
| 198 | if (RectangleF.Union(r1, r2).Contains(p))
|
---|
| 199 | {
|
---|
[3174] | 200 | //if (p1.Y < p2.Y) //SWNE
|
---|
| 201 | //{
|
---|
| 202 | // o = r1.Left + (((r2.Left - r1.Left) * (p.Y - r1.Bottom)) / (r2.Bottom - r1.Bottom));
|
---|
| 203 | // u = r1.Right + (((r2.Right - r1.Right) * (p.Y - r1.Top)) / (r2.Top - r1.Top));
|
---|
| 204 | // return ((p.X > o) && (p.X < u));
|
---|
| 205 | //} else //NWSE
|
---|
| 206 | //{
|
---|
| 207 | // o = r1.Left + (((r2.Left - r1.Left) * (p.Y - r1.Top)) / (r2.Top - r1.Top));
|
---|
| 208 | // u = r1.Right + (((r2.Right - r1.Right) * (p.Y - r1.Bottom)) / (r2.Bottom - r1.Bottom));
|
---|
| 209 | // return ((p.X > o) && (p.X < u));
|
---|
| 210 | //}
|
---|
| 211 | return true;
|
---|
[2768] | 212 | }
|
---|
| 213 | return false;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | /// <summary>
|
---|
| 217 | /// Moves the connection with the given shift
|
---|
| 218 | /// </summary>
|
---|
| 219 | /// <param name="p"></param>
|
---|
| 220 | public override void MoveBy(Point p)
|
---|
| 221 | {
|
---|
| 222 | if (From.AttachedTo != null || To.AttachedTo != null) return;
|
---|
| 223 |
|
---|
| 224 | Rectangle rec = this.Rectangle;
|
---|
| 225 | rec.Inflate(20, 20);
|
---|
| 226 | this.From.MoveBy(p);
|
---|
| 227 | this.To.MoveBy(p);
|
---|
| 228 | this.Invalidate();
|
---|
| 229 | this.Invalidate(rec);
|
---|
| 230 | }
|
---|
| 231 | /// <summary>
|
---|
| 232 | /// Updates pens and brushes.
|
---|
| 233 | /// <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
|
---|
| 234 | /// 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
|
---|
| 235 | /// 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
|
---|
| 236 | /// 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
|
---|
| 237 | /// 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
|
---|
| 238 | /// the 'not implemented' one if you try to fill a custom cap...).
|
---|
| 239 | /// </remarks>
|
---|
| 240 | /// </summary>
|
---|
| 241 | protected override void UpdatePaintingMaterial()
|
---|
| 242 | {
|
---|
| 243 | base.UpdatePaintingMaterial();
|
---|
| 244 | #region Hack
|
---|
| 245 | //see the code comments of the LinePenStyle to understand the problem and this hack
|
---|
| 246 | if (this.PenStyle is LinePenStyle)
|
---|
| 247 | {
|
---|
| 248 |
|
---|
| 249 | LinePenStyle lp = PenStyle as LinePenStyle;
|
---|
| 250 | if (lp.StartCap == System.Drawing.Drawing2D.LineCap.NoAnchor)
|
---|
| 251 | {
|
---|
| 252 | leftPen = null;
|
---|
| 253 | }
|
---|
| 254 | else
|
---|
| 255 | {
|
---|
| 256 |
|
---|
| 257 |
|
---|
| 258 | if (lp.StartCap == System.Drawing.Drawing2D.LineCap.Custom)
|
---|
| 259 | {
|
---|
| 260 | //leftPen.StartCap = System.Drawing.Drawing2D.LineCap.Custom;
|
---|
| 261 | //AdjustableArrowCap ccap = new AdjustableArrowCap(lp.Width+2, lp.Width+2, true);
|
---|
| 262 | //leftPen.CustomStartCap = ccap;
|
---|
| 263 | leftPen = new Pen(lp.Color, lp.Width * generalizationration);
|
---|
| 264 | leftPen.CustomStartCap = LinePenStyle.GenerallizationCap; //change to something like lp.CustomStartCap if you have more than one custom cap
|
---|
| 265 | capsshift = standardsshift;
|
---|
| 266 | }
|
---|
| 267 | else if (lp.StartCap == LineCap.ArrowAnchor)
|
---|
| 268 | {
|
---|
| 269 | leftPen = new Pen(lp.Color, lp.Width * capsratio);
|
---|
| 270 | leftPen.StartCap = lp.StartCap;
|
---|
| 271 | capsshift = arrowshift;
|
---|
| 272 | }
|
---|
| 273 | else
|
---|
| 274 | {
|
---|
| 275 | leftPen = new Pen(lp.Color, lp.Width * capsratio);
|
---|
| 276 | leftPen.StartCap = lp.StartCap;
|
---|
| 277 | capsshift = standardsshift;
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | if (lp.EndCap == System.Drawing.Drawing2D.LineCap.NoAnchor)
|
---|
| 282 | {
|
---|
| 283 | rightPen = null;
|
---|
| 284 | }
|
---|
| 285 | else
|
---|
| 286 | {
|
---|
| 287 |
|
---|
| 288 | if (lp.EndCap == System.Drawing.Drawing2D.LineCap.Custom)
|
---|
| 289 | {
|
---|
| 290 | //leftPen.StartCap = System.Drawing.Drawing2D.LineCap.Custom;
|
---|
| 291 | //AdjustableArrowCap ccap = new AdjustableArrowCap(lp.Width+2, lp.Width+2, true);
|
---|
| 292 | //leftPen.CustomStartCap = ccap;
|
---|
| 293 | //rightPen = new Pen(lp.Color, lp.Width * generalizationration);
|
---|
| 294 | //rightPen.CustomEndCap = lp.CustomEndCap;
|
---|
| 295 | //capsshift = standardsshift;
|
---|
| 296 | Pen.CustomEndCap = LinePenStyle.GenerallizationCap;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | else if (lp.EndCap == LineCap.ArrowAnchor)
|
---|
| 300 | {
|
---|
| 301 | rightPen = new Pen(lp.Color, lp.Width * capsratio);
|
---|
| 302 | rightPen.EndCap = lp.EndCap;
|
---|
| 303 | capsshift = arrowshift;
|
---|
| 304 | }
|
---|
| 305 | else
|
---|
| 306 | {
|
---|
| 307 | rightPen = new Pen(lp.Color, lp.Width * capsratio);
|
---|
| 308 | rightPen.EndCap = lp.EndCap;
|
---|
| 309 | capsshift = standardsshift;
|
---|
| 310 | }
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | }
|
---|
| 314 | #endregion
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 |
|
---|
| 318 |
|
---|
| 319 | #endregion
|
---|
| 320 |
|
---|
| 321 | }
|
---|
| 322 | }
|
---|