From fdb17678900c556837ed60f6b735c31177f90160 Mon Sep 17 00:00:00 2001 From: LadyAliceMargatroid Date: Sat, 24 Aug 2024 06:08:59 -0700 Subject: [PATCH] Some code refactoring to make it easier on me Make some collections deserving to be readonly, to be readonly List within lists contrast a bit better StringBuilder instead of list of strings Added dungen's generation stats to debug window --- .../DunGenPlus/DevTools/DevDebugManager.cs | 40 +++++++---- .../DunGenPlus/DevTools/DevDebugManagerUI.cs | 57 +++++++-------- .../DevTools/Panels/DunGenPlusPanel.cs | 52 +++++++------- .../DunGenPlus/DevTools/Panels/MainPanel.cs | 21 +++--- .../DevTools/UIElements/BaseInputField.cs | 4 -- .../DevTools/UIElements/BaseUIElement.cs | 9 +-- .../DevTools/UIElements/BoolInputField.cs | 7 +- .../UIElements/Collections/Parameters.cs | 71 +++++++++++++++++++ .../DevTools/UIElements/DropdownInputField.cs | 7 +- .../DevTools/UIElements/FloatInputField.cs | 21 ++++-- .../DevTools/UIElements/IntInputField.cs | 21 ++++-- .../DevTools/UIElements/IntSliderField.cs | 13 ++-- .../DevTools/UIElements/ListUIElement.cs | 15 ++-- .../DevTools/UIElements/StringInputField.cs | 7 +- .../DevTools/UIElements/TextUIElement.cs | 3 +- .../DevTools/UIElements/Vector3InputField.cs | 9 +-- DunGenPlus/DunGenPlus/DunGenPlus.csproj | 1 + .../Patches/DungeonGeneratorPatch.cs | 11 +++ 18 files changed, 245 insertions(+), 124 deletions(-) create mode 100644 DunGenPlus/DunGenPlus/DevTools/UIElements/Collections/Parameters.cs diff --git a/DunGenPlus/DunGenPlus/DevTools/DevDebugManager.cs b/DunGenPlus/DunGenPlus/DevTools/DevDebugManager.cs index 6fcb9ce..527adca 100644 --- a/DunGenPlus/DunGenPlus/DevTools/DevDebugManager.cs +++ b/DunGenPlus/DunGenPlus/DevTools/DevDebugManager.cs @@ -129,28 +129,38 @@ namespace DunGenPlus.DevTools { } public void OnDungeonFinished(DungeonGenerator generator, GenerationStatus status) { - var textList = new List(); + // Albino said StringBuilder + // I forget it even exists ngl + var textList = new StringBuilder(); + + // seeds + textList.AppendLine($"Initial seed: {MainPanel.Instance.seedInputField.inputField.text}"); + textList.AppendLine($"Last seed: {generator.ChosenSeed}"); + if (status == GenerationStatus.Complete) { - textList.Add($"Tiles: {generator.CurrentDungeon.AllTiles.Count}"); - textList.Add($"Main Tiles: {generator.CurrentDungeon.MainPathTiles.Count}"); - textList.Add($"Branch Tiles: {generator.CurrentDungeon.BranchPathTiles.Count}"); - textList.Add($"Doors: {generator.CurrentDungeon.Doors.Count}"); + textList.AppendLine($"Tiles: {generator.CurrentDungeon.AllTiles.Count}"); + textList.AppendLine($"Main Tiles: {generator.CurrentDungeon.MainPathTiles.Count}"); + textList.AppendLine($"Branch Tiles: {generator.CurrentDungeon.BranchPathTiles.Count}"); + textList.AppendLine($"Doors: {generator.CurrentDungeon.Doors.Count}"); } else if (status == GenerationStatus.Failed) { - textList.Add("Failed"); + textList.AppendLine("Failed"); } + textList.AppendLine(); - textList.Add("DunGen"); - textList.Add(generator.GenerationStats.ToString()); + var stats = generator.GenerationStats; + textList.AppendLine("DunGen"); + textList.AppendLine($"Retrys: {stats.TotalRetries}"); + textList.AppendLine($"Pre Process Time: {stats.PreProcessTime:F2} ms"); + textList.AppendLine($"Main Path Time: {stats.MainPathGenerationTime:F2} ms"); + textList.AppendLine($"Branch Path Time: {stats.BranchPathGenerationTime:F2} ms"); + textList.AppendLine($"Post Process Time: {stats.PostProcessTime:F2} ms"); + textList.AppendLine($"Total Time: {stats.TotalTime:F2} ms"); - SetNewSeed(); - statsTextMesh.text = string.Join("\n", textList); + statsTextMesh.text = textList.ToString(); } - private void SetNewSeed(){ - foreach(var p in panels) { - var mainPanel = p as MainPanel; - if (mainPanel) mainPanel.seedInputField.Set(dungeon.Generator.ChosenSeed); - } + public void RecordNewSeed(int seed){ + MainPanel.Instance.seedInputField.Set(seed); } private void UpdatePlusPanel() { diff --git a/DunGenPlus/DunGenPlus/DevTools/DevDebugManagerUI.cs b/DunGenPlus/DunGenPlus/DevTools/DevDebugManagerUI.cs index bc7faf4..edb42b5 100644 --- a/DunGenPlus/DunGenPlus/DevTools/DevDebugManagerUI.cs +++ b/DunGenPlus/DunGenPlus/DevTools/DevDebugManagerUI.cs @@ -2,6 +2,7 @@ using DunGenPlus.Collections; using DunGenPlus.DevTools.Panels; using DunGenPlus.DevTools.UIElements; +using DunGenPlus.DevTools.UIElements.Collections; using LethalLevelLoader; using System; using System.Collections.Generic; @@ -37,17 +38,17 @@ namespace DunGenPlus.DevTools { public GameObject listUIPrefab; public GameObject optionsUIPrefab; - public TextUIElement CreateHeaderUIField(Transform parentTransform, string title, float offset) { + public TextUIElement CreateHeaderUIField(Transform parentTransform, TitleParameter titleParameter) { var gameObject = Instantiate(headerUIPrefab, parentTransform); var field = gameObject.GetComponent(); - field.SetupBase(title, offset); + field.SetupBase(titleParameter); return field; } - public TextUIElement CreateTextUIField(Transform parentTransform, string title, float offset) { + public TextUIElement CreateTextUIField(Transform parentTransform, TitleParameter titleParameter) { var gameObject = Instantiate(textUIPrefab, parentTransform); var field = gameObject.GetComponent(); - field.SetupBase(title, offset); + field.SetupBase(titleParameter); return field; } @@ -59,81 +60,81 @@ namespace DunGenPlus.DevTools { return Instantiate(verticalLayoutUIPrefab, parentTransform).transform; } - public IntInputField CreateIntInputField(Transform parentTransform, string title, float offset, int baseValue, Action setAction, int defaultValue = 0){ + public IntInputField CreateIntInputField(Transform parentTransform, TitleParameter titleParameter, IntParameter intParameter, Action setAction){ var gameObject = Instantiate(intInputFieldPrefab, parentTransform); var field = gameObject.GetComponent(); - field.SetupInputField(title, offset, baseValue, setAction, defaultValue); + field.SetupInputField(titleParameter, intParameter, setAction); return field; } - public FloatInputField CreateFloatInputField(Transform parentTransform, string title, float offset, float baseValue, Action setAction, float defaultValue = 0f){ + public FloatInputField CreateFloatInputField(Transform parentTransform, TitleParameter titleParameter, FloatParameter floatParameter, Action setAction){ var gameObject = Instantiate(floatInputFieldPrefab, parentTransform); var field = gameObject.GetComponent(); - field.SetupInputField(title, offset, baseValue, setAction, defaultValue); + field.SetupInputField(titleParameter, floatParameter, setAction); return field; } - public BoolInputField CreateBoolInputField(Transform parentTransform, string title, float offset, bool baseValue, Action setAction){ + public BoolInputField CreateBoolInputField(Transform parentTransform, TitleParameter titleParameter, bool baseValue, Action setAction){ var gameObject = Instantiate(boolInputFieldPrefab, parentTransform); var field = gameObject.GetComponent(); - field.SetupInputField(title, offset, baseValue, setAction, false); + field.SetupInputField(titleParameter, baseValue, setAction); return field; } - public StringInputField CreateStringInputField(Transform parentTransform, string title, float offset, string baseValue, Action setAction){ + public StringInputField CreateStringInputField(Transform parentTransform, TitleParameter titleParameter, string baseValue, Action setAction){ var gameObject = Instantiate(stringInputFieldPrefab, parentTransform); var field = gameObject.GetComponent(); - field.SetupInputField(title, offset, baseValue, setAction, string.Empty); + field.SetupInputField(titleParameter, baseValue, setAction); return field; } - public Vector3InputField CreateVector3InputField(Transform parentTransform, string title, float offset, Vector3 baseValue, Action setAction){ + public Vector3InputField CreateVector3InputField(Transform parentTransform, TitleParameter titleParameter, Vector3 baseValue, Action setAction){ var gameObject = Instantiate(vector3InputFieldPrefab, parentTransform); var field = gameObject.GetComponent(); - field.SetupInputField(title, offset, baseValue, setAction, Vector3.zero); + field.SetupInputField(titleParameter, baseValue, setAction); return field; } - public IntSliderField CreateIntSliderField(Transform parentTransform, string title, float offset, int baseValue, Action setAction, int defaultValue = 0){ + public IntSliderField CreateIntSliderField(Transform parentTransform, TitleParameter titleParameter, IntParameter intParameter, Action setAction){ var gameObject = Instantiate(intSliderFieldPrefab, parentTransform); var field = gameObject.GetComponent(); - field.SetupInputField(title, offset, baseValue, setAction, defaultValue); + field.SetupInputField(titleParameter, intParameter, setAction); return field; } - public ListUIElement CreateListUIField(Transform parentTransform, string title, float offset, List list){ + public ListUIElement CreateListUIField(Transform parentTransform, TitleParameter titleParameter, List list){ var gameObject = Instantiate(listUIPrefab, parentTransform); var field = gameObject.GetComponent(); - field.SetupList(title, offset, list); + field.SetupList(titleParameter, list); return field; } - public DropdownInputField CreateOptionsUIField(Transform parentTransform, string title, float offset, int baseValue, Action setAction, Func convertIndex, IEnumerable options){ + public DropdownInputField CreateOptionsUIField(Transform parentTransform, TitleParameter titleParameter, int baseValue, Action setAction, Func convertIndex, IEnumerable options){ var gameObject = Instantiate(optionsUIPrefab, parentTransform); var field = gameObject.GetComponent(); - field.SetupDropdown(title, offset, baseValue, setAction, convertIndex, options); + field.SetupDropdown(titleParameter, baseValue, setAction, convertIndex, options); return field; } - public DropdownInputField CreateLevelOptionsUIField(Transform parentTransform, string title, float offset, int baseValue, Action setAction){ + public DropdownInputField CreateLevelOptionsUIField(Transform parentTransform, TitleParameter titleParameter, int baseValue, Action setAction){ var mainPanel = MainPanel.Instance; - return CreateOptionsUIField(parentTransform, title, offset, baseValue, setAction, (i) => mainPanel.levels[i], mainPanel.levelOptions); + return CreateOptionsUIField(parentTransform, titleParameter, baseValue, setAction, (i) => mainPanel.levels[i], mainPanel.levelOptions); } - public DropdownInputField CreateTileOptionsUIField(Transform parentTransform, string title, float offset, int baseValue, Action setAction){ + public DropdownInputField CreateTileOptionsUIField(Transform parentTransform, TitleParameter titleParameter, int baseValue, Action setAction){ var assetCache = DunGenPlusPanel.Instance.selectedAssetCache; - return CreateOptionsUIField(parentTransform, title, offset, baseValue, setAction, (i) => assetCache.tiles.list[i].Item, assetCache.tiles.options); + return CreateOptionsUIField(parentTransform, titleParameter, baseValue, setAction, (i) => assetCache.tiles.list[i].Item, assetCache.tiles.options); } - public DropdownInputField CreateArchetypeOptionsUIField(Transform parentTransform, string title, float offset, int baseValue, Action setAction){ + public DropdownInputField CreateArchetypeOptionsUIField(Transform parentTransform, TitleParameter titleParameter, int baseValue, Action setAction){ var assetCache = DunGenPlusPanel.Instance.selectedAssetCache; - return CreateOptionsUIField(parentTransform, title, offset, baseValue, setAction, (i) => assetCache.archetypes.list[i].Item, assetCache.archetypes.options); + return CreateOptionsUIField(parentTransform, titleParameter, baseValue, setAction, (i) => assetCache.archetypes.list[i].Item, assetCache.archetypes.options); } - public DropdownInputField CreateCopyNodeBehaviourOptionsUIField(Transform parentTransform, string title, float offset, int baseValue, Action setAction){ + public DropdownInputField CreateCopyNodeBehaviourOptionsUIField(Transform parentTransform, TitleParameter titleParameter, int baseValue, Action setAction){ var options = Enum.GetNames(typeof(DunGenExtenderProperties.CopyNodeBehaviour)); - return CreateOptionsUIField(parentTransform, title, offset, baseValue, setAction, (i) => (DunGenExtenderProperties.CopyNodeBehaviour)i, options); + return CreateOptionsUIField(parentTransform, titleParameter, baseValue, setAction, (i) => (DunGenExtenderProperties.CopyNodeBehaviour)i, options); } } diff --git a/DunGenPlus/DunGenPlus/DevTools/Panels/DunGenPlusPanel.cs b/DunGenPlus/DunGenPlus/DevTools/Panels/DunGenPlusPanel.cs index 3918924..e8c9da6 100644 --- a/DunGenPlus/DunGenPlus/DevTools/Panels/DunGenPlusPanel.cs +++ b/DunGenPlus/DunGenPlus/DevTools/Panels/DunGenPlusPanel.cs @@ -11,6 +11,8 @@ using TMPro; using UnityEngine; using UnityEngine.UI; using DunGenPlus.DevTools.UIElements; +using DunGenPlus.DevTools.UIElements.Collections; +using System.Collections.ObjectModel; namespace DunGenPlus.DevTools.Panels { internal class DunGenPlusPanel : BasePanel { @@ -38,26 +40,28 @@ namespace DunGenPlus.DevTools.Panels { public class DungeonFlowCacheAssets { public DunGenExtenderProperties originalProperties; + // Albino said that readonly is safer public struct Collection { - public List list; - public Dictionary dictionary; - public IEnumerable options; + public ReadOnlyCollection list; + public ReadOnlyDictionary dictionary; + public ReadOnlyCollection options; public Collection(List list) { - this.list = list; + this.list = new ReadOnlyCollection(list); - dictionary = new Dictionary(); + var tempDictionary = new Dictionary(); for(var i = 0; i < list.Count; i++) { - dictionary.Add(list[i], i); + tempDictionary.Add(list[i], i); } + dictionary = new ReadOnlyDictionary(tempDictionary); - options = list.Select(l => l.ToString()); + options = new ReadOnlyCollection(list.Select(l => l.ToString()).ToList()); } } - public Collection> tileSets; - public Collection> tiles; - public Collection> archetypes; + public readonly Collection> tileSets; + public readonly Collection> tiles; + public readonly Collection> archetypes; public DungeonFlowCacheAssets(DunGenExtender extender){ originalProperties = extender.Properties.Copy(); @@ -158,35 +162,35 @@ namespace DunGenPlus.DevTools.Panels { var parentTransform = selectedGameObject.transform; var properties = selectedExtenderer.Properties; - manager.CreateBoolInputField(parentTransform, "Activate DunGenPlus", 0f, selectedExtenderer.Active, SetActivateDunGenPlus); + manager.CreateBoolInputField(parentTransform, "Activate DunGenPlus", selectedExtenderer.Active, SetActivateDunGenPlus); manager.CreateSpaceUIField(parentTransform); var mainPathTransform = manager.CreateVerticalLayoutUIField(parentTransform); mainPathParentGameobject = mainPathTransform.gameObject; - manager.CreateHeaderUIField(parentTransform, "Main Path", 0f); - manager.CreateIntSliderField(parentTransform, "Main Path Count", 0f, properties.MainPathCount, SetMainPathCount); + manager.CreateHeaderUIField(parentTransform, "Main Path"); + manager.CreateIntSliderField(parentTransform, "Main Path Count", new IntParameter(properties.MainPathCount, 1, 10), SetMainPathCount); mainPathTransform.SetAsLastSibling(); - manager.CreateTileOptionsUIField(mainPathTransform, "Main Room Tile Prefab", 0f, selectedAssetCache.tiles.dictionary[properties.MainRoomTilePrefab], SetMainRoom); - manager.CreateCopyNodeBehaviourOptionsUIField(mainPathTransform, "Copy Node Behaviour", 0f, (int)properties.MainPathCopyNodeBehaviour, SetCopyNodeBehaviour); + manager.CreateTileOptionsUIField(mainPathTransform, "Main Room Tile Prefab", selectedAssetCache.tiles.dictionary[properties.MainRoomTilePrefab], SetMainRoom); + manager.CreateCopyNodeBehaviourOptionsUIField(mainPathTransform, "Copy Node Behaviour", (int)properties.MainPathCopyNodeBehaviour, SetCopyNodeBehaviour); manager.CreateSpaceUIField(parentTransform); var dungeonBoundsTransform = manager.CreateVerticalLayoutUIField(parentTransform); dungeonBoundsParentGameobject = dungeonBoundsTransform.gameObject; - manager.CreateHeaderUIField(parentTransform, "Dungeon Bounds", 0f); - manager.CreateBoolInputField(parentTransform, "Use Dungeon Bounds", 0f, properties.UseDungeonBounds, SetUseDungeonBounds); + manager.CreateHeaderUIField(parentTransform, "Dungeon Bounds"); + manager.CreateBoolInputField(parentTransform, "Use Dungeon Bounds", properties.UseDungeonBounds, SetUseDungeonBounds); dungeonBoundsTransform.SetAsLastSibling(); - manager.CreateVector3InputField(dungeonBoundsTransform, "Size Base", 0f, properties.DungeonSizeBase, SetDungeonBoundsSizeBase); - manager.CreateVector3InputField(dungeonBoundsTransform, "Size Factor", 0f, properties.DungeonSizeFactor, SetDungeonBoundsSizeFactor); - manager.CreateVector3InputField(dungeonBoundsTransform, "Position Offset", 0f, properties.DungeonPositionOffset, SetDungeonBoundsPosOffset); - manager.CreateVector3InputField(dungeonBoundsTransform, "Position Pivot", 0f, properties.DungeonPositionPivot, SetDungeonBoundsPosPivot); + manager.CreateVector3InputField(dungeonBoundsTransform, "Size Base", properties.DungeonSizeBase, SetDungeonBoundsSizeBase); + manager.CreateVector3InputField(dungeonBoundsTransform, "Size Factor", properties.DungeonSizeFactor, SetDungeonBoundsSizeFactor); + manager.CreateVector3InputField(dungeonBoundsTransform, "Position Offset", properties.DungeonPositionOffset, SetDungeonBoundsPosOffset); + manager.CreateVector3InputField(dungeonBoundsTransform, "Position Pivot", properties.DungeonPositionPivot, SetDungeonBoundsPosPivot); manager.CreateSpaceUIField(parentTransform); var archetypesTransform = manager.CreateVerticalLayoutUIField(parentTransform); archetypesNodesParentGameobject = archetypesTransform.gameObject; - manager.CreateHeaderUIField(parentTransform, "Archetypes Normal Nodes", 0f); - manager.CreateBoolInputField(parentTransform, "Add Archetypes", 0f, properties.AddArchetypesToNormalNodes, SetAddArchetypes); + manager.CreateHeaderUIField(parentTransform, "Archetypes Normal Nodes"); + manager.CreateBoolInputField(parentTransform, "Add Archetypes", properties.AddArchetypesToNormalNodes, SetAddArchetypes); archetypesTransform.SetAsLastSibling(); - manager.CreateListUIField(archetypesTransform, "Normal Node Archetypes", 0f, properties.NormalNodeArchetypes); + manager.CreateListUIField(archetypesTransform, "Normal Node Archetypes", properties.NormalNodeArchetypes); manager.CreateSpaceUIField(parentTransform); dungeonBoundsHelperGameObject.SetActive(selectedExtenderer.Properties.UseDungeonBounds); diff --git a/DunGenPlus/DunGenPlus/DevTools/Panels/MainPanel.cs b/DunGenPlus/DunGenPlus/DevTools/Panels/MainPanel.cs index fa9731c..c80dc15 100644 --- a/DunGenPlus/DunGenPlus/DevTools/Panels/MainPanel.cs +++ b/DunGenPlus/DunGenPlus/DevTools/Panels/MainPanel.cs @@ -1,5 +1,6 @@ using DunGen; using DunGenPlus.DevTools.UIElements; +using DunGenPlus.DevTools.UIElements.Collections; using LethalLevelLoader; using System; using System.Collections.Generic; @@ -29,22 +30,22 @@ namespace DunGenPlus.DevTools.Panels { var gen = dungeon.Generator; var parentTransform = mainGameObject.transform; - manager.CreateHeaderUIField(parentTransform, "Dungeon Generator", 0f); - seedInputField = manager.CreateIntInputField(parentTransform, "Seed", 0f, gen.Seed, SetSeed); - manager.CreateBoolInputField(parentTransform, "Randomize Seed", 0f, gen.ShouldRandomizeSeed, SetRandomSeed); + manager.CreateHeaderUIField(parentTransform, "Dungeon Generator"); + seedInputField = manager.CreateIntInputField(parentTransform, "Seed", gen.Seed, SetSeed); + manager.CreateBoolInputField(parentTransform, "Randomize Seed", gen.ShouldRandomizeSeed, SetRandomSeed); manager.CreateSpaceUIField(parentTransform); - manager.CreateIntInputField(parentTransform, "Max Attempts", 0f, gen.MaxAttemptCount, SetMaxAttempts, 10); + manager.CreateIntInputField(parentTransform, "Max Attempts", new IntParameter(gen.MaxAttemptCount, 0, 500, 0), SetMaxAttempts); manager.CreateSpaceUIField(parentTransform); - manager.CreateBoolInputField(parentTransform, "Generate Async", 0f, gen.GenerateAsynchronously, SetGenerateAsync); - manager.CreateFloatInputField(parentTransform, "Max Async (ms)", 0f, gen.MaxAsyncFrameMilliseconds, SetMaxAsync); - manager.CreateFloatInputField(parentTransform, "Pause Betwoon Rooms", 0f, gen.PauseBetweenRooms, SetPauseBetweenRooms); + manager.CreateBoolInputField(parentTransform, "Generate Async", gen.GenerateAsynchronously, SetGenerateAsync); + manager.CreateFloatInputField(parentTransform, "Max Async (ms)", new FloatParameter(gen.MaxAsyncFrameMilliseconds, 0f, float.MaxValue), SetMaxAsync); + manager.CreateFloatInputField(parentTransform, "Pause Between Rooms", new FloatParameter(gen.PauseBetweenRooms, 0f, float.MaxValue), SetPauseBetweenRooms); manager.CreateSpaceUIField(parentTransform); - manager.CreateHeaderUIField(parentTransform, "Levels", 0f); - manager.CreateLevelOptionsUIField(parentTransform, "Level", 0f, 0, SetLevel); - lengthMultiplierField = manager.CreateTextUIField(parentTransform, "Length Multiplier", 0f); + manager.CreateHeaderUIField(parentTransform, "Levels"); + manager.CreateLevelOptionsUIField(parentTransform, "Level", 0, SetLevel); + lengthMultiplierField = manager.CreateTextUIField(parentTransform, "Length Multiplier"); SetLevel(levels[0]); } diff --git a/DunGenPlus/DunGenPlus/DevTools/UIElements/BaseInputField.cs b/DunGenPlus/DunGenPlus/DevTools/UIElements/BaseInputField.cs index 3dde0b4..f47c76b 100644 --- a/DunGenPlus/DunGenPlus/DevTools/UIElements/BaseInputField.cs +++ b/DunGenPlus/DunGenPlus/DevTools/UIElements/BaseInputField.cs @@ -8,10 +8,6 @@ using UnityEngine.UI; namespace DunGenPlus.DevTools.UIElements { internal abstract class BaseInputField : BaseUIElement { - public virtual void SetupInputField(string titleText, float offset, T baseValue, Action setAction, T defaultValue){ - SetupBase(titleText, offset); - } - public abstract void Set(T value); protected int ParseTextInt(string text, int defaultValue = 0) { diff --git a/DunGenPlus/DunGenPlus/DevTools/UIElements/BaseUIElement.cs b/DunGenPlus/DunGenPlus/DevTools/UIElements/BaseUIElement.cs index ed128fb..a4555ae 100644 --- a/DunGenPlus/DunGenPlus/DevTools/UIElements/BaseUIElement.cs +++ b/DunGenPlus/DunGenPlus/DevTools/UIElements/BaseUIElement.cs @@ -1,4 +1,5 @@ -using System; +using DunGenPlus.DevTools.UIElements.Collections; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -16,11 +17,11 @@ namespace DunGenPlus.DevTools.UIElements { public LayoutElement layoutElement; internal float layoutOffset; - public void SetupBase(string titleText, float offset) { - title = titleText; + public void SetupBase(TitleParameter titleParameter) { + title = titleParameter.text; SetText(title); - layoutOffset = offset; + layoutOffset = titleParameter.offset; if (layoutElement) { layoutElement.minWidth -= layoutOffset; } diff --git a/DunGenPlus/DunGenPlus/DevTools/UIElements/BoolInputField.cs b/DunGenPlus/DunGenPlus/DevTools/UIElements/BoolInputField.cs index 4b877fa..376310a 100644 --- a/DunGenPlus/DunGenPlus/DevTools/UIElements/BoolInputField.cs +++ b/DunGenPlus/DunGenPlus/DevTools/UIElements/BoolInputField.cs @@ -1,4 +1,5 @@ -using System; +using DunGenPlus.DevTools.UIElements.Collections; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -11,8 +12,8 @@ namespace DunGenPlus.DevTools.UIElements { public Toggle toggle; - public override void SetupInputField(string title, float offset, bool baseValue, Action setAction, bool defaultValue) { - base.SetupInputField(title, offset, baseValue, setAction, defaultValue); + public void SetupInputField(TitleParameter titleParameter, bool baseValue, Action setAction) { + SetupBase(titleParameter); toggle.onValueChanged.AddListener((t) => SetValue(setAction, t)); Set(baseValue); diff --git a/DunGenPlus/DunGenPlus/DevTools/UIElements/Collections/Parameters.cs b/DunGenPlus/DunGenPlus/DevTools/UIElements/Collections/Parameters.cs new file mode 100644 index 0000000..579693f --- /dev/null +++ b/DunGenPlus/DunGenPlus/DevTools/UIElements/Collections/Parameters.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DunGenPlus.DevTools.UIElements.Collections { + internal struct TitleParameter { + public string text; + public float offset; + + public TitleParameter(string text, float offset = 0f) { + this.text = text; + this.offset = offset; + } + + public static implicit operator TitleParameter(string text) => new TitleParameter(text); + + } + + internal struct IntParameter { + public int baseValue; + public int minValue; + public int maxValue; + public int defaultValue; + + + public IntParameter(int baseValue, int defaultValue = 0) { + this.baseValue = baseValue; + this.minValue = int.MinValue; + this.maxValue = int.MaxValue; + this.defaultValue = defaultValue; + } + + public IntParameter(int baseValue, int minValue, int maxValue, int defaultValue = 0) { + this.baseValue = baseValue; + this.minValue = minValue; + this.maxValue = maxValue; + this.defaultValue = defaultValue; + } + + public static implicit operator IntParameter(int baseValue) => new IntParameter(baseValue); + + } + + internal struct FloatParameter { + public float baseValue; + public float minValue; + public float maxValue; + public float defaultValue; + + + public FloatParameter(float baseValue, float defaultValue = 0f) { + this.baseValue = baseValue; + this.minValue = int.MinValue; + this.maxValue = int.MaxValue; + this.defaultValue = defaultValue; + } + + public FloatParameter(float baseValue, float minValue, float maxValue, float defaultValue = 0f) { + this.baseValue = baseValue; + this.minValue = minValue; + this.maxValue = maxValue; + this.defaultValue = defaultValue; + } + + public static implicit operator FloatParameter(float baseValue) => new FloatParameter(baseValue); + + } + +} diff --git a/DunGenPlus/DunGenPlus/DevTools/UIElements/DropdownInputField.cs b/DunGenPlus/DunGenPlus/DevTools/UIElements/DropdownInputField.cs index 3b3557c..3583426 100644 --- a/DunGenPlus/DunGenPlus/DevTools/UIElements/DropdownInputField.cs +++ b/DunGenPlus/DunGenPlus/DevTools/UIElements/DropdownInputField.cs @@ -1,4 +1,5 @@ -using System; +using DunGenPlus.DevTools.UIElements.Collections; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -12,8 +13,8 @@ namespace DunGenPlus.DevTools.UIElements public TMP_Dropdown dropDown; - public void SetupDropdown(string titleText, float offset, int baseValue, Action setAction, Func convertIndex, IEnumerable options) { - SetupBase(titleText, offset); + public void SetupDropdown(TitleParameter titleParameter, int baseValue, Action setAction, Func convertIndex, IEnumerable options) { + SetupBase(titleParameter); dropDown.options = options.Select(c => { return new TMP_Dropdown.OptionData(c.Substring(0, Math.Min(24, c.Length))); diff --git a/DunGenPlus/DunGenPlus/DevTools/UIElements/FloatInputField.cs b/DunGenPlus/DunGenPlus/DevTools/UIElements/FloatInputField.cs index 77c88a4..7ea21b6 100644 --- a/DunGenPlus/DunGenPlus/DevTools/UIElements/FloatInputField.cs +++ b/DunGenPlus/DunGenPlus/DevTools/UIElements/FloatInputField.cs @@ -1,28 +1,35 @@ -using System; +using DunGenPlus.DevTools.UIElements.Collections; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TMPro; +using UnityEngine; namespace DunGenPlus.DevTools.UIElements { internal class FloatInputField : BaseInputField { public TMP_InputField inputField; - internal float defaultValue = 0f; + internal float minValue; + internal float maxValue; + internal float defaultValue; - public override void SetupInputField(string title, float offset, float baseValue, Action setAction , float defaultValue) { - base.SetupInputField(title, offset, baseValue, setAction, defaultValue); - this.defaultValue = defaultValue; + public void SetupInputField(TitleParameter titleParameter, FloatParameter floatParameter, Action setAction) { + SetupBase(titleParameter); + minValue = floatParameter.minValue; + maxValue = floatParameter.maxValue; + defaultValue = floatParameter.defaultValue; inputField.onValueChanged.AddListener((t) => SetValue(setAction, t)); - Set(baseValue); + Set(floatParameter.baseValue); } private void SetValue(Action setAction, string text) { Plugin.logger.LogInfo($"Setting {title} to {text}"); - setAction.Invoke(ParseTextFloat(text, defaultValue)); + var value = ParseTextFloat(text, defaultValue); + setAction.Invoke(Mathf.Clamp(value, minValue, maxValue)); } public override void Set(float value){ diff --git a/DunGenPlus/DunGenPlus/DevTools/UIElements/IntInputField.cs b/DunGenPlus/DunGenPlus/DevTools/UIElements/IntInputField.cs index ab0dec3..21c9857 100644 --- a/DunGenPlus/DunGenPlus/DevTools/UIElements/IntInputField.cs +++ b/DunGenPlus/DunGenPlus/DevTools/UIElements/IntInputField.cs @@ -1,27 +1,34 @@ -using System; +using DunGenPlus.DevTools.UIElements.Collections; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TMPro; +using UnityEngine; namespace DunGenPlus.DevTools.UIElements { internal class IntInputField : BaseInputField { public TMP_InputField inputField; - internal int defaultValue = 0; + internal int minValue; + internal int maxValue; + internal int defaultValue; - public override void SetupInputField(string title, float offset, int baseValue, Action setAction , int defaultValue) { - base.SetupInputField(title, offset, baseValue, setAction, defaultValue); - this.defaultValue = defaultValue; + public void SetupInputField(TitleParameter titleParameter, IntParameter intParameter, Action setAction) { + SetupBase(titleParameter); + minValue = intParameter.minValue; + maxValue = intParameter.maxValue; + defaultValue = intParameter.defaultValue; inputField.onValueChanged.AddListener((t) => SetValue(setAction, t)); - Set(baseValue); + Set(intParameter.baseValue); } private void SetValue(Action setAction, string text) { Plugin.logger.LogInfo($"Setting {title} to {text}"); - setAction.Invoke(ParseTextInt(text, defaultValue)); + var value = ParseTextInt(text, defaultValue); + setAction.Invoke(Mathf.Clamp(value, minValue, maxValue)); } public override void Set(int value){ diff --git a/DunGenPlus/DunGenPlus/DevTools/UIElements/IntSliderField.cs b/DunGenPlus/DunGenPlus/DevTools/UIElements/IntSliderField.cs index 83e866b..7692cc1 100644 --- a/DunGenPlus/DunGenPlus/DevTools/UIElements/IntSliderField.cs +++ b/DunGenPlus/DunGenPlus/DevTools/UIElements/IntSliderField.cs @@ -1,4 +1,5 @@ -using System; +using DunGenPlus.DevTools.UIElements.Collections; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -12,14 +13,14 @@ namespace DunGenPlus.DevTools.UIElements { public Slider inputField; public TextMeshProUGUI textMesh; - internal int defaultValue = 0; - public override void SetupInputField(string title, float offset, int baseValue, Action setAction , int defaultValue) { - base.SetupInputField(title, offset, baseValue, setAction, defaultValue); - this.defaultValue = defaultValue; + public void SetupInputField(TitleParameter titleParameter, IntParameter intParameter, Action setAction) { + SetupBase(titleParameter); + inputField.minValue = inputField.minValue; + inputField.maxValue = inputField.maxValue; inputField.onValueChanged.AddListener((t) => SetValue(setAction, t)); - Set(baseValue); + Set(intParameter.baseValue); } private void SetValue(Action setAction, float value) { diff --git a/DunGenPlus/DunGenPlus/DevTools/UIElements/ListUIElement.cs b/DunGenPlus/DunGenPlus/DevTools/UIElements/ListUIElement.cs index 1725d78..6f13cc4 100644 --- a/DunGenPlus/DunGenPlus/DevTools/UIElements/ListUIElement.cs +++ b/DunGenPlus/DunGenPlus/DevTools/UIElements/ListUIElement.cs @@ -1,6 +1,7 @@ using DunGen; using DunGenPlus.Collections; using DunGenPlus.DevTools.Panels; +using DunGenPlus.DevTools.UIElements.Collections; using LethalLevelLoader; using System; using System.Collections; @@ -10,6 +11,7 @@ using System.Text; using System.Threading.Tasks; using TMPro; using UnityEngine; +using UnityEngine.UI; namespace DunGenPlus.DevTools.UIElements { internal class ListUIElement : BaseUIElement { @@ -20,8 +22,11 @@ namespace DunGenPlus.DevTools.UIElements { internal IList list; internal Type listType; - public void SetupList(string titleText, float offset, List list) { - SetupBase(titleText, offset); + public void SetupList(TitleParameter titleParameter, List list) { + SetupBase(titleParameter); + + var cValue = Mathf.LerpUnclamped(0.4f, 0.6f, titleParameter.offset / 100f); + listTransform.GetComponent().color = new Color(cValue, cValue, cValue, 1f); this.list = list; listType = typeof(T); @@ -55,13 +60,13 @@ namespace DunGenPlus.DevTools.UIElements { if (listType == typeof(DungeonArchetype)){ var entry = (DungeonArchetype)list[index]; var baseValue = DunGenPlusPanel.Instance.selectedAssetCache.archetypes.dictionary[entry]; - DevDebugManager.Instance.CreateArchetypeOptionsUIField(copyParentTransform, "Archetype", layoutOffset + 24f, baseValue, (t) => list[index] = t); + DevDebugManager.Instance.CreateArchetypeOptionsUIField(copyParentTransform, new TitleParameter("Archetype", layoutOffset + 24f), baseValue, (t) => list[index] = t); } else if (listType == typeof(NodeArchetype)) { var entry = (NodeArchetype)list[index]; - DevDebugManager.Instance.CreateStringInputField(copyParentTransform, "Label", layoutOffset + 24f, entry.label, (t) => entry.label = t); - DevDebugManager.Instance.CreateListUIField(copyParentTransform, "Archetypes", layoutOffset + 24f, entry.archetypes); + DevDebugManager.Instance.CreateStringInputField(copyParentTransform, new TitleParameter("Label", layoutOffset + 24f), entry.label, (t) => entry.label = t); + DevDebugManager.Instance.CreateListUIField(copyParentTransform, new TitleParameter("Archetypes", layoutOffset + 24f), entry.archetypes); } copy.SetActive(true); diff --git a/DunGenPlus/DunGenPlus/DevTools/UIElements/StringInputField.cs b/DunGenPlus/DunGenPlus/DevTools/UIElements/StringInputField.cs index 340ecd0..ae9a282 100644 --- a/DunGenPlus/DunGenPlus/DevTools/UIElements/StringInputField.cs +++ b/DunGenPlus/DunGenPlus/DevTools/UIElements/StringInputField.cs @@ -1,4 +1,5 @@ -using System; +using DunGenPlus.DevTools.UIElements.Collections; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -11,8 +12,8 @@ namespace DunGenPlus.DevTools.UIElements public TMP_InputField inputField; - public override void SetupInputField(string title, float offset, string baseValue, Action setAction, string defaultValue) { - base.SetupInputField(title, offset, baseValue, setAction, defaultValue); + public void SetupInputField(TitleParameter titleParameter, string baseValue, Action setAction) { + SetupBase(titleParameter); inputField.onValueChanged.AddListener((t) => SetValue(setAction, t)); Set(baseValue); diff --git a/DunGenPlus/DunGenPlus/DevTools/UIElements/TextUIElement.cs b/DunGenPlus/DunGenPlus/DevTools/UIElements/TextUIElement.cs index 64309bd..318fe81 100644 --- a/DunGenPlus/DunGenPlus/DevTools/UIElements/TextUIElement.cs +++ b/DunGenPlus/DunGenPlus/DevTools/UIElements/TextUIElement.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; namespace DunGenPlus.DevTools.UIElements { internal class TextUIElement : BaseUIElement { - + // left empty cause abstract BaseUIElement is abstract, can't be a component + // but this can be, and BaseUIElement gives it all the functionality required to work as a simple Text UI Element (as name implies jajajajaja) } } diff --git a/DunGenPlus/DunGenPlus/DevTools/UIElements/Vector3InputField.cs b/DunGenPlus/DunGenPlus/DevTools/UIElements/Vector3InputField.cs index b4ad5b0..b520327 100644 --- a/DunGenPlus/DunGenPlus/DevTools/UIElements/Vector3InputField.cs +++ b/DunGenPlus/DunGenPlus/DevTools/UIElements/Vector3InputField.cs @@ -1,4 +1,5 @@ -using System; +using DunGenPlus.DevTools.UIElements.Collections; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -17,9 +18,9 @@ namespace DunGenPlus.DevTools.UIElements { public TMP_InputField zInputField; private Vector3 _value; - public override void SetupInputField(string titleText, float offset, Vector3 baseValue, Action setAction, Vector3 defaultValue) { - base.SetupInputField(titleText, offset, baseValue, setAction, defaultValue); - + public void SetupInputField(TitleParameter titleParameter, Vector3 baseValue, Action setAction) { + SetupBase(titleParameter); + xInputField.onValueChanged.AddListener((t) => SetXValue(setAction, t)); yInputField.onValueChanged.AddListener((t) => SetYValue(setAction, t)); zInputField.onValueChanged.AddListener((t) => SetZValue(setAction, t)); diff --git a/DunGenPlus/DunGenPlus/DunGenPlus.csproj b/DunGenPlus/DunGenPlus/DunGenPlus.csproj index 8689e66..fd9a5f8 100644 --- a/DunGenPlus/DunGenPlus/DunGenPlus.csproj +++ b/DunGenPlus/DunGenPlus/DunGenPlus.csproj @@ -160,6 +160,7 @@ + diff --git a/DunGenPlus/DunGenPlus/Patches/DungeonGeneratorPatch.cs b/DunGenPlus/DunGenPlus/Patches/DungeonGeneratorPatch.cs index 4cf00b8..2d1cfdc 100644 --- a/DunGenPlus/DunGenPlus/Patches/DungeonGeneratorPatch.cs +++ b/DunGenPlus/DunGenPlus/Patches/DungeonGeneratorPatch.cs @@ -12,10 +12,19 @@ using DunGenPlus.Utils; using DunGenPlus.Generation; using DunGenPlus.Managers; using DunGenPlus.Collections; +using DunGenPlus.DevTools; namespace DunGenPlus.Patches { internal class DungeonGeneratorPatch { + [HarmonyPostfix] + [HarmonyPatch(typeof(DungeonGenerator), "InnerGenerate")] + public static void InnerGeneratePatch(ref DungeonGenerator __instance, bool isRetry, ref IEnumerator __result){ + if (DevDebugManager.Instance && !isRetry) { + DevDebugManager.Instance.RecordNewSeed(__instance.ChosenSeed); + } + } + [HarmonyPostfix] [HarmonyPatch(typeof(DungeonGenerator), "GenerateMainPath")] public static void GenerateMainPathPatch(ref DungeonGenerator __instance, ref IEnumerator __result){ @@ -34,6 +43,8 @@ namespace DunGenPlus.Patches { } + + [HarmonyTranspiler] [HarmonyPatch(typeof(DungeonGenerator), "GenerateMainPath", MethodType.Enumerator)] public static IEnumerable GenerateMainPathPatch(IEnumerable instructions){