-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMainForm.cs
392 lines (354 loc) · 16.2 KB
/
MainForm.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
namespace GCEd
{
public partial class MainForm : Form
{
private ViewState viewState;
private Settings settings;
public MainForm()
{
InitializeComponent();
DoubleBuffered = true;
settings = new Settings();
canvas.Settings = settings;
viewState = new ViewState();
canvas.ViewState = viewState;
operationsList.ViewState = viewState;
operationProperties.ViewState = viewState;
lineEditor.ViewState = viewState;
viewState.NewProgram();
KeyPreview = true;
Icon = System.Drawing.Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
}
private bool ConfirmAbandonDirty()
{
if (!viewState.IsDirty) return true;
var result = MessageBox.Show(viewState.CurrentFile == null ? "Save current file?" : $"Save \"{viewState.CurrentFile}\"?", "GCEd", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
if (result == DialogResult.Cancel) return false;
if (result == DialogResult.No) return true;
if (viewState.CurrentFile != null)
{
viewState.SaveProgram(viewState.CurrentFile);
}
else
{
if (saveFileDialog.ShowDialog() != DialogResult.OK) return false;
viewState.SaveProgram(saveFileDialog.FileName);
}
return true;
}
private void NewFile()
{
if (!ConfirmAbandonDirty()) return;
viewState.SaveUndoState();
Environment.CurrentDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
viewState.NewProgram();
Text = "GCEd";
}
private void SaveFile()
{
if (viewState.CurrentFile == null) SaveFileAs();
else viewState.SaveProgram(viewState.CurrentFile);
}
private void SaveFileAs()
{
if (saveFileDialog.ShowDialog() != DialogResult.OK) return;
Environment.CurrentDirectory = Path.GetDirectoryName(saveFileDialog.FileName)!;
viewState.SaveProgram(saveFileDialog.FileName);
Text = "GCEd [" + saveFileDialog.FileName + "]";
}
private void OpenFile()
{
if (openFileDialog.ShowDialog() != DialogResult.OK) return;
if (!ConfirmAbandonDirty()) return;
viewState.SaveUndoState();
Environment.CurrentDirectory = Path.GetDirectoryName(openFileDialog.FileName)!;
viewState.LoadProgram(openFileDialog.FileName);
Text = "GCEd [" + openFileDialog.FileName + "]";
}
private void SelectAll(bool skipRapid)
{
var selectedOperations = new List<GOperation>();
foreach (var operation in viewState.Operations)
{
if (!operation.Line.IsVisible) continue;
if (operation.Line.Instruction == GInstruction.G0 && skipRapid) continue;
selectedOperations.Add(operation);
}
viewState.SetSelection(selectedOperations);
}
private void DeleteSelected()
{
viewState.SaveUndoState();
viewState.DeleteOperations(viewState.SelectedOperations);
}
private void Undo()
{
viewState.Undo();
}
private void Redo()
{
viewState.Redo();
}
private void ConvertToAbsolute()
{
viewState.SaveUndoState();
viewState.ConvertToAbsolute(viewState.SelectedOperations);
}
private void ConvertToRelative()
{
viewState.SaveUndoState();
viewState.ConvertToRelative(viewState.SelectedOperations);
}
private void ConvertToOutline()
{
using var form = new OutlineParameters();
if (form.ShowDialog() != DialogResult.OK) return;
viewState.SaveUndoState();
viewState.ConvertToOutline(viewState.SelectedOperations, form.Thickness, form.LeftFirst, form.SkipLeft, form.SkipRight, form.BothForward);
}
private void AddNewLine(bool before)
{
viewState.SaveUndoState();
viewState.AppendNewLine(viewState.LastSelectedOperation, before);
viewState.FocusLineEditor();
}
private void AddComment(bool before)
{
viewState.SaveUndoState();
viewState.AppendNewLine(viewState.LastSelectedOperation, before, new GLine("; "));
viewState.FocusLineEditor();
}
private void ToggleComment()
{
viewState.SaveUndoState();
viewState.ToggleComment(viewState.SelectedOperations);
}
private void AddG0(bool before)
{
canvas.SuspendPanningToSelection();
viewState.SaveUndoState();
viewState.AppendNewLine(viewState.LastSelectedOperation, before, new GLine { Instruction = GInstruction.G0 });
canvas.StartMouseEndMove();
canvas.ResumePanningToSelection();
}
private void AddG1(bool before)
{
canvas.SuspendPanningToSelection();
viewState.SaveUndoState();
viewState.AppendNewLine(viewState.LastSelectedOperation, before, new GLine { Instruction = GInstruction.G1 });
canvas.StartMouseEndMove();
canvas.ResumePanningToSelection();
}
private void AddG2(bool before)
{
canvas.SuspendPanningToSelection();
viewState.SaveUndoState();
viewState.AppendNewLine(viewState.LastSelectedOperation, before, new GLine { Instruction = GInstruction.G2 });
canvas.StartMouseEndMove();
canvas.ResumePanningToSelection();
}
private void AddG3(bool before)
{
canvas.SuspendPanningToSelection();
viewState.SaveUndoState();
viewState.AppendNewLine(viewState.LastSelectedOperation, before, new GLine { Instruction = GInstruction.G3 });
canvas.StartMouseEndMove();
canvas.ResumePanningToSelection();
}
private void AddBackground()
{
using var openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Images (*.bmp, *.jpg, *.png, *.gif)|*.bmp;*.jpg;*.jpeg;*.png;*.gif|All files|*.*";
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() != DialogResult.OK) return;
viewState.AppendNewLine(null, true, new GLine(";.background X0 Y0 I100 J100 S100 P\"" + Path.GetRelativePath(Path.GetDirectoryName(viewState.CurrentFile) ?? Environment.CurrentDirectory, openFileDialog.FileName) + "\""));
}
private void AddText()
{
using var textGenerator = new TextGenerator();
if (textGenerator.ShowDialog() != DialogResult.OK || textGenerator.Program == null) return;
var subProgram = textGenerator.Program;
viewState.SaveUndoState();
var baseOperation = viewState.LastSelectedOperation;
if (baseOperation == null) baseOperation = viewState.Operations.LastOrDefault();
if (baseOperation == null || !baseOperation.Absolute)
{
subProgram.Lines.AddFirst(new GLine { Instruction = GInstruction.G90 });
subProgram.Lines.AddLast(new GLine { Instruction = GInstruction.G91 });
}
viewState.AppendProgram(viewState.LastSelectedOperation, subProgram);
}
private void Copy()
{
var code = String.Join(Environment.NewLine, viewState.SelectedOperations.Select(operation => operation.Line.ToString()));
Clipboard.SetText(code);
}
private void Cut()
{
Copy();
DeleteSelected();
}
private void Paste(bool before)
{
var code = Clipboard.GetText();
if (String.IsNullOrWhiteSpace(code)) return;
var subProgram = new GProgram();
subProgram.Load(new StringReader(code));
viewState.SaveUndoState();
viewState.AppendProgram(viewState.LastSelectedOperation, subProgram, before);
}
public void ToggleGrid()
{
if (settings.ShowMinorGrid)
{
settings.ShowMinorGrid = false;
settings.ShowMajorGrid = false;
settings.ShowOriginGrid = false;
}
else if (!settings.ShowOriginGrid) settings.ShowOriginGrid = true;
else if (!settings.ShowMajorGrid) settings.ShowMajorGrid = true;
else settings.ShowMinorGrid = true;
canvas.Invalidate();
}
public void ToggleSnapToGrid()
{
settings.SnapToGrid = !settings.SnapToGrid;
}
public void ToggleSnapToItems()
{
settings.SnapToItems = !settings.SnapToItems;
}
public void ToggleSnapToAxes()
{
settings.SnapToAxes = !settings.SnapToAxes;
}
public void ToggleFPS()
{
settings.ShowFPS = !settings.ShowFPS;
canvas.Invalidate();
}
public void ToggleCoords()
{
settings.ShowCursorCoords = !settings.ShowCursorCoords;
if (!settings.ShowCursorCoords) settings.ShowItemCoords = !settings.ShowItemCoords;
canvas.Invalidate();
}
private void About()
{
using var about = new About();
about.ShowDialog();
}
protected override void OnKeyDown(KeyEventArgs e)
{
e.Handled = true;
var editorFocused = false;
IContainerControl? container = this;
while (container != null)
{
editorFocused = container.ActiveControl is TextBox;
container = container.ActiveControl as IContainerControl;
}
if (e.KeyCode == Keys.N && e.Modifiers == Keys.Control) NewFile();
else if (e.KeyCode == Keys.O && e.Modifiers == Keys.Control) OpenFile();
else if (e.KeyCode == Keys.S && e.Modifiers == Keys.Control) SaveFile();
else if (e.KeyCode == Keys.S && e.Modifiers == (Keys.Control | Keys.Shift)) SaveFileAs();
else if (e.KeyCode == Keys.Menu) { }
else if (editorFocused) { e.Handled = false; }
else if (e.KeyCode == Keys.D0 && e.Modifiers == Keys.None) AddG0(false);
else if (e.KeyCode == Keys.D0 && e.Modifiers == Keys.Shift) AddG0(true);
else if (e.KeyCode == Keys.D1 && e.Modifiers == Keys.None) AddG1(false);
else if (e.KeyCode == Keys.D1 && e.Modifiers == Keys.Shift) AddG1(true);
else if (e.KeyCode == Keys.D2 && e.Modifiers == Keys.None) AddG2(false);
else if (e.KeyCode == Keys.D2 && e.Modifiers == Keys.Shift) AddG2(true);
else if (e.KeyCode == Keys.D3 && e.Modifiers == Keys.None) AddG3(false);
else if (e.KeyCode == Keys.D3 && e.Modifiers == Keys.Shift) AddG3(true);
else if (e.KeyCode == Keys.A && e.Modifiers == Keys.None) ConvertToAbsolute();
else if (e.KeyCode == Keys.A && e.Modifiers == Keys.Shift) ConvertToRelative();
else if (e.KeyCode == Keys.A && e.Modifiers == Keys.Control) SelectAll(false);
else if (e.KeyCode == Keys.A && e.Modifiers == (Keys.Control | Keys.Shift)) SelectAll(true);
else if (e.KeyCode == Keys.A && e.Modifiers == Keys.Alt) ToggleSnapToAxes();
else if (e.KeyCode == Keys.B && e.Modifiers == Keys.Control) AddBackground();
else if (e.KeyCode == Keys.C && e.Modifiers == Keys.None) ToggleCoords();
else if (e.KeyCode == Keys.C && e.Modifiers == Keys.Control) Copy();
else if (e.KeyCode == Keys.E && e.Modifiers == Keys.None) canvas.StartMouseEndMove();
else if (e.KeyCode == Keys.E && e.Modifiers == Keys.Shift) canvas.StartMouseOffsetMove();
else if (e.KeyCode == Keys.F && e.Modifiers == Keys.None) ToggleFPS();
else if (e.KeyCode == Keys.G && e.Modifiers == Keys.None) ToggleGrid();
else if (e.KeyCode == Keys.G && e.Modifiers == Keys.Alt) ToggleSnapToGrid();
else if (e.KeyCode == Keys.H && e.Modifiers == Keys.None) canvas.PanZoomViewToFit();
else if (e.KeyCode == Keys.H && e.Modifiers == Keys.Shift) canvas.PanViewToSelection();
else if (e.KeyCode == Keys.I && e.Modifiers == Keys.None) AddNewLine(false);
else if (e.KeyCode == Keys.I && e.Modifiers == Keys.Shift) AddNewLine(true);
else if (e.KeyCode == Keys.I && e.Modifiers == Keys.Alt) ToggleSnapToItems();
else if (e.KeyCode == Keys.O && e.Modifiers == Keys.None) ConvertToOutline();
else if (e.KeyCode == Keys.P && e.Modifiers == Keys.None) canvas.StartMousePan();
else if (e.KeyCode == Keys.R && e.Modifiers == Keys.None) canvas.StartMouseRotate();
else if (e.KeyCode == Keys.S && e.Modifiers == Keys.None) canvas.StartMouseScale();
else if (e.KeyCode == Keys.T && e.Modifiers == Keys.None) canvas.StartMouseTranslate();
else if (e.KeyCode == Keys.T && e.Modifiers == Keys.Control) AddText();
else if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control) Paste(false);
else if (e.KeyCode == Keys.V && e.Modifiers == (Keys.Control | Keys.Shift)) Paste(true);
else if (e.KeyCode == Keys.X && e.Modifiers == Keys.Control) Cut();
else if (e.KeyCode == Keys.Y && e.Modifiers == Keys.Control) Redo();
else if (e.KeyCode == Keys.Z && e.Modifiers == Keys.Control) Undo();
else if (e.KeyCode == Keys.Delete && e.Modifiers == Keys.None) DeleteSelected();
else if (e.KeyCode == Keys.Escape) canvas.Abort();
else if (e.KeyCode == Keys.OemSemicolon && e.Modifiers == Keys.None) AddComment(false);
else if (e.KeyCode == Keys.OemSemicolon && e.Modifiers == Keys.Shift) AddComment(true);
else if (e.KeyCode == Keys.OemQuestion && e.Modifiers == Keys.None) ToggleComment();
else if (e.KeyCode == Keys.Oemplus && e.Modifiers == Keys.None) canvas.Zoom(1.1f);
else if (e.KeyCode == Keys.OemMinus && e.Modifiers == Keys.None) canvas.Zoom(1 / 1.1f);
else { e.Handled = false; }
if (e.Handled && e.Alt) { e.SuppressKeyPress = true; }
base.OnKeyDown(e);
}
private void newToolStripMenuItem_Click(object sender, EventArgs e) => NewFile();
private void openToolStripMenuItem_Click(object sender, EventArgs e) => OpenFile();
private void saveToolStripMenuItem_Click(object sender, EventArgs e) => SaveFile();
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) => SaveFileAs();
private void exitToolStripMenuItem_Click(object sender, EventArgs e) => Close();
private void undoToolStripMenuItem_Click(object sender, EventArgs e) => Undo();
private void redoToolStripMenuItem_Click(object sender, EventArgs e) => Redo();
private void cutToolStripMenuItem_Click(object sender, EventArgs e) => Cut();
private void copyToolStripMenuItem_Click(object sender, EventArgs e) => Copy();
private void pasteToolStripMenuItem_Click(object sender, EventArgs e) => Paste(false);
private void deleteToolStripMenuItem_Click(object sender, EventArgs e) => DeleteSelected();
private void selectAllToolStripMenuItem_Click(object sender, EventArgs e) => SelectAll(false);
private void convertToAbsoluteToolStripMenuItem_Click(object sender, EventArgs e) => ConvertToAbsolute();
private void convertToRelativeToolStripMenuItem_Click(object sender, EventArgs e) => ConvertToRelative();
private void moveEndpointToolStripMenuItem_Click(object sender, EventArgs e) => canvas.StartMouseEndMove();
private void moveOffsetToolStripMenuItem_Click(object sender, EventArgs e) => canvas.StartMouseOffsetMove();
private void showGridToolStripMenuItem_Click(object sender, EventArgs e) => ToggleGrid();
private void snapToGridToolStripMenuItem_Click(object sender, EventArgs e) => ToggleSnapToGrid();
private void snapToItemsToolStripMenuItem_Click(object sender, EventArgs e) => ToggleSnapToItems();
private void showCoordinatesToolStripMenuItem_Click(object sender, EventArgs e) => ToggleCoords();
private void showPerformanceStatsToolStripMenuItem_Click(object sender, EventArgs e) => ToggleFPS();
private void newLineAfterCurrentToolStripMenuItem_Click(object sender, EventArgs e) => AddNewLine(false);
private void newLineToolStripMenuItem_Click(object sender, EventArgs e) => AddNewLine(true);
private void rapidToolStripMenuItem_Click(object sender, EventArgs e) => AddG0(false);
private void lineToolStripMenuItem_Click(object sender, EventArgs e) => AddG1(false);
private void clockwiseArcToolStripMenuItem_Click(object sender, EventArgs e) => AddG2(false);
private void counterclockwiseArcToolStripMenuItem_Click(object sender, EventArgs e) => AddG3(false);
private void commentToolStripMenuItem_Click(object sender, EventArgs e) => AddComment(false);
private void commentBeforeCurrentToolStripMenuItem_Click(object sender, EventArgs e) => AddComment(true);
private void backgroundImageToolStripMenuItem_Click(object sender, EventArgs e) => AddBackground();
private void textShapeToolStripMenuItem_Click(object sender, EventArgs e) => AddText();
private void aboutToolStripMenuItem_Click(object sender, EventArgs e) => About();
private void panzoomToFitToolStripMenuItem_Click(object sender, EventArgs e) => canvas.PanZoomViewToFit();
private void panViewToSelectionToolStripMenuItem_Click(object sender, EventArgs e) => canvas.PanViewToSelection();
private void translateToolStripMenuItem_Click(object sender, EventArgs e) => canvas.StartMouseTranslate();
private void snapToAxesToolStripMenuItem_Click(object sender, EventArgs e) => ToggleSnapToAxes();
private void convertToOutlineToolStripMenuItem_Click(object sender, EventArgs e) => ConvertToOutline();
private void rotateToolStripMenuItem_Click(object sender, EventArgs e) => canvas.StartMouseRotate();
private void commentUncommentLinesToolStripMenuItem_Click(object sender, EventArgs e) => ToggleComment();
private void zoomInToolStripMenuItem_Click(object sender, EventArgs e) => canvas.Zoom(1.5f);
private void zoomOutToolStripMenuItem_Click(object sender, EventArgs e) => canvas.Zoom(1 / 1.5f);
private void panViewToolStripMenuItem_Click(object sender, EventArgs e) => canvas.StartMousePan();
}
}