diff --git a/REPOConfig/ConfigEntryStore.cs b/REPOConfig/ConfigEntryStore.cs new file mode 100644 index 0000000..40c0c00 --- /dev/null +++ b/REPOConfig/ConfigEntryStore.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using BepInEx.Configuration; + +namespace REPOConfig +{ + public class ConfigEntryStore + { + internal Dictionary ChangedEntryValues { get; } = []; + internal Dictionary OriginalEntryValues { get; } = []; + } +} \ No newline at end of file diff --git a/REPOConfig/ConfigMenu.cs b/REPOConfig/ConfigMenu.cs index 868eefa..20dbf93 100644 --- a/REPOConfig/ConfigMenu.cs +++ b/REPOConfig/ConfigMenu.cs @@ -1,12 +1,12 @@ using System; using System.Collections.Generic; -using System.Globalization; using System.Linq; using System.Text.RegularExpressions; using BepInEx.Bootstrap; using BepInEx.Configuration; using MenuLib; using MenuLib.MonoBehaviors; +using REPOConfig.Strategies; using TMPro; using UnityEngine; @@ -14,16 +14,29 @@ namespace REPOConfig; internal sealed class ConfigMenu { - private static readonly Dictionary changedEntryValues = new(); + private static readonly ConfigEntryStore configEntryStore = new(); - private static readonly Dictionary originalEntryValues = new(); + private static readonly IDictionary configEntryStrategies = CreateStrategiesDictionary(); internal static REPOButton lastClickedModButton; private static readonly List currentModButtons = []; private static bool hasPopupMenuOpened; - + + private static Dictionary CreateStrategiesDictionary() + { + List strategies = + [ + new StringConfigEntryStrategy(), + new BooleanConfigEntryStrategy(), + new IntConfigEntryStrategy(), + new FloatConfigEntryStrategy() + ]; + + return strategies.ToDictionary(s => s.TargetType, s => s); + } + internal static void Initialize() { //Main Menu Button is created in 'Entry.MenuPageMain_StartHook' @@ -37,7 +50,7 @@ internal static void Initialize() internal static void CreateModMenu() { - changedEntryValues.Clear(); + configEntryStore.ChangedEntryValues.Clear(); lastClickedModButton = null; @@ -48,12 +61,12 @@ internal static void CreateModMenu() if (hasPopupMenuOpened) return false; - if (changedEntryValues.Count == 0) + if (configEntryStore.ChangedEntryValues.Count == 0) return true; MenuAPI.OpenPopup("Unsaved Changes", Color.red, "You have unsaved changes, are you sure you want to exit?", () => { repoPopupPage.ClosePage(true); - changedEntryValues.Clear(); + configEntryStore.ChangedEntryValues.Clear(); hasPopupMenuOpened = false; }, () => hasPopupMenuOpened = false); @@ -80,7 +93,7 @@ internal static void CreateModMenu() CreateModList(repoPopupPage); repoPopupPage.AddElement(parent => MenuAPI.CreateREPOButton("Back", () => { - if (changedEntryValues.Count == 0 || hasPopupMenuOpened) + if (configEntryStore.ChangedEntryValues.Count == 0 || hasPopupMenuOpened) { repoPopupPage.ClosePage(true); return; @@ -90,7 +103,7 @@ internal static void CreateModMenu() () => { repoPopupPage.ClosePage(true); - changedEntryValues.Clear(); + configEntryStore.ChangedEntryValues.Clear(); hasPopupMenuOpened = false; }, () => hasPopupMenuOpened = false); @@ -122,7 +135,7 @@ private static void CreateModList(REPOPopupPage mainModPage) if (lastClickedModButton == modButton) return; - if (changedEntryValues.Count == 0) + if (configEntryStore.ChangedEntryValues.Count == 0) { OpenPage(); return; @@ -131,7 +144,7 @@ private static void CreateModList(REPOPopupPage mainModPage) MenuAPI.OpenPopup("Unsaved Changes", Color.red, "You have unsaved changes, are you sure you want to exit?", () => { - changedEntryValues.Clear(); + configEntryStore.ChangedEntryValues.Clear(); OpenPage(); hasPopupMenuOpened = false; }, () => hasPopupMenuOpened = false); @@ -145,18 +158,18 @@ void OpenPage() var modPage = MenuAPI.CreateREPOPopupPage(modName, REPOPopupPage.PresetSide.Right, false, false, spacing: 5f); modPage.scrollView.scrollSpeed = 3f; - modPage.onEscapePressed = () => !hasPopupMenuOpened && changedEntryValues.Count == 0; + modPage.onEscapePressed = () => !hasPopupMenuOpened && configEntryStore.ChangedEntryValues.Count == 0; modPage.AddElement(mainPageParent => { MenuAPI.CreateREPOButton("Save Changes", () => { - var cachedEntries = changedEntryValues.ToArray(); - changedEntryValues.Clear(); + var cachedEntries = configEntryStore.ChangedEntryValues.ToArray(); + configEntryStore.ChangedEntryValues.Clear(); foreach (var (key, value) in cachedEntries) { key.BoxedValue = value; - originalEntryValues[key] = value; + configEntryStore.OriginalEntryValues[key] = value; } }, mainPageParent, new Vector2(370f, 18f)); @@ -165,10 +178,10 @@ void OpenPage() modPage.AddElement(mainPageParent => { MenuAPI.CreateREPOButton("Revert", () => { - if (changedEntryValues.Count == 0) + if (configEntryStore.ChangedEntryValues.Count == 0) return; - changedEntryValues.Clear(); + configEntryStore.ChangedEntryValues.Clear(); OpenPage(); }, mainPageParent, new Vector2(585f, 18f)); }); @@ -184,7 +197,7 @@ void ResetToDefault() foreach (var configEntryBase in configEntryBases) configEntryBase.BoxedValue = configEntryBase.DefaultValue; - changedEntryValues.Clear(); + configEntryStore.ChangedEntryValues.Clear(); OpenPage(); } }, scrollView); @@ -227,233 +240,68 @@ private static void CreateModEntries(REPOPopupPage modPage, ConfigEntryBase[] co var modName = FixNaming(entry.Definition.Key); //var description = Entry.showDescriptions.Value ? entry.Description.Description.Replace("\n", string.Empty) : string.Empty; - originalEntryValues.Remove(entry); - originalEntryValues.Add(entry, entry.BoxedValue); - - switch (entry) - { - case ConfigEntry: - { - modPage.AddElementToScrollView(scrollView => - { - var repoToggle = MenuAPI.CreateREPOToggle(modName, b => - { - if (originalEntryValues.TryGetValue(entry, out var originalValue) && b == (bool) originalValue) - { - changedEntryValues.Remove(entry); - return; - } - - changedEntryValues[entry] = b; - }, scrollView, defaultValue: (bool)entry.BoxedValue); - repoToggle.labelTMP.fontStyle = FontStyles.Normal; - return repoToggle.rectTransform; - }); - break; - } - case ConfigEntry: { - modPage.AddElementToScrollView(scrollView => { - float min, max; - var precision = 2; - - if (entry.Description.AcceptableValues is AcceptableValueRange acceptableValueRange) - { - min = acceptableValueRange.MinValue; - max = acceptableValueRange.MaxValue; - - precision = Mathf.Max(GetDecimalPlaces(min), GetDecimalPlaces(max), GetDecimalPlaces((float) entry.DefaultValue), 2); - } - else - { - var absoluteDefaultValue = Math.Abs((float) entry.BoxedValue); - - if (absoluteDefaultValue == 0) - min = -(max = 100); - else if (absoluteDefaultValue <= .001) - min = -(max = 10f); - else if (absoluteDefaultValue <= .01) - min = -(max = 50f); - else if (absoluteDefaultValue <= 100) - min = -(max = absoluteDefaultValue * 3f); - else - min = -(max = absoluteDefaultValue * 2); - } - - var repoSlider = MenuAPI.CreateREPOSlider(modName, string.Empty, f => //description - { - if (originalEntryValues.TryGetValue(entry, out var originalValue) && Math.Abs(f - (float) originalValue) < float.Epsilon) - { - changedEntryValues.Remove(entry); - return; - } - - changedEntryValues[entry] = f; - }, scrollView, defaultValue: (float)entry.BoxedValue, min: min, max: max, precision: precision); - repoSlider.descriptionTMP.fontStyle = repoSlider.labelTMP.fontStyle = FontStyles.Normal; - - /*if (description.Length <= 43) - return repoSlider.rectTransform;*/ - - //repoSlider.descriptionTMP.maxVisibleCharacters = repoSlider.repoTextScroller.maxCharacters = 43; - //repoSlider.repoTextScroller.scrollingSpeedInSecondsPerCharacter = Entry.descriptionScrollSpeed.Value; - - /*repoSlider.repoTextScroller.endWaitTime = repoSlider.repoTextScroller.initialWaitTime = 5f; - repoSlider.repoTextScroller.startWaitTime = 3f; - - repoSlider.descriptionTMP.alignment = TextAlignmentOptions.Left; - modPage.StartCoroutine(repoSlider.repoTextScroller.Animate());*/ - - - return repoSlider.rectTransform; - }); - break; - } - case ConfigEntry: { - modPage.AddElementToScrollView(scrollView => { - int min; - int max; - - if (entry.Description.AcceptableValues is AcceptableValueRange acceptableValueRange) - { - min = acceptableValueRange.MinValue; - max = acceptableValueRange.MaxValue; - } - else - { - var absoluteDefaultValue = Math.Abs((int) entry.BoxedValue); - - min = absoluteDefaultValue switch - { - 0 => -(max = 100), - <= 100 => -(max = absoluteDefaultValue * 3), - _ => -(max = absoluteDefaultValue * 2) - }; - } - - var repoSlider = MenuAPI.CreateREPOSlider(modName, string.Empty, i => //description - { - if (originalEntryValues.TryGetValue(entry, out var originalValue) && i == (int) originalValue) - { - changedEntryValues.Remove(entry); - return; - } - - changedEntryValues[entry] = i; - }, scrollView, defaultValue: (int) entry.BoxedValue, min: min, max: max); - repoSlider.descriptionTMP.fontStyle = repoSlider.labelTMP.fontStyle = FontStyles.Normal; - - /*if (description.Length <= 43) - return repoSlider.rectTransform;*/ - - /*repoSlider.descriptionTMP.maxVisibleCharacters = repoSlider.repoTextScroller.maxCharacters = 43; - repoSlider.repoTextScroller.scrollingSpeedInSecondsPerCharacter = Entry.descriptionScrollSpeed.Value; - - repoSlider.repoTextScroller.endWaitTime = repoSlider.repoTextScroller.initialWaitTime = 5f; - repoSlider.repoTextScroller.startWaitTime = 3f; + configEntryStore.OriginalEntryValues.Remove(entry); + configEntryStore.OriginalEntryValues.Add(entry, entry.BoxedValue); - repoSlider.descriptionTMP.alignment = TextAlignmentOptions.Left; - modPage.StartCoroutine(repoSlider.repoTextScroller.Animate());*/ - - return repoSlider.rectTransform; - }); - break; - } - case ConfigEntry when entry.Description.AcceptableValues is AcceptableValueList acceptableValueList: { - modPage.AddElementToScrollView(scrollView => { - var repoSlider = MenuAPI.CreateREPOSlider(modName, string.Empty, s => //description - { - if (originalEntryValues.TryGetValue(entry, out var originalValue) && s == (string) originalValue) - { - changedEntryValues.Remove(entry); - return; - } - - changedEntryValues[entry] = s; - }, scrollView, acceptableValueList.AcceptableValues, (string)entry.BoxedValue); - repoSlider.descriptionTMP.fontStyle = repoSlider.labelTMP.fontStyle = FontStyles.Normal; - - /*if (description.Length <= 43) - return repoSlider.rectTransform; - - repoSlider.descriptionTMP.maxVisibleCharacters = repoSlider.repoTextScroller.maxCharacters = 43; - repoSlider.repoTextScroller.scrollingSpeedInSecondsPerCharacter = Entry.descriptionScrollSpeed.Value; - - repoSlider.repoTextScroller.endWaitTime = repoSlider.repoTextScroller.initialWaitTime = 5f; - repoSlider.repoTextScroller.startWaitTime = 3f; + if (configEntryStrategies.TryGetValue(entry.SettingType, out var strategy)) + { + modPage.AddElementToScrollView(scrollView => strategy.Execute( + scrollView, + entry, + new EntryParameters(modName, configEntryStore) + )); - repoSlider.descriptionTMP.alignment = TextAlignmentOptions.Left; - modPage.StartCoroutine(repoSlider.repoTextScroller.Animate());*/ - - return repoSlider.rectTransform; - }); - break; - } - case ConfigEntry: - { - modPage.AddElementToScrollView(scrollView => - { - var defaultValue = (string) entry.DefaultValue; - - var repoInputField = MenuAPI.CreateREPOInputField(modName, s => - { - if (originalEntryValues.TryGetValue(entry, out var originalValue) && s == (string) originalValue) - { - changedEntryValues.Remove(entry); - return; - } - - changedEntryValues[entry] = s; - }, scrollView, Vector2.zero, false, !string.IsNullOrEmpty(defaultValue) ? defaultValue : "", (string) entry.BoxedValue); - repoInputField.labelTMP.fontStyle = repoInputField.inputStringSystem.inputTMP.fontStyle = FontStyles.Normal; - - return repoInputField.rectTransform; - }); - break; - } - case not null when entry.SettingType.IsSubclassOf(typeof(Enum)): - { - var enumType = entry.SettingType; - var values = Enum.GetNames(enumType); - - modPage.AddElementToScrollView(scrollView => - { - var repoSlider = MenuAPI.CreateREPOSlider(modName, string.Empty, i => //description - { - var enumValue = Enum.Parse(enumType, values[i]); - - if (originalEntryValues.TryGetValue(entry, out var originalValue) && enumValue == originalValue) - { - changedEntryValues.Remove(entry); - return; - } - - changedEntryValues[entry] = enumValue; - }, scrollView, values, entry.BoxedValue.ToString()); - repoSlider.descriptionTMP.fontStyle = repoSlider.labelTMP.fontStyle = FontStyles.Normal; - - /*if (description.Length <= 43) - return repoSlider.rectTransform; - - repoSlider.descriptionTMP.maxVisibleCharacters = repoSlider.repoTextScroller.maxCharacters = 43; - repoSlider.repoTextScroller.scrollingSpeedInSecondsPerCharacter = Entry.descriptionScrollSpeed.Value; - - repoSlider.repoTextScroller.endWaitTime = repoSlider.repoTextScroller.initialWaitTime = 5f; - repoSlider.repoTextScroller.startWaitTime = 3f; + continue; + } - repoSlider.descriptionTMP.alignment = TextAlignmentOptions.Left; - modPage.StartCoroutine(repoSlider.repoTextScroller.Animate());*/ - - return repoSlider.rectTransform; - }); - break; - } + if (entry.SettingType.IsSubclassOf(typeof(Enum))) + { + HandleEnumEntry(modPage, modName, entry); } } modPage.AddElementToScrollView(scrollView => MenuAPI.CreateREPOSpacer(scrollView, size: new Vector2(0, 20)).rectTransform); } } - + + private static void HandleEnumEntry(REPOPopupPage modPage, string modName, ConfigEntryBase entry) + { + var enumType = entry.SettingType; + var values = Enum.GetNames(enumType); + + modPage.AddElementToScrollView(scrollView => + { + var repoSlider = MenuAPI.CreateREPOSlider(modName, string.Empty, i => //description + { + var enumValue = Enum.Parse(enumType, values[i]); + + if (configEntryStore.OriginalEntryValues.TryGetValue(entry, out var originalValue) && + enumValue == originalValue) + { + configEntryStore.ChangedEntryValues.Remove(entry); + return; + } + + configEntryStore.ChangedEntryValues[entry] = enumValue; + }, scrollView, values, entry.BoxedValue.ToString()); + repoSlider.descriptionTMP.fontStyle = repoSlider.labelTMP.fontStyle = FontStyles.Normal; + + /*if (description.Length <= 43) + return repoSlider.rectTransform; + + repoSlider.descriptionTMP.maxVisibleCharacters = repoSlider.repoTextScroller.maxCharacters = 43; + repoSlider.repoTextScroller.scrollingSpeedInSecondsPerCharacter = Entry.descriptionScrollSpeed.Value; + + repoSlider.repoTextScroller.endWaitTime = repoSlider.repoTextScroller.initialWaitTime = 5f; + repoSlider.repoTextScroller.startWaitTime = 3f; + + repoSlider.descriptionTMP.alignment = TextAlignmentOptions.Left; + modPage.StartCoroutine(repoSlider.repoTextScroller.Animate());*/ + + return repoSlider.rectTransform; + }); + } + private static Dictionary GetModConfigEntries() { var repoConfigs = new Dictionary(); @@ -488,13 +336,4 @@ private static string FixNaming(string input) return input.Trim(); } - - private static int GetDecimalPlaces(float value) - { - var valueAsString = value.ToString(CultureInfo.InvariantCulture); - - var decimalPoint = valueAsString.IndexOf('.'); - - return decimalPoint == -1 ? 0 : valueAsString[(decimalPoint + 1)..].Length; - } } \ No newline at end of file diff --git a/REPOConfig/Extensions/FloatExtensions.cs b/REPOConfig/Extensions/FloatExtensions.cs new file mode 100644 index 0000000..a8413f3 --- /dev/null +++ b/REPOConfig/Extensions/FloatExtensions.cs @@ -0,0 +1,16 @@ +using System.Globalization; + +namespace REPOConfig.Extensions +{ + internal static class FloatExtensions + { + internal static int GetDecimalPlaces(this float value) + { + var valueAsString = value.ToString(CultureInfo.InvariantCulture); + + var decimalPoint = valueAsString.IndexOf('.'); + + return decimalPoint == -1 ? 0 : valueAsString[(decimalPoint + 1)..].Length; + } + } +} \ No newline at end of file diff --git a/REPOConfig/Strategies/BooleanConfigEntryStrategy.cs b/REPOConfig/Strategies/BooleanConfigEntryStrategy.cs new file mode 100644 index 0000000..7e0a4f3 --- /dev/null +++ b/REPOConfig/Strategies/BooleanConfigEntryStrategy.cs @@ -0,0 +1,27 @@ +using System; +using BepInEx.Configuration; +using MenuLib; +using TMPro; +using UnityEngine; + +namespace REPOConfig.Strategies +{ + public class BooleanConfigEntryStrategy : ConfigEntryStrategy + { + protected override RectTransform Execute(Transform scrollView, ConfigEntry entry, EntryParameters parameters) + { + var repoToggle = MenuAPI.CreateREPOToggle(parameters.ModName, b => + { + if (parameters.Store.OriginalEntryValues.TryGetValue(entry, out var originalValue) && b == (bool) originalValue) + { + parameters.Store.ChangedEntryValues.Remove(entry); + return; + } + + parameters.Store.ChangedEntryValues[entry] = b; + }, scrollView, defaultValue: (bool)entry.BoxedValue); + repoToggle.labelTMP.fontStyle = FontStyles.Normal; + return repoToggle.rectTransform; + } + } +} \ No newline at end of file diff --git a/REPOConfig/Strategies/ConfigEntryStrategy.cs b/REPOConfig/Strategies/ConfigEntryStrategy.cs new file mode 100644 index 0000000..fcb2193 --- /dev/null +++ b/REPOConfig/Strategies/ConfigEntryStrategy.cs @@ -0,0 +1,25 @@ +using System; +using BepInEx.Configuration; +using UnityEngine; + +namespace REPOConfig.Strategies +{ + public abstract class ConfigEntryStrategy : IConfigEntryStrategy + { + public Type TargetType { get; } = typeof(T); + + public RectTransform Execute(Transform scrollView, ConfigEntryBase entry, EntryParameters parameters) + { + return Execute(scrollView, (ConfigEntry)entry, parameters); + } + + protected abstract RectTransform Execute(Transform scrollView, ConfigEntry entry, EntryParameters parameters); + } + + internal interface IConfigEntryStrategy + { + Type TargetType { get; } + + RectTransform Execute(Transform scrollView, ConfigEntryBase entry, EntryParameters parameters); + } +} \ No newline at end of file diff --git a/REPOConfig/Strategies/EntryParameters.cs b/REPOConfig/Strategies/EntryParameters.cs new file mode 100644 index 0000000..609312f --- /dev/null +++ b/REPOConfig/Strategies/EntryParameters.cs @@ -0,0 +1,8 @@ +namespace REPOConfig.Strategies +{ + public sealed record EntryParameters(string ModName, ConfigEntryStore Store) + { + public string ModName { get; } = ModName; + public ConfigEntryStore Store { get; } = Store; + } +} diff --git a/REPOConfig/Strategies/FloatConfigEntryStrategy.cs b/REPOConfig/Strategies/FloatConfigEntryStrategy.cs new file mode 100644 index 0000000..569ca01 --- /dev/null +++ b/REPOConfig/Strategies/FloatConfigEntryStrategy.cs @@ -0,0 +1,70 @@ +using System; +using BepInEx.Configuration; +using MenuLib; +using REPOConfig.Extensions; +using TMPro; +using UnityEngine; + +namespace REPOConfig.Strategies +{ + public class FloatConfigEntryStrategy : ConfigEntryStrategy + { + protected override RectTransform Execute(Transform scrollView, ConfigEntry entry, EntryParameters parameters) + { + float min, max; + var precision = 2; + + if (entry.Description.AcceptableValues is AcceptableValueRange acceptableValueRange) + { + min = acceptableValueRange.MinValue; + max = acceptableValueRange.MaxValue; + + precision = Mathf.Max(min.GetDecimalPlaces(), max.GetDecimalPlaces(), + ((float)entry.DefaultValue).GetDecimalPlaces(), 2); + } + else + { + var absoluteDefaultValue = Math.Abs((float)entry.BoxedValue); + + if (absoluteDefaultValue == 0) + min = -(max = 100); + else if (absoluteDefaultValue <= .001) + min = -(max = 10f); + else if (absoluteDefaultValue <= .01) + min = -(max = 50f); + else if (absoluteDefaultValue <= 100) + min = -(max = absoluteDefaultValue * 3f); + else + min = -(max = absoluteDefaultValue * 2); + } + + var repoSlider = MenuAPI.CreateREPOSlider(parameters.ModName, string.Empty, f => //description + { + if (parameters.Store.OriginalEntryValues.TryGetValue(entry, out var originalValue) && + Math.Abs(f - (float)originalValue) < float.Epsilon) + { + parameters.Store.ChangedEntryValues.Remove(entry); + return; + } + + parameters.Store.ChangedEntryValues[entry] = f; + }, scrollView, defaultValue: (float)entry.BoxedValue, min: min, max: max, precision: precision); + repoSlider.descriptionTMP.fontStyle = repoSlider.labelTMP.fontStyle = FontStyles.Normal; + + /*if (description.Length <= 43) + return repoSlider.rectTransform;*/ + + //repoSlider.descriptionTMP.maxVisibleCharacters = repoSlider.repoTextScroller.maxCharacters = 43; + //repoSlider.repoTextScroller.scrollingSpeedInSecondsPerCharacter = Entry.descriptionScrollSpeed.Value; + + /*repoSlider.repoTextScroller.endWaitTime = repoSlider.repoTextScroller.initialWaitTime = 5f; + repoSlider.repoTextScroller.startWaitTime = 3f; + + repoSlider.descriptionTMP.alignment = TextAlignmentOptions.Left; + modPage.StartCoroutine(repoSlider.repoTextScroller.Animate());*/ + + + return repoSlider.rectTransform; + } + } +} \ No newline at end of file diff --git a/REPOConfig/Strategies/IntConfigEntryStrategy.cs b/REPOConfig/Strategies/IntConfigEntryStrategy.cs new file mode 100644 index 0000000..5099ebd --- /dev/null +++ b/REPOConfig/Strategies/IntConfigEntryStrategy.cs @@ -0,0 +1,61 @@ +using System; +using BepInEx.Configuration; +using MenuLib; +using TMPro; +using UnityEngine; + +namespace REPOConfig.Strategies +{ + public class IntConfigEntryStrategy : ConfigEntryStrategy + { + protected override RectTransform Execute(Transform scrollView, ConfigEntry entry, EntryParameters parameters) + { + int min; + int max; + + if (entry.Description.AcceptableValues is AcceptableValueRange acceptableValueRange) + { + min = acceptableValueRange.MinValue; + max = acceptableValueRange.MaxValue; + } + else + { + var absoluteDefaultValue = Math.Abs((int)entry.BoxedValue); + + min = absoluteDefaultValue switch + { + 0 => -(max = 100), + <= 100 => -(max = absoluteDefaultValue * 3), + _ => -(max = absoluteDefaultValue * 2) + }; + } + + var repoSlider = MenuAPI.CreateREPOSlider(parameters.ModName, string.Empty, i => //description + { + if (parameters.Store.OriginalEntryValues.TryGetValue(entry, out var originalValue) && + i == (int)originalValue) + { + parameters.Store.ChangedEntryValues.Remove(entry); + return; + } + + parameters.Store.ChangedEntryValues[entry] = i; + }, scrollView, defaultValue: (int)entry.BoxedValue, min: min, max: max); + repoSlider.descriptionTMP.fontStyle = repoSlider.labelTMP.fontStyle = FontStyles.Normal; + + /*if (description.Length <= 43) + return repoSlider.rectTransform;*/ + + /*repoSlider.descriptionTMP.maxVisibleCharacters = repoSlider.repoTextScroller.maxCharacters = 43; + repoSlider.repoTextScroller.scrollingSpeedInSecondsPerCharacter = Entry.descriptionScrollSpeed.Value; + + repoSlider.repoTextScroller.endWaitTime = repoSlider.repoTextScroller.initialWaitTime = 5f; + repoSlider.repoTextScroller.startWaitTime = 3f; + + repoSlider.descriptionTMP.alignment = TextAlignmentOptions.Left; + modPage.StartCoroutine(repoSlider.repoTextScroller.Animate());*/ + + return repoSlider.rectTransform; + } + } +} \ No newline at end of file diff --git a/REPOConfig/Strategies/StringConfigEntryStrategy.cs b/REPOConfig/Strategies/StringConfigEntryStrategy.cs new file mode 100644 index 0000000..21e61ed --- /dev/null +++ b/REPOConfig/Strategies/StringConfigEntryStrategy.cs @@ -0,0 +1,66 @@ +using System; +using BepInEx.Configuration; +using MenuLib; +using TMPro; +using UnityEngine; + +namespace REPOConfig.Strategies +{ + public class StringConfigEntryStrategy : ConfigEntryStrategy + { + protected override RectTransform Execute(Transform scrollView, ConfigEntry entry, EntryParameters parameters) + { + return entry.Description.AcceptableValues is AcceptableValueList acceptableValueList + ? HandleListEntry(scrollView, entry, parameters, acceptableValueList) + : HandleStringEntry(scrollView, entry, parameters); + } + + private RectTransform HandleStringEntry(Transform scrollView, ConfigEntry entry, EntryParameters parameters) + { + var defaultValue = (string) entry.DefaultValue; + + var repoInputField = MenuAPI.CreateREPOInputField(parameters.ModName, s => + { + if (parameters.Store.OriginalEntryValues.TryGetValue(entry, out var originalValue) && s == (string) originalValue) + { + parameters.Store.ChangedEntryValues.Remove(entry); + return; + } + + parameters.Store.ChangedEntryValues[entry] = s; + }, scrollView, Vector2.zero, false, !string.IsNullOrEmpty(defaultValue) ? defaultValue : "", (string) entry.BoxedValue); + repoInputField.labelTMP.fontStyle = repoInputField.inputStringSystem.inputTMP.fontStyle = FontStyles.Normal; + + return repoInputField.rectTransform; + } + + private RectTransform HandleListEntry(Transform scrollView, ConfigEntry entry, EntryParameters parameters, AcceptableValueList acceptableValueList) + { + var repoSlider = MenuAPI.CreateREPOSlider(parameters.ModName, string.Empty, s => //description + { + if (parameters.Store.OriginalEntryValues.TryGetValue(entry, out var originalValue) && s == (string) originalValue) + { + parameters.Store.ChangedEntryValues.Remove(entry); + return; + } + + parameters.Store.ChangedEntryValues[entry] = s; + }, scrollView, acceptableValueList.AcceptableValues, (string)entry.BoxedValue); + repoSlider.descriptionTMP.fontStyle = repoSlider.labelTMP.fontStyle = FontStyles.Normal; + + /*if (description.Length <= 43) + return repoSlider.rectTransform; + + repoSlider.descriptionTMP.maxVisibleCharacters = repoSlider.repoTextScroller.maxCharacters = 43; + repoSlider.repoTextScroller.scrollingSpeedInSecondsPerCharacter = Entry.descriptionScrollSpeed.Value; + + repoSlider.repoTextScroller.endWaitTime = repoSlider.repoTextScroller.initialWaitTime = 5f; + repoSlider.repoTextScroller.startWaitTime = 3f; + + repoSlider.descriptionTMP.alignment = TextAlignmentOptions.Left; + modPage.StartCoroutine(repoSlider.repoTextScroller.Animate());*/ + + return repoSlider.rectTransform; + } + } +} \ No newline at end of file