-
Notifications
You must be signed in to change notification settings - Fork 2
/
MainForm.cs
436 lines (383 loc) · 16.4 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
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
/*
Maneubo is an application that provides a virtual maneuvering board and target
motion analysis.
http://www.adammil.net/Maneubo
Copyright (C) 2011-2020 Adam Milazzo
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using AdamMil.Utilities;
namespace Maneubo
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
board.WasChanged = false; // prevent the setting of default property values from affecting whether the board is considered changed
NewBoard();
board.SelectedTool = board.AddUnitTool;
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
base.OnClosing(e);
if(!e.Cancel && !TrySaveChanges()) e.Cancel = true;
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if(!e.Handled && e.Modifiers == Keys.None)
{
if(e.KeyCode == Keys.P) tbPointer.PerformClick();
else if(e.KeyCode == Keys.U) tbAddUnit.PerformClick();
else if(e.KeyCode == Keys.O) tbAddObservation.PerformClick();
else if(e.KeyCode == Keys.T) tbTMA.PerformClick();
else if(e.KeyCode == Keys.L) tbLine.PerformClick();
else if(e.KeyCode == Keys.C) tbCircle.PerformClick();
else if(e.KeyCode == Keys.I) tbIntercept.PerformClick();
else if(e.KeyCode == Keys.F3) tbUnitShape.ShowDropDown();
else if(e.KeyCode == Keys.F4) tbWaypointType.ShowDropDown();
else return;
e.Handled = true;
}
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if(m.Msg == 0x312) // WM_HOTKEY
{
switch(m.WParam.ToInt32())
{
case SaveTimeKeyId:
if(stopwatch != null && !stopwatch.IsDisposed) stopwatch.SaveTime();
break;
case ToggleStopwatchKeyId:
if(stopwatch != null && !stopwatch.IsDisposed) stopwatch.Toggle();
break;
}
}
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
// see if NoRepeat is supported. the actual key registered doesn't matter, since we'll immediately unregister it
noRepeatSupported = RegisterHotKey(Handle, 0, HkAlt|HkCtrl|HkShift|HkNoRepeat, 'W');
UnregisterHotKey(Handle, 0);
string dataDirectory = Program.GetDataDirectory();
if(dataDirectory != null)
{
string configFile = Path.Combine(dataDirectory, "config.txt");
if(File.Exists(configFile))
{
using(StreamReader reader = new StreamReader(configFile))
{
while(true)
{
string line = reader.ReadLine();
if(line == null) break;
line = line.Trim();
if(line.Length == 0) continue;
int equals = line.IndexOf('=');
if(equals == -1) continue;
string key = line.Substring(0, equals).Trim(), value = line.Substring(equals+1).Trim();
switch(key.ToLowerInvariant())
{
case "savetimehotkey": int.TryParse(value, out saveTimeKey); break;
case "togglestopwatchhotkey": int.TryParse(value, out toggleStopwatchKey); break;
}
}
}
}
}
UpdateHotKeys();
}
const int SaveTimeKeyId=0, ToggleStopwatchKeyId=1; // global hotkey IDs
const int HkAlt=1, HkCtrl=2, HkShift=4, HkNoRepeat=0x4000; // global hotkey flags
bool CloseBoard()
{
if(!TrySaveChanges()) return false;
board.Clear();
fileName = null;
return true;
}
void OpenBoard()
{
var dialog = new OpenFileDialog();
dialog.DefaultExt = "vmb";
dialog.Filter = "Virtual Maneuvering Boards (*.vmb)|*.vmb|All Files (*.*)|*.*";
dialog.Title = "Select the maneuvering board to open.";
dialog.SupportMultiDottedExtensions = true;
if(fileName != null)
{
string directory = Path.GetDirectoryName(fileName);
if(Directory.Exists(directory)) dialog.InitialDirectory = directory;
}
if(dialog.ShowDialog() == DialogResult.OK && TrySaveChanges()) OpenBoard(dialog.FileName);
}
void OpenBoard(string fileName)
{
try
{
board.Load(fileName);
this.fileName = fileName;
}
catch(Exception ex)
{
MessageBox.Show("An error occurred while loading " + fileName + ". (" + ex.Message + ")", "Load failed",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
void NewBoard()
{
if(CloseBoard())
{
var ownShip = new UnitShape() { Name = "Own ship", Type = UnitShapeType.OwnShip };
board.Center = AdamMil.Mathematics.Geometry.Point2.Empty;
board.ZoomFactor = 1.0/32;
board.RootShapes.Add(ownShip);
board.ReferenceShape = ownShip;
board.SelectedShape = ownShip;
board.WasChanged = false;
}
}
bool SaveBoard()
{
return fileName == null ? SaveBoardAs() : SaveBoard(fileName);
}
bool SaveBoard(string fileName)
{
try
{
board.Save(fileName);
this.fileName = fileName;
return true;
}
catch(Exception ex)
{
MessageBox.Show("An error occurred while save " + fileName + ". (" + ex.Message + ")", "Save failed",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return false;
}
bool SaveBoardAs()
{
var dialog = new SaveFileDialog();
dialog.DefaultExt = "vmb";
dialog.Filter = "Virtual Maneuvering Boards (*.vmb)|*.vmb|All Files (*.*)|*.*";
dialog.Title = "Where would you like to save this file?";
dialog.SupportMultiDottedExtensions = true;
if(fileName != null)
{
string directory = Path.GetDirectoryName(fileName);
if(Directory.Exists(directory)) dialog.InitialDirectory = directory;
}
return dialog.ShowDialog() == DialogResult.OK && SaveBoard(dialog.FileName);
}
bool SetBackgroundImage()
{
var form = new BackgroundImageForm();
if(form.ShowDialog() == DialogResult.OK)
{
if(board.BackgroundImage == null) // only set the center and scale if there was no background image alread. otherwise, reuse the
{ // previous settings
board.BackgroundImageCenter = board.Center;
board.BackgroundImageScale = ((double)board.Width / form.Image.Width) / board.ZoomFactor;
}
board.BackgroundImage = form.Image;
board.SelectedTool = board.SetupBackgroundTool;
return true;
}
return false;
}
bool TrySaveChanges()
{
if(board.WasChanged)
{
DialogResult result = MessageBox.Show("Save changes to your maneuvering board?", "Save changes?", MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button3);
if(result == DialogResult.Cancel || result == DialogResult.Yes && !SaveBoard()) return false;
}
return true;
}
void UpdateHotKeys()
{
UnregisterHotKey(Handle, SaveTimeKeyId);
UnregisterHotKey(Handle, ToggleStopwatchKeyId);
int noRepeatFlag = noRepeatSupported ? HkNoRepeat : 0;
if(saveTimeKey != 0) RegisterHotKey(Handle, SaveTimeKeyId, saveTimeKey>>16, saveTimeKey&0xFFFF);
if(toggleStopwatchKey != 0) RegisterHotKey(Handle, ToggleStopwatchKeyId, toggleStopwatchKey>>16, toggleStopwatchKey&0xFFFF);
}
void board_BackgroundImageChanged(object sender, EventArgs e)
{
miRemoveBackground.Enabled = board.BackgroundImage != null;
if(board.SelectedTool == board.SetupBackgroundTool) board.SelectedTool = board.PointerTool;
}
void board_ReferenceShapeChanged(object sender, EventArgs e) =>
tbAddObservation.Enabled = board.ReferenceShape != null || board.AddObservationTool.Type == PositionalDataType.Waypoint;
void board_StatusTextChanged(object sender, EventArgs e) => lblToolStatus.Text = board.StatusText;
void board_ToolChanged(object sender, EventArgs e)
{
ToolStripButton toolBarButton;
if(board.SelectedTool == board.PointerTool) toolBarButton = tbPointer;
else if(board.SelectedTool == board.AddUnitTool) toolBarButton = tbAddUnit;
else if(board.SelectedTool == board.AddObservationTool) toolBarButton = tbAddObservation;
else if(board.SelectedTool == board.TMATool) toolBarButton = tbTMA;
else if(board.SelectedTool == board.AddLineTool) toolBarButton = tbLine;
else if(board.SelectedTool == board.AddCircleTool) toolBarButton = tbCircle;
else if(board.SelectedTool == board.InterceptTool) toolBarButton = tbIntercept;
else if(board.SelectedTool == board.SetupBackgroundTool) toolBarButton = tbSetBackground;
else if(board.SelectedTool == board.SetupProjectionTool) toolBarButton = tbSetProjection;
else throw new NotImplementedException();
foreach(ToolStripButton button in toolStrip.Items.OfType<ToolStripButton>()) button.Checked = button == toolBarButton;
}
void miAbout_Click(object sender, EventArgs e)
{
using(var form = new AboutBox()) form.ShowDialog();
}
void miAdvanceTime_Click(object sender, EventArgs e)
{
using(var form = new AdvanceTimeForm() { Time = lastTimeAdvance })
{
if(form.ShowDialog() == DialogResult.OK)
{
board.AdvanceTime(form.Time);
lastTimeAdvance = form.Time;
}
}
}
void miBackgroundImage_Click(object sender, EventArgs e) => SetBackgroundImage();
void miBoardOptions_Click(object sender, EventArgs e)
{
using(var form = new BoardOptionsForm(board))
{
if(form.ShowDialog() == DialogResult.OK)
{
board.ShowAllObservations = form.ShowAllObservations;
board.UnitSystem = form.UnitSystem;
board.BackColor = form.BoardBackgroundColor;
board.ObservationColor = form.ObservationColor;
board.ReferenceColor = form.ReferenceColor;
board.ScaleColor1 = form.ScaleColor1;
board.ScaleColor2 = form.ScaleColor2;
board.SelectedColor = form.SelectedColor;
board.TMAColor = form.TMAColor;
board.UnselectedColor = form.UnselectedColor;
}
}
}
void miContactShape_Click(object sender, EventArgs e)
{
var menuItem = (ToolStripMenuItem)sender;
tbAddUnit.Image = menuItem.Image;
foreach(ToolStripMenuItem item in tbUnitShape.DropDownItems) item.Checked = item == sender;
board.AddUnitTool.Type = (UnitShapeType)menuItem.Tag;
tbAddUnit.PerformClick();
}
void miExit_Click(object sender, EventArgs e) => Close();
void miNew_Click(object sender, EventArgs e) => NewBoard();
void miObsType_Click(object sender, EventArgs e)
{
var menuItem = (ToolStripMenuItem)sender;
tbAddObservation.Image = menuItem.Image;
foreach(ToolStripMenuItem item in tbWaypointType.DropDownItems) item.Checked = item == sender;
board.AddObservationTool.Type = (PositionalDataType)menuItem.Tag;
tbAddObservation.Enabled = board.ReferenceShape != null || board.AddObservationTool.Type == PositionalDataType.Waypoint;
if(tbAddObservation.Enabled) tbAddObservation.PerformClick();
else if(tbAddObservation.Checked) tbPointer.PerformClick();
}
void miOpen_Click(object sender, EventArgs e) => OpenBoard();
void miProgramOptions_Click(object sender, EventArgs e)
{
using(var form = new ProgramOptionsForm())
{
form.SaveTimeHotkey = saveTimeKey;
form.ToggleStopwatchHotkey = toggleStopwatchKey;
if(form.ShowDialog() == DialogResult.OK)
{
saveTimeKey = form.SaveTimeHotkey;
toggleStopwatchKey = form.ToggleStopwatchHotkey;
UpdateHotKeys();
string dataPath = Program.GetDataDirectory();
if(dataPath != null)
{
string configFile = Path.Combine(dataPath, "config.txt");
try
{
using(var writer = new StreamWriter(configFile))
{
writer.WriteLine("saveTimeHotkey=" + saveTimeKey.ToStringInvariant());
writer.WriteLine("toggleStopwatchHotkey=" + toggleStopwatchKey.ToStringInvariant());
}
return;
}
catch { }
}
if(dataPath == null) dataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
MessageBox.Show("Unable to save program settings to " + dataPath, "Unable to save settings",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
void miQuickInterceptTool_Click(object sender, EventArgs e)
{
UnitShape reference = board.ReferenceShape as UnitShape, selected = board.SelectedShape as UnitShape;
using(var form = new InterceptForm(reference, selected, board.UnitSystem, false))
{
if(form.ShowDialog() == DialogResult.OK && reference != null) board.ApplyIntercept(reference, form);
}
}
void miRemoveBackground_Click(object sender, EventArgs e) => board.BackgroundImage = null;
void miSave_Click(object sender, EventArgs e) => SaveBoard();
void miSaveAs_Click(object sender, EventArgs e) => SaveBoardAs();
void miStopwatch_Click(object sender, EventArgs e)
{
if(stopwatch == null || stopwatch.IsDisposed)
{
stopwatch = new StopwatchForm();
stopwatch.StartPosition = FormStartPosition.Manual;
stopwatch.Location = PointToScreen(new Point(board.Right-stopwatch.Width, board.Top));
}
if(stopwatch.WindowState != FormWindowState.Minimized && stopwatch.Visible) stopwatch.Hide();
else if(!stopwatch.Visible) stopwatch.Show();
else stopwatch.WindowState = FormWindowState.Normal;
}
void tbAddObservation_Click(object sender, EventArgs e) => board.SelectedTool = board.AddObservationTool;
void tbAddUnit_Click(object sender, EventArgs e) => board.SelectedTool = board.AddUnitTool;
void tbCircle_Click(object sender, EventArgs e) => board.SelectedTool = board.AddCircleTool;
void tbIntercept_Click(object sender, EventArgs e) => board.SelectedTool = board.InterceptTool;
void tbLine_Click(object sender, EventArgs e) => board.SelectedTool = board.AddLineTool;
void tbPointer_Click(object sender, EventArgs e) => board.SelectedTool = board.PointerTool;
void tbSetBackground_Click(object sender, EventArgs e)
{
if(board.BackgroundImage != null || SetBackgroundImage()) board.SelectedTool = board.SetupBackgroundTool;
}
void tbSetProjection_Click(object sender, EventArgs e) => board.SelectedTool = board.SetupProjectionTool;
void tbTMA_Click(object sender, EventArgs e) => board.SelectedTool = board.TMATool;
StopwatchForm stopwatch;
string fileName;
TimeSpan lastTimeAdvance;
int saveTimeKey, toggleStopwatchKey;
bool noRepeatSupported;
[DllImport("user32.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
}
}