/// <summary> /// Initializes a new instance of the <see cref="ShowGauges"/> class with the specified /// initially selected <see cref="ResourceClass"/> and <see cref="GaugeDisplay"/> flags. /// </summary> /// <param name="resource"><para> /// The identifier of the <see cref="ResourceClass"/> to select initially. Possible values /// include the pseudo-resources <see cref="ResourceClass.StandardMorale"/> and <see /// cref="ResourceClass.StandardStrength"/>. /// </para><para>-or-</para><para> /// A null reference to select the <see cref="ResourceClass.StandardStrength"/> /// pseudo-resource.</para></param> /// <param name="flags"> /// A <see cref="GaugeDisplay"/> value indicating which display flags to select initially. /// </param> public ShowGauges(string resource, GaugeDisplay flags) { InitializeComponent(); Resource = resource; ResourceFlags = flags; // read specified display flags into check boxes NeverToggle.IsChecked = String.IsNullOrEmpty(resource); AlwaysToggle.IsChecked = ((flags & GaugeDisplay.Always) != 0); StackToggle.IsChecked = ((flags & GaugeDisplay.Stack) != 0); // adjust column width of Resource list view DependencyPropertyDescriptor.FromProperty( ListView.ActualWidthProperty, typeof(ListView)) .AddValueChanged(VariableList, OnVariableWidthChanged); // show standard unit resources VariableList.Items.Add(ResourceClass.StandardStrength); VariableList.Items.Add(ResourceClass.StandardMorale); VariableList.AddSeparator(); // show all scenario resources foreach (VariableClass variable in MasterSection.Instance.Variables.Resources.Values) { VariableList.Items.Add(variable); } // select specified resource, if any if (resource != null) { foreach (object item in VariableList.Items) { VariableClass variable = item as VariableClass; if (variable != null && variable.Id == resource) { VariableList.SelectAndShow(variable); break; } } } // select standard strength by default if (VariableList.SelectedItems.Count == 0) { VariableList.SelectAndShow(0); } }
/// <summary> /// Raises and handles the <see cref="Window.Closing"/> event.</summary> /// <param name="args"> /// A <see cref="CancelEventArgs"/> object containing event data.</param> /// <remarks><para> /// <b>OnClosing</b> raises the <see cref="Window.Closing"/> event by calling the base class /// implementation of <see cref="Window.OnClosing"/> with the specified <paramref /// name="args"/>. /// </para><para> /// If the event was not requested to <see cref="CancelEventArgs.Cancel"/>, <b>OnClosing</b> /// handles the <see cref="Window.Closing"/> as follows: /// </para><para> /// If the <see cref="Window.DialogResult"/> is not <c>true</c>, indicating that the user /// cancelled the dialog and wants to discard all changes, <b>OnClosing</b> quits /// immediately. /// </para><para> /// Otherwise, <b>OnClosing</b> reads the control contents of this dialog into the <see /// cref="Resource"/> and <see cref="ResourceFlags"/> properties.</para></remarks> protected override void OnClosing(CancelEventArgs args) { base.OnClosing(args); if (args.Cancel) { return; } // user cancelled dialog, ignore changes if (DialogResult != true) { return; } // read selected resource into Resource property Resource = null; if (NeverToggle.IsChecked == false) { var variable = VariableList.SelectedItem as VariableClass; if (variable != null) { Resource = variable.Id; } } // read selected display mode into ResourceFlags property ResourceFlags = 0; if (AlwaysToggle.IsChecked == true) { ResourceFlags |= GaugeDisplay.Always; } if (StackToggle.IsChecked == true) { ResourceFlags |= GaugeDisplay.Stack; } }
/// <summary> /// Draws a resource gauge on the specified <see cref="Site"/>.</summary> /// <param name="context"> /// The <see cref="DrawingContext"/> for the drawing.</param> /// <param name="site"> /// The <see cref="Site"/> to receive the resource gauge.</param> /// <exception cref="ArgumentNullException"> /// <paramref name="context"/> is a null reference.</exception> /// <remarks> /// The origin of the specified <paramref name="context"/> must have been centered on the /// specified <paramref name="site"/>.</remarks> public void DrawGauge(DrawingContext context, Site site) { if (context == null) { ThrowHelper.ThrowArgumentNullException("context"); } var units = site.Units; int count = units.Count; if (count == 0) { return; } // initialize geometric data if changed MapView mapView = this._renderer.MapView; RegularPolygon polygon = mapView.MapGrid.Element; if (polygon != this._polygon) { Initialize(polygon); } // quit if gauge invisible if (this._gaugeBox.Width == 0.0) { return; } // get current resource and display flags string resource = mapView.GaugeResource; if (String.IsNullOrEmpty(resource)) { return; } GaugeDisplay flags = mapView.GaugeResourceFlags; int min = 0, max = 0, value = 0; // traverse unit stack from top to bottom for (int i = count - 1; i >= 0; i--) { Unit unit = (Unit)units[i]; string id = resource; // map standard pseudo-resources to actual unit resources if (id == ResourceClass.StandardStrength.Id) { id = unit.UnitClass.StrengthResource; } else if (id == ResourceClass.StandardMorale.Id) { id = unit.UnitClass.MoraleResource; } // get resource values if valid and present if (!String.IsNullOrEmpty(id)) { Variable variable = null; if (unit.Resources.Variables.TryGetValue(id, out variable)) { min += variable.Minimum; max += variable.Maximum; value += variable.Value; } } // stop after topmost unit unless showing stack if ((flags & GaugeDisplay.Stack) == 0) { break; } } // quit if no valid resource found if (min == max) { return; } // show resource if depleted or always shown if (value < max || (flags & GaugeDisplay.Always) != 0) { var brush = MediaObjects.GetBrush(MediaObjects.DangerBrushes, value, min, max); context.DrawRectangle(brush, MediaObjects.ThinPen, this._gaugeBox); } }