Added setup for DunGen generation dev tools

This commit is contained in:
LadyAliceMargatroid 2024-08-23 12:23:16 -07:00
parent e2aba5fcdb
commit 3160685123
28 changed files with 1502 additions and 44 deletions

View file

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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) {
if (int.TryParse(text, out var result)){
return result;
} else {
Plugin.logger.LogWarning($"Couldn't parse {text} into an int");
return defaultValue;
}
}
protected float ParseTextFloat(string text, float defaultValue = 0f) {
if (float.TryParse(text, out var result)){
return result;
} else {
Plugin.logger.LogWarning($"Couldn't parse {text} into a float");
return defaultValue;
}
}
}
}

View file

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace DunGenPlus.DevTools.UIElements {
internal abstract class BaseUIElement : MonoBehaviour {
public TextMeshProUGUI titleTextMesh;
internal string title;
public LayoutElement layoutElement;
internal float layoutOffset;
public void SetupBase(string titleText, float offset) {
title = titleText;
SetText(title);
layoutOffset = offset;
if (layoutElement) {
layoutElement.minWidth -= layoutOffset;
}
}
public void SetText(string value) {
titleTextMesh.text = value;
}
}
}

View file

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TMPro;
using UnityEngine.UI;
namespace DunGenPlus.DevTools.UIElements {
internal class BoolInputField : BaseInputField<bool> {
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);
toggle.onValueChanged.AddListener((t) => SetValue(setAction, t));
Set(baseValue);
}
private void SetValue(Action<bool> setAction, bool state) {
Plugin.logger.LogInfo($"Setting {title} to {state}");
setAction.Invoke(state);
}
public override void Set(bool state){
toggle.isOn = state;
}
}
}

View file

@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TMPro;
using UnityEngine.UI;
namespace DunGenPlus.DevTools.UIElements
{
internal class DropdownInputField : BaseUIElement {
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);
dropDown.options = options.Select(c => {
return new TMP_Dropdown.OptionData(c.Substring(0, Math.Min(24, c.Length)));
}).ToList();
dropDown.onValueChanged.AddListener((t) => SetValue(setAction, convertIndex, t));
dropDown.value = baseValue;
}
private void SetValue<T>(Action<T> setAction, Func<int, T> convertIndex, int index) {
var value = convertIndex.Invoke(index);
Plugin.logger.LogInfo($"Setting {title} to {value}");
setAction.Invoke(value);
}
}
}

View file

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TMPro;
namespace DunGenPlus.DevTools.UIElements
{
internal class FloatInputField : BaseInputField<float> {
public TMP_InputField inputField;
internal float defaultValue = 0f;
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;
inputField.onValueChanged.AddListener((t) => SetValue(setAction, t));
Set(baseValue);
}
private void SetValue(Action<float> setAction, string text) {
Plugin.logger.LogInfo($"Setting {title} to {text}");
setAction.Invoke(ParseTextFloat(text, defaultValue));
}
public override void Set(float value){
inputField.text = value.ToString();
}
}
}

View file

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TMPro;
namespace DunGenPlus.DevTools.UIElements {
internal class IntInputField : BaseInputField<int> {
public TMP_InputField inputField;
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;
inputField.onValueChanged.AddListener((t) => SetValue(setAction, t));
Set(baseValue);
}
private void SetValue(Action<int> setAction, string text) {
Plugin.logger.LogInfo($"Setting {title} to {text}");
setAction.Invoke(ParseTextInt(text, defaultValue));
}
public override void Set(int value){
inputField.text = value.ToString();
}
}
}

View file

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TMPro;
using UnityEngine.UI;
namespace DunGenPlus.DevTools.UIElements {
internal class IntSliderField : BaseInputField<int> {
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;
inputField.onValueChanged.AddListener((t) => SetValue(setAction, t));
Set(baseValue);
}
private void SetValue(Action<int> setAction, float value) {
Plugin.logger.LogInfo($"Setting {title} to {value}");
setAction.Invoke((int)value);
}
public override void Set(int value){
inputField.value = value;
textMesh.text = value.ToString();
}
}
}

View file

@ -0,0 +1,77 @@
using DunGen;
using DunGenPlus.Collections;
using DunGenPlus.DevTools.Panels;
using LethalLevelLoader;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TMPro;
using UnityEngine;
namespace DunGenPlus.DevTools.UIElements {
internal class ListUIElement : BaseUIElement {
public GameObject templatePrefab;
public Transform listTransform;
internal IList list;
internal Type listType;
public void SetupList<T>(string titleText, float offset, List<T> list) {
SetupBase(titleText, offset);
this.list = list;
listType = typeof(T);
for(var i = 0; i < list.Count; ++i) {
CreateEntry(i);
}
}
public void AddElement() {
object item = null;
if (listType == typeof(DungeonArchetype)) {
item = null;
} else if (listType == typeof(NodeArchetype)) {
item = new NodeArchetype();
}
list.Add(item);
CreateEntry(list.Count - 1);
}
public void RemoveElement(){
if (list.Count == 0) return;
list.RemoveAt(list.Count - 1);
Destroy(listTransform.GetChild(listTransform.childCount - 1).gameObject);
}
public void CreateEntry(int index){
var copy = CreateCopy(index);
var copyParentTransform = copy.transform.Find("Items");
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);
}
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);
}
copy.SetActive(true);
}
public GameObject CreateCopy(int index){
var copy = Instantiate(templatePrefab, listTransform);
copy.transform.Find("Element").GetComponent<TextMeshProUGUI>().text = $"Element {index}";
return copy;
}
}
}

View file

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TMPro;
namespace DunGenPlus.DevTools.UIElements
{
internal class StringInputField : BaseInputField<string> {
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);
inputField.onValueChanged.AddListener((t) => SetValue(setAction, t));
Set(baseValue);
}
private void SetValue(Action<string> setAction, string text) {
Plugin.logger.LogInfo($"Setting {title} to {text}");
setAction.Invoke(text);
}
public override void Set(string value){
inputField.text = value;
}
}
}

View file

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DunGenPlus.DevTools.UIElements {
internal class TextUIElement : BaseUIElement {
}
}

View file

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
namespace DunGenPlus.DevTools.UIElements {
internal class Vector3InputField : BaseInputField<Vector3> {
public TMP_InputField xInputField;
public TMP_InputField yInputField;
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);
xInputField.onValueChanged.AddListener((t) => SetXValue(setAction, t));
yInputField.onValueChanged.AddListener((t) => SetYValue(setAction, t));
zInputField.onValueChanged.AddListener((t) => SetZValue(setAction, t));
Set(baseValue);
}
private void SetXValue(Action<Vector3> setAction, string text){
Plugin.logger.LogInfo($"Setting {title}.x to {text}");
_value.x = ParseTextFloat(text);
setAction.Invoke(_value);
}
private void SetYValue(Action<Vector3> setAction, string text){
Plugin.logger.LogInfo($"Setting {title}.y to {text}");
_value.y = ParseTextFloat(text);
setAction.Invoke(_value);
}
private void SetZValue(Action<Vector3> setAction, string text){
Plugin.logger.LogInfo($"Setting {title}.z to {text}");
_value.z = ParseTextFloat(text);
setAction.Invoke(_value);
}
public override void Set(Vector3 value){
_value = value;
xInputField.text = value.x.ToString();
yInputField.text = value.y.ToString();
zInputField.text = value.z.ToString();
}
}
}