Example #1
0
        private GameObject CreateSetting(NameAttribute name, DescriptionAttribute description, GameObject prefab, string labelName)
        {
            GameObject setting = NGUITools.AddChild(uiGrid.gameObject, prefab);

            setting.name = "Custom Setting (" + name.Name + ")";

            Transform labelTransform = setting.transform.Find(labelName);

            SetLabelText(labelTransform, name.Name, name.Localize);

            DescriptionHolder descriptionHolder = setting.AddComponent <DescriptionHolder>();

            descriptionHolder.SetDescription(description?.Description ?? string.Empty, description?.Localize ?? false);

            menuItems.Add(setting);
            return(setting);
        }
Example #2
0
        private void AddSliderSetting(ModSettingsBase modSettings, FieldInfo field, NameAttribute name, DescriptionAttribute description, SliderAttribute range)
        {
            // Create menu
            GameObject    setting  = CreateSetting(name, description, sliderPrefab, "Label_FOV");
            ConsoleSlider slider   = setting.GetComponent <ConsoleSlider>();
            UILabel       uiLabel  = slider.m_SliderObject.GetComponentInChildren <UILabel>();
            UISlider      uiSlider = slider.m_SliderObject.GetComponentInChildren <UISlider>();

            // Sanitize user values, especially if the field type is int
            bool  isFloat       = IsFloatType(field.FieldType);
            float from          = isFloat ? range.From : Mathf.Round(range.From);
            float to            = isFloat ? range.To : Mathf.Round(range.To);
            int   numberOfSteps = range.NumberOfSteps;

            if (numberOfSteps < 0)
            {
                numberOfSteps = isFloat ? 1 : Mathf.RoundToInt(Mathf.Abs(from - to)) + 1;
            }
            string numberFormat = range.NumberFormat;

            if (string.IsNullOrEmpty(numberFormat))
            {
                numberFormat = isFloat ? SliderAttribute.DefaultFloatFormat : SliderAttribute.DefaultIntFormat;
            }

            // Add listeners to update setting value
            EventDelegate.Callback callback = new Action(() => UpdateSliderValue(modSettings, field, uiSlider, uiLabel, from, to, numberFormat));
            EventDelegate.Set(slider.onChange, callback);
            EventDelegate.Set(uiSlider.onChange, callback);
            modSettings.AddRefreshAction(() => UpdateSlider(modSettings, field, uiSlider, uiLabel, from, to, numberFormat));

            // Set default value and number of steps
            float defaultValue = Convert.ToSingle(field.GetValue(modSettings));

            uiSlider.value         = (defaultValue - from) / (to - from);
            uiSlider.numberOfSteps = numberOfSteps;
            UpdateSliderLabel(field, uiLabel, defaultValue, numberFormat);

            // Control visibility
            SetVisibilityListener(modSettings, field, setting, lastHeader);
        }
Example #3
0
        private void AddChoiceSetting(ModSettingsBase modSettings, FieldInfo field, NameAttribute name, DescriptionAttribute description, ChoiceAttribute choice)
        {
            // Create menu item
            GameObject      setting  = CreateSetting(name, description, comboBoxPrefab, "Label");
            ConsoleComboBox comboBox = setting.GetComponent <ConsoleComboBox>();

            // Add selectable values
            comboBox.items.Clear();
            foreach (string choiceName in choice.Names)
            {
                comboBox.items.Add(choiceName);
            }
            comboBox.m_Localize = choice.Localize;

            // Add listener and set default value
            EventDelegate.Set(comboBox.onChange, new Action(() => UpdateChoiceValue(modSettings, field, comboBox.GetCurrentIndex())));
            modSettings.AddRefreshAction(() => UpdateChoiceComboBox(modSettings, field, comboBox));

            // Control visibility
            SetVisibilityListener(modSettings, field, setting, lastHeader);
        }
        private void AddKeySetting(ModSettingsBase modSettings, FieldInfo field, NameAttribute name, DescriptionAttribute description)
        {
            // Create menu item
            GameObject setting         = CreateSetting(name, description, ObjectPrefabs.KeyEntryPrefab, "Label");
            GameObject keyButtonObject = setting.transform.FindChild("Keybinding_Button").gameObject;

            CustomKeybinding customKeybinding = setting.AddComponent <CustomKeybinding>();

            customKeybinding.keyRebindingButton    = keyButtonObject.GetComponent <KeyRebindingButton>();
            customKeybinding.currentKeycodeSetting = (KeyCode)field.GetValue(modSettings);
            customKeybinding.RefreshLabelValue();

            UIButton uiButton = keyButtonObject.GetComponent <UIButton>();

            EventDelegate.Set(uiButton.onClick, new Action(customKeybinding.OnClick));
            customKeybinding.OnChange = new Action(() => UpdateKeyValue(modSettings, field, customKeybinding));
            modSettings.AddRefreshAction(() => UpdateKeyChoice(modSettings, field, customKeybinding));

            // Control visibility
            SetVisibilityListener(modSettings, field, setting, lastHeader);
        }
Example #5
0
        internal static void GetAttributes(FieldInfo field, out SectionAttribute section, out NameAttribute name,
                                           out DescriptionAttribute description, out SliderAttribute slider, out ChoiceAttribute choice)
        {
            // Must be assigned at least once, so assign to null first
            section     = null;
            name        = null;
            description = null;
            slider      = null;
            choice      = null;

            object[] attributes = field.GetCustomAttributes(true);
            foreach (object attribute in attributes)
            {
                if (attribute is SectionAttribute sectionAttribute)
                {
                    section = sectionAttribute;
                }
                else if (attribute is NameAttribute nameAttribute)
                {
                    name = nameAttribute;
                }
                else if (attribute is DescriptionAttribute descriptionAttribute)
                {
                    description = descriptionAttribute;
                }
                else if (attribute is SliderAttribute sliderAttribute)
                {
                    slider = sliderAttribute;
                }
                else if (attribute is ChoiceAttribute choiceAttribute)
                {
                    choice = choiceAttribute;
                }
            }
        }