Пример #1
0
        /// <summary>
        /// Get string from user settings or, as fallback, from default settings.
        /// </summary>
        /// <param name="section">Name of section.</param>
        /// <param name="name">Name of key.</param>
        public string GetString(string section, string name)
        {
            string text = GetValue(section, name);

            if (text != null)
            {
                return(text);
            }

            ModSettingsKey key;

            if (config.Key(section, name, ModSettingsKey.KeyType.Text, out key))
            {
                return(key.text.text);
            }

            throw NewMissingKeyException(section, name);
        }
Пример #2
0
        /// <summary>
        /// Load settings from IniData.
        /// </summary>
        private void LoadSettings()
        {
            // Read settings
            foreach (SectionData section in data.Sections.Where(x => x.SectionName != ModSettingsReader.internalSection))
            {
                // Section title
                AddSectionTitle(section.SectionName);
                MovePosition(spacing);
                List <string> comments = section.Comments;
                int           comment  = 0;

                foreach (KeyData key in section.Keys)
                {
                    // Setting label
                    TextLabel settingName = AddKeyName(key.KeyName);

                    // Setting field
                    ModSettingsKey configKey;
                    if (config && config.Key(section.SectionName, key.KeyName, out configKey))
                    {
                        settingName.ToolTip     = defaultToolTip;
                        settingName.ToolTipText = configKey.description;

                        // Use config file
                        switch (configKey.type)
                        {
                        case ModSettingsKey.KeyType.Toggle:
                            bool toggle;
                            AddCheckBox((bool.TryParse(key.Value, out toggle) && toggle) || configKey.toggle.value);
                            break;

                        case ModSettingsKey.KeyType.MultipleChoice:
                            int selected;
                            if (!int.TryParse(key.Value, out selected))
                            {
                                selected = configKey.multipleChoice.selected;
                            }
                            var multipleChoice = GetSlider();
                            multipleChoice.SetIndicator(configKey.multipleChoice.choices, selected);
                            SetSliderIndicator(multipleChoice);
                            break;

                        case ModSettingsKey.KeyType.Slider:
                            var sliderKey = configKey.slider;
                            int startValue;
                            if (!int.TryParse(key.Value, out startValue))
                            {
                                startValue = configKey.slider.value;
                            }
                            var slider = GetSlider();
                            slider.SetIndicator(sliderKey.min, sliderKey.max, startValue);
                            SetSliderIndicator(slider);
                            break;

                        case ModSettingsKey.KeyType.FloatSlider:
                            var   floatSliderKey = configKey.floatSlider;
                            float floatStartValue;
                            if (!float.TryParse(key.Value, out floatStartValue))
                            {
                                floatStartValue = configKey.floatSlider.value;
                            }
                            var floatSlider = GetSlider();
                            floatSlider.SetIndicator(floatSliderKey.min, floatSliderKey.max, floatStartValue);
                            SetSliderIndicator(floatSlider);
                            break;

                        case ModSettingsKey.KeyType.Tuple:
                            var tuple = AddTuple(key.Value);
                            tuple.First.Numeric = tuple.Second.Numeric = true;
                            break;

                        case ModSettingsKey.KeyType.FloatTuple:
                            AddTuple(key.Value);     // TextBox.Numeric doesn't allow dot
                            break;

                        case ModSettingsKey.KeyType.Text:
                            TextBox textBox = GetTextbox(95, 40, key.Value);
                            modTextBoxes.Add(textBox);
                            break;

                        case ModSettingsKey.KeyType.Color:
                            AddColorPicker(key.Value, configKey);
                            break;
                        }
                    }
                    else
                    {
                        // Legacy support
                        if (comment < comments.Count)
                        {
                            settingName.ToolTip     = defaultToolTip;
                            settingName.ToolTipText = comments[comment];
                            comment++;
                        }

                        if (key.Value == "True")
                        {
                            AddCheckBox(true);
                        }
                        else if (key.Value == "False")
                        {
                            AddCheckBox(false);
                        }
                        else if (key.Value.Contains(ModSettingsReader.tupleDelimiterChar))
                        {
                            AddTuple(key.Value);
                        }
                        else if (ModSettingsReader.IsHexColor(key.Value))
                        {
                            AddColorPicker(key.Value);
                        }
                        else
                        {
                            TextBox textBox = GetTextbox(95, 40, key.Value);
                            modTextBoxes.Add(textBox);
                        }
                    }

                    MovePosition(spacing);
                }
            }
        }
        /// <summary>
        /// Load settings from IniData.
        /// This will be read from the ini file on disk the first time.
        /// </summary>
        private void LoadSettings()
        {
            // Read file
            if (data == null)
            {
                data = parser.ReadFile(path);
                ModSettingsReader.UpdateSettings(ref data, defaultSettings, Mod);
                config = ModSettingsReader.GetConfig(Mod);
            }

            // Read settings
            foreach (SectionData section in data.Sections.Where(x => x.SectionName != ModSettingsReader.internalSection))
            {
                // Section label
                TextLabel textLabel = new TextLabel();
                textLabel.Text                = section.SectionName;
                textLabel.TextColor           = sectionTitleColor;
                textLabel.Position            = new Vector2(x, y);
                textLabel.HorizontalAlignment = HorizontalAlignment.None;
                currentPanel.Components.Add(textLabel);
                MovePosition(spacing + 4);
                List <string> comments = section.Comments;
                int           comment  = 0;

                foreach (KeyData key in section.Keys)
                {
                    // Setting label
                    TextLabel settingName = new TextLabel();
                    settingName.Text                = key.KeyName;
                    settingName.Position            = new Vector2(x, y);
                    settingName.HorizontalAlignment = HorizontalAlignment.None;
                    if (comment < comments.Count)
                    {
                        settingName.ToolTip     = defaultToolTip;
                        settingName.ToolTipText = comments[comment];
                        comment++;
                    }
                    currentPanel.Components.Add(settingName);

                    // Setting field
                    ModSettingsKey configKey;
                    if (config && config.Key(section.SectionName, key.KeyName, out configKey))
                    {
                        settingName.ToolTipText = configKey.description;

                        // Use config file
                        switch (configKey.type)
                        {
                        case ModSettingsKey.KeyType.Toggle:
                            AddCheckBox(key.Value == "True");
                            break;

                        case ModSettingsKey.KeyType.Slider:
                            var slider = configKey.slider;
                            int startValue;
                            if (!int.TryParse(key.Value, out startValue))
                            {
                                startValue = 0;
                            }
                            AddSlider(slider.min, slider.max, startValue, key.KeyName);
                            break;

                        case ModSettingsKey.KeyType.FloatSlider:
                            var   floatSlider = configKey.floatSlider;
                            float floatStartValue;
                            if (!float.TryParse(key.Value, out floatStartValue))
                            {
                                floatStartValue = 0;
                            }
                            AddSlider(floatSlider.min, floatSlider.max, floatStartValue, key.KeyName);
                            break;

                        case ModSettingsKey.KeyType.Tuple:
                        case ModSettingsKey.KeyType.FloatTuple:
                            int index  = key.Value.IndexOf(ModSettingsReader.tupleDelimiterChar);
                            var first  = GetTextbox(95, 19.6f, key.Value.Substring(0, index));
                            var second = GetTextbox(116, 19.6f, key.Value.Substring(index + ModSettingsReader.tupleDelimiterChar.Length));
                            modTuples.Add(new Tuple <TextBox, TextBox>(first, second));
                            break;

                        case ModSettingsKey.KeyType.Text:
                        case ModSettingsKey.KeyType.MultipleChoice:     //TODO
                            TextBox textBox = GetTextbox(95, 40, key.Value);
                            modTextBoxes.Add(textBox);
                            break;

                        case ModSettingsKey.KeyType.Color:
                            TextBox colorBox = GetTextbox(95, 40, key.Value);
                            modTextBoxes.Add(colorBox);
                            int hexColor;
                            if (colorBox.DefaultText.Length != 8 || !int.TryParse(colorBox.DefaultText, System.Globalization.NumberStyles.HexNumber,
                                                                                  System.Globalization.CultureInfo.InvariantCulture, out hexColor))
                            {
                                colorBox.DefaultText = "FFFFFFFF";
                            }
                            // Use box background as a preview of the color
                            Color32 color = ModSettingsReader.ColorFromString(colorBox.DefaultText);
                            colorBox.BackgroundColor = color;
                            colorBox.ToolTip         = defaultToolTip;
                            colorBox.ToolTipText     = color.ToString();
                            break;
                        }
                    }
                    else
                    {
                        // Legacy support
                        if (key.Value == "True")
                        {
                            AddCheckBox(true);
                        }
                        else if (key.Value == "False")
                        {
                            AddCheckBox(false);
                        }
                        else if (key.Value.Contains(ModSettingsReader.tupleDelimiterChar)) // Tuple
                        {
                            int index  = key.Value.IndexOf(ModSettingsReader.tupleDelimiterChar);
                            var first  = GetTextbox(95, 19.6f, key.Value.Substring(0, index));
                            var second = GetTextbox(116, 19.6f, key.Value.Substring(index + ModSettingsReader.tupleDelimiterChar.Length));
                            modTuples.Add(new Tuple <TextBox, TextBox>(first, second));
                        }
                        else
                        {
                            TextBox textBox = GetTextbox(95, 40, key.Value);
                            modTextBoxes.Add(textBox);

                            // Color
                            if (textBox.DefaultText.Length == 8)
                            {
                                // Check if is a hex number or just a string with lenght eight
                                int hexColor;
                                if (int.TryParse(textBox.DefaultText, System.Globalization.NumberStyles.HexNumber,
                                                 System.Globalization.CultureInfo.InvariantCulture, out hexColor))
                                {
                                    // Use box background as a preview of the color
                                    Color32 color = ModSettingsReader.ColorFromString(textBox.DefaultText);
                                    textBox.BackgroundColor = color;
                                    textBox.ToolTip         = defaultToolTip;
                                    textBox.ToolTipText     = color.ToString();
                                }
                            }
                        }
                    }

                    MovePosition(spacing);
                }
            }
        }