-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
419 lines (367 loc) · 17.1 KB
/
MainWindow.xaml.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using WpfApplication2.Elements;
using WpfApplication2.Helpers;
using WpfApplication2.Layers;
using Microsoft.Win32;
using Size = WpfApplication2.Layers.Size;
namespace WpfApplication2
{
/// <summary>
/// Логика взаимодействия для MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void drawingSurface_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DrawingLayer.GetInstance.IsChanged = true;
bool isLin = false; // for line
Point pointClicked = e.GetPosition(drawingSurface);
OxFigure visual = null;
if (SelectionLayer.GetInstance.CurrentTool != Tools.Erase &&
SelectionLayer.GetInstance.CurrentTool != Tools.Hand && SelectionLayer.GetInstance.CurrentTool != Tools.Fill)
{
#region point
if (SelectionLayer.GetInstance.CurrentTool == Tools.Point)
{
visual = new OxPoint {Name = OxFigure.Shape.Point};
}
#endregion
#region square
if (SelectionLayer.GetInstance.CurrentTool == Tools.Square) //
{
visual = new OxRectangle {Name = OxFigure.Shape.Square};
}
#endregion
#region ellipse
if (SelectionLayer.GetInstance.CurrentTool == Tools.Ellipse)
{
visual = new OxCircle {Name = OxFigure.Shape.Ellispe};
}
#endregion
#region line
if (SelectionLayer.GetInstance.CurrentTool == Tools.Line)
{
visual = new OxLine {Name = OxFigure.Shape.Line};
if (DrawingLayer.GetInstance.IsPoint)
{
DrawingLayer.GetInstance.Point2 = pointClicked;
isLin = true;
}
else
DrawingLayer.GetInstance.Point1 = pointClicked;
DrawingLayer.GetInstance.IsPoint = !DrawingLayer.GetInstance.IsPoint;
}
#endregion
#region rect
if (SelectionLayer.GetInstance.CurrentTool == Tools.Rectangle)
{
visual = new OxRectangle {Name = OxFigure.Shape.Rectangle};
}
#endregion
if (visual == null)
{
return;
}
if (SelectionLayer.GetInstance.CurrentTool == Tools.Line && isLin)
{
DrawingLayer.GetInstance.DrawFigure(ref visual, pointClicked, false);
drawingSurface.AddVisual(visual);
UndoRedoLayer.GetInstance.Add(new AddElementCommand(visual, this.drawingSurface));
}
else if (SelectionLayer.GetInstance.CurrentTool != Tools.Line)
{
DrawingLayer.GetInstance.DrawFigure(ref visual, pointClicked, false);
drawingSurface.AddVisual(visual);
//здесь добавление
UndoRedoLayer.GetInstance.Add(new AddElementCommand(visual, this.drawingSurface));
}
}
else
{
#region erase
if (SelectionLayer.GetInstance.CurrentTool == Tools.Erase)
{
visual = drawingSurface.GetVisual(pointClicked);
if (visual != null)
{
UndoRedoLayer.GetInstance.Add(new RemoveElementCommand(visual, this.drawingSurface));
drawingSurface.DeleteVisual(visual);
}
}
#endregion
#region hand
if (SelectionLayer.GetInstance.CurrentTool == Tools.Hand)
{
Point topLeftCorner = new Point();
visual = drawingSurface.GetVisual(pointClicked);
if (visual != null)
{
if (visual.Name == OxFigure.Shape.Line)
{
DrawingLayer.GetInstance.IsClicked = true;
DrawingLayer.GetInstance.DrawFigure(ref visual, topLeftCorner, true);
DrawingLayer.GetInstance.ClickOffset = topLeftCorner - pointClicked;
DrawingLayer.GetInstance.IsDragging = true;
if (DrawingLayer.GetInstance.SelectedVisual != null && DrawingLayer.GetInstance.SelectedVisual != visual)
DrawingLayer.GetInstance.ClearSelection();
DrawingLayer.GetInstance.SelectedVisual = visual;
DrawingLayer.GetInstance.LastPoint = topLeftCorner;
DrawingLayer.GetInstance.IsSel = true;
}
else
{
DrawingLayer.GetInstance.IsClicked = true;
topLeftCorner.X = visual.ContentBounds.TopLeft.X + DrawingLayer.GetInstance.DrawingPen.Thickness / 2;
topLeftCorner.Y = visual.ContentBounds.TopLeft.Y + DrawingLayer.GetInstance.DrawingPen.Thickness / 2;
DrawingLayer.GetInstance.DrawFigure(ref visual, topLeftCorner, true);
DrawingLayer.GetInstance.ClickOffset = topLeftCorner - pointClicked;
DrawingLayer.GetInstance.IsDragging = true;
if (DrawingLayer.GetInstance.SelectedVisual != null && DrawingLayer.GetInstance.SelectedVisual != visual)
DrawingLayer.GetInstance.ClearSelection();
DrawingLayer.GetInstance.SelectedVisual = visual;
DrawingLayer.GetInstance.IsSel = true;
DrawingLayer.GetInstance.LastPoint = topLeftCorner;
}
UndoRedoLayer.GetInstance.Add(new MoveCommand(DrawingLayer.GetInstance.SelectedVisual, this.drawingSurface, DrawingLayer.GetInstance.LastPoint, pointClicked));
}
else if (DrawingLayer.GetInstance.IsSel)
{
OxFigure selectedVisual = DrawingLayer.GetInstance.SelectedVisual;
DrawingLayer.GetInstance.DrawFigure(ref selectedVisual, DrawingLayer.GetInstance.LastPoint, false);
DrawingLayer.GetInstance.IsSel = false;
//UndoRedoLayer.GetInstance.Add(new MoveCommand(selectedVisual, this.drawingSurface, DrawingLayer.GetInstance.LastPoint, topLeftCorner));
}
}
#endregion
#region fill
if (SelectionLayer.GetInstance.CurrentTool == Tools.Fill)
{
visual = drawingSurface.GetVisual(pointClicked);
if (visual != null)
{
DrawingLayer.GetInstance.IsFill = true;
Point topLeftCorner = new Point(visual.ContentBounds.TopLeft.X + DrawingLayer.GetInstance.DrawingPen.Thickness / 2,
visual.ContentBounds.TopLeft.Y + DrawingLayer.GetInstance.DrawingPen.Thickness / 2);
UndoRedoLayer.GetInstance.Add(new FillCommand(visual, topLeftCorner, SelectionLayer.GetInstance.CurrentColor, visual.Color));
visual.Color = SelectionLayer.GetInstance.CurrentColor;
DrawingLayer.GetInstance.IsClicked = true;
DrawingLayer.GetInstance.DrawFigure(ref visual, topLeftCorner, false);
DrawingLayer.GetInstance.IsClicked = false;
}
else
{
DrawingLayer.GetInstance.DrawingBrush =
(Brush)DrawingLayer.GetInstance.BrushConverter.ConvertFromString(SelectionLayer.GetInstance.CurrentColor.ToString());
drawingSurface.Background = DrawingLayer.GetInstance.DrawingBrush;
}
}
#endregion
}
}
private void drawingSurface_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (DrawingLayer.GetInstance.IsSel)
{
OxFigure selectedVisual = DrawingLayer.GetInstance.SelectedVisual;
DrawingLayer.GetInstance.DrawFigure(ref selectedVisual, DrawingLayer.GetInstance.LastPoint, false);
DrawingLayer.GetInstance.IsSel = false;
}
DrawingLayer.GetInstance.IsClicked = false;
DrawingLayer.GetInstance.IsDragging = false;
}
private void mouse_move(object sender, MouseEventArgs e)
{
Point position = Mouse.GetPosition(drawingSurface);
coords.Items[0] = Math.Round(position.X) + " : " + Math.Round(position.Y);
if (DrawingLayer.GetInstance.IsDragging)
{
Point pointDragged = new Point(1, 1);
if (DrawingLayer.GetInstance.SelectedVisual.Name == OxFigure.Shape.Line)
{
pointDragged = position;
}
else
{
pointDragged = position + DrawingLayer.GetInstance.ClickOffset;
}
var selectedVisual = DrawingLayer.GetInstance.SelectedVisual;
//UndoRedoLayer.GetInstance.Add(new MoveCommand(selectedVisual, this.drawingSurface, pointDragged, position));
DrawingLayer.GetInstance.DrawFigure(ref selectedVisual, pointDragged, true);
DrawingLayer.GetInstance.LastPoint = pointDragged;
}
}
private void About_click(object sender, RoutedEventArgs e)
{
var wind = new About();
wind.Show();
}
private void Help_click(object sender, RoutedEventArgs e)
{
var wind = new help();
wind.Show();
}
//pozition and remove
#region current tool
private void hand_click(object sender, RoutedEventArgs e)
{
SelectionLayer.GetInstance.CurrentTool = Tools.Hand;
DrawingLayer.GetInstance.IsPoint = false;
DrawingLayer.GetInstance.IsFill = false;
}
private void point_click(object sender, RoutedEventArgs e)
{
SelectionLayer.GetInstance.CurrentTool = Tools.Point;
DrawingLayer.GetInstance.CheakClearSelection();
}
private void line_click(object sender, RoutedEventArgs e)
{
SelectionLayer.GetInstance.CurrentTool = Tools.Line;
DrawingLayer.GetInstance.CheakClearSelection();
}
private void square_click(object sender, RoutedEventArgs e)
{
SelectionLayer.GetInstance.CurrentTool = Tools.Square;
DrawingLayer.GetInstance.CheakClearSelection();
}
private void rect_click(object sender, RoutedEventArgs e)
{
SelectionLayer.GetInstance.CurrentTool = Tools.Rectangle;
DrawingLayer.GetInstance.CheakClearSelection();
}
private void ellipse_click(object sender, RoutedEventArgs e)
{
SelectionLayer.GetInstance.CurrentTool = Tools.Ellipse;
DrawingLayer.GetInstance.CheakClearSelection();
}
private void erase_click(object sender, RoutedEventArgs e)
{
SelectionLayer.GetInstance.CurrentTool = Tools.Erase;
DrawingLayer.GetInstance.CheakClearSelection();
}
private void fill_click(object sender, RoutedEventArgs e)
{
SelectionLayer.GetInstance.CurrentTool = Tools.Fill;
DrawingLayer.GetInstance.CheakClearSelection();
}
private void colorPick_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
{
SelectionLayer.GetInstance.CurrentColor = colorPick.SelectedColor;
DrawingLayer.GetInstance.ColorChange = true;
DrawingLayer.GetInstance.CheakClearSelection();
DrawingLayer.GetInstance.LastColor = SelectionLayer.GetInstance.CurrentColor;
}
#endregion
#region menu
private void exit_click(object sender, RoutedEventArgs e)
{
if (DrawingLayer.GetInstance.IsChanged)
{
var dialogRes = System.Windows.MessageBox.Show("Хотите сохранить изменения?","Save?",MessageBoxButton.YesNo);
if (dialogRes == MessageBoxResult.Yes)
save_click(sender, e);
}
WpfApplication2.App.Current.Shutdown();
}
private void clear_click(object sender, RoutedEventArgs e)
{
DrawingLayer.GetInstance.IsSave = false;
drawingSurface.DelAll();
SelectionLayer.GetInstance.CurrentTool = Tools.Hand;
SelectionLayer.GetInstance.CurrentColor = Colors.Black;
colorPick.SelectedColor = Colors.Black;
hand_btn.IsChecked = true;
}
private void open_click(object sender, RoutedEventArgs e)
{
OpenFileDialog openDialog = new OpenFileDialog();
openDialog.FileName = "";
openDialog.Filter = "All Files (*.*)|*.*|Image files|*.svg;";
openDialog.Title = "Открыть файл";
var dialogResult = openDialog.ShowDialog();
if (dialogResult.HasValue && dialogResult.Value)
{
/*ImageBrush ibrush = new ImageBrush();
ibrush.ImageSource = new BitmapImage(new Uri(openDialog.FileName.ToString()));
drawingSurface.Background = ibrush; */
SerializationLayer.DeserializeFromXML(openDialog.FileName, ref this.drawingSurface);
}
}
private void save_click(object sender, RoutedEventArgs e)
{
if (!DrawingLayer.GetInstance.IsSave)
{
save_as_click(sender,e);
}
if (DrawingLayer.GetInstance.FileName != null)
SerializationLayer.SerializeToXML(drawingSurface, DrawingLayer.GetInstance.FileName);
DrawingLayer.GetInstance.IsChanged = false;
}
private void save_as_click(object sender, RoutedEventArgs e)
{
DrawingLayer.GetInstance.IsSave = true;
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = "Image files|*.svg";
var dialogResult = saveDialog.ShowDialog();
if (dialogResult.HasValue && dialogResult.Value)
{
DrawingLayer.GetInstance.FileName = saveDialog.FileName;
save_click(sender, e);
}
else
{
DrawingLayer.GetInstance.IsSave = false;
}
DrawingLayer.GetInstance.IsChanged = false;
}
private void setFigSize_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (setFigSize.SelectedIndex == (int)0)
SelectionLayer.GetInstance.CurrentSize = Size.Small;
else if (setFigSize.SelectedIndex == 1)
SelectionLayer.GetInstance.CurrentSize = Size.Medium;
else SelectionLayer.GetInstance.CurrentSize = Size.Large;
}
private void undo_click(object sender, RoutedEventArgs e)
{
UndoRedoLayer.GetInstance.Undo();
}
private void redo_click(object sender, RoutedEventArgs e)
{
UndoRedoLayer.GetInstance.Redo();
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
}
private void Open_Checked(object sender, RoutedEventArgs e)
{
}
private void New_click(object sender, RoutedEventArgs e)
{
if (DrawingLayer.GetInstance.IsChanged)
{
var dialogRes = System.Windows.MessageBox.Show("Хотите сохранить изменения?", "Save?", MessageBoxButton.YesNo);
if (dialogRes == MessageBoxResult.Yes)
save_click(sender, e);
}
DrawingLayer.GetInstance.IsSave = false;
drawingSurface.DelAll();
SelectionLayer.GetInstance.CurrentTool = Tools.Hand;
SelectionLayer.GetInstance.CurrentColor = Colors.Black;
colorPick.SelectedColor = Colors.Black;
hand_btn.IsChecked = true;
}
private void triangle_click(object sender, RoutedEventArgs e)
{
}
#endregion
}
}