Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
        /// <summary>
        /// Creates a new item for the "Variables" <see cref="ListView"/> on the <see
        /// cref="VariablesTab"/> page, containing the specified <see cref="VariableClass"/>
        /// identifier and associated modifier value for the specified <see cref="ModifierTarget"/>.
        /// </summary>
        /// <param name="id">
        /// The <see cref="VariableClass.Id"/> string of a <see cref="VariableClass"/>.</param>
        /// <param name="target">
        /// A <see cref="ModifierTarget"/> value indicating which modifier value to add.</param>
        /// <returns>
        /// The new <see cref="VariableListItem"/> if one was created and added to the "Variables"
        /// <see cref="ListView"/>; otherwise, a null reference.</returns>
        /// <remarks>
        /// <b>CreateVariableItem</b> immediately returns a null reference if the specified
        /// <paramref name="id"/> is not found in the currently selected default <see
        /// cref="VariableModifierDictionary"/>, or if the element that matches the specified
        /// <paramref name="id"/> does not define a modifier value for the specified <paramref
        /// name="target"/>.</remarks>

        private VariableListItem CreateVariableItem(string id, ModifierTarget target)
        {
            if (this._defaultVariableModifiers == null)
            {
                return(null);
            }

            // get default modifier if present
            VariableModifier defaultModifier;

            if (!this._defaultVariableModifiers.TryGetValue(id, out defaultModifier))
            {
                return(null);
            }

            // get value for specified target, if any
            int?defaultValue = defaultModifier.GetByTarget(target);

            if (defaultValue == null)
            {
                return(null);
            }

            // add current modifier if necessary
            VariableModifier currentModifier;

            if (!this._currentVariableModifiers.TryGetValue(id, out currentModifier))
            {
                currentModifier = (VariableModifier)defaultModifier.Clone();
                this._currentVariableModifiers.Add(id, currentModifier);
            }

            // format variable as modifier value
            int?currentValue         = currentModifier.GetByTarget(target);
            VariableListItem newItem = new VariableListItem(id,
                                                            VariableClass.FormatUnscaled(currentValue, target),
                                                            VariableClass.FormatUnscaled(defaultValue.Value, true), target);

            ItemCollection items = VariableList.Items;

            for (int i = 0; i < items.Count; i++)
            {
                VariableListItem item = (VariableListItem)items[i];

                // sort alphabetically, with basic values before modifiers
                if (String.CompareOrdinal(item.Id, newItem.Id) > 0)
                {
                    items.Insert(i, newItem);
                    return(newItem);
                }
            }

            // append to end of list
            items.Add(newItem);
            return(newItem);
        }
Beispiel #3
0
        /// <overloads>
        /// Creates a new item for the "Variables" <see cref="ListView"/> on the <see
        /// cref="VariablesTab"/> page, containing the specified <see cref="VariableClass"/>
        /// identifier and associated value.</overloads>
        /// <summary>
        /// Creates a new item for the "Variables" <see cref="ListView"/> on the <see
        /// cref="VariablesTab"/> page, containing the specified <see cref="VariableClass"/>
        /// identifier and associated basic value.</summary>
        /// <param name="id">
        /// The <see cref="VariableClass.Id"/> string of a <see cref="VariableClass"/>.</param>
        /// <returns>
        /// The new <see cref="VariableListItem"/> if one was created and added to the "Variables"
        /// <see cref="ListView"/>; otherwise, a null reference.</returns>
        /// <remarks>
        /// <b>CreateVariableItem</b> immediately returns a null reference if the specified
        /// <paramref name="id"/> is not found in the currently selected default <see
        /// cref="VariableValueDictionary"/>.</remarks>

        private VariableListItem CreateVariableItem(string id)
        {
            if (this._defaultVariables == null)
            {
                return(null);
            }

            // get default value if present
            int defaultValue;

            if (!this._defaultVariables.TryGetValue(id, out defaultValue))
            {
                return(null);
            }

            // add current value if necessary
            int currentValue;

            if (!this._currentVariables.TryGetValue(id, out currentValue))
            {
                currentValue = defaultValue;
            }

            // format variable as basic value
            VariableListItem newItem = new VariableListItem(id,
                                                            VariableClass.FormatUnscaled(currentValue, false),
                                                            VariableClass.FormatUnscaled(defaultValue, false), null);

            ItemCollection items = VariableList.Items;

            for (int i = 0; i < items.Count; i++)
            {
                VariableListItem item = (VariableListItem)items[i];

                // sort alphabetically, with basic values before modifiers
                if (item.Id == newItem.Id || String.CompareOrdinal(item.Id, newItem.Id) > 0)
                {
                    items.Insert(i, newItem);
                    return(newItem);
                }
            }

            // append to end of list
            items.Add(newItem);
            return(newItem);
        }