-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainPage.xaml.cs
More file actions
607 lines (536 loc) · 20.1 KB
/
MainPage.xaml.cs
File metadata and controls
607 lines (536 loc) · 20.1 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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
using Microsoft.UI.Xaml.Controls;
using Windows.Foundation;
namespace C_Snap_Snap
{
public sealed partial class MainPage : Page
{
public MainPage()
{
InitializeComponent();
canvas.Children.Add(new Variable("test", new Point(20, 20)));
canvas.Children.Add(new Function("test", new Point(20, 20)));
canvas.Children.Add(new IfStatement("test", new Point(20, 20)));
}
/*private readonly Point MouseConstant = new Point(177, 100);
private readonly Color formBackground = Color.FromArgb(51, 51, 51);
private readonly Color menuStripText = Color.FromArgb(204, 204, 204);
private readonly Color menuStripItemText = Color.FromArgb(204, 204, 204);
private readonly Color menuStripItemSelectedText = Color.FromArgb(255, 255, 255);
private readonly Color mainBackground = Color.FromArgb(30, 30, 30);
private readonly Color sidePanelBackground = Color.FromArgb(37, 37, 38);
private readonly Color filesBackground = Color.FromArgb(45, 45, 45);
public static TabControls.MainPanel Files { get; set; } = new TabControls.MainPanel();
private Point MousePos;
private bool initDone = false;
private bool openingFile = false;
private int prevIndex = -1;
private bool resettingLanguage = false;
private readonly List<string> filePaths = new List<string>();
private readonly List<Block> blocks = new List<Block>();
private Block selectedBlock;
private Point distanceFromMouse = new Point(0, 0);
private int counter = 1;
public MainPage()
{
InitializeComponent();
MenuStrip.Renderer = new ToolStripProfessionalRenderer(new MenuStripColorTable());
}
private void Form1_Load(object sender, EventArgs e)
{
this.BackColor = formBackground;
MenuStrip.ForeColor = menuStripText;
foreach (ToolStripMenuItem tab in MenuStrip.Items)
{
foreach (ToolStripItem button in tab.DropDownItems)
{
button.ForeColor = menuStripItemText;
}
}
Files.Size = new Size(splitContainer2.Panel2.Width, splitContainer2.Panel2.Height);
Files.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
//Files.SetStyle(ControlStyles.DoubleBuffered, true);
//Files.DoubleBuffered = true;
Files.Visible = false;
splitContainer2.Panel2.Controls.Add(Files);
splitContainer2.Panel2.BackColor = mainBackground;
splitContainer2.Panel1.BackColor = sidePanelBackground;
splitContainer1.Panel1.BackColor = sidePanelBackground;
BlockCloser.BackColor = mainBackground;
Language.SelectedIndex = 0;
for (int i = 0; i < Blocks.TabCount; i++)
{
Blocks.SelectedIndex = i;
Blocks.SelectedTab.BackColor = sidePanelBackground;
}
Blocks.SelectedIndex = 0;
blocks.Add(new Variable(Blocks.TabPages[0].Name, new Point(0, 0), true)); // need to adjust position
blocks.Add(new IfStatement(Blocks.TabPages[1].Name, new Point(0, 0), true));
//TODO: IfElseStatement - TabPages[1]
//TODO: IfElifElseStatement - TabPages[1]
//TODO: ForLoop - TabPages[2]
//TODO: WhileLoop - TabPages[2]
//TODO: DoWhileLoop - TabPages[2]
//TODO: MathExpressions - TabPages[3]
Blocks.SelectedTab.Invalidate();
initDone = true;
}
private void SplitContainer2_SplitterMoved(object sender, SplitterEventArgs e)
{
if (splitContainer2.SplitterDistance <= 50)
{
splitContainer2.Panel1Collapsed = true;
BlockCloser.Text = ">";
}
else
{
splitContainer2.Panel1Collapsed = false;
BlockCloser.Text = "<";
}
}
private void BlockCloser_Click(object sender, EventArgs e)
{
if (splitContainer2.Panel1Collapsed)
{
splitContainer2.Panel1Collapsed = false;
BlockCloser.Text = "<";
}
else
{
splitContainer2.Panel1Collapsed = true;
BlockCloser.Text = ">";
}
}
private void Language_SelectedIndexChanged(object sender, EventArgs e)
{
if (!initDone) return;
if (resettingLanguage)
{
resettingLanguage = false;
return;
}
if (Files.SelectedIndex != -1)
{
var saveChanges = MessageBox.Show("Any unsaved work will be deleted! Would you like to save changes?", "Warning!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if (saveChanges == DialogResult.Cancel)
{
resettingLanguage = true;
Language.SelectedIndex = prevIndex;
return;
}
}
if (Language.SelectedIndex == 0)
{
Files.Visible = false;
Export.Enabled = false;
prevIndex = 0;
}
else
{
Files.Visible = true;
AddFile(sender, e);
Export.Enabled = true;
prevIndex = Language.SelectedIndex;
}
}
private void AddFile(object sender, EventArgs e)
{
if (!Files.Visible) return;
if (openingFile) return;
SaveFileDialog sfd = new SaveFileDialog
{
RestoreDirectory = true,
Filter = "C-Snap-Snap file (*.snap)|*.snap|All files (*.*)|*.*"
};
var isFileSelected = sfd.ShowDialog();
if (isFileSelected == DialogResult.Cancel)
{
Language.SelectedIndex = prevIndex;
return;
}
foreach (TabPage currentFile in Files.TabPages)
{
if (currentFile.Name == sfd.FileName)
{
Files.SelectedTab = currentFile;
return;
}
}
var file = new FileInfo(sfd.FileName);
filePaths.Add(file.FullName);
TabPage newFile = new TabPage
{
Name = sfd.FileName,
Text = file.Name,
BackColor = mainBackground,
ForeColor = mainBackground
};
newFile.MouseDown += new MouseEventHandler(Files_MouseDown);
newFile.MouseUp += new MouseEventHandler(Files_MouseUp);
newFile.Paint += new PaintEventHandler(NewFiles_Paint);
newFile.MouseMove += new MouseEventHandler(NewFile_MouseMove);
//newFile.DoubleBuffered = true;
Files.TabPages.Add(newFile);
Files.SelectedIndex = Files.TabCount - 1;
blocks.Add(new Function(Files.SelectedTab.Name, null, null, new Point(30, 30), "int", "main", null, false));
File.WriteAllText(sfd.FileName, "");
}
private string FileExt()
{
switch (Language.Text)
{
case "C": return ".c";
case "C#": return ".cs";
case "C++": return ".cpp";
//TODO: add more languages
}
return Language.Text;
}
private void NewFiles_Paint(object sender, PaintEventArgs e)
{
foreach (var block in blocks)
{
if (block.File == Files.SelectedTab.Name || block.File == Blocks.SelectedTab.Name) block.Draw(e.Graphics, block == selectedBlock);
}
/*using (Pen pen = new Pen(Color.Blue, 3))
{
int size = 3;
e.Graphics.DrawEllipse(pen, new Rectangle(MousePos.X - size, MousePos.Y - size, 2 * size, 2 * size));
if (selectedBlock != null) e.Graphics.DrawEllipse(pen, new Rectangle(selectedBlock.Pos.X - size, selectedBlock.Pos.Y - size, 2 * size, 2 * size));
}*/
}
/*private void NewFile_MouseMove(object sender, MouseEventArgs e)
{
MousePos = PointToClient(new Point(Cursor.Position.X - MouseConstant.X, Cursor.Position.Y - MouseConstant.Y)); // need to change MouseConstant to change dynamically with screen
if (selectedBlock == null) return;
selectedBlock.Pos = new Point(e.X + distanceFromMouse.X, e.Y + distanceFromMouse.Y);
UpdatePos();
Files.SelectedTab.Invalidate();
}
private void Files_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (blocks.Count == 0) return;
foreach (var block in blocks)
{
if (block.IsHover(MousePos) >= 0)
{
if (block.File == Files.SelectedTab.Name || block.File == Blocks.SelectedTab.Name) selectedBlock = block;
}
}
if (selectedBlock != null)
{
if (selectedBlock.IsDefault)
{
selectedBlock = selectedBlock.Clone();
blocks.Add(selectedBlock);
}
selectedBlock.UnSnap();
distanceFromMouse = new Point(selectedBlock.Pos.X - MousePos.X, selectedBlock.Pos.Y - MousePos.Y);
}
}
}
private void Files_MouseUp(object sender, MouseEventArgs e)
{
if (selectedBlock == null) return;
foreach (var block in blocks)
{
if (selectedBlock == block) continue;
int section = block.IsHover(MousePos);
if (section >= 0)
{
block.SnapTo(selectedBlock, section);
Files.SelectedTab.Invalidate();
break;
}
}
selectedBlock = null;
}
private void UpdatePos()
{
selectedBlock.UpdatePos(new Point(MousePos.X + distanceFromMouse.X, MousePos.Y + distanceFromMouse.Y));
}
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
var saveChanges = MessageBox.Show("Any unsaved work will be deleted! Would you like to save changes?", "Warning!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if (saveChanges == DialogResult.Cancel) e.Cancel = true;
else if (saveChanges == DialogResult.Yes)
{
// save changes to .snap file
}
}
// temporary method because blocks are drawn on main drawing area
private void Blocks_SelectedIndexChanged(object sender, EventArgs e)
{
Files.SelectedTab?.Invalidate();
}
private void Export_Click(object sender, EventArgs e)
{
TabPage file = Files.SelectedTab;
string output = "#include <iostream>\nusing namespace std;\n";
foreach (Block b in blocks)
{
if (b is Function) output += ExportFunction(b as Function);
}
int index = file.Name.LastIndexOf(".");
File.WriteAllText(file.Name.Substring(0, index) + FileExt(), output);
}
private string ExportBlock(Block b)
{
if (b is Variable) return ExportVariable(b as Variable);
else if (b is IfStatement) return ExportIfStatement(b as IfStatement);
else if (b is Function) return ExportFunction(b as Function);
// will add more
else return b.ToString();
}
private string ExportVariable(Variable block)
{
return new string('\t', counter) + block.ToString();
}
private string ExportIfStatement(IfStatement block)
{
string output = new string('\t', counter) + block.ToString();
if (block.Inside == null) return output += new string('\t', counter) + "}\n";
counter++;
Block next = block.Inside;
output += ExportBlock(next);
while (next.Next != null)
{
next = next.Next;
output += ExportBlock(next);
}
counter--;
output += new string('\t', counter) + "}\n";
return output;
}
private string ExportFunction(Function block)
{
string output = block.ToString();
Block next = block;
while (next.Next != null)
{
next = next.Next;
output += ExportBlock(next);
}
return output + "}";
}
private static Point GetPoint(string str)
{
string[] strs = str.Substring(1, str.Length - 2).Split(',');
return new Point(int.Parse(strs[0]), int.Parse(strs[1]));
}
private void AddBlocksFromFile(OpenFileDialog ofd)
{
StreamReader reader = File.OpenText(ofd.FileName);
List<Block> tempBlocks = new List<Block>();
string line;
string type = null;
string[] components = null;
int i = 0;
while ((line = reader.ReadLine()) != null)
{
if (components != null)
{
if (i == components.Length)
{
switch (type)
{
case "Variable": tempBlocks.Add(new Variable(ofd.FileName, null, null, GetPoint(components[3]), components[4], components[5], components[6], false)); break;
case "IfStatement": tempBlocks.Add(new IfStatement(ofd.FileName, null, null, GetPoint(components[3]), null, components[5], false)); break;
case "Function": tempBlocks.Add(new Function(ofd.FileName, null, null, GetPoint(components[3]), components[4], components[5], components[6] == "null" ? "" : components[6], false)); break;
}
components = null;
i = 0;
continue;
}
components[i] = line.Split(':')[1];
i++;
}
else if (line.StartsWith("Language:"))
{
Language.SelectedItem = line.Split(':')[1];
if (Language.SelectedIndex == 0)
{
MessageBox.Show("Unsupported file language!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
else if (line.StartsWith("\tVariable:"))
{
type = "Variable";
components = new string[7];
}
else if (line.StartsWith("\tIfStatement"))
{
type = "IfStatement";
components = new string[6];
}
else if (line.StartsWith("\tFunction"))
{
type = "Function";
components = new string[7];
}
}
blocks.AddRange(tempBlocks);
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openingFile = true;
OpenFileDialog ofd = new OpenFileDialog
{
Filter = "Snap Files (*.snap)|*.snap|All Files (*.*)|*.*"
};
DialogResult isFileSelected = ofd.ShowDialog();
if (isFileSelected == DialogResult.Cancel)
{
return;
}
foreach (TabPage currentFile in Files.TabPages)
{
if (currentFile.Name == ofd.FileName)
{
Files.SelectedTab = currentFile;
return;
}
}
AddBlocksFromFile(ofd);
Files.Visible = true;
FileInfo file = new FileInfo(ofd.FileName);
filePaths.Add(file.FullName);
TabPage newFile = new TabPage
{
Name = ofd.FileName,
Text = file.Name,
BackColor = formBackground,
ForeColor = formBackground
};
newFile.MouseDown += new MouseEventHandler(Files_MouseDown);
newFile.MouseUp += new MouseEventHandler(Files_MouseUp);
newFile.Paint += new PaintEventHandler(NewFiles_Paint);
newFile.MouseMove += new MouseEventHandler(NewFile_MouseMove);
Files.TabPages.Add(newFile);
Files.SelectedIndex = Files.TabCount - 1;
openingFile = false;
splitContainer2.Panel2.BackColor = filesBackground;
}
private void exportToolStripMenuItem_Click(object sender, EventArgs e)
{
Export_Click(sender, e);
}
private void exportAllToolStripMenuItem_Click(object sender, EventArgs e)
{
TabPage current = Files.SelectedTab;
foreach (TabPage file in Files.TabPages)
{
Files.SelectedTab = file;
Export_Click(sender, e);
}
Files.SelectedTab = current;
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
AddFile(sender, e);
}
}
public class MenuStripColorTable : ProfessionalColorTable
{
public override Color ToolStripDropDownBackground
{
get
{
return Color.FromArgb(30, 30, 30);
}
}
public override Color ImageMarginGradientBegin
{
get
{
return Color.FromArgb(30, 30, 30);
}
}
public override Color ImageMarginGradientMiddle
{
get
{
return Color.FromArgb(30, 30, 30);
}
}
public override Color ImageMarginGradientEnd
{
get
{
return Color.FromArgb(30, 30, 30);
}
}
public override Color MenuBorder
{
get
{
return Color.FromArgb(69, 69, 69);
}
}
public override Color MenuItemBorder
{
get
{
return Color.FromArgb(30, 30, 30);
}
}
public override Color MenuItemSelected
{
get
{
return Color.FromArgb(4, 57, 94);
}
}
public override Color MenuStripGradientBegin
{
get
{
return Color.FromArgb(60, 60, 60);
}
}
public override Color MenuStripGradientEnd
{
get
{
return Color.FromArgb(60, 60, 60);
}
}
public override Color MenuItemSelectedGradientBegin
{
get
{
return Color.FromArgb(69, 70, 70);
}
}
public override Color MenuItemSelectedGradientEnd
{
get
{
return Color.FromArgb(69, 70, 70);
}
}
public override Color MenuItemPressedGradientBegin
{
get
{
return Color.FromArgb(69, 70, 70);
}
}
public override Color MenuItemPressedGradientEnd
{
get
{
return Color.FromArgb(69, 70, 70);
}
}
public override Color SeparatorDark
{
get
{
return Color.FromArgb(69, 69, 69);
}
}
}*/
}