-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ShapeEditor.cs
212 lines (188 loc) · 8.27 KB
/
ShapeEditor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows;
using System.Windows.Media;
using System.Windows.Input;
namespace Amporis.WPF.ShapeEditor
{
public class ShapeEditor : Grid
{
#region Fields & props
Rectangle rLT, rCT, rRT, rLC, rRC, rLB, rCB, rRB, rCover;
const double rectSize = 10;
bool moving = false, resizing = false;
Point mysPosStart, objPos, objSizeStart;
public FrameworkElement CapturedElement { get; private set; }
#endregion
#region Constructor
public ShapeEditor()
{
Visibility = Visibility.Collapsed;
rCover = new Rectangle() // almost transparent cover rectangle for catching clicks
{
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Margin = new Thickness(rectSize / 2.0),
Fill = new SolidColorBrush(Color.FromArgb(1, 255, 255, 255)),
};
rCover.MouseLeftButtonDown += RCover_MouseLeftButtonDown;
rCover.MouseLeftButtonUp += RCover_MouseLeftButtonUp;
rCover.MouseMove += RCover_MouseMove;
Children.Add(rCover);
rLT = AddRect("rLT", HorizontalAlignment.Left, VerticalAlignment.Top, Cursors.SizeNWSE);
rCT = AddRect("rCT", HorizontalAlignment.Center, VerticalAlignment.Top, Cursors.SizeNS);
rRT = AddRect("rRT", HorizontalAlignment.Right, VerticalAlignment.Top, Cursors.SizeNESW);
rLC = AddRect("rLC", HorizontalAlignment.Left, VerticalAlignment.Center, Cursors.SizeWE);
rRC = AddRect("rRC", HorizontalAlignment.Right, VerticalAlignment.Center, Cursors.SizeWE);
rLB = AddRect("rLB", HorizontalAlignment.Left, VerticalAlignment.Bottom, Cursors.SizeNESW);
rCB = AddRect("rCB", HorizontalAlignment.Center, VerticalAlignment.Bottom, Cursors.SizeNS);
rRB = AddRect("rRB", HorizontalAlignment.Right, VerticalAlignment.Bottom, Cursors.SizeNWSE);
}
// Method for creating small rectangles at the corners and sides
private Rectangle AddRect(string name, HorizontalAlignment ha, VerticalAlignment va, Cursor crs)
{
var rect = new Rectangle() // small rectangles at the corners and sides
{
HorizontalAlignment = ha,
VerticalAlignment = va,
Width = rectSize,
Height = rectSize,
Stroke = new SolidColorBrush(Colors.Gray), // small rectangles color
Fill = new SolidColorBrush(Color.FromArgb(1, 255, 255, 255)),
Cursor = crs,
Name = name,
};
rect.MouseLeftButtonDown += Rect_MouseLeftButtonDown;
rect.MouseLeftButtonUp += Rect_MouseLeftButtonUp;
rect.MouseMove += Rect_MouseMove;
Children.Add(rect);
return rect;
}
#endregion
#region Click on the shape - moving
private void RCover_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
moving = true;
mysPosStart = e.GetPosition(this.Parent as Canvas);
objPos = new Point(Canvas.GetLeft(this), Canvas.GetTop(this));
objSizeStart = new Point(rCover.ActualWidth, rCover.ActualHeight);
rCover.CaptureMouse();
rCover.Cursor = Cursors.SizeAll;
e.Handled = true;
}
private void RCover_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
moving = false;
rCover.ReleaseMouseCapture();
rCover.Cursor = Cursors.Arrow;
}
private void RCover_MouseMove(object sender, MouseEventArgs e)
{
if (moving)
{
var mysPosTed = e.GetPosition(this.Parent as Canvas);
double x = mysPosTed.X - mysPosStart.X + objPos.X;
Canvas.SetLeft(this, x);
double y = mysPosTed.Y - mysPosStart.Y + objPos.Y;
Canvas.SetTop(this, y);
}
}
#endregion
#region Click on the conner or side of the shape - resizing
private void Rect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
resizing = true;
var rec = sender as FrameworkElement;
mysPosStart = e.GetPosition(this.Parent as Canvas);
objPos = new Point(Canvas.GetLeft(this), Canvas.GetTop(this));
objSizeStart = new Point(this.ActualWidth, this.ActualHeight);
rec.CaptureMouse();
e.Handled = true;
}
private void Rect_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
resizing = false;
var rec = sender as FrameworkElement;
rec.ReleaseMouseCapture();
}
private void Rect_MouseMove(object sender, MouseEventArgs e)
{
if (resizing)
{
var mysPosTed = e.GetPosition(this.Parent as FrameworkElement);
var prvek = sender as FrameworkElement;
if (prvek.Name[1] != 'C')
{
double width;
if (prvek.Name[1] == 'L')
{
width = mysPosStart.X - mysPosTed.X + objSizeStart.X;
Canvas.SetLeft(this, Canvas.GetLeft(this) - (width - Width));
} else
width = mysPosTed.X - mysPosStart.X + objSizeStart.X;
Width = Math.Max(width, 0);
}
if (prvek.Name[2] != 'C')
{
double height;
if (prvek.Name[2] == 'T')
{
height = mysPosStart.Y - mysPosTed.Y + objSizeStart.Y;
Canvas.SetTop(this, Canvas.GetTop(this) - (height - Height));
}
else
height = mysPosTed.Y - mysPosStart.Y + objSizeStart.Y;
Height = Math.Max(height, 0);
}
}
}
#endregion
#region Editing shape
// Start editing a shape (element)
public void CaptureElement(FrameworkElement element, MouseButtonEventArgs mouse = null)
{
if (CapturedElement != null)
{
if (CapturedElement == element)
return;
ReleaseElement();
}
CapturedElement = element;
Canvas.SetLeft(this, Canvas.GetLeft(element) - rectSize / 2.0);
Canvas.SetTop(this, Canvas.GetTop(element) - rectSize / 2.0);
Width = element.Width + rectSize;
Height = element.Height + rectSize;
((Canvas)element.Parent).Children.Remove(element);
Children.Insert(0, element);
element.Margin = new Thickness(rectSize / 2.0);
element.HorizontalAlignment = HorizontalAlignment.Stretch;
element.VerticalAlignment = VerticalAlignment.Stretch;
element.Width = double.NaN;
element.Height = double.NaN;
Visibility = Visibility.Visible;
if (mouse != null)
RCover_MouseLeftButtonDown(rCover, mouse);
}
// End editing shape
public void ReleaseElement()
{
if (CapturedElement == null) return;
FrameworkElement element = CapturedElement;
Children.Remove(element);
((Canvas)Parent).Children.Add(element);
Canvas.SetLeft(element, Canvas.GetLeft(this) + rectSize / 2.0);
Canvas.SetTop(element, Canvas.GetTop(this) + rectSize / 2.0);
element.Width = Width - rectSize;
element.Height = Height - rectSize;
element.Margin = new Thickness(0);
CapturedElement = null;
Visibility = Visibility.Collapsed;
}
#endregion
}
}