-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPositionalDataForm.cs
272 lines (240 loc) · 9.81 KB
/
PositionalDataForm.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
/*
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.Linq;
using System.Windows.Forms;
using AdamMil.Mathematics.Geometry;
using System.Collections.Generic;
namespace Maneubo
{
partial class PositionalDataForm : DataForm
{
public PositionalDataForm()
{
InitializeComponent();
}
public PositionalDataForm(PositionalDataShape posData, UnitSystem unitSystem) : this()
{
this.unitSystem = unitSystem;
txtTime.Text = ManeuveringBoard.GetTimeString(posData.Time);
txtTime.Focus();
txtTime.SelectAll();
Text = grpObservation.Text = (posData is Waypoint ? "Waypoint" : "Observation") + " Data";
PositionalDataShape previousPosition = null;
UnitShape observer = posData is Observation ? ((Observation)posData).Observer : null;
for(int index = posData.Parent.Children.IndexOf(posData)-1; index >= 0; index--)
{
previousPosition = posData.Parent.Children[index] as PositionalDataShape;
if(previousPosition != null && previousPosition.GetType() == posData.GetType() &&
(!(previousPosition is Observation) || ((Observation)previousPosition).Observer == observer))
{
break;
}
else
{
previousPosition = null;
}
}
if(previousPosition != null) previousTime = previousPosition.Time;
if(posData is BearingObservation)
{
double bearing = ((BearingObservation)posData).Bearing;
grpPrevious.Enabled = false;
grpObserved.Enabled = false;
txtObserverDistance.Enabled = false;
txtObserverBearing.Text = (bearing * MathConst.RadiansToDegrees).ToString("0.##");
txtObserverBearing.Tag = bearing;
txtObservedBearing.WasChanged = false; // ignore programmatic change
}
else
{
this.posDataPoint = posData.Position;
FillRelativeTo((UnitShape)posData.Parent, txtObservedBearing, txtObservedDistance, out observedPoint);
if(posData is PointObservation)
{
FillRelativeTo(observer, txtObserverBearing, txtObserverDistance, out observerPoint);
}
else
{
grpObserver.Enabled = false;
grpObserved.Text = "Relative to Unit";
}
if(previousPosition == null) grpPrevious.Enabled = false;
else FillRelativeTo(previousPosition, txtPreviousBearing, txtPreviousDistance, out previousPoint);
waypoint = posData is Waypoint;
if(waypoint) waypointTimes = posData.Parent.Children.OfType<Waypoint>().Where(w => w != posData).Select(w => w.Time).ToList();
}
}
public double Bearing
{
get { return (double)txtObserverBearing.Tag; }
}
public Point2 Position
{
get { return posDataPoint.Value; }
}
public TimeSpan Time
{
get
{
TimeSpan time;
TryParseTime(txtTime.Text, out time);
return time;
}
}
void FillRelativeTo(ChangeTrackingTextBox txtBearing, ChangeTrackingTextBox txtDistance, Point2 relativePoint)
{
double angle = ManeuveringBoard.AngleBetween(relativePoint, posDataPoint.Value);
txtBearing.Text = (angle * MathConst.RadiansToDegrees).ToString("0.##");
txtBearing.Tag = angle;
double distance = relativePoint.DistanceTo(posDataPoint.Value);
txtDistance.Text = ManeuveringBoard.GetDistanceString(distance, unitSystem);
txtDistance.Tag = distance;
txtBearing.WasChanged = txtDistance.WasChanged = false;
}
void FillRelativeTo(Shape relativeTo, ChangeTrackingTextBox txtBearing, ChangeTrackingTextBox txtDistance, out Point2 relativePoint)
{
FillRelativeTo(txtBearing, txtDistance, relativeTo.Position);
relativePoint = relativeTo.Position;
}
void FillRelativeTo(Shape relativeTo, ChangeTrackingTextBox txtBearing, ChangeTrackingTextBox txtDistance, out Point2? relativePoint)
{
FillRelativeTo(txtBearing, txtDistance, relativeTo.Position);
relativePoint = relativeTo.Position;
}
bool TryParseTime(string text, out TimeSpan timeSpan)
{
bool relative;
if(!TryParseTime(text, out timeSpan, out relative) || relative && !previousTime.HasValue) return false;
if(relative) timeSpan += previousTime.Value;
return true;
}
void UpdateObservationPoint(ChangeTrackingTextBox txtBearing, ChangeTrackingTextBox txtDistance, Point2 fromPoint,
ChangeTrackingTextBox txtOtherBearing1, ChangeTrackingTextBox txtOtherDistance1, Point2? otherPoint1,
ChangeTrackingTextBox txtOtherBearing2, ChangeTrackingTextBox txtOtherDistance2, Point2? otherPoint2)
{
if(Validate(txtBearing, txtDistance))
{
double bearing = (double)txtBearing.Tag, distance = (double)txtDistance.Tag;
posDataPoint = fromPoint + new Vector2(0, distance).Rotate(-bearing);
if(otherPoint1.HasValue) FillRelativeTo(txtOtherBearing1, txtOtherDistance1, otherPoint1.Value);
if(otherPoint2.HasValue) FillRelativeTo(txtOtherBearing2, txtOtherDistance2, otherPoint2.Value);
txtBearing.WasChanged = txtDistance.WasChanged = false;
}
}
bool Validate(ChangeTrackingTextBox txtBearing, ChangeTrackingTextBox txtDistance)
{
double value;
if(txtBearing.WasChanged)
{
if(TryParseAngle(txtBearing.Text, out value))
{
txtBearing.Tag = value;
}
else
{
if(string.IsNullOrEmpty(txtBearing.Text.Trim())) ShowRequiredMessage("Bearing");
else ShowInvalidAngle(txtBearing.Text);
txtBearing.Focus();
return false;
}
}
if(txtDistance.WasChanged)
{
if(TryParseLength(txtDistance.Text, unitSystem, out value))
{
txtDistance.Tag = value;
}
else
{
if(string.IsNullOrEmpty(txtDistance.Text.Trim())) ShowRequiredMessage("Distance");
else ShowInvalidLength(txtDistance.Text);
txtDistance.Focus();
return false;
}
}
return true;
}
void btnOK_Click(object sender, EventArgs e)
{
// if the user presses Enter, the Leave event of the textbox won't be raised, so we need to ensure that the same logic gets executed.
// first we'll ensure that the logic would succeed if executed. if not, then we abort
if(!Validate(txtObservedBearing, txtObservedDistance) || !Validate(txtObserverBearing, txtObserverDistance) ||
!Validate(txtPreviousBearing, txtPreviousDistance))
{
return;
}
// now that we know the Leave event should succeed, force it to be raised
btnOK.Focus();
TimeSpan time;
if(!TryParseTime(txtTime.Text, out time))
{
if(string.IsNullOrEmpty(txtTime.Text.Trim())) ShowRequiredMessage("Time");
else ShowInvalidTime(txtTime.Text, true, true);
return;
}
else if(waypointTimes != null && waypointTimes.Contains(time))
{
MessageBox.Show("Two waypoints cannot have the same time.", "Invalid time", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
DialogResult = DialogResult.OK;
}
void txtPrevious_Leave(object sender, EventArgs e)
{
if(txtPreviousBearing.WasChanged || txtPreviousDistance.WasChanged)
{
UpdateObservationPoint(txtPreviousBearing, txtPreviousDistance, previousPoint.Value,
txtObservedBearing, txtObservedDistance, observedPoint,
txtObserverBearing, txtObserverDistance, observerPoint);
}
}
void txtObserver_Leave(object sender, EventArgs e)
{
if(txtObserverBearing.WasChanged || txtObserverDistance.WasChanged)
{
if(posDataPoint.HasValue)
{
UpdateObservationPoint(txtObserverBearing, txtObserverDistance, observerPoint.Value,
txtObservedBearing, txtObservedDistance, observedPoint,
txtPreviousBearing, txtPreviousDistance, previousPoint);
}
else
{
Validate(txtObserverBearing, txtObserverDistance);
}
}
}
void txtObserved_Leave(object sender, EventArgs e)
{
if(txtObservedBearing.WasChanged || txtObservedDistance.WasChanged)
{
UpdateObservationPoint(txtObservedBearing, txtObservedDistance, observedPoint,
txtObserverBearing, txtObserverDistance, observerPoint,
txtPreviousBearing, txtPreviousDistance, previousPoint);
}
}
readonly Point2 observedPoint;
readonly Point2? observerPoint, previousPoint;
readonly TimeSpan? previousTime;
readonly UnitSystem unitSystem;
readonly List<TimeSpan> waypointTimes;
readonly bool waypoint;
Point2? posDataPoint;
}
}