/// <summary> /// Handles the <see cref="ButtonBase.Click"/> event for the "Remove Variable" <see /// cref="Button"/>.</summary> /// <param name="sender"> /// The <see cref="Object"/> where the event handler is attached.</param> /// <param name="args"> /// A <see cref="RoutedEventArgs"/> object containing event data.</param> /// <remarks> /// <b>OnVariableRemove</b> removes the first selected item in the "Variable" list view from /// that list view and from the <see cref="CurrentVariables"/> collection, and sets the <see /// cref="SectionTabItem.DataChanged"/> flag.</remarks> private void OnVariableRemove(object sender, RoutedEventArgs args) { args.Handled = true; // retrieve selected faction, if any int index = VariableList.SelectedIndex; if (index < 0) { return; } VariableClass variable = (VariableClass)VariableList.Items[index]; // delete existing ID references var variables = CurrentVariables; if (!SectionTabItem.ProcessAllIdentifiers(variables, variable.Id, null)) { return; } // select item in the same position VariableList.Items.Refresh(); if (VariableList.Items.Count > 0) { VariableList.SelectAndShow(Math.Min(VariableList.Items.Count - 1, index)); } // broadcast data changes EnableListButtons(); SectionTab.DataChanged = true; }
/// <summary> /// Handles the <see cref="NumericUpDown.ValueChanged"/> event for the "Value" <see /// cref="NumericUpDown"/> control on the <see cref="VariablesTab"/> page.</summary> /// <param name="sender"> /// The <see cref="NumericUpDown"/> control sending the event.</param> /// <param name="args"> /// An <see cref="EventArgs"/> object containing event data.</param> /// <remarks> /// <b>OnVariableChanged</b> updates the "Value" column of the first selected item in the /// "Variables" list view, as well as the corresponding backing dictionary.</remarks> private void OnVariableChanged(object sender, EventArgs args) { if (!this._initialized) { return; } // retrieve selected variable entry, if any int index = VariableList.SelectedIndex; if (index < 0) { return; } var item = (VariableListItem)VariableList.Items[index]; // extract decimal value and modifier flag decimal oldValue; if (item.Target == null) { oldValue = VariableClass.ParseUnscaled(item.Value); } else { ModifierTarget target; if (!VariableClass.TryParseUnscaled(item.Value, out oldValue, out target)) { Debug.Fail("OnVariableChanged: TryParseUnscaled failed."); } Debug.Assert(target == item.Target.Value); } // check if variable value has actually changed if (VariableUpDown.Value == oldValue) { return; } int value = (int)VariableUpDown.Value; // update variable value in collection string formatValue; if (item.Target == null) { this._currentVariables[item.Id] = value; formatValue = VariableClass.FormatUnscaled(value, false); } else { this._currentVariableModifiers[item.Id].SetByTarget(item.Target.Value, value); formatValue = VariableClass.FormatUnscaled(value, item.Target.Value); } // update variable value in list view item.Value = formatValue; VariableList.Items.Refresh(); VariableList.SelectAndShow(index); }
/// <summary> /// Handles the <see cref="ButtonBase.Click"/> event for the "Add Variable" <see /// cref="Button"/>.</summary> /// <param name="sender"> /// The <see cref="Object"/> where the event handler is attached.</param> /// <param name="args"> /// A <see cref="RoutedEventArgs"/> object containing event data.</param> /// <remarks><para> /// <b>OnVariableAdd</b> displays a <see cref="Dialog.ChangeIdentifier"/> dialog, followed /// by a <see cref="Dialog.ChangeVariable"/> dialog, allowing the user to define a new /// variable. The new variable copies the properties of the first selected item in the /// "Variable" list view, if any; otherwise, it is created with default properties. /// </para><para> /// If the user confirmed both dialogs, <b>OnVariableAdd</b> adds the new variable to the /// "Variable" list view and to the <see cref="CurrentVariables"/> collection, and sets the /// <see cref="SectionTabItem.DataChanged"/> flag.</para></remarks> private void OnVariableAdd(object sender, RoutedEventArgs args) { args.Handled = true; // ask user for new variable ID var variables = CurrentVariables; var dialog = new Dialog.ChangeIdentifier(CurrentDefaultId, Global.Strings.TitleVariableIdEnter, variables.ContainsKey, false); dialog.Owner = MainWindow.Instance; if (dialog.ShowDialog() != true) { return; } // retrieve new variable ID string id = String.Intern(dialog.Identifier); // create new variable based on selected variable, if any VariableClass variable, selection = VariableList.SelectedItem as VariableClass; if (selection == null) { variable = VariableClass.Create(id, CurrentCategory); } else { variable = (VariableClass)selection.Clone(); variable.Id = id; } // let user make changes to new variable var variableDialog = new Dialog.ChangeVariable(variable) { Owner = MainWindow.Instance }; if (variableDialog.ShowDialog() != true) { return; } // add variable to section table variables.Add(id, variable); // update list view and select new item VariableList.Items.Refresh(); VariableList.SelectAndShow(variable); // broadcast data changes EnableListButtons(); SectionTab.DataChanged = true; }
/// <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> /// Resets all current values in the "Variables" <see cref="ListView"/> on the <see /// cref="VariablesTab"/> page to their default values.</summary> /// <remarks> /// <b>ResetVariables</b> copies the "Default Value" column to the "Value" column for all /// items in the "Variables" list view, and then reselects the currently selected item to /// update all other controls.</remarks> private void ResetVariables() { if (VariableList.Items.Count == 0) { return; } // reset all current values to their default values for (int i = 0; i < VariableList.Items.Count; i++) { VariableListItem item = (VariableListItem)VariableList.Items[i]; item.Value = item.DefaultValue; } VariableList.Items.Refresh(); // reselect current selection to update other controls int index = VariableList.SelectedIndex; VariableList.SelectedIndex = -1; VariableList.SelectAndShow(Math.Max(0, index)); }
/// <summary> /// Checks the "Category" <see cref="RadioButton"/> that corresponds to the specified <see /// cref="VariableClass"/>, which is also selected in the "Variable" <see cref="ListView"/>. /// </summary> /// <param name="variable"> /// The <see cref="VariableClass"/> to select.</param> /// <exception cref="InvalidEnumArgumentException"> /// <paramref name="variable"/> specifies an invalid <see cref="VariableClass.Category"/> /// value.</exception> /// <remarks> /// Checking a "Category" radio button automatically shows the corresponding <see /// cref="VariableClass"/> objects in the "Variable" list view, via <see /// cref="OnCategoryChecked"/>.</remarks> private void SelectVariable(VariableClass variable) { switch (variable.Category) { case VariableCategory.Attribute: AttributeToggle.IsChecked = true; break; case VariableCategory.Resource: ResourceToggle.IsChecked = true; break; default: ThrowHelper.ThrowInvalidEnumArgumentException("variable.Category", (int)variable.Category, typeof(VariableCategory)); break; } // list view was initialized by checking radio button VariableList.SelectAndShow(variable); }
/// <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; } } }