private void UpdateKeyChoice(ModSettingsBase modSettings, FieldInfo field, CustomKeybinding customKeybinding) { KeyCode keyCode = (KeyCode)field.GetValue(modSettings); customKeybinding.currentKeycodeSetting = keyCode; customKeybinding.keyRebindingButton.SetValueLabel(keyCode.ToString()); }
internal static void ValidateFields(ModSettingsBase modSettings) { foreach (FieldInfo field in modSettings.GetFields()) { ValidateFieldAttributes(modSettings, field); } }
internal virtual void AddSettings(ModSettingsBase modSettings) { foreach (FieldInfo field in modSettings.GetFields()) { Attributes.GetAttributes(field, out SectionAttribute section, out NameAttribute name, out DescriptionAttribute description, out SliderAttribute slider, out ChoiceAttribute choice); if (section != null) { AddHeader(section); } else if (lastHeader == null) { AddPaddingHeader(); } if (slider != null) { AddSliderSetting(modSettings, field, name, description, slider); } else if (choice != null) { AddChoiceSetting(modSettings, field, name, description, choice); } else { // No Slider or Choice annotation, determine GUI object from field type Type fieldType = field.FieldType; if (fieldType == typeof(UnityEngine.KeyCode)) { AddKeySetting(modSettings, field, name, description); } else if (fieldType.IsEnum) { AddChoiceSetting(modSettings, field, name, description, ChoiceAttribute.ForEnumType(fieldType)); } else if (fieldType == typeof(bool)) { AddChoiceSetting(modSettings, field, name, description, ChoiceAttribute.YesNoAttribute); } else if (IsFloatType(fieldType)) { AddSliderSetting(modSettings, field, name, description, SliderAttribute.DefaultFloatRange); } else if (IsIntegerType(fieldType)) { AddSliderSetting(modSettings, field, name, description, SliderAttribute.DefaultIntRange); } else { throw new ArgumentException("Unsupported field type: " + fieldType.Name); } } } }
internal override void AddSettings(ModSettingsBase modSettings) { base.AddSettings(modSettings); tabSettings.Add(modSettings); menuGroup.NotifyChildAdded(modSettings.IsVisible()); modSettings.AddVisibilityListener((visible) => { menuGroup.NotifyChildVisible(visible); }); }
private void UpdateChoiceValue(ModSettingsBase modSettings, FieldInfo field, int selectedIndex) { Type fieldType = field.FieldType; if (fieldType.IsEnum) { fieldType = Enum.GetUnderlyingType(fieldType); } SetSettingsField(modSettings, field, Convert.ChangeType(selectedIndex, fieldType, null)); }
private static void UpdateSlider(ModSettingsBase modSettings, FieldInfo field, UISlider slider, UILabel label, float from, float to, string numberFormat) { float sliderValue = from + slider.value * (to - from); if (SliderMatchesField(modSettings, field, sliderValue)) { return; } float value = Convert.ToSingle(field.GetValue(modSettings)); slider.value = (value - from) / (to - from); UpdateSliderLabel(field, label, value, numberFormat); }
private static bool SliderMatchesField(ModSettingsBase modSettings, FieldInfo field, float sliderValue) { if (IsFloatType(field.FieldType)) { float oldValue = Convert.ToSingle(field.GetValue(modSettings)); return(sliderValue == oldValue); } else { long oldValue = Convert.ToInt64(field.GetValue(modSettings)); long longValue = (long)Mathf.Round(sliderValue); return(oldValue == longValue); } }
private void SetVisibilityListener(ModSettingsBase modSettings, FieldInfo field, GameObject guiObject, Header header) { bool startVisible = modSettings.IsFieldVisible(field); if (guiObject.activeSelf != startVisible) { guiObject.SetActive(startVisible); } header?.NotifyChildAdded(startVisible); modSettings.AddVisibilityListener(field, (visible) => { guiObject.SetActive(visible); header?.NotifyChildVisible(visible); uiGrid.repositionNow = true; }); }
private static void ValidateFieldAttributes(ModSettingsBase modSettings, FieldInfo field) { GetAttributes(field, out SectionAttribute section, out NameAttribute name, out DescriptionAttribute description, out SliderAttribute slider, out ChoiceAttribute choice); Type fieldType = field.FieldType; if (name == null) { throw new ArgumentException("[ModSettings] Mod settings contain field without a name attribute", field.Name); } else if (string.IsNullOrEmpty(name.Name)) { throw new ArgumentException("[ModSettings] Setting name attribute must have a non-empty value", field.Name); } if (section != null && string.IsNullOrEmpty(section.Title)) { throw new ArgumentException("[ModSettings] Section title attribute must have a non-empty value", field.Name); } if (slider != null && choice != null) { throw new ArgumentException("[ModSettings] Field cannot be annotated with both 'Slider' and 'Choice' attributes", field.Name); } else if (slider != null) { slider.ValidateFor(modSettings, field); } else if (choice != null) { choice.ValidateFor(modSettings, field); } else if (!IsSupportedType(fieldType)) { throw new ArgumentException("[ModSettings] Field type " + fieldType.Name + " is not supported", field.Name); } if (fieldType.IsEnum) { ValidateEnum(field, fieldType); } }
internal static void RegisterSettings(ModSettingsBase modSettings, Position targetPosition) { if (targetPosition == null) { throw new ArgumentNullException("targetPosition"); } else if (settings.Contains(modSettings)) { throw new ArgumentException("[ModSettings] Cannot add the same settings object multiple times", "modSettings"); } else if (guiBuilt) { throw new InvalidOperationException("[ModSettings] RegisterSettings called after the GUI has been built.\n" + "Call this method before Panel_CustomXPSetup::Awake, preferably from your mod's OnLoad method"); } settings.Add(modSettings); settingsAtPosition[targetPosition.Index].Add(modSettings); }
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); }
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); }
private void UpdateSliderValue(ModSettingsBase modSettings, FieldInfo field, UISlider slider, UILabel label, float from, float to, string numberFormat) { float sliderValue = from + slider.value * (to - from); if (SliderMatchesField(modSettings, field, sliderValue)) { return; } if (IsIntegerType(field.FieldType)) { sliderValue = Mathf.Round(sliderValue); } UpdateSliderLabel(field, label, sliderValue, numberFormat); SetSettingsField(modSettings, field, Convert.ChangeType(sliderValue, field.FieldType, null)); if (modSettings.IsVisible() && slider.numberOfSteps > 1) { GameAudioManager.PlayGUISlider(); } }
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); }
internal static void RegisterSettings(ModSettingsBase modSettings, string modName, MenuType menuType) { if (string.IsNullOrEmpty(modName)) { throw new ArgumentException("[ModSettings] Mod name must be a non-empty string", "modName"); } else if (mainMenuSettings.Contains(modSettings) || inGameSettings.Contains(modSettings)) { throw new ArgumentException("[ModSettings] Cannot add the same settings object multiple times", "modSettings"); } else if (modSettingsGUI != null) { throw new InvalidOperationException("[ModSettings] RegisterSettings called after the GUI has been built.\n" + "Call this method before Panel_CustomXPSetup::Awake, preferably from your mod's OnLoad method"); } if (menuType != MenuType.InGameOnly) { mainMenuSettings.Add(modSettings); } if (menuType != MenuType.MainMenuOnly) { inGameSettings.Add(modSettings); } if (settingsByModName.TryGetValue(modName, out List <ModSettingsBase> settingsList)) { settingsList.Add(modSettings); } else { settingsList = new List <ModSettingsBase> { modSettings }; settingsByModName.Add(modName, settingsList); } }
private void UpdateKeyValue(ModSettingsBase modSettings, FieldInfo field, CustomKeybinding customKeybinding) { SetSettingsField(modSettings, field, customKeybinding.currentKeycodeSetting); }
private static void UpdateChoiceComboBox(ModSettingsBase modSettings, FieldInfo field, ConsoleComboBox comboBox) { int value = Convert.ToInt32(field.GetValue(modSettings)); comboBox.value = comboBox.items[value]; }
protected virtual void SetSettingsField(ModSettingsBase modSettings, FieldInfo field, object newValue) { modSettings.SetFieldValue(field, newValue); }
protected override void SetSettingsField(ModSettingsBase modSettings, FieldInfo field, object newValue) { base.SetSettingsField(modSettings, field, newValue); settingsGUI.NotifySettingsNeedConfirmation(); }