CraftSynth.ImageEditor – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System.Drawing;
2 using System.Windows.Forms;
3  
4 namespace CraftSynth.ImageEditor
5 {
6 /// <summary>
7 /// Connector tool (a Connector is a series of connected straight lines where each line is drawn individually and at least one of the ends is anchored to another object)
8 /// </summary>
9 internal class ToolConnector : ToolObject
10 {
11 public ToolConnector()
12 {
13 Cursor = new Cursor(GetType(), "Pencil.cur");
14 }
15  
16 private DrawConnector newConnector;
17 private bool _drawingInProcess = false; // Set to true when drawing
18  
19 /// <summary>
20 /// Left mouse button is pressed
21 /// </summary>
22 /// <param name="drawArea"></param>
23 /// <param name="e"></param>
24 public override void OnMouseDown(DrawArea drawArea, MouseEventArgs e)
25 {
26 if (e.Button == MouseButtons.Right)
27 {
28 _drawingInProcess = false;
29 newConnector = null;
30 } else
31 {
32 Point p = drawArea.BackTrackMouse(new Point(e.X, e.Y));
33 int objectID = -1;
34 p = TestForConnection(drawArea, p, out objectID);
35  
36 if (_drawingInProcess == false)
37 {
38 newConnector = new DrawConnector(p.X, p.Y, p.X + 1, p.Y + 1, drawArea.LineColor, drawArea.LineWidth, drawArea.PenType, drawArea.EndCap);
39 newConnector.EndPoint = new Point(p.X + 1, p.Y + 1);
40 if (objectID > -1)
41 {
42 newConnector.StartIsAnchored = true;
43 newConnector.StartObjectId = objectID;
44 }
45 AddNewObject(drawArea, newConnector);
46 _drawingInProcess = true;
47 } else
48 {
49 // Drawing is in process, so simply add a new point
50 newConnector.AddPoint(p);
51 newConnector.EndPoint = p;
52 if (objectID > -1)
53 {
54 newConnector.EndIsAnchored = true;
55 newConnector.EndObjectId = objectID;
56 _drawingInProcess = false;
57 }
58 }
59  
60 }
61 }
62  
63 private static Point TestForConnection(DrawArea drawArea, Point p, out int objectID)
64 {
65 // Determine if within 5 pixels of a connection point
66 // Step 1: see if a 5 x 5 rectangle centered on the mouse cursor intersects with an object
67 // Step 2: If it does, then see if there is a connection point within the rectangle
68 // Step 3: If there is, move the point to the connection point, record the object's id in the connector
69 //
70 objectID = -1;
71 Rectangle testRectangle = new Rectangle(p.X - 2, p.Y - 2, 5, 5);
72 int al = drawArea.TheLayers.ActiveLayerIndex;
73 bool connectionHere = false;
74 Point h = new Point(-1, -1);
75 GraphicsList gl = drawArea.TheLayers[al].Graphics;
76 for (int i = 1; i < gl.Count; i++)
77 {
78 if (gl[i].IntersectsWith(testRectangle))
79 {
80 DrawObject obj = (DrawObject)gl[i];
81 for (int j = 1; j < obj.HandleCount + 1; j++)
82 {
83 h = obj.GetHandle(j);
84 if (testRectangle.Contains(h))
85 {
86 connectionHere = true;
87 p = h;
88 objectID = obj.ID;
89 // obj.DrawConnection(drawArea., j);
90 break;
91 }
92 }
93 }
94 if (connectionHere)
95 break;
96 }
97 return p;
98 }
99  
100 /// <summary>
101 /// Mouse move - resize new polygon
102 /// </summary>
103 /// <param name="drawArea"></param>
104 /// <param name="e"></param>
105 public override void OnMouseMove(DrawArea drawArea, MouseEventArgs e)
106 {
107 drawArea.Cursor = Cursor;
108  
109 if (e.Button !=
110 MouseButtons.Left)
111 return;
112  
113 if (newConnector == null)
114 return; // precaution
115  
116 Point point = drawArea.BackTrackMouse(new Point(e.X, e.Y));
117 int objectID;
118 point = TestForConnection(drawArea, point, out objectID);
119 // move last point
120 newConnector.MoveHandleTo(point, newConnector.HandleCount);
121 drawArea.Refresh();
122 if (objectID > -1)
123 {
124 newConnector.EndIsAnchored = true;
125 newConnector.EndObjectId = objectID;
126 _drawingInProcess = false;
127 }
128 }
129  
130 #region Destruction
131 private bool _disposed = false;
132  
133 protected override void Dispose(bool disposing)
134 {
135 if (!this._disposed)
136 {
137 if (disposing)
138 {
139 // Free any managed objects here.
140 if (this.newConnector != null)
141 {
142 this.newConnector.Dispose();
143 }
144 }
145  
146 // Free any unmanaged objects here.
147  
148 this._disposed = true;
149 }
150 base.Dispose(disposing);
151 }
152  
153 ~ToolConnector()
154 {
155 this.Dispose(false);
156 }
157 #endregion
158 }
159 }