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