forked from AdamMil/Maneubo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTMAForm.cs
207 lines (179 loc) · 4.95 KB
/
TMAForm.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
using System;
using System.ComponentModel;
using System.Windows.Forms;
using AdamMil.Mathematics.Geometry;
namespace Maneubo
{
partial class TMAForm : DataForm
{
public TMAForm()
{
InitializeComponent();
}
public TMAForm(UnitSystem unitSystem) : this()
{
this.unitSystem = unitSystem;
}
public event EventHandler ApplySolution, AutoSolve, CourseChanged, Optimize, SpeedChanged;
public double Course
{
get { return txtCourse.Tag == null ? 0 : (double)txtCourse.Tag; }
set
{
txtCourse.Tag = value;
txtCourse.Text = (value * MathConst.RadiansToDegrees).ToString("0.##");
txtCourse.WasChanged = false;
}
}
public double Speed
{
get { return txtSpeed.Tag == null ? 0 : (double)txtSpeed.Tag; }
set
{
txtSpeed.Tag = value;
txtSpeed.Text = ManeuveringBoard.GetSpeedString(value, unitSystem);
txtSpeed.WasChanged = false;
}
}
public bool LockCourse
{
get { return chkLockCourse.Checked; }
set { chkLockCourse.Checked = value; }
}
public bool LockSpeed
{
get { return chkLockSpeed.Checked; }
set { chkLockSpeed.Checked = value; }
}
public double? MinCourse
{
get { return ParseCourse(txtMinCourse); }
}
public double? MaxCourse
{
get { return ParseCourse(txtMaxCourse); }
}
public double? MinSpeed
{
get { return ParseSpeed(txtMinSpeed); }
}
public double? MaxSpeed
{
get { return ParseSpeed(txtMaxSpeed); }
}
public void FocusCourse()
{
txtCourse.Focus();
}
public void FocusSpeed()
{
txtSpeed.Focus();
}
public void ToggleCourseLock()
{
LockCourse = !LockCourse;
}
public void ToggleSpeedLock()
{
LockSpeed = !LockSpeed;
}
double? ParseSpeed(TextBox textBox)
{
double speed;
return TryParseSpeed(textBox.Text, unitSystem, out speed) ? (double?)speed : null;
}
bool ValidateSpeed(TextBox textBox)
{
double speed;
if(!string.IsNullOrEmpty(textBox.Text.Trim()) && !TryParseSpeed(textBox.Text, unitSystem, out speed))
{
ShowInvalidSpeed(textBox.Text);
textBox.Focus();
return false;
}
return true;
}
void btnApply_Click(object sender, EventArgs e)
{
if(ApplySolution != null) ApplySolution(this, EventArgs.Empty);
}
void btnAuto_Click(object sender, EventArgs e)
{
if(!ValidateCourse(txtMinCourse) || !ValidateCourse(txtMaxCourse) || !ValidateSpeed(txtMinSpeed) || !ValidateSpeed(txtMaxSpeed))
{
return;
}
if(AutoSolve != null) AutoSolve(this, EventArgs.Empty);
}
void btnOptimize_Click(object sender, EventArgs e)
{
if(Optimize != null) Optimize(this, EventArgs.Empty);
}
void txtCourse_Validating(object sender, CancelEventArgs e)
{
if(txtCourse.WasChanged)
{
double value;
if(double.TryParse(txtCourse.Text.Trim(), out value))
{
value *= MathConst.DegreesToRadians;
while(value < 0) value += Math.PI*2;
while(value >= Math.PI*2) value -= Math.PI*2;
txtCourse.Tag = value;
if(CourseChanged != null) CourseChanged(this, EventArgs.Empty);
}
else if(string.IsNullOrEmpty(txtCourse.Text.Trim()))
{
Course = Course; // invoke the property setter to reset the text
}
else
{
ShowInvalidAngle(txtCourse.Text);
e.Cancel = true;
return;
}
txtCourse.WasChanged = false;
}
}
void txtSpeed_Validating(object sender, CancelEventArgs e)
{
if(txtSpeed.WasChanged)
{
double value;
if(TryParseSpeed(txtSpeed.Text, unitSystem, out value))
{
txtSpeed.Tag = value;
if(SpeedChanged != null) SpeedChanged(this, EventArgs.Empty);
}
else if(string.IsNullOrEmpty(txtSpeed.Text.Trim()))
{
Speed = Speed; // invoke the property setter to reset the text
}
else
{
ShowInvalidSpeed(txtSpeed.Text);
e.Cancel = true;
return;
}
txtCourse.WasChanged = false;
}
}
readonly UnitSystem unitSystem;
static double? ParseCourse(TextBox textBox)
{
double angle;
return TryParseAngle(textBox.Text, out angle) ? (double?)angle : null;
}
static bool ValidateCourse(TextBox textBox)
{
double angle;
if(!string.IsNullOrEmpty(textBox.Text.Trim()) && !TryParseAngle(textBox.Text, out angle))
{
ShowInvalidAngle(textBox.Text);
textBox.Focus();
return false;
}
return true;
}
}
}