-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
501 lines (401 loc) · 16.8 KB
/
Form1.cs
File metadata and controls
501 lines (401 loc) · 16.8 KB
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using GraphicalEditor.Shapes;
using GraphicalEditor.Interfaces;
using GraphicalEditor.Commands;
using GraphicalEditor.IO;
using GraphicalEditor.Strategy;
using static GraphicalEditor.Util.Enums;
using GraphicalEditor.Decorator;
namespace GraphicalEditor
{
public partial class Form : System.Windows.Forms.Form
{
private Color paintColor;
private Color PaintColor
{
get
{
return paintColor;
}
set
{
if (value == paintColor)
return;
paintColor = value;
}
}
private ToolItem currentTool;
private ToolItem CurrentTool
{
get
{
return currentTool;
}
set
{
if (value == currentTool)
return;
currentTool = value;
label_SelectedTool.Text = string.Format("Selected Tool: {0} " , currentTool.ToString());
UpdateToolButtonsCheckedState();
}
}
private Point dragMouseLocation = new Point(0, 0);
private Rectangle previousShapeBounds = new Rectangle();
private Rectangle newShapeBounds = new Rectangle();
private bool isChoosingColor = false;
private bool holdingMouseDown = false;
private CommandHandler commandHandler;
private DrawHandler DrawHandlerInstance { get { return DrawHandler.Instance; } }
public Form()
{
InitializeComponent();
commandHandler = new CommandHandler();
CurrentTool = ToolItem.None;
//Register events
commandHandler.OnExecute += OnCommandExecute;
commandHandler.OnUndo += OnCommandUndo;
}
private void UpdateHitStatus(Point currentPoint)
{
DrawHandlerInstance.UpdateHitstatusByCurrentPoint(currentPoint);
SetCursor();
}
private void SetCursor()
{
if (!DrawHandlerInstance.HasSelectedAShape)
{
Cursor = Cursors.Default;
return;
}
switch (DrawHandlerInstance.CurrentHitStatus)
{
case HitStatus.Drag:
Cursor = Cursors.SizeAll;
break;
case HitStatus.ResizeBottom:
case HitStatus.ResizeTop:
Cursor = Cursors.SizeNS;
break;
case HitStatus.ResizeLeft:
case HitStatus.ResizeRight:
Cursor = Cursors.SizeWE;
break;
case HitStatus.ResizeBottomLeft:
case HitStatus.ResizeTopRight:
Cursor = Cursors.SizeNESW;
break;
case HitStatus.ResizeBottomRight:
case HitStatus.ResizeTopLeft:
Cursor = Cursors.SizeNWSE;
break;
default:
Cursor = Cursors.Default;
break;
}
}
private void UpdateToolButtonsCheckedState()
{
Button_Brush.Checked = false;
Button_Ellipse.Checked = false;
Button_Line.Checked = false;
Button_Pencil.Checked = false;
Button_Rectangle.Checked = false;
Button_ColorPicker.Checked = false;
switch (CurrentTool)
{
case ToolItem.Brush:
Button_Brush.Checked = true;
break;
case ToolItem.Ellipse:
Button_Ellipse.Checked = true;
break;
case ToolItem.Line:
Button_Line.Checked = true;
break;
case ToolItem.Pencil:
Button_Pencil.Checked = true;
break;
case ToolItem.Rectangle:
Button_Rectangle.Checked = true;
break;
case ToolItem.ColorPicker:
Button_ColorPicker.Checked = true;
break;
}
}
# region General Events
private void OnCommandUndo(ICommand command)
{
PictureBox_DrawArea.Invalidate();
}
private void OnCommandExecute(ICommand command)
{
PictureBox_DrawArea.Invalidate();
}
#endregion
#region GUI General Events
private void PictureBox_DrawArea_Paint(object sender, PaintEventArgs e)
{
DrawHandlerInstance.RedrawShapes(e.Graphics);
}
private void PictureBox_ColorPicker_MouseDown(object sender, MouseEventArgs e)
{
isChoosingColor = true;
}
private void PictureBox_ColorPicker_MouseUp(object sender, MouseEventArgs e)
{
isChoosingColor = false;
}
private void PictureBox_ColorPicker_MouseMove(object sender, MouseEventArgs e)
{
if (isChoosingColor)
{
Bitmap bmp = (Bitmap)PictureBox_ColorPicker.Image.Clone();
if (e.X < 0 || e.Y < 0)
return;
if (e.X >= bmp.Width)
return;
if (e.Y >= bmp.Height)
return;
PaintColor = bmp.GetPixel(e.X, e.Y);
Trackbar_ColorPicker_Red.Value = PaintColor.R;
Trackbar_Colorpicker_Green.Value = PaintColor.G;
Trackbar_Colorpicker_Blue.Value = PaintColor.B;
Trackbar_Colorpicker_Alpha.Value = PaintColor.A;
Label_Colorpicker_redval.Text = PaintColor.R.ToString();
Label_Colorpicker_greenval.Text = PaintColor.G.ToString();
Label_Colorpicker_blueval.Text = PaintColor.B.ToString();
Label_Colorpicker_alphaval.Text = PaintColor.A.ToString();
Picturebox_CurrentColor.BackColor = PaintColor;
}
}
private void Picturebox_DrawArea_MouseDown(object sender, MouseEventArgs e)
{
holdingMouseDown = true;
if (DrawHandlerInstance.CurrentHitStatus == HitStatus.None)
switch (CurrentTool)
{
case ToolItem.Rectangle:
ShapeObject rectangle = new ShapeObject(new RectangleShape(), Constants.SHAPE_DEFAULT_WIDTH, Constants.SHAPE_DEFAULT_HEIGHT, e.Location, PaintColor);
commandHandler.AddCommand(new CreateShapeCommand(rectangle));
CurrentTool = ToolItem.None;
break;
case ToolItem.Ellipse:
ShapeObject ellipse = new ShapeObject(new EllipseShape(), Constants.SHAPE_DEFAULT_WIDTH, Constants.SHAPE_DEFAULT_HEIGHT, e.Location, PaintColor);
commandHandler.AddCommand(new CreateShapeCommand(ellipse));
CurrentTool = ToolItem.None;
break;
case ToolItem.None:
DrawHandlerInstance.SelectShapeFromPoint(e.Location);
break;
}
else if (DrawHandlerInstance.CurrentHitStatus != HitStatus.Drag)
{
if (DrawHandlerInstance.SelectedShape != null)
previousShapeBounds = DrawHandlerInstance.SelectedShape.Bounds;
}
dragMouseLocation = e.Location;
UpdateHitStatus(e.Location);
PictureBox_DrawArea.Invalidate();
}
private void PictureBox_DrawArea_MouseUp(object sender, MouseEventArgs e)
{
holdingMouseDown = false;
if (DrawHandlerInstance.HasSelectedAShape)
switch (CurrentTool)
{
case ToolItem.Line:
Graphics g = PictureBox_DrawArea.CreateGraphics();
g.DrawLine(new Pen(new SolidBrush(PaintColor)), dragMouseLocation, e.Location);
g.Dispose();
break;
case ToolItem.None:
if (DrawHandlerInstance.CurrentHitStatus == HitStatus.Drag)
{
ICommand MoveCommand = new MoveShapeCommand(DrawHandlerInstance.SelectedShape, dragMouseLocation, e.Location);
commandHandler.AddCommand(MoveCommand);
}
else if (DrawHandlerInstance.CurrentHitStatus != HitStatus.None)
{
ICommand ResizeCommand = new ResizeShapeCommand(DrawHandlerInstance.SelectedShape, previousShapeBounds, newShapeBounds);
commandHandler.AddCommand(ResizeCommand);
}
break;
}
PictureBox_DrawArea.Invalidate();
}
private void PictureBox_DrawArea_MouseMove(object sender, MouseEventArgs e)
{
if (holdingMouseDown)
{
switch (CurrentTool)
{
case ToolItem.None:
if (DrawHandlerInstance.CurrentHitStatus == HitStatus.Drag)
DrawHandlerInstance.MoveSelectedShape(e.Location);
else
newShapeBounds = DrawHandlerInstance.ResizeSelectedShape(e.Location);
break;
}
}
else
UpdateHitStatus(e.Location);
PictureBox_DrawArea.Invalidate();
}
#endregion
#region GUI Button Events
private void Button_Brush_Click(object sender, EventArgs e)
{
CurrentTool = ToolItem.Brush;
}
private void Button_Rectangle_Click(object sender, EventArgs e)
{
CurrentTool = ToolItem.Rectangle;
}
private void Button_Ellipse_Click(object sender, EventArgs e)
{
CurrentTool = ToolItem.Ellipse;
}
private void Button_Pencil_Click(object sender, EventArgs e)
{
CurrentTool = ToolItem.Pencil;
}
private void Button_Group_Click(object sender, EventArgs e)
{
if (!DrawHandlerInstance.HasSelectedAShape)
return;
ICommand groupCommand = new GroupCommand(DrawHandlerInstance.SelectedShapes);
commandHandler.AddCommand(groupCommand);
}
private void Button_line_Click(object sender, EventArgs e)
{
CurrentTool = ToolItem.Line;
}
private void Button_ColorPicker_Click(object sender, EventArgs e)
{
CurrentTool = ToolItem.ColorPicker;
}
private void Button_New_Click(object sender, EventArgs e)
{
PictureBox_DrawArea.Refresh();
PictureBox_DrawArea.Image = null;
}
private void Button_Load_Click(object sender, EventArgs e)
{
SaveLoadController saveLoad = new SaveLoadController();
OpenFileDialog o = new OpenFileDialog();
o.Filter = "Graphic files|*.graphic";
if (o.ShowDialog() == DialogResult.OK)
{
commandHandler.AddCommand(new LoadCommand(DrawHandlerInstance.ShapeList, saveLoad.LoadShapes(o.FileName)));
}
}
private void Button_Save_Click(object sender, EventArgs e)
{
SaveLoadController saveLoad = new SaveLoadController();
SaveFileDialog s = new SaveFileDialog();
s.Filter = "Graphic files|*.graphic";
if (s.ShowDialog() == DialogResult.OK)
{
if (File.Exists(s.FileName))
{
File.Delete(s.FileName);
}
saveLoad.SaveShapes(DrawHandlerInstance.ShapeList, s.FileName);
}
}
private void Trackbar_ColorPicker_Red_Scroll(object sender, EventArgs e)
{
PaintColor = Color.FromArgb(Trackbar_Colorpicker_Alpha.Value, Trackbar_ColorPicker_Red.Value, Trackbar_Colorpicker_Green.Value, Trackbar_Colorpicker_Blue.Value);
Picturebox_CurrentColor.BackColor = PaintColor;
Label_Colorpicker_redval.Text = string.Format("R: {0} ", PaintColor.R.ToString());
}
private void Trackbar_ColorPicker_Green_Scroll(object sender, EventArgs e)
{
PaintColor = Color.FromArgb(Trackbar_Colorpicker_Alpha.Value, Trackbar_ColorPicker_Red.Value, Trackbar_Colorpicker_Green.Value, Trackbar_Colorpicker_Blue.Value);
Picturebox_CurrentColor.BackColor = PaintColor;
Label_Colorpicker_greenval.Text = string.Format("G: {0} " , PaintColor.G.ToString());
}
private void Trackbar_ColorPicker_Blue_Scroll(object sender, EventArgs e)
{
PaintColor = Color.FromArgb(Trackbar_Colorpicker_Alpha.Value, Trackbar_ColorPicker_Red.Value, Trackbar_Colorpicker_Green.Value, Trackbar_Colorpicker_Blue.Value);
Picturebox_CurrentColor.BackColor = PaintColor;
Label_Colorpicker_blueval.Text = string.Format("B: {0} " , PaintColor.B.ToString());
}
private void PictureBox_DrawArea_MouseClick(object sender, MouseEventArgs e)
{
if (CurrentTool == ToolItem.ColorPicker)
{
Bitmap bmp = new Bitmap(PictureBox_DrawArea.Width, PictureBox_DrawArea.Height);
Graphics g = Graphics.FromImage(bmp);
Rectangle rect = PictureBox_DrawArea.RectangleToScreen(PictureBox_DrawArea.ClientRectangle);
g.CopyFromScreen(rect.Location, Point.Empty, PictureBox_DrawArea.Size);
g.Dispose();
PaintColor = bmp.GetPixel(e.X, e.Y);
Picturebox_CurrentColor.BackColor = PaintColor;
Trackbar_ColorPicker_Red.Value = PaintColor.R;
Trackbar_Colorpicker_Green.Value = PaintColor.G;
Trackbar_Colorpicker_Blue.Value = PaintColor.B;
Trackbar_Colorpicker_Alpha.Value = PaintColor.A;
Label_Colorpicker_redval.Text = PaintColor.R.ToString();
Label_Colorpicker_greenval.Text = PaintColor.G.ToString();
Label_Colorpicker_blueval.Text = PaintColor.B.ToString();
Label_Colorpicker_alphaval.Text = PaintColor.A.ToString();
bmp.Dispose();
}
}
private void Trackbar_ColorPicker_Alpha_Scroll(object sender, EventArgs e)
{
PaintColor = Color.FromArgb(Trackbar_Colorpicker_Alpha.Value, Trackbar_ColorPicker_Red.Value, Trackbar_Colorpicker_Green.Value, Trackbar_Colorpicker_Blue.Value);
Picturebox_CurrentColor.BackColor = PaintColor;
Label_Colorpicker_alphaval.Text = string.Format("A: {0} ", PaintColor.A.ToString());
}
private void Button_Undo_Click(object sender, EventArgs e)
{
commandHandler.Undo();
DrawHandlerInstance.ClearSelection();
}
private void Button_Redo_Click(object sender, EventArgs e)
{
commandHandler.Redo();
DrawHandlerInstance.ClearSelection();
}
private void button_Top_Decorator_Click(object sender, EventArgs e)
{
if (!DrawHandlerInstance.HasSelectedAShape)
return;
if (string.IsNullOrWhiteSpace(textbox_DecoratorText.Text))
return;
DrawHandlerInstance.AddDecoratorToSelectedShape(new TopDecorator(textbox_DecoratorText.Text));
}
private void button_Bottom_Decorator_Click(object sender, EventArgs e)
{
if (!DrawHandlerInstance.HasSelectedAShape)
return;
if (string.IsNullOrWhiteSpace(textbox_DecoratorText.Text))
return;
DrawHandlerInstance.AddDecoratorToSelectedShape(new BottomDecorator(textbox_DecoratorText.Text));
}
private void button_Left_Decorator_Click(object sender, EventArgs e)
{
if (!DrawHandlerInstance.HasSelectedAShape)
return;
if (string.IsNullOrWhiteSpace(textbox_DecoratorText.Text))
return;
DrawHandlerInstance.AddDecoratorToSelectedShape(new LeftDecorator(textbox_DecoratorText.Text));
}
private void button_Right_Decorator_Click(object sender, EventArgs e)
{
if (!DrawHandlerInstance.HasSelectedAShape)
return;
if (string.IsNullOrWhiteSpace(textbox_DecoratorText.Text))
return;
DrawHandlerInstance.AddDecoratorToSelectedShape(new RightDecorator(textbox_DecoratorText.Text));
}
#endregion
}
}