1 | using System;
|
---|
2 | using System.Drawing;
|
---|
3 | using System.Windows.Forms;
|
---|
4 |
|
---|
5 | namespace Netron.Diagramming.Core
|
---|
6 | {
|
---|
7 | // ----------------------------------------------------------------------
|
---|
8 | /// <summary>
|
---|
9 | /// Tool to draw rectangles on the canvas.
|
---|
10 | /// </summary>
|
---|
11 | // ----------------------------------------------------------------------
|
---|
12 | public class RectangleTool : AbstractDrawingTool
|
---|
13 | {
|
---|
14 | #region Fields
|
---|
15 |
|
---|
16 | #endregion
|
---|
17 |
|
---|
18 | #region Properties
|
---|
19 |
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | #region Constructor
|
---|
23 | public RectangleTool() : base("Rectangle Tool")
|
---|
24 | {
|
---|
25 | }
|
---|
26 | public RectangleTool(string name): base(name)
|
---|
27 | {
|
---|
28 |
|
---|
29 | }
|
---|
30 | #endregion
|
---|
31 |
|
---|
32 | #region Methods
|
---|
33 |
|
---|
34 | // ------------------------------------------------------------------
|
---|
35 | /// <summary>
|
---|
36 | /// Activates the tool - the cursor is set to the Icons.DrawRectangle.
|
---|
37 | /// </summary>
|
---|
38 | // ------------------------------------------------------------------
|
---|
39 | protected override void OnActivateTool()
|
---|
40 | {
|
---|
41 | base.OnActivateTool();
|
---|
42 | Cursor = CursorPalette.DrawRectangle;
|
---|
43 | }
|
---|
44 |
|
---|
45 | // ------------------------------------------------------------------
|
---|
46 | /// <summary>
|
---|
47 | /// Draws a ghost rectangle on the canvas if 'IsActive' and 'started'
|
---|
48 | /// is true.
|
---|
49 | /// </summary>
|
---|
50 | /// <param name="e">MouseEventArgs</param>
|
---|
51 | // ------------------------------------------------------------------
|
---|
52 | protected override void OnMouseMove(MouseEventArgs e)
|
---|
53 | {
|
---|
54 | base.OnMouseMove(e);
|
---|
55 | if (IsActive && started)
|
---|
56 | {
|
---|
57 | Point point = new Point(e.X, e.Y);
|
---|
58 |
|
---|
59 | Controller.View.PaintGhostRectangle(startingPoint, point);
|
---|
60 |
|
---|
61 | Controller.View.Invalidate(System.Drawing.Rectangle.Inflate(
|
---|
62 | Controller.View.Ghost.Rectangle, 20, 20));
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | // ------------------------------------------------------------------
|
---|
67 | /// <summary>
|
---|
68 | /// This method will be called when the user has finished drawing a
|
---|
69 | /// ghost rectangle or bundle and initiates the actual creation of a
|
---|
70 | /// bundle and the addition to the model via the appropriate command.
|
---|
71 | /// </summary>
|
---|
72 | // ------------------------------------------------------------------
|
---|
73 | protected override void GhostDrawingComplete()
|
---|
74 | {
|
---|
75 | if ((IsActive == false) ||
|
---|
76 | (started == false))
|
---|
77 | {
|
---|
78 | return;
|
---|
79 | }
|
---|
80 |
|
---|
81 | try
|
---|
82 | {
|
---|
83 | SimpleRectangle shape = new SimpleRectangle(this.Controller.Model);
|
---|
84 | shape.Width = (int) Rectangle.Width;
|
---|
85 | shape.Height = (int) Rectangle.Height;
|
---|
86 | AddShapeCommand cmd = new AddShapeCommand(
|
---|
87 | this.Controller,
|
---|
88 | shape,
|
---|
89 | new Point((int) Rectangle.X, (int)Rectangle.Y));
|
---|
90 |
|
---|
91 | this.Controller.UndoManager.AddUndoCommand(cmd);
|
---|
92 | cmd.Redo();
|
---|
93 | }
|
---|
94 | catch
|
---|
95 | {
|
---|
96 | base.Controller.DeactivateTool(this);
|
---|
97 | Controller.View.Invalidate();
|
---|
98 | throw;
|
---|
99 | }
|
---|
100 |
|
---|
101 | //base.Controller.DeactivateTool(this);
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | #endregion
|
---|
106 | }
|
---|
107 |
|
---|
108 | }
|
---|