CraftSynth.ImageEditor – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Diagnostics; |
||
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 | /// Rectangle graphic object |
||
13 | /// </summary> |
||
14 | [Serializable] |
||
15 | public class DrawRectangle : DrawObject |
||
16 | { |
||
17 | private Rectangle rectangle; |
||
18 | |||
19 | private const string entryRectangle = "Rect"; |
||
20 | |||
21 | protected Rectangle Rectangle |
||
22 | { |
||
23 | get { return rectangle; } |
||
24 | set { rectangle = value; } |
||
25 | } |
||
26 | |||
27 | /// <summary> |
||
28 | /// Clone this instance |
||
29 | /// </summary> |
||
30 | public override DrawObject Clone() |
||
31 | { |
||
32 | DrawRectangle drawRectangle = new DrawRectangle(); |
||
33 | drawRectangle.rectangle = rectangle; |
||
34 | |||
35 | FillDrawObjectFields(drawRectangle); |
||
36 | return drawRectangle; |
||
37 | } |
||
38 | |||
39 | public DrawRectangle() |
||
40 | { |
||
41 | SetRectangle(0, 0, 1, 1); |
||
42 | } |
||
43 | |||
44 | #region Destruction |
||
45 | private bool _disposed = false; |
||
46 | |||
47 | protected override void Dispose(bool disposing) |
||
48 | { |
||
49 | if (!this._disposed) |
||
50 | { |
||
51 | if (disposing) |
||
52 | { |
||
53 | // Free any managed objects here. |
||
54 | } |
||
55 | |||
56 | // Free any unmanaged objects here. |
||
57 | |||
58 | this._disposed = true; |
||
59 | } |
||
60 | base.Dispose(disposing); |
||
61 | } |
||
62 | |||
63 | ~DrawRectangle() |
||
64 | { |
||
65 | this.Dispose(false); |
||
66 | } |
||
67 | #endregion |
||
68 | |||
69 | public DrawRectangle(int x, int y, int width, int height, Color lineColor, Color fillColor, bool filled, int lineWidth, DrawingPens.PenType penType, LineCap endCap) |
||
70 | { |
||
71 | Center = new Point(x + (width / 2), y + (height / 2)); |
||
72 | rectangle.X = x; |
||
73 | rectangle.Y = y; |
||
74 | rectangle.Width = width; |
||
75 | rectangle.Height = height; |
||
76 | Color = lineColor; |
||
77 | FillColor = fillColor; |
||
78 | Filled = filled; |
||
79 | PenWidth = lineWidth; |
||
80 | EndCap = endCap; |
||
81 | PenType = penType; |
||
82 | TipText = String.Format("Rectangle Center @ {0}, {1}", Center.X, Center.Y); |
||
83 | } |
||
84 | |||
85 | /// <summary> |
||
86 | /// Draw rectangle |
||
87 | /// </summary> |
||
88 | /// <param name="g"></param> |
||
89 | public override void Draw(Graphics g) |
||
90 | { |
||
91 | Pen pen; |
||
92 | Brush b = new SolidBrush(FillColor); |
||
93 | |||
94 | if (DrawPen == null) |
||
95 | { |
||
96 | pen = new Pen(Color, PenWidth); |
||
97 | DrawingPens.SetCurrentPen(ref pen, PenType, EndCap); |
||
98 | } |
||
99 | else |
||
100 | pen = (Pen) DrawPen.Clone(); |
||
101 | |||
102 | GraphicsPath gp = new GraphicsPath(); |
||
103 | gp.AddRectangle(GetNormalizedRectangle(Rectangle)); |
||
104 | // Rotate the path about it's center if necessary |
||
105 | if (Rotation != 0) |
||
106 | { |
||
107 | RectangleF pathBounds = gp.GetBounds(); |
||
108 | Matrix m = new Matrix(); |
||
109 | m.RotateAt(Rotation, new PointF(pathBounds.Left + (pathBounds.Width / 2), pathBounds.Top + (pathBounds.Height / 2)), MatrixOrder.Append); |
||
110 | gp.Transform(m); |
||
111 | } |
||
112 | |||
113 | if (Filled) g.FillPath(b, gp); |
||
114 | g.DrawPath(pen, gp); |
||
115 | |||
116 | gp.Dispose(); |
||
117 | pen.Dispose(); |
||
118 | b.Dispose(); |
||
119 | } |
||
120 | |||
121 | protected void SetRectangle(int x, int y, int width, int height) |
||
122 | { |
||
123 | rectangle.X = x; |
||
124 | rectangle.Y = y; |
||
125 | rectangle.Width = width; |
||
126 | rectangle.Height = height; |
||
127 | } |
||
128 | |||
129 | /// <summary> |
||
130 | /// Get number of handles |
||
131 | /// </summary> |
||
132 | public override int HandleCount |
||
133 | { |
||
134 | get { return 8; } |
||
135 | } |
||
136 | /// <summary> |
||
137 | /// Get number of connection points |
||
138 | /// </summary> |
||
139 | public override int ConnectionCount |
||
140 | { |
||
141 | get { return HandleCount; } |
||
142 | } |
||
143 | public override Point GetConnection(int connectionNumber) |
||
144 | { |
||
145 | return GetHandle(connectionNumber); |
||
146 | } |
||
147 | /// <summary> |
||
148 | /// Get handle point by 1-based number |
||
149 | /// </summary> |
||
150 | /// <param name="handleNumber"></param> |
||
151 | /// <returns></returns> |
||
152 | public override Point GetHandle(int handleNumber) |
||
153 | { |
||
154 | int x, y, xCenter, yCenter; |
||
155 | |||
156 | xCenter = rectangle.X + rectangle.Width / 2; |
||
157 | yCenter = rectangle.Y + rectangle.Height / 2; |
||
158 | x = rectangle.X; |
||
159 | y = rectangle.Y; |
||
160 | |||
161 | switch (handleNumber) |
||
162 | { |
||
163 | case 1: |
||
164 | x = rectangle.X; |
||
165 | y = rectangle.Y; |
||
166 | break; |
||
167 | case 2: |
||
168 | x = xCenter; |
||
169 | y = rectangle.Y; |
||
170 | break; |
||
171 | case 3: |
||
172 | x = rectangle.Right; |
||
173 | y = rectangle.Y; |
||
174 | break; |
||
175 | case 4: |
||
176 | x = rectangle.Right; |
||
177 | y = yCenter; |
||
178 | break; |
||
179 | case 5: |
||
180 | x = rectangle.Right; |
||
181 | y = rectangle.Bottom; |
||
182 | break; |
||
183 | case 6: |
||
184 | x = xCenter; |
||
185 | y = rectangle.Bottom; |
||
186 | break; |
||
187 | case 7: |
||
188 | x = rectangle.X; |
||
189 | y = rectangle.Bottom; |
||
190 | break; |
||
191 | case 8: |
||
192 | x = rectangle.X; |
||
193 | y = yCenter; |
||
194 | break; |
||
195 | } |
||
196 | return new Point(x, y); |
||
197 | } |
||
198 | |||
199 | /// <summary> |
||
200 | /// Hit test. |
||
201 | /// Return value: -1 - no hit |
||
202 | /// 0 - hit anywhere |
||
203 | /// > 1 - handle number |
||
204 | /// </summary> |
||
205 | /// <param name="point"></param> |
||
206 | /// <returns></returns> |
||
207 | public override int HitTest(Point point) |
||
208 | { |
||
209 | if (Selected) |
||
210 | { |
||
211 | for (int i = 1; i <= HandleCount; i++) |
||
212 | { |
||
213 | if (GetHandleRectangle(i).Contains(point)) |
||
214 | return i; |
||
215 | } |
||
216 | } |
||
217 | |||
218 | if (PointInObject(point)) |
||
219 | return 0; |
||
220 | return -1; |
||
221 | } |
||
222 | |||
223 | protected override bool PointInObject(Point point) |
||
224 | { |
||
225 | return rectangle.Contains(point); |
||
226 | } |
||
227 | |||
228 | public override Rectangle GetBounds(Graphics g) |
||
229 | { |
||
230 | return rectangle; |
||
231 | } |
||
232 | |||
233 | /// <summary> |
||
234 | /// Get cursor for the handle |
||
235 | /// </summary> |
||
236 | /// <param name="handleNumber"></param> |
||
237 | /// <returns></returns> |
||
238 | public override Cursor GetHandleCursor(int handleNumber) |
||
239 | { |
||
240 | switch (handleNumber) |
||
241 | { |
||
242 | case 1: |
||
243 | return Cursors.SizeNWSE; |
||
244 | case 2: |
||
245 | return Cursors.SizeNS; |
||
246 | case 3: |
||
247 | return Cursors.SizeNESW; |
||
248 | case 4: |
||
249 | return Cursors.SizeWE; |
||
250 | case 5: |
||
251 | return Cursors.SizeNWSE; |
||
252 | case 6: |
||
253 | return Cursors.SizeNS; |
||
254 | case 7: |
||
255 | return Cursors.SizeNESW; |
||
256 | case 8: |
||
257 | return Cursors.SizeWE; |
||
258 | default: |
||
259 | return Cursors.Default; |
||
260 | } |
||
261 | } |
||
262 | |||
263 | /// <summary> |
||
264 | /// Move handle to new point (resizing) |
||
265 | /// </summary> |
||
266 | /// <param name="point"></param> |
||
267 | /// <param name="handleNumber"></param> |
||
268 | public override void MoveHandleTo(Point point, int handleNumber) |
||
269 | { |
||
270 | int left = Rectangle.Left; |
||
271 | int top = Rectangle.Top; |
||
272 | int right = Rectangle.Right; |
||
273 | int bottom = Rectangle.Bottom; |
||
274 | |||
275 | switch (handleNumber) |
||
276 | { |
||
277 | case 1: |
||
278 | left = point.X; |
||
279 | top = point.Y; |
||
280 | break; |
||
281 | case 2: |
||
282 | top = point.Y; |
||
283 | break; |
||
284 | case 3: |
||
285 | right = point.X; |
||
286 | top = point.Y; |
||
287 | break; |
||
288 | case 4: |
||
289 | right = point.X; |
||
290 | break; |
||
291 | case 5: |
||
292 | right = point.X; |
||
293 | bottom = point.Y; |
||
294 | break; |
||
295 | case 6: |
||
296 | bottom = point.Y; |
||
297 | break; |
||
298 | case 7: |
||
299 | left = point.X; |
||
300 | bottom = point.Y; |
||
301 | break; |
||
302 | case 8: |
||
303 | left = point.X; |
||
304 | break; |
||
305 | } |
||
306 | Dirty = true; |
||
307 | SetRectangle(left, top, right - left, bottom - top); |
||
308 | } |
||
309 | |||
310 | |||
311 | public override bool IntersectsWith(Rectangle rectangle) |
||
312 | { |
||
313 | return Rectangle.IntersectsWith(rectangle); |
||
314 | } |
||
315 | |||
316 | /// <summary> |
||
317 | /// Move object |
||
318 | /// </summary> |
||
319 | /// <param name="deltaX"></param> |
||
320 | /// <param name="deltaY"></param> |
||
321 | public override void Move(int deltaX, int deltaY) |
||
322 | { |
||
323 | rectangle.X += deltaX; |
||
324 | rectangle.Y += deltaY; |
||
325 | Dirty = true; |
||
326 | } |
||
327 | |||
328 | public override void Dump() |
||
329 | { |
||
330 | base.Dump(); |
||
331 | |||
332 | Trace.WriteLine("rectangle.X = " + rectangle.X.ToString(CultureInfo.InvariantCulture)); |
||
333 | Trace.WriteLine("rectangle.Y = " + rectangle.Y.ToString(CultureInfo.InvariantCulture)); |
||
334 | Trace.WriteLine("rectangle.Width = " + rectangle.Width.ToString(CultureInfo.InvariantCulture)); |
||
335 | Trace.WriteLine("rectangle.Height = " + rectangle.Height.ToString(CultureInfo.InvariantCulture)); |
||
336 | } |
||
337 | |||
338 | /// <summary> |
||
339 | /// Normalize rectangle |
||
340 | /// </summary> |
||
341 | public override void Normalize() |
||
342 | { |
||
343 | rectangle = GetNormalizedRectangle(rectangle); |
||
344 | } |
||
345 | |||
346 | /// <summary> |
||
347 | /// Save objevt to serialization stream |
||
348 | /// </summary> |
||
349 | /// <param name="info">Contains all data being written to disk</param> |
||
350 | /// <param name="orderNumber">Index of the Layer being saved</param> |
||
351 | /// <param name="objectIndex">Index of the drawing object in the Layer</param> |
||
352 | public override void SaveToStream(SerializationInfo info, int orderNumber, int objectIndex) |
||
353 | { |
||
354 | info.AddValue( |
||
355 | String.Format(CultureInfo.InvariantCulture, |
||
356 | "{0}{1}-{2}", |
||
357 | entryRectangle, orderNumber, objectIndex), |
||
358 | rectangle); |
||
359 | |||
360 | base.SaveToStream(info, orderNumber, objectIndex); |
||
361 | } |
||
362 | |||
363 | /// <summary> |
||
364 | /// LOad object from serialization stream |
||
365 | /// </summary> |
||
366 | /// <param name="info"></param> |
||
367 | /// <param name="orderNumber"></param> |
||
368 | /// <param name="objectIndex"></param> |
||
369 | public override void LoadFromStream(SerializationInfo info, int orderNumber, int objectIndex) |
||
370 | { |
||
371 | rectangle = (Rectangle)info.GetValue( |
||
372 | String.Format(CultureInfo.InvariantCulture, |
||
373 | "{0}{1}-{2}", |
||
374 | entryRectangle, orderNumber, objectIndex), |
||
375 | typeof(Rectangle)); |
||
376 | |||
377 | base.LoadFromStream(info, orderNumber, objectIndex); |
||
378 | } |
||
379 | |||
380 | #region Helper Functions |
||
381 | public static Rectangle GetNormalizedRectangle(int x1, int y1, int x2, int y2) |
||
382 | { |
||
383 | if (x2 < x1) |
||
384 | { |
||
385 | int tmp = x2; |
||
386 | x2 = x1; |
||
387 | x1 = tmp; |
||
388 | } |
||
389 | |||
390 | if (y2 < y1) |
||
391 | { |
||
392 | int tmp = y2; |
||
393 | y2 = y1; |
||
394 | y1 = tmp; |
||
395 | } |
||
396 | return new Rectangle(x1, y1, x2 - x1, y2 - y1); |
||
397 | } |
||
398 | |||
399 | public static Rectangle GetNormalizedRectangle(Point p1, Point p2) |
||
400 | { |
||
401 | return GetNormalizedRectangle(p1.X, p1.Y, p2.X, p2.Y); |
||
402 | } |
||
403 | |||
404 | public static Rectangle GetNormalizedRectangle(Rectangle r) |
||
405 | { |
||
406 | return GetNormalizedRectangle(r.X, r.Y, r.X + r.Width, r.Y + r.Height); |
||
407 | } |
||
408 | #endregion Helper Functions |
||
409 | } |
||
410 | } |