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
This commit is contained in:
LadyAliceMargatroid 2024-08-24 06:08:59 -07:00
parent 3160685123
commit fdb1767890
18 changed files with 245 additions and 124 deletions

View file

@ -8,10 +8,6 @@ using UnityEngine.UI;
namespace DunGenPlus.DevTools.UIElements {
internal abstract class BaseInputField<T> : BaseUIElement {
public virtual void SetupInputField(string titleText, float offset, T baseValue, Action<T> setAction, T defaultValue){
SetupBase(titleText, offset);
}
public abstract void Set(T value);
protected int ParseTextInt(string text, int defaultValue = 0) {

View file

@ -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;
}

View file

@ -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<bool> setAction, bool defaultValue) {
base.SetupInputField(title, offset, baseValue, setAction, defaultValue);
public void SetupInputField(TitleParameter titleParameter, bool baseValue, Action<bool> setAction) {
SetupBase(titleParameter);
toggle.onValueChanged.AddListener((t) => SetValue(setAction, t));
Set(baseValue);

View file

@ -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);
}
}

View file

@ -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<T>(string titleText, float offset, int baseValue, Action<T> setAction, Func<int, T> convertIndex, IEnumerable<string> options) {
SetupBase(titleText, offset);
public void SetupDropdown<T>(TitleParameter titleParameter, int baseValue, Action<T> setAction, Func<int, T> convertIndex, IEnumerable<string> options) {
SetupBase(titleParameter);
dropDown.options = options.Select(c => {
return new TMP_Dropdown.OptionData(c.Substring(0, Math.Min(24, c.Length)));

View file

@ -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<float> {
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<float> setAction , float defaultValue) {
base.SetupInputField(title, offset, baseValue, setAction, defaultValue);
this.defaultValue = defaultValue;
public void SetupInputField(TitleParameter titleParameter, FloatParameter floatParameter, Action<float> 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<float> 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){

View file

@ -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<int> {
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<int> setAction , int defaultValue) {
base.SetupInputField(title, offset, baseValue, setAction, defaultValue);
this.defaultValue = defaultValue;
public void SetupInputField(TitleParameter titleParameter, IntParameter intParameter, Action<int> 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<int> 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){

View file

@ -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<int> setAction , int defaultValue) {
base.SetupInputField(title, offset, baseValue, setAction, defaultValue);
this.defaultValue = defaultValue;
public void SetupInputField(TitleParameter titleParameter, IntParameter intParameter, Action<int> 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<int> setAction, float value) {

View file

@ -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<T>(string titleText, float offset, List<T> list) {
SetupBase(titleText, offset);
public void SetupList<T>(TitleParameter titleParameter, List<T> list) {
SetupBase(titleParameter);
var cValue = Mathf.LerpUnclamped(0.4f, 0.6f, titleParameter.offset / 100f);
listTransform.GetComponent<Image>().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);

View file

@ -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<string> setAction, string defaultValue) {
base.SetupInputField(title, offset, baseValue, setAction, defaultValue);
public void SetupInputField(TitleParameter titleParameter, string baseValue, Action<string> setAction) {
SetupBase(titleParameter);
inputField.onValueChanged.AddListener((t) => SetValue(setAction, t));
Set(baseValue);

View file

@ -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)
}
}

View file

@ -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<Vector3> setAction, Vector3 defaultValue) {
base.SetupInputField(titleText, offset, baseValue, setAction, defaultValue);
public void SetupInputField(TitleParameter titleParameter, Vector3 baseValue, Action<Vector3> setAction) {
SetupBase(titleParameter);
xInputField.onValueChanged.AddListener((t) => SetXValue(setAction, t));
yInputField.onValueChanged.AddListener((t) => SetYValue(setAction, t));
zInputField.onValueChanged.AddListener((t) => SetZValue(setAction, t));