CraftSynth.ImageEditor – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Collections; |
||
3 | using System.Drawing; |
||
4 | using System.Drawing.Drawing2D; |
||
5 | using System.Globalization; |
||
6 | using System.Runtime.Serialization; |
||
7 | using System.Windows.Forms; |
||
8 | |||
9 | namespace CraftSynth.ImageEditor |
||
10 | { |
||
11 | /// <summary> |
||
12 | /// Polygon graphic object |
||
13 | /// </summary> |
||
14 | //[Serializable] |
||
15 | public class DrawPolygon : DrawLine |
||
16 | { |
||
17 | private ArrayList pointArray; // list of points |
||
18 | private Cursor handleCursor; |
||
19 | |||
20 | private const string entryLength = "Length"; |
||
21 | private const string entryPoint = "Point"; |
||
22 | |||
23 | private bool _disposed; |
||
24 | |||
25 | |||
26 | public DrawPolygon() |
||
27 | { |
||
28 | pointArray = new ArrayList(); |
||
29 | |||
30 | LoadCursor(); |
||
31 | Initialize(); |
||
32 | } |
||
33 | |||
34 | #region Destruction |
||
35 | protected override void Dispose(bool disposing) |
||
36 | { |
||
37 | if (!this._disposed) |
||
38 | { |
||
39 | if (disposing) |
||
40 | { |
||
41 | // Free any managed objects here. |
||
42 | if (this.handleCursor != null) |
||
43 | { |
||
44 | this.handleCursor.Dispose(); |
||
45 | } |
||
46 | this._disposed = true; |
||
47 | } |
||
48 | |||
49 | // Free any unmanaged objects here. |
||
50 | |||
51 | this._disposed = true; |
||
52 | } |
||
53 | base.Dispose(disposing); |
||
54 | } |
||
55 | |||
56 | ~DrawPolygon() |
||
57 | { |
||
58 | this.Dispose(false); |
||
59 | } |
||
60 | #endregion |
||
61 | |||
62 | public DrawPolygon(int x1, int y1, int x2, int y2, Color lineColor, int lineWidth, DrawingPens.PenType penType, LineCap endCap) |
||
63 | { |
||
64 | pointArray = new ArrayList(); |
||
65 | pointArray.Add(new Point(x1, y1)); |
||
66 | pointArray.Add(new Point(x2, y2)); |
||
67 | Color = lineColor; |
||
68 | PenWidth = lineWidth; |
||
69 | PenType = penType; |
||
70 | EndCap = endCap; |
||
71 | |||
72 | LoadCursor(); |
||
73 | Initialize(); |
||
74 | } |
||
75 | |||
76 | /// <summary> |
||
77 | /// Clone this instance |
||
78 | /// </summary> |
||
79 | public override DrawObject Clone() |
||
80 | { |
||
81 | DrawPolygon drawPolygon = new DrawPolygon(); |
||
82 | |||
83 | foreach (Point p in pointArray) |
||
84 | { |
||
85 | drawPolygon.pointArray.Add(p); |
||
86 | } |
||
87 | |||
88 | FillDrawObjectFields(drawPolygon); |
||
89 | return drawPolygon; |
||
90 | } |
||
91 | |||
92 | public override void Draw(Graphics g) |
||
93 | { |
||
94 | g.SmoothingMode = SmoothingMode.AntiAlias; |
||
95 | Pen pen; |
||
96 | |||
97 | if (DrawPen == null) |
||
98 | { |
||
99 | pen = new Pen(Color, PenWidth); |
||
100 | DrawingPens.SetCurrentPen(ref pen, PenType, EndCap); |
||
101 | } |
||
102 | else |
||
103 | pen = DrawPen.Clone() as Pen; |
||
104 | |||
105 | // Convert the array of points to a GraphicsPath object so lines are mitered correctly at the intersections |
||
106 | // (not to mention the object is drawn faster then drawing individual lines) |
||
107 | Point[] pts = new Point[pointArray.Count]; |
||
108 | for (int i = 0; i < pointArray.Count; i++) |
||
109 | { |
||
110 | Point px = (Point)pointArray[i]; |
||
111 | pts[i] = px; |
||
112 | } |
||
113 | byte[] types = new byte[pointArray.Count]; |
||
114 | for (int i = 0; i < pointArray.Count; i++) |
||
115 | types[i] = (byte)PathPointType.Line; |
||
116 | GraphicsPath gp = new GraphicsPath(pts, types); |
||
117 | |||
118 | // Rotate the path about it's center if necessary |
||
119 | if (Rotation != 0) |
||
120 | { |
||
121 | RectangleF pathBounds = gp.GetBounds(); |
||
122 | Matrix m = new Matrix(); |
||
123 | m.RotateAt(Rotation, new PointF(pathBounds.Left + (pathBounds.Width / 2), pathBounds.Top + (pathBounds.Height / 2)), MatrixOrder.Append); |
||
124 | gp.Transform(m); |
||
125 | } |
||
126 | g.DrawPath(pen, gp); |
||
127 | //g.DrawCurve(pen, pts); |
||
128 | // |
||
129 | //// For DrawBeziers() to work, the pts array must have a minimum of 4 points. |
||
130 | //// The pts array may have more than 4 points, but if so, then after the first 4 points, remaining points must be in sets of 3 for the call to work. |
||
131 | //// The following code will adjust the pts array to properly fit these requirements. |
||
132 | //int numPoints = pts.Length; |
||
133 | //if (numPoints - 4 <= 0) |
||
134 | //{ |
||
135 | // // Cannot call DrawBeziers() so return, drawing nothing. |
||
136 | // gp.Dispose(); |
||
137 | // pen.Dispose(); |
||
138 | // return; |
||
139 | //} |
||
140 | //while ((numPoints - 4) % 3 != 0 && numPoints - 4 > 0) |
||
141 | //{ |
||
142 | // // Chop off the last point from the pts array |
||
143 | // numPoints--; |
||
144 | // Array.Resize(ref pts, numPoints); |
||
145 | //} |
||
146 | //g.DrawBeziers(pen, pts); |
||
147 | gp.Dispose(); |
||
148 | if (pen != null) |
||
149 | pen.Dispose(); |
||
150 | } |
||
151 | |||
152 | public void AddPoint(Point point) |
||
153 | { |
||
154 | pointArray.Add(point); |
||
155 | } |
||
156 | |||
157 | public override int HandleCount |
||
158 | { |
||
159 | get { return pointArray.Count; } |
||
160 | } |
||
161 | |||
162 | /// <summary> |
||
163 | /// Get handle point by 1-based number |
||
164 | /// </summary> |
||
165 | /// <param name="handleNumber"></param> |
||
166 | /// <returns></returns> |
||
167 | public override Point GetHandle(int handleNumber) |
||
168 | { |
||
169 | if (handleNumber < 1) |
||
170 | handleNumber = 1; |
||
171 | |||
172 | if (handleNumber > pointArray.Count) |
||
173 | handleNumber = pointArray.Count; |
||
174 | |||
175 | return ((Point)pointArray[handleNumber - 1]); |
||
176 | } |
||
177 | |||
178 | public override Cursor GetHandleCursor(int handleNumber) |
||
179 | { |
||
180 | return handleCursor; |
||
181 | } |
||
182 | |||
183 | public override void MoveHandleTo(Point point, int handleNumber) |
||
184 | { |
||
185 | if (handleNumber < 1) |
||
186 | handleNumber = 1; |
||
187 | |||
188 | if (handleNumber > pointArray.Count) |
||
189 | handleNumber = pointArray.Count; |
||
190 | |||
191 | pointArray[handleNumber - 1] = point; |
||
192 | Dirty = true; |
||
193 | Invalidate(); |
||
194 | } |
||
195 | |||
196 | public override void Move(int deltaX, int deltaY) |
||
197 | { |
||
198 | int n = pointArray.Count; |
||
199 | |||
200 | for (int i = 0; i < n; i++) |
||
201 | { |
||
202 | Point point; |
||
203 | point = new Point(((Point)pointArray[i]).X + deltaX, ((Point)pointArray[i]).Y + deltaY); |
||
204 | |||
205 | pointArray[i] = point; |
||
206 | } |
||
207 | Dirty = true; |
||
208 | Invalidate(); |
||
209 | } |
||
210 | |||
211 | public override void SaveToStream(SerializationInfo info, int orderNumber, int objectIndex) |
||
212 | { |
||
213 | info.AddValue( |
||
214 | String.Format(CultureInfo.InvariantCulture, |
||
215 | "{0}{1}-{2}", |
||
216 | entryLength, orderNumber, objectIndex), |
||
217 | pointArray.Count); |
||
218 | |||
219 | int i = 0; |
||
220 | foreach (Point p in pointArray) |
||
221 | { |
||
222 | info.AddValue( |
||
223 | String.Format(CultureInfo.InvariantCulture, |
||
224 | "{0}{1}-{2}-{3}", |
||
225 | new object[] {entryPoint, orderNumber, objectIndex, i++}), |
||
226 | p); |
||
227 | } |
||
228 | base.SaveToStream(info, orderNumber, objectIndex); |
||
229 | } |
||
230 | |||
231 | public override void LoadFromStream(SerializationInfo info, int orderNumber, int objectIndex) |
||
232 | { |
||
233 | int n = info.GetInt32( |
||
234 | String.Format(CultureInfo.InvariantCulture, |
||
235 | "{0}{1}-{2}", |
||
236 | entryLength, orderNumber, objectIndex)); |
||
237 | |||
238 | for (int i = 0; i < n; i++) |
||
239 | { |
||
240 | Point point; |
||
241 | point = (Point)info.GetValue( |
||
242 | String.Format(CultureInfo.InvariantCulture, |
||
243 | "{0}{1}-{2}-{3}", |
||
244 | new object[] {entryPoint, orderNumber, objectIndex, i}), |
||
245 | typeof (Point)); |
||
246 | |||
247 | pointArray.Add(point); |
||
248 | } |
||
249 | base.LoadFromStream(info, orderNumber, objectIndex); |
||
250 | } |
||
251 | |||
252 | /// <summary> |
||
253 | /// Create graphic object used for hit test |
||
254 | /// </summary> |
||
255 | protected override void CreateObjects() |
||
256 | { |
||
257 | if (AreaPath != null) |
||
258 | return; |
||
259 | |||
260 | // Create closed path which contains all polygon vertexes |
||
261 | AreaPath = new GraphicsPath(); |
||
262 | |||
263 | int x1 = 0, y1 = 0; // previous point |
||
264 | |||
265 | IEnumerator enumerator = pointArray.GetEnumerator(); |
||
266 | |||
267 | if (enumerator.MoveNext()) |
||
268 | { |
||
269 | x1 = ((Point)enumerator.Current).X; |
||
270 | y1 = ((Point)enumerator.Current).Y; |
||
271 | } |
||
272 | |||
273 | while (enumerator.MoveNext()) |
||
274 | { |
||
275 | int x2, y2; // current point |
||
276 | x2 = ((Point)enumerator.Current).X; |
||
277 | y2 = ((Point)enumerator.Current).Y; |
||
278 | |||
279 | AreaPath.AddLine(x1, y1, x2, y2); |
||
280 | |||
281 | x1 = x2; |
||
282 | y1 = y2; |
||
283 | } |
||
284 | |||
285 | AreaPath.CloseFigure(); |
||
286 | |||
287 | // Create region from the path |
||
288 | AreaRegion = new Region(AreaPath); |
||
289 | } |
||
290 | |||
291 | private void LoadCursor() |
||
292 | { |
||
293 | handleCursor = new Cursor(GetType(), "PolyHandle.cur"); |
||
294 | } |
||
295 | } |
||
296 | } |