protected static void LoadPositionalData(PositionalDataShape shape, XmlReader reader) { string timeStr = reader.GetStringAttribute("time"); Match m = timeRe.Match(reader.GetStringAttribute("time")); if (!m.Success) { throw new System.IO.InvalidDataException("\"" + timeStr + "\" is not a valid time value."); } int hours = 0, minutes = int.Parse(m.Groups[2].Value, CultureInfo.InvariantCulture), seconds = 0; if (m.Groups[3].Success) { hours = minutes; minutes = int.Parse(m.Groups[3].Value, CultureInfo.InvariantCulture); if (m.Groups[4].Success) { seconds = int.Parse(m.Groups[4].Value, CultureInfo.InvariantCulture); } } shape.Time = new TimeSpan(hours, minutes, seconds); if (m.Groups[1].Success) { shape.Time = -shape.Time; } }
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(); } } }
internal void SortChildren() { // we'll sort the observations based on observer, type, and time, to allow observations to find the adjacent observations in a // chain just by taking the observations at adjacent indices // make note of the indexes of all the items int[] indexes = new int[Children.Count]; for (int i = 0; i < indexes.Length; i++) { indexes[i] = i; } // sort the indexes Dictionary <UnitShape, int> observerNumbers = new Dictionary <UnitShape, int>(); Array.Sort(indexes, (ai, bi) => { // first sort observations after non-observations PositionalDataShape a = Children[ai] as PositionalDataShape, b = Children[bi] as PositionalDataShape; if (a == null) { return(b == null ? ai - bi : -1); // maintain the order between other types of shapes } else if (b == null) { return(1); } // then sort observations by type Type aType = a.GetType(), bType = b.GetType(); if (aType != bType) { return(aType.Name.CompareTo(bType.Name)); } // then sort by observer if (aType != typeof(Waypoint)) { Observation oa = (Observation)a, ob = (Observation)b; if (oa.Observer != ob.Observer) { if (!observerNumbers.ContainsKey(oa.Observer)) { observerNumbers.Add(oa.Observer, observerNumbers.Count); } if (!observerNumbers.ContainsKey(ob.Observer)) { observerNumbers.Add(ob.Observer, observerNumbers.Count); } return(observerNumbers[oa.Observer] - observerNumbers[ob.Observer]); } } // then sort by time return(a.Time.CompareTo(b.Time)); }); int[] backIndexes = new int[indexes.Length]; for (int i = 0; i < backIndexes.Length; i++) { backIndexes[indexes[i]] = i; } for (int i = 0; i < indexes.Length; i++) { if (indexes[i] != i) { Children.Swap(i, indexes[i]); indexes[backIndexes[i]] = indexes[i]; backIndexes[indexes[i]] = backIndexes[i]; } } }