-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassBlock.cs
479 lines (463 loc) · 17.8 KB
/
ClassBlock.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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BlockDiagramCreator
{
[Serializable]
abstract class Block
{
protected bool selected = false;
protected Point layout;
protected string inscription;
public int X { get { return layout.X; } }
public int Y { get { return layout.Y; } }
public Point Layout { get { return layout; } }
public Block(int x, int y, string inscription)
{
layout = new Point(x, y);
this.inscription = inscription;
}
public string GetText()
{
return inscription;
}
public void SetText(string text)
{
inscription = text;
}
public void UnSelect()
{
selected = false;
}
public bool IsSelected()
{
return selected;
}
public abstract void Select();
public abstract void Draw(Graphics canvas);
public abstract bool Click(int x, int y);
public abstract void SetLayout(int x, int y);
public abstract void Remove();
public abstract LinkPoint CheckPoint(int x, int y, bool isInput);
}
[Serializable]
class OperatingBlock : Block
{
private readonly (int width, int height) size = (120,40);
private LinkPoint input;
private LinkPoint output;
private Rectangle rec;
public OperatingBlock(int x, int y, string inscription) : base(x, y, inscription)
{
input = new LinkPoint(x, y - (size.height / 2), true, this);
output = new LinkPoint(x, y + (size.height / 2), false, this);
rec = new Rectangle(new Point(x - (size.width/2), y - (size.height / 2)), new Size(size.width, size.height));
}
// sets as selected
public override void Select()
{
selected = true;
//pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
input.SetDrawingLinkPoint();
output.SetDrawingLinkPoint();
}
// draws block with all components
public override void Draw(Graphics canvas)
{
Pen pen = new Pen(Color.Black, 3);
Font font = new Font(new FontFamily("Arial"), 12, FontStyle.Regular, GraphicsUnit.Pixel);
Brush brush = new SolidBrush(Color.Black);
pen.DashStyle = selected ? System.Drawing.Drawing2D.DashStyle.Dash : System.Drawing.Drawing2D.DashStyle.Solid;
canvas.DrawRectangle(pen, rec);
canvas.FillRectangle(new SolidBrush(Color.White), rec);
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
canvas.DrawString(inscription, font, brush, rec, stringFormat);
input.Draw(canvas);
output.Draw(canvas);
}
// checks if block was clicked
public override bool Click(int x, int y)
{
if (rec.Left <= x && rec.Right >= x && rec.Top <= y && rec.Bottom >= y) return true;
return false;
}
// changes layout block with all components
public override void SetLayout(int x, int y)
{
rec = new Rectangle(new Point(x - (size.width / 2), y - (size.height / 2)), new Size(size.width, size.height));
input.SetLayout(x, y - (size.height / 2));
output.SetLayout(x, y + (size.height / 2));
layout.X = x;
layout.Y = y;
}
// removes block with all components
public override void Remove()
{
input.Remove();
output.Remove();
input = null;
output = null;
}
// checks wchitch link point was clicked
public override LinkPoint CheckPoint(int x, int y, bool isInput)
{
if (input.CheckPoint(x, y, isInput)) return input;
else if (output.CheckPoint(x, y, isInput)) return output;
return null;
}
}
[Serializable]
class DecidingBlock : Block
{
private readonly (int width, int height) size = (120, 100);
private Rectangle stringRect;
private LinkPoint input;
private LinkPoint trueOutput;
private LinkPoint falseOutput;
private Point[] points = new Point[4];
public DecidingBlock(int x, int y, string inscription) : base(x, y, inscription)
{
input = new LinkPoint(x, y - (size.height / 2), true, this);
trueOutput = new LinkPoint(x + (size.width / 2), y, false, this);
falseOutput = new LinkPoint(x - (size.width / 2), y, false, this);
points[0] = new Point(x - (size.width / 2), y);
points[1] = new Point(x, y - (size.height / 2));
points[2] = new Point(x + (size.width / 2), y);
points[3] = new Point(x, y + (size.height / 2));
stringRect = new Rectangle(new Point(x - (size.width / 4), y - (size.height / 4)), new Size((size.width / 2), (size.height / 2)));
}
// sets as selected
public override void Select()
{
selected = true;
input.SetDrawingLinkPoint();
trueOutput.SetDrawingLinkPoint();
falseOutput.SetDrawingLinkPoint();
}
// draws block with all components
public override void Draw(Graphics canvas)
{
Pen pen = new Pen(Color.Black, 3);
Font font = new Font(new FontFamily("Arial"), 12, FontStyle.Regular, GraphicsUnit.Pixel);
Brush brush = new SolidBrush(Color.Black);
pen.DashStyle = selected ? System.Drawing.Drawing2D.DashStyle.Dash : System.Drawing.Drawing2D.DashStyle.Solid;
canvas.DrawPolygon(pen, points);
canvas.FillPolygon(new SolidBrush(Color.White), points);
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
canvas.DrawString(inscription, font, brush, stringRect, stringFormat);
canvas.DrawString("T", font, brush,layout.X + (size.width / 2) + 3, -12 + Layout.Y);
canvas.DrawString("F", font, brush, Layout.X - (size.width / 2) - 12, -12 + Layout.Y);
input.Draw(canvas);
trueOutput.Draw(canvas);
falseOutput.Draw(canvas);
}
// checks if block was clicked
public override bool Click(int x, int y)
{
double xx = Math.Abs(points[1].X - x) * 7 / 13;
if (xx + points[0].Y - 35 <= y && -xx + points[0].Y + 35 >= y)
{
return true;
}
return false;
}
// changes layout block with all components
public override void SetLayout(int x, int y)
{
stringRect = new Rectangle(new Point(x - (size.width / 4), y - (size.height / 4)), new Size((size.width / 2), (size.height / 2)));
points[0] = new Point(x - (size.width / 2), y);
points[1] = new Point(x, y - (size.height / 2));
points[2] = new Point(x + (size.width / 2), y);
points[3] = new Point(x, y + (size.height / 2));
input.SetLayout(x, y - (size.height / 2));
trueOutput.SetLayout(x+ (size.width / 2), y);
falseOutput.SetLayout(x- (size.width / 2), y);
layout.X = x;
layout.Y = y;
}
// removes block with all components
public override void Remove()
{
input.Remove();
trueOutput.Remove();
falseOutput.Remove();
input = null;
trueOutput = null;
falseOutput = null;
}
// checks wchitch link point was clicked
public override LinkPoint CheckPoint(int x, int y, bool isInput)
{
if (input.CheckPoint(x, y, isInput)) return input;
else if (trueOutput.CheckPoint(x, y, isInput)) return trueOutput;
else if (falseOutput.CheckPoint(x, y, isInput)) return falseOutput;
return null;
}
}
[Serializable]
class StartBlock : Block
{
private readonly (int width, int height) size = (100, 40);
private LinkPoint linkpoint;
private Rectangle rec;
public StartBlock(int x, int y, string inscription) : base(x, y, inscription)
{
linkpoint = new LinkPoint(x, y + (size.height / 2), false, this);
rec = new Rectangle(new Point(x - (size.width/2), y - (size.height/2)), new Size(size.width, size.height));
}
// sets as selected
public override void Select()
{
selected = true;
//pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
linkpoint.SetDrawingLinkPoint();
}
// draws block with all components
public override void Draw(Graphics canvas)
{
Pen pen = new Pen(Color.Black, 3);
Font font = new Font(new FontFamily("Arial"), 12, FontStyle.Regular, GraphicsUnit.Pixel);
Brush brush = new SolidBrush(Color.Black);
pen.DashStyle = selected ? System.Drawing.Drawing2D.DashStyle.Dash : System.Drawing.Drawing2D.DashStyle.Solid;
pen.Color = Color.Green;
canvas.FillEllipse(new SolidBrush(Color.White), rec);
canvas.DrawEllipse(pen, rec);
pen.Color = Color.Black;
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
canvas.DrawString(inscription, font, brush, layout.X, layout.Y, stringFormat);
linkpoint.Draw(canvas);
}
// checks if block was clicked
public override bool Click(int x, int y)
{
int a = 50;
int b = 20;
if ((double)((layout.X - x) * (layout.X - x)) / (double)(a * a) + (double)((layout.Y - y) * (layout.Y - y)) / (double)(b * b) <= 1) return true;
return false;
}
// changes layout block with all components
public override void SetLayout(int x, int y)
{
rec = new Rectangle(new Point(x - (size.width / 2), y - (size.height / 2)), new Size(size.width, size.height));
linkpoint.SetLayout(x, y + (size.height / 2));
layout.X = x;
layout.Y = y;
}
// removes block with all components
public override void Remove()
{
linkpoint.Remove();
linkpoint = null;
}
// checks wchitch link point was clicked
public override LinkPoint CheckPoint(int x, int y, bool isInput)
{
if (linkpoint.CheckPoint(x, y, isInput)) return linkpoint;
else return null;
}
}
[Serializable]
class EndBlock : Block
{
private readonly (int width, int height) size = (100, 40);
private LinkPoint linkpoint;
private Rectangle rec;
public EndBlock(int x, int y, string inscription) : base(x, y, inscription)
{
linkpoint = new LinkPoint(x, y - 20, true, this);
rec = new Rectangle(new Point(x - (size.width / 2), y - (size.height / 2)), new Size(size.width, size.height));
}
// sets as selected
public override void Select()
{
selected = true;
linkpoint.SetDrawingLinkPoint();
}
// draws block with all components
public override void Draw(Graphics canvas)
{
Pen pen = new Pen(Color.Black, 3);
Font font = new Font(new FontFamily("Arial"), 12, FontStyle.Regular, GraphicsUnit.Pixel);
Brush brush = new SolidBrush(Color.Black);
pen.DashStyle = selected ? System.Drawing.Drawing2D.DashStyle.Dash : System.Drawing.Drawing2D.DashStyle.Solid;
pen.Color = Color.Red;
canvas.FillEllipse(new SolidBrush(Color.White), rec);
canvas.DrawEllipse(pen, rec);
pen.Color = Color.Black;
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
canvas.DrawString(inscription, font, brush, layout.X, layout.Y, stringFormat);
linkpoint.Draw(canvas);
}
public override bool Click(int x, int y)
{
int a = 50;
int b = 20;
if ((double)((layout.X - x) * (layout.X - x)) / (double)(a * a) + (double)((layout.Y - y) * (layout.Y - y)) / (double)(b * b) <= 1) return true;
return false;
}
// changes layout block with all components
public override void SetLayout(int x, int y)
{
rec = new Rectangle(new Point(x - (size.width / 2), y - (size.height / 2)), new Size(size.width, size.height));
linkpoint.SetLayout(x, y - (size.height / 2));
layout.X = x;
layout.Y = y;
}
// removes block with all components
public override void Remove()
{
linkpoint.Remove();
linkpoint = null;
}
// checks wchitch link point was clicked
public override LinkPoint CheckPoint(int x, int y, bool isInput)
{
if (linkpoint.CheckPoint(x, y, isInput)) return linkpoint;
else return null;
}
}
[Serializable]
class LinkPoint
{
private readonly (int width, int height) size = (8, 8);
protected bool isDrawingLinkPoint = false;
protected Link link;
protected Rectangle square;
protected bool used = false;
protected bool input = true;
public Point Layout { get; private set; }
public Block Block { get; }
public LinkPoint(Point layout, bool isInput, Block block)
{
square = new Rectangle(new Point(layout.X - (size.width/2), layout.Y - (size.height / 2)), new Size(size.width, size.height));
Layout = layout;
Block = block;
isDrawingLinkPoint = input = isInput;
}
public LinkPoint(int x, int y, bool isInput, Block block)
{
square = new Rectangle(new Point(x - (size.width / 2), y - (size.height / 2)), new Size(size.width, size.height));
Layout = new Point(x, y);
Block = block;
isDrawingLinkPoint = input = isInput;
}
public void Draw(Graphics canvas)
{
if (!used)
{
canvas.DrawEllipse(Pens.Black, square);
canvas.FillEllipse(new SolidBrush(input ? Color.White : Color.Black), square);
}
else
{
if (isDrawingLinkPoint) link.Draw(canvas);
}
}
// changes layout
public void SetLayout(int x, int y)
{
Layout = new Point(x, y);
square = new Rectangle(new Point(x - (size.width / 2), y - (size.height / 2)), new Size(size.width, size.height));
}
// removes link point and link
public void Remove()
{
if (link != null)
{
link.Remove();
link = null;
}
}
// checks if point was clicked
public bool CheckPoint(int x, int y, bool isInput)
{
if (square.Left <= x && square.Right >= x && square.Top <= y && square.Bottom >= y && input == isInput && !used)
{
return true;
}
return false;
}
// sets link
public void SetLink(Link link)
{
if(link != null)
{
this.link = link;
used = true;
}
else
{
isDrawingLinkPoint = true;
used = false;
}
}
// link point draws link
public void SetDrawingLinkPoint()
{
isDrawingLinkPoint = true;
if(link != null) link.ChangeDrawingLinkPoint(this);
}
// link point does not draw link
public void DeleteDrawingLinkPoint()
{
isDrawingLinkPoint = false;
}
}
[Serializable]
class Link
{
protected LinkPoint from;
protected LinkPoint to;
public Link(LinkPoint from, LinkPoint to)
{
this.from = from;
this.to = to;
}
// draws link
public void Draw(Graphics canvas)
{
Pen pen = new Pen(Color.Black, 1);
// create arrow cap
System.Drawing.Drawing2D.GraphicsPath graphicsPath = new System.Drawing.Drawing2D.GraphicsPath();
Point[] points = new Point[3];
points[0] = new Point(0, 0);
points[1] = new Point(-4, -7);
points[2] = new Point(4, -7);
graphicsPath.AddPolygon(points);
pen.CustomEndCap = new System.Drawing.Drawing2D.CustomLineCap(graphicsPath, null);
canvas.DrawLine(pen, from.Layout.X, from.Layout.Y, to.Layout.X, to.Layout.Y);
}
// unlinks linkpoints and remove link
public void Remove()
{
from.SetLink(null);
to.SetLink(null);
}
// sets link point which draw link during drawing
public void ChangeDrawingLinkPoint(LinkPoint linkPoint)
{
if(linkPoint == from)
{
to.DeleteDrawingLinkPoint();
}
else
{
from.DeleteDrawingLinkPoint();
}
}
}
}