1 | using System;
|
---|
2 | using System.Drawing;
|
---|
3 | using System.Windows.Forms;
|
---|
4 |
|
---|
5 | namespace Netron.Diagramming.Core {
|
---|
6 |
|
---|
7 | class ScribbleTool : 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 | #region Constructor
|
---|
18 | /// <summary>
|
---|
19 | /// Initializes a new instance of the <see cref="T:ScribbleTool"/> class.
|
---|
20 | /// </summary>
|
---|
21 | /// <param name="name">The name of the tool.</param>
|
---|
22 | public ScribbleTool(string name)
|
---|
23 | : base(name) {
|
---|
24 | }
|
---|
25 | #endregion
|
---|
26 |
|
---|
27 | #region Methods
|
---|
28 |
|
---|
29 | /// <summary>
|
---|
30 | /// Called when the tool is activated.
|
---|
31 | /// </summary>
|
---|
32 | protected override void OnActivateTool() {
|
---|
33 | Controller.View.CurrentCursor = CursorPalette.Cross;
|
---|
34 | this.SuspendOtherTools();
|
---|
35 | doDraw = false;
|
---|
36 | points = new Point[1];
|
---|
37 | }
|
---|
38 |
|
---|
39 | /// <summary>
|
---|
40 | /// Handles the mouse down event
|
---|
41 | /// </summary>
|
---|
42 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
43 | /// <returns>Returns 'true' if the event was handled, otherwise 'false'.</returns>
|
---|
44 | public bool MouseDown(MouseEventArgs e) {
|
---|
45 | if (e == null)
|
---|
46 | throw new ArgumentNullException("The argument object is 'null'");
|
---|
47 | if (e.Button == MouseButtons.Left && Enabled && !IsSuspended) {
|
---|
48 | Point p = e.Location;
|
---|
49 | if (Closure(p)) {
|
---|
50 | points[points.Length - 1] = points[0];
|
---|
51 | doDraw = false;
|
---|
52 | DeactivateTool();
|
---|
53 | Package();
|
---|
54 |
|
---|
55 | return true;
|
---|
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 | if (e == null)
|
---|
71 | throw new ArgumentNullException("The argument object is 'null'");
|
---|
72 | Point point = e.Location;
|
---|
73 | if (IsActive && doDraw) {
|
---|
74 | points[points.Length - 1] = e.Location;
|
---|
75 | Controller.View.PaintGhostLine(MultiPointType.Curve, points);
|
---|
76 | //Controller.View.Invalidate(System.Drawing.Rectangle.Inflate(Controller.View.Ghost.Rectangle, 20, 20));
|
---|
77 | //TODO: find a more performant way to invalidate the area
|
---|
78 | Controller.View.Invalidate();
|
---|
79 | }
|
---|
80 | }
|
---|
81 | private bool Closure(Point p) {
|
---|
82 | for (int k = 0; k < points.Length - 1; k++) {
|
---|
83 | if (new Rectangle(points[k].X - 6, points[k].Y - 6, 12, 12).Contains(p)) {
|
---|
84 | DialogResult res = MessageBox.Show("Do you wish to close the curve?", "Closure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
|
---|
85 | if (res == DialogResult.Yes)
|
---|
86 | return true;
|
---|
87 |
|
---|
88 | }
|
---|
89 | }
|
---|
90 | return false;
|
---|
91 | }
|
---|
92 | /// <summary>
|
---|
93 | ///
|
---|
94 | /// </summary>
|
---|
95 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
96 | public void MouseUp(MouseEventArgs e) {
|
---|
97 | if (IsActive) {
|
---|
98 |
|
---|
99 | }
|
---|
100 | }
|
---|
101 | private void Package() {
|
---|
102 | if ((points == null) ||
|
---|
103 | (points.Length < 1) ||
|
---|
104 | (points[0] == Point.Empty)) {
|
---|
105 | return;
|
---|
106 | }
|
---|
107 |
|
---|
108 | MultiPointShape shape = new MultiPointShape(
|
---|
109 | this.Controller.Model,
|
---|
110 | points,
|
---|
111 | MultiPointType.Curve);
|
---|
112 |
|
---|
113 | AddMultiPointShapeCommand cmd = new AddMultiPointShapeCommand(this.Controller, shape);
|
---|
114 | this.Controller.UndoManager.AddUndoCommand(cmd);
|
---|
115 | this.Controller.View.ResetGhost();
|
---|
116 | cmd.Redo();
|
---|
117 | }
|
---|
118 | #endregion
|
---|
119 |
|
---|
120 | #region IKeyboardListener Members
|
---|
121 |
|
---|
122 | public void KeyDown(KeyEventArgs e) {
|
---|
123 |
|
---|
124 |
|
---|
125 | if (e.KeyData == System.Windows.Forms.Keys.Escape && IsActive) {
|
---|
126 | DeactivateTool();
|
---|
127 | Package();
|
---|
128 | e.Handled = true;
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | public void KeyUp(KeyEventArgs e) {
|
---|
133 | }
|
---|
134 |
|
---|
135 | public void KeyPress(KeyPressEventArgs e) {
|
---|
136 | }
|
---|
137 |
|
---|
138 | #endregion
|
---|
139 | }
|
---|
140 |
|
---|
141 | }
|
---|