1 | using System;
|
---|
2 | using System.Diagnostics;
|
---|
3 | using System.Collections;
|
---|
4 | using System.Collections.Generic;
|
---|
5 | using System.Drawing;
|
---|
6 | using System.Windows.Forms;
|
---|
7 |
|
---|
8 | namespace Netron.Diagramming.Core
|
---|
9 | {
|
---|
10 |
|
---|
11 | class PolygonTool : AbstractTool, IMouseListener, IKeyboardListener
|
---|
12 | {
|
---|
13 |
|
---|
14 | #region Fields
|
---|
15 | /// <summary>
|
---|
16 | /// the location of the mouse when the motion starts
|
---|
17 | /// </summary>
|
---|
18 |
|
---|
19 | private bool doDraw;
|
---|
20 | private Point[] points;
|
---|
21 | #endregion
|
---|
22 |
|
---|
23 | #region Constructor
|
---|
24 | /// <summary>
|
---|
25 | /// Initializes a new instance of the <see cref="T:PolygonTool"/> class.
|
---|
26 | /// </summary>
|
---|
27 | /// <param name="name">The name of the tool.</param>
|
---|
28 | public PolygonTool(string name) : base(name)
|
---|
29 | {
|
---|
30 | }
|
---|
31 | #endregion
|
---|
32 |
|
---|
33 | #region Methods
|
---|
34 |
|
---|
35 | /// <summary>
|
---|
36 | /// Called when the tool is activated.
|
---|
37 | /// </summary>
|
---|
38 | protected override void OnActivateTool()
|
---|
39 | {
|
---|
40 | Controller.View.CurrentCursor =CursorPalette.Cross;
|
---|
41 | this.SuspendOtherTools();
|
---|
42 | doDraw = false;
|
---|
43 | points = new Point[1];
|
---|
44 | }
|
---|
45 |
|
---|
46 | /// <summary>
|
---|
47 | /// Handles the mouse down event
|
---|
48 | /// </summary>
|
---|
49 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
50 | /// <returns>Returns 'true' if the event was handled, otherwise 'false'.</returns>
|
---|
51 | public bool MouseDown(MouseEventArgs e)
|
---|
52 | {
|
---|
53 | if (e == null)
|
---|
54 | throw new ArgumentNullException("The argument object is 'null'");
|
---|
55 | if (e.Button == MouseButtons.Left && Enabled && !IsSuspended)
|
---|
56 | {
|
---|
57 | Array.Resize<Point>(ref points, points.Length + 1);
|
---|
58 | points[points.Length-2]= e.Location;
|
---|
59 | doDraw = true;
|
---|
60 | return true;
|
---|
61 | }
|
---|
62 | return false;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /// <summary>
|
---|
66 | /// Handles the mouse move event
|
---|
67 | /// </summary>
|
---|
68 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
69 | public void MouseMove(MouseEventArgs e)
|
---|
70 | {
|
---|
71 | if (e == null)
|
---|
72 | throw new ArgumentNullException("The argument object is 'null'");
|
---|
73 | Point point = e.Location;
|
---|
74 | if(IsActive && doDraw)
|
---|
75 | {
|
---|
76 | points[points.Length - 1] = e.Location;
|
---|
77 | Controller.View.PaintGhostLine( MultiPointType.Polygon, points);
|
---|
78 | //Controller.View.Invalidate(System.Drawing.Rectangle.Inflate(Controller.View.Ghost.Rectangle, 20, 20));
|
---|
79 | //TODO: find a more performant way to invalidate the area
|
---|
80 | Controller.View.Invalidate();
|
---|
81 | }
|
---|
82 | }
|
---|
83 | /// <summary>
|
---|
84 | ///
|
---|
85 | /// </summary>
|
---|
86 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
87 | public void MouseUp(MouseEventArgs e)
|
---|
88 | {
|
---|
89 | if (IsActive)
|
---|
90 | {
|
---|
91 |
|
---|
92 | }
|
---|
93 | }
|
---|
94 | private void Package()
|
---|
95 | {
|
---|
96 | if ((points == null) ||
|
---|
97 | (points.Length < 1) ||
|
---|
98 | (points[0] == Point.Empty))
|
---|
99 | {
|
---|
100 | return;
|
---|
101 | }
|
---|
102 |
|
---|
103 | MultiPointShape shape = new MultiPointShape(
|
---|
104 | this.Controller.Model,
|
---|
105 | points,
|
---|
106 | MultiPointType.Polygon);
|
---|
107 |
|
---|
108 | AddMultiPointShapeCommand cmd = new AddMultiPointShapeCommand(this.Controller, shape);
|
---|
109 | this.Controller.UndoManager.AddUndoCommand(cmd);
|
---|
110 | cmd.Redo();
|
---|
111 | }
|
---|
112 | #endregion
|
---|
113 |
|
---|
114 | #region IKeyboardListener Members
|
---|
115 |
|
---|
116 | public void KeyDown(KeyEventArgs e)
|
---|
117 | {
|
---|
118 | if(e.KeyData == Keys.Escape && IsActive)
|
---|
119 | {
|
---|
120 | DeactivateTool();
|
---|
121 | Package();
|
---|
122 | e.Handled = true;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | public void KeyUp(KeyEventArgs e)
|
---|
127 | {
|
---|
128 | }
|
---|
129 |
|
---|
130 | public void KeyPress(KeyPressEventArgs e)
|
---|
131 | {
|
---|
132 | }
|
---|
133 |
|
---|
134 | #endregion
|
---|
135 | }
|
---|
136 |
|
---|
137 | }
|
---|