Exemple #1
0
        /// <summary>
        ///     Generates the settings menu
        /// </summary>
        //TODO: The sub-functions need to update the UI element based on the reflected value on startup/settings reload
        public void UpdateUI()
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            optionsPanel.ClearPanels();

            //TODO: Holy f*****g hell this is ugly
            //Loop over each setting menu and all the sub-settings
            foreach (PropertyInfo settingInfo in GameSettings.GetSettingClasses())
            {
                //If it has the don't show attribute, then, well... don't show it
                if (settingInfo.DontShowObject())
                {
                    continue;
                }

                object settingGroupInstance = settingInfo.GetStaticValue <object>();

                //Get display text
                string settingGroupName = settingInfo.GetObjectDisplayText();

                //Create a menu module
                OptionsMenu optionOptionsMenu = new OptionsMenu(settingGroupName);
                GameObject  panel             = optionsPanel.AddPanel(optionOptionsMenu);

                //Get each property in the settings
                FieldInfo[] menuFields =
                    settingInfo.PropertyType.GetFields(BindingFlags.Instance | BindingFlags.Public |
                                                       BindingFlags.NonPublic);
                foreach (FieldInfo settingField in menuFields)
                {
                    //If it has the don't show attribute, then, well... don't show it
                    if (settingField.DontShowObject())
                    {
                        continue;
                    }

                    Type fieldType = settingField.FieldType;

                    if (fieldType == typeof(int))
                    {
                        CreateIntSlider(settingField.GetValue <int>(settingGroupInstance), settingField, panel);
                    }
                    else if (fieldType == typeof(float))
                    {
                        CreateFloatSlider(settingField.GetValue <float>(settingGroupInstance), settingField, panel);
                    }
                    else if (fieldType == typeof(bool))
                    {
                        CreateBoolToggle(settingField.GetValue <bool>(settingGroupInstance), settingField, panel);
                    }
                    else if (fieldType == typeof(string))
                    {
                        CreateStringField(settingField.GetValue <string>(settingGroupInstance), settingField,
                                          optionOptionsMenu);
                    }
                    else if (fieldType == typeof(Resolution))
                    {
                        CreateResolutionDropdown(settingField.GetValue <Resolution>(settingGroupInstance), settingField,
                                                 panel);
                    }
                    else if (fieldType.IsEnum)
                    {
                        CreateEnumDropdown(settingField.GetValue <int>(settingGroupInstance), settingField, panel);
                    }
                    else
                    {
                        Logger.Error("UI Element for setting of type {FullName} is not supported!",
                                     fieldType.FullName);
                    }
                }
            }

            stopwatch.Stop();
            Logger.Debug("Time taken to update settings UI: {TotalMilliseconds}ms",
                         stopwatch.Elapsed.TotalMilliseconds);
        }