Esempio n. 1
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;
                }
            }
        }
Esempio n. 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);
        }