/// <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="Variable"/> and <see cref="VariableFlags"/> 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 variable into Variable property Variable = VariableList.SelectedItem as VariableClass; // read selected display mode into VariableFlags property VariableFlags = 0; if (BasicToggle.IsChecked == true) { VariableFlags |= VariableDisplay.Basic; } if (ModifierToggle.IsChecked == true) { VariableFlags |= VariableDisplay.Modifier; } if (NumbersToggle.IsChecked == true) { VariableFlags |= VariableDisplay.Numbers; } if (ShadesToggle.IsChecked == true) { VariableFlags |= VariableDisplay.Shades; } // clear both properties if no valid display mode selected if ((VariableFlags & VariableDisplay.BasicAndModifier) == 0 || (VariableFlags & VariableDisplay.NumbersAndShades) == 0 || ((VariableFlags & VariableDisplay.Basic) == 0 && Variable.Category == VariableCategory.Counter)) { Variable = null; VariableFlags = 0; } }
/// <summary> /// Draws variable values 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 variable values.</param> /// <exception cref="ArgumentNullException"> /// <paramref name="context"/> or <paramref name="site"/> 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 Draw(DrawingContext context, Site site) { if (context == null) { ThrowHelper.ThrowArgumentNullException("context"); } if (site == null) { ThrowHelper.ThrowArgumentNullException("site"); } // get current variable and display flags MapView mapView = this._renderer.MapView; VariableClass variable = mapView.ShownVariable; PointI value = site.AddVariables(mapView.WorldState, variable); VariableDisplay flags = mapView.ShownVariableFlags; // draw value as shade if desired if ((flags & VariableDisplay.Shades) != 0) { int index, shadeMinimum, shadeMaximum, shadeValue; Brush[] brushes = MediaObjects.ShadeGrayBrushes; // update minimum and maximum values if necessary if (ValueRangeChanged) { FindValueRange(variable, value); } // default to basic values unless only Modifier is selected if ((flags & VariableDisplay.BasicAndModifier) == VariableDisplay.Modifier) { shadeMinimum = this._minimum.Y; shadeMaximum = this._maximum.Y; shadeValue = value.Y; } else { shadeMinimum = this._minimum.X; shadeMaximum = this._maximum.X; shadeValue = value.X; } /* * Always use a medium shade if the total range is one. * Otherwise, calculate appropriate shade and restrict index to legal range, * just in case ValueRangeChanged should have been set but wasn't. */ if (shadeMinimum == shadeMaximum) { index = brushes.Length / 2; } else { double range = shadeMaximum - shadeMinimum + 1; index = (int)((shadeValue - shadeMinimum) / range * brushes.Length); index = Math.Max(0, Math.Min(brushes.Length - 1, index)); } context.DrawGeometry(brushes[index], null, mapView.ElementGeometry); } // draw value as number if desired if ((flags & VariableDisplay.Numbers) != 0) { // check if output must be recreated if (this._lastText == null || value != this._lastValue || (flags & VariableDisplay.BasicAndModifier) != (this._lastFlags & VariableDisplay.BasicAndModifier)) { // store current input and display flags this._lastValue = value; this._lastFlags = flags; // default to no output this._lastText = null; string text = ""; // always output basic value if desired if ((flags & VariableDisplay.Basic) != 0) { text = variable.Format(value.X, false); } // output modifier value only if non-zero if ((flags & VariableDisplay.Modifier) != 0 && value.Y != 0) { if (text.Length > 0) { text += Environment.NewLine + variable.Format(value.Y, true); } else { text = variable.Format(value.Y, true); } } // create formatted text if required if (text.Length > 0) { if (this._typeface == null) { this._typeface = this._renderer.Control.GetTypeface(); this._emSize = this._renderer.Control.FontSize; } this._lastText = new FormattedText(text, ApplicationInfo.Culture, FlowDirection.LeftToRight, this._typeface, this._emSize, Brushes.White); // center text horizontally & vertically this._lastText.TextAlignment = TextAlignment.Center; this._lastTextOrigin = new Point(0, -this._lastText.Height / 2.0); } } // draw formatted output text, if any if (this._lastText != null) { context.DrawText(this._lastText, this._lastTextOrigin); } } }
/// <summary> /// Initializes a new instance of the <see cref="ShowVariable"/> class with the specified /// initially selected <see cref="VariableClass"/> and <see cref="VariableDisplay"/> flags. /// </summary> /// <param name="variable"><para> /// The <see cref="VariableClass"/> to select initially. /// </para><para>-or-</para><para> /// A null reference to select the first <see cref="AttributeClass"/>, <see /// cref="ResourceClass"/>, or <see cref="CounterClass"/>, in that order.</para></param> /// <param name="flags"> /// A <see cref="VariableDisplay"/> value indicating which display flags to select /// initially.</param> /// <exception cref="ArgumentException"> /// <paramref name="variable"/> is neither a null reference nor an element of the <see /// cref="VariableSection"/> collection that matches its <see /// cref="VariableClass.Category"/>.</exception> /// <exception cref="InvalidEnumArgumentException"> /// <paramref name="variable"/> specifies an invalid <see cref="VariableClass.Category"/>. /// </exception> public ShowVariable(VariableClass variable, VariableDisplay flags) { InitializeComponent(); if (variable != null) { VariableSection variables = MasterSection.Instance.Variables; var dictionary = variables.GetVariables(variable.Category); // specified variable must be part of its collection if (!dictionary.ContainsKey(variable.Id)) { ThrowHelper.ThrowArgumentException( "variable", Global.Strings.ArgumentNotNullOrVariable); } } Variable = variable; VariableFlags = flags; // read specified display flags into check boxes if ((flags & VariableDisplay.Basic) != 0) { BasicToggle.IsChecked = true; } if ((flags & VariableDisplay.Modifier) != 0) { ModifierToggle.IsChecked = true; } if ((flags & VariableDisplay.Numbers) != 0) { NumbersToggle.IsChecked = true; } if ((flags & VariableDisplay.Shades) != 0) { ShadesToggle.IsChecked = true; } // adjust column width of Variable list view DependencyPropertyDescriptor.FromProperty( ListView.ActualWidthProperty, typeof(ListView)) .AddValueChanged(VariableList, OnVariableWidthChanged); if (variable != null) { // select specified variable, if any switch (variable.Category) { case VariableCategory.Attribute: AttributeToggle.IsChecked = true; break; case VariableCategory.Counter: CounterToggle.IsChecked = true; break; case VariableCategory.Resource: ResourceToggle.IsChecked = true; break; default: ThrowHelper.ThrowInvalidEnumArgumentException("variable.Category", (int)variable.Category, typeof(VariableCategory)); break; } VariableList.SelectAndShow(variable); } else { // select category with defined variables if (MasterSection.Instance.Variables.Attributes.Count > 0) { AttributeToggle.IsChecked = true; } else if (MasterSection.Instance.Variables.Resources.Count > 0) { ResourceToggle.IsChecked = true; } else if (MasterSection.Instance.Variables.Counters.Count > 0) { CounterToggle.IsChecked = true; } else { AttributeToggle.IsChecked = true; } } }