-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
350 lines (307 loc) · 8.54 KB
/
MainForm.cs
File metadata and controls
350 lines (307 loc) · 8.54 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace Hotwire
{
public partial class MainForm : Form
{
private DebugForm _debugForm;
private Configuration _configuration;
private Data _data = new Data();
private HotwirePort _port;
private bool _settingOrigin;
public MainForm()
{
InitializeComponent();
panelPreview.Data = _data;
string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
path = Path.Combine(path, "configuration.xml");
_configuration = LoadData<Configuration>(path);
if (_configuration == null)
{
MessageBox.Show("No previous configuration could be loaded. Please check your settings!");
_configuration = new Configuration();
}
_debugForm = new DebugForm();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private T LoadData<T>(string path) where T : class
{
if (File.Exists(path))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
try
{
using (Stream stream = File.OpenRead(path))
return (T)serializer.Deserialize(stream);
}
catch
{ }
}
return null;
}
private void SaveData<T>(string path, T data)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
try
{
using (Stream stream = File.Open(path, FileMode.Create))
serializer.Serialize(stream, data);
}
catch
{ }
}
private void configurationToolStripMenuItem_Click(object sender, EventArgs e)
{
using (ConfigurationDialog dialog = new ConfigurationDialog())
{
dialog.Configuration = _configuration;
if (dialog.ShowDialog() == DialogResult.OK)
{
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "configuration.xml");
SaveData<Configuration>(path, _configuration);
}
}
}
private string ShowFileOpenDialog()
{
using (OpenFileDialog dialog = new OpenFileDialog())
{
dialog.CheckFileExists = true;
dialog.RestoreDirectory = true;
dialog.Filter = "*.dat|*.dat|All files (*.*)|*.*";
if (dialog.ShowDialog() == DialogResult.OK)
return dialog.FileName;
}
return null;
}
private void buttonBrowseLeft_Click(object sender, EventArgs e)
{
string path = ShowFileOpenDialog();
if (!string.IsNullOrEmpty(path))
{
textBoxInputDataLeft.Text = path;
_data.LeftInputPath = path;
RefreshPreview();
}
}
private void buttonBrowseRight_Click(object sender, EventArgs e)
{
string path = ShowFileOpenDialog();
if (!string.IsNullOrEmpty(path))
{
textBoxInputDataRight.Text = path;
_data.RightInputPath = path;
RefreshPreview();
}
}
private void RefreshPreview()
{
panelPreview.Refresh();
}
private void buttonLoadDataLeft_Click(object sender, EventArgs e)
{
if (File.Exists(_data.LeftInputPath))
{
Profile profile = new Profile();
profile.FileName = _data.LeftInputPath;
_data.LeftProfile = profile;
RefreshPreview();
}
}
private void buttonLoadDataRight_Click(object sender, EventArgs e)
{
if (File.Exists(_data.RightInputPath))
{
Profile profile = new Profile();
profile.FileName = _data.RightInputPath;
_data.RightProfile = profile;
RefreshPreview();
}
}
private void textBoxInputDataLeft_TextChanged(object sender, EventArgs e)
{
try
{
if (File.Exists(textBoxInputDataLeft.Text))
_data.LeftInputPath = textBoxInputDataLeft.Text;
}
catch
{ }
}
private void textBoxInputDataRight_TextChanged(object sender, EventArgs e)
{
try
{
if (File.Exists(textBoxInputDataRight.Text))
_data.RightInputPath = textBoxInputDataRight.Text;
}
catch
{ }
}
private void textBoxScaleLeft_TextChanged(object sender, EventArgs e)
{
double scale;
if (double.TryParse(textBoxScaleLeft.Text, out scale))
{
_data.ScaleLeft = scale;
RefreshPreview();
}
}
private void textBoxScaleRight_TextChanged(object sender, EventArgs e)
{
double scale;
if (double.TryParse(textBoxScaleRight.Text, out scale))
{
_data.ScaleRight = scale;
RefreshPreview();
}
}
private void textBoxOffsetXLeft_TextChanged(object sender, EventArgs e)
{
double offset;
if (double.TryParse(textBoxOffsetXLeft.Text, out offset))
{
_data.LeftXOffset = offset;
RefreshPreview();
}
}
private void textBoxOffsetXRight_TextChanged(object sender, EventArgs e)
{
double offset;
if (double.TryParse(textBoxOffsetXRight.Text, out offset))
{
_data.RightXOffset = offset;
RefreshPreview();
}
}
private void textBoxOffsetYLeft_TextChanged(object sender, EventArgs e)
{
double offset;
if (double.TryParse(textBoxOffsetYLeft.Text, out offset))
{
_data.LeftYOffset = offset;
RefreshPreview();
}
}
private void textBoxOffsetYRight_TextChanged(object sender, EventArgs e)
{
double offset;
if (double.TryParse(textBoxOffsetYRight.Text, out offset))
{
_data.RightYOffset = offset;
RefreshPreview();
}
}
private void ValidateDouble(object sender, CancelEventArgs e)
{
double value;
e.Cancel = !double.TryParse(((TextBox)sender).Text, out value);
}
private void manualMovementsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (_port == null || !_port.IsOpen)
MessageBox.Show("You need to choose the port before using it");
else
{
using (ManualControlDialog dialog = new ManualControlDialog())
{
dialog.Port = _port;
dialog.Configuration = _configuration;
dialog.ShowDialog();
}
}
}
private void toolStripMenuItemPortSelection_DropDownOpening(object sender, EventArgs e)
{
string[] portNames = SerialPort.GetPortNames();
toolStripMenuItemPortSelection.DropDownItems.Clear();
toolStripMenuItemPortSelection.DropDownItems.AddRange(portNames.Select(n => new ToolStripMenuItem(n, null, PortSelected)).ToArray());
}
private void PortSelected(object sender, EventArgs e)
{
string portName = ((ToolStripMenuItem)sender).Text;
if (_port != null && _port.IsOpen)
{
_port.Close();
_port.Dispose();
}
_port = new HotwirePort(portName, 9600);
_port.Open();
_debugForm.Port = _port;
}
private void textBoxTotalDisplayScale_TextChanged(object sender, EventArgs e)
{
double scale;
if (double.TryParse(textBoxTotalDisplayScale.Text, out scale))
{
_data.ViewScale = scale;
RefreshPreview();
}
}
private void UpdateOrigin(MouseEventArgs e)
{
double minx, maxx, miny, maxy, scale, width, height;
IEnumerable<Vector2> leftProfile, rightProfile;
_data.ProcessProfiles(panelPreview.Width, out leftProfile, out rightProfile, out minx, out maxx, out miny, out maxy, out scale, out width, out height);
double xPoint = (e.X - (panelPreview.Width - width) / 2) / scale + minx;
double yPoint = ((panelPreview.Height + height / 2) / 2 - e.Y) / scale + miny;
labelInfo.Text = string.Format("Setting origin at {0} / {1} (Adapt configuration to match lengths!)", xPoint, yPoint);
_data.Origin = new Vector2(xPoint, yPoint);
RefreshPreview();
}
private void panelPreview_MouseDown(object sender, MouseEventArgs e)
{
_settingOrigin = true;
UpdateOrigin(e);
}
private void panelPreview_MouseUp(object sender, MouseEventArgs e)
{
_settingOrigin = false;
UpdateOrigin(e);
}
private void panelPreview_MouseMove(object sender, MouseEventArgs e)
{
if (_settingOrigin)
UpdateOrigin(e);
}
private void buttonCut_Click(object sender, EventArgs e)
{
double minx, maxx, miny, maxy, scale, width, height;
IEnumerable<Vector2> leftProfile, rightProfile;
_data.ProcessProfiles(panelPreview.Width, out leftProfile, out rightProfile, out minx, out maxx, out miny, out maxy, out scale, out width, out height);
HotwireControl control = new HotwireControl(_configuration, _port);
Vector2 last = null;
foreach (Vector2 point in leftProfile)
{
if (last != null)
{
Vector2 vec = point - last;
control.MoveRelative(vec.x, vec.y);
}
last = point;
}
}
private void showHideDebugInformationToolStripMenuItem_Click(object sender, EventArgs e)
{
if (_debugForm.Visible)
_debugForm.Hide();
else
_debugForm.Show();
showHideDebugInformationToolStripMenuItem.Checked = _debugForm.Visible;
}
}
}