예제 #1
0
 /// <summary>
 /// Adds a <see cref="ModChoiceOption"/> to the <see cref="ModOptions"/> menu.
 /// </summary>
 /// <param name="id"></param>
 /// <param name="label"></param>
 /// <param name="memberInfoMetadata">The metadata of the corresponding member.</param>
 /// <param name="choiceAttribute">The defined or generated <see cref="ChoiceAttribute"/> of the member.</param>
 private void BuildModChoiceOption(string id, string label,
                                   MemberInfoMetadata <T> memberInfoMetadata, ChoiceAttribute choiceAttribute)
 {
     if (memberInfoMetadata.ValueType.IsEnum && (choiceAttribute.Options == null || !choiceAttribute.Options.Any()))
     {
         // Enum-based choice where the values are parsed from the enum type
         string[] options = Enum.GetNames(memberInfoMetadata.ValueType);
         string   value   = memberInfoMetadata.GetValue(ConfigFileMetadata.Config).ToString();
         AddChoiceOption(id, label, options, value);
     }
     else if (memberInfoMetadata.ValueType.IsEnum)
     {
         // Enum-based choice where the values are defined as custom strings
         string[] options = choiceAttribute.Options;
         string   name    = memberInfoMetadata.GetValue(ConfigFileMetadata.Config).ToString();
         int      index   = Math.Max(Array.IndexOf(Enum.GetNames(memberInfoMetadata.ValueType), name), 0);
         AddChoiceOption(id, label, options, index);
     }
     else if (memberInfoMetadata.ValueType == typeof(string))
     {
         // string-based choice value
         string[] options = choiceAttribute.Options;
         string   value   = memberInfoMetadata.GetValue <string>(ConfigFileMetadata.Config);
         AddChoiceOption(id, label, options, value);
     }
     else if (memberInfoMetadata.ValueType == typeof(int))
     {
         // index-based choice value
         string[] options = choiceAttribute.Options;
         int      index   = memberInfoMetadata.GetValue <int>(ConfigFileMetadata.Config);
         AddChoiceOption(id, label, options, index);
     }
 }
        /// <summary>
        /// Sets the value in the <see cref="Config"/>, optionally saving the <see cref="Config"/> to disk if the
        /// <see cref="MenuAttribute.SaveEvents.ChangeValue"/> flag is set, before passing off to
        /// <see cref="InvokeOnChangeEvents{TSource}(ModOptionAttributeMetadata{T}, object, TSource)"/>
        /// to invoke any methods specified with an <see cref="OnChangeAttribute"/>.
        /// </summary>
        /// <param name="sender">The sender of the original choice changed event.</param>
        /// <param name="e">The <see cref="ChoiceChangedEventArgs"/> for the choice changed event.</param>
        public void HandleChoiceChanged(object sender, ChoiceChangedEventArgs e)
        {
            if (TryGetMetadata(e.Id, out ModOptionAttributeMetadata <T> modOptionMetadata))
            {
                // Set the value in the Config
                MemberInfoMetadata <T> memberInfoMetadata = modOptionMetadata.MemberInfoMetadata;
                ChoiceAttribute        choiceAttribute    = modOptionMetadata.ModOptionAttribute as ChoiceAttribute;

                if (memberInfoMetadata.ValueType.IsEnum && (choiceAttribute.Options == null || !choiceAttribute.Options.Any()))
                {
                    // Enum-based choice where the values are parsed from the enum type
                    object value = Enum.Parse(memberInfoMetadata.ValueType, e.Value);
                    memberInfoMetadata.SetValue(Config, value);
                }
                else if (memberInfoMetadata.ValueType.IsEnum)
                {
                    // Enum-based choice where the values are defined as custom strings
                    object value = Enum.Parse(memberInfoMetadata.ValueType, Enum.GetNames(memberInfoMetadata.ValueType)[e.Index]);
                    memberInfoMetadata.SetValue(Config, value);
                }
                else if (memberInfoMetadata.ValueType == typeof(string))
                {
                    // string-based choice value
                    string value = e.Value;
                    memberInfoMetadata.SetValue(Config, value);
                }
                else if (memberInfoMetadata.ValueType == typeof(int))
                {
                    // index-based choice value
                    int value = e.Index;
                    memberInfoMetadata.SetValue(Config, value);
                }

                // Optionally save the Config to disk
                if (MenuAttribute.SaveOn.HasFlag(MenuAttribute.SaveEvents.ChangeValue))
                {
                    Config.Save();
                }

                // Invoke any OnChange methods specified
                InvokeOnChangeEvents(modOptionMetadata, sender, e);
            }
        }