-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDataForm.cs
185 lines (172 loc) · 7.24 KB
/
DataForm.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
/*
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.Text.RegularExpressions;
using System.Windows.Forms;
using AdamMil.Mathematics.Geometry;
namespace Maneubo
{
class DataForm : Form
{
protected static void ShowInvalidAngle(string text)
{
MessageBox.Show(text + " is not a valid angle.", "Invalid angle", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
protected static void ShowInvalidLength(string text)
{
MessageBox.Show(text + " is not a valid length. Enter a number followed by a unit such as ft, km, kyd, m, mi, nm, " +
"nmi, or yd.", "Invalid size", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
protected static void ShowInvalidSpeed(string text)
{
MessageBox.Show(text + " is not a valid speed. Enter a number followed by a unit such as kn, kph, kt, kts, m/s, or "+
"mph.", "Invalid speed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
protected static void ShowInvalidTime(string text, bool allowRelative, bool allowNegative)
{
string message = text + " is not a valid time. You may specify a time as a number of minutes, or in the form mm:ss or hh:mm:ss.";
if(allowNegative) message += " You may prepend a minus sign to input a negative time.";
if(allowRelative)
{
message += " If there was a previous time, you may prepend a plus sign to indicate that the time should be interpreted relative to " +
"the previous one.";
}
MessageBox.Show(message, "Invalid time", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
protected static void ShowRequiredMessage(string name)
{
MessageBox.Show(name + " is required.", name + " is required", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
protected static bool TryParseAngle(string text, out double angle)
{
Match m = angleRe.Match(text);
if(!m.Success)
{
angle = 0;
return false;
}
if(!double.TryParse(m.Groups["number"].Value, out angle)) return false;
angle *= MathConst.DegreesToRadians;
while(angle < 0) angle += Math.PI*2;
while(angle >= Math.PI*2) angle -= Math.PI*2;
return true;
}
protected static bool TryParseLength(string text, UnitSystem unitSystem, out double meters)
{
Match m = lengthRe.Match(text);
if(!m.Success || !double.TryParse(m.Groups["number"].Value, out meters))
{
meters = 0;
return false;
}
else
{
LengthUnit unit;
switch(m.Groups["unit"].Value.ToLowerInvariant())
{
case "ft": unit = LengthUnit.Foot; break;
case "km": unit = LengthUnit.Kilometer; break;
case "kyd": unit = LengthUnit.Kiloyard; break;
case "m": unit = LengthUnit.Meter; break;
case "mi": unit = LengthUnit.Mile; break;
case "nm": case "nmi": unit = LengthUnit.NauticalMile; break;
case "yd": unit = LengthUnit.Yard; break;
default:
switch(unitSystem)
{
case UnitSystem.Imperial: unit = LengthUnit.Mile; break;
case UnitSystem.Metric: unit = LengthUnit.Kilometer; break;
case UnitSystem.NauticalImperial: case UnitSystem.NauticalMetric: unit = LengthUnit.NauticalMile; break;
default: unit = LengthUnit.Meter; break;
}
break;
}
meters = ManeuveringBoard.ConvertFromUnit(meters, unit);
return true;
}
}
protected static bool TryParseSpeed(string text, UnitSystem unitSystem, out double metersPerSecond)
{
Match m = speedRe.Match(text);
if(!m.Success || !double.TryParse(m.Groups["number"].Value, out metersPerSecond))
{
metersPerSecond = 0;
return false;
}
else
{
SpeedUnit unit;
switch(m.Groups["unit"].Value.ToLowerInvariant())
{
case "kn": case "kt": case "kts": unit = SpeedUnit.Knots; break;
case "kph": unit = SpeedUnit.KilometersPerHour; break;
case "mph": unit = SpeedUnit.MilesPerHour; break;
case "mps": case "m/s": unit = SpeedUnit.MetersPerSecond; break;
default:
switch(unitSystem)
{
case UnitSystem.Imperial: unit = SpeedUnit.MilesPerHour; break;
case UnitSystem.Metric: unit = SpeedUnit.KilometersPerHour; break;
case UnitSystem.NauticalImperial:
case UnitSystem.NauticalMetric: unit = SpeedUnit.Knots; break;
default: unit = SpeedUnit.MetersPerSecond; break;
}
break;
}
metersPerSecond = ManeuveringBoard.ConvertFromUnit(metersPerSecond, unit);
return true;
}
}
protected bool TryParseTime(string text, out TimeSpan time, out bool relative)
{
Match m = timeRe.Match(text);
if(!m.Success)
{
time = new TimeSpan();
relative = false;
return false;
}
else
{
double minutes;
double.TryParse(m.Groups["minutes"].Value, out minutes);
if(Math.Truncate(minutes) != minutes)
{
time = TimeSpan.FromMinutes(minutes);
}
else
{
int hours, seconds;
int.TryParse(m.Groups["hours"].Value, out hours);
int.TryParse(m.Groups["seconds"].Value, out seconds);
time = new TimeSpan(hours, (int)minutes, seconds);
}
if(m.Groups["neg"].Success) time = -time;
relative = m.Groups["rel"].Success;
return true;
}
}
static readonly Regex angleRe = new Regex(@"^\s*(?<number>\d+|\d*[\.,]\d+)\s*(°\s*)?$");
static readonly Regex lengthRe = new Regex(@"^\s*(?<number>\d+|\d*[\.,]\d+)\s*(?:(?<unit>ft|k(?:m|yd)|mi?|nmi?|yd)\s*)?$",
RegexOptions.IgnoreCase);
static readonly Regex speedRe = new Regex(@"^\s*(?<number>\d+|\d*[\.,]\d+)\s*(?:(?<unit>k(?:n|ph|ts?)|m(?:\/s|p[sh]))\s*)?$",
RegexOptions.IgnoreCase);
static readonly Regex timeRe = new Regex(@"^\s*(?<rel>\+)?\s*(?<neg>-)?\s*(?:(?<minutes>\d*\.\d+)|(?<minutes>\d+)(?::(?<seconds>\d+))?|(?<hours>\d+):(?<minutes>\d+):(?<seconds>\d+))?\s*$",
RegexOptions.IgnoreCase);
}
}