rimworld-animation-studio/Assets/Scripts/DialogBoxes/SelectDefNamesDialog.cs
2022-09-15 00:17:44 -05:00

87 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
namespace RimWorldAnimationStudio
{
public class SelectDefNamesDialog : DialogBox
{
private Actor actor;
public void Start()
{
Initialize();
}
public void AddDefName(InputField field)
{
Debug.Log("Attempting to add new def name");
if (field?.text == null || field.text == "")
{ Debug.LogWarning("Input field is null"); return; }
if (Workspace.defNames.Contains(field.text))
{ Debug.LogWarning("Def name is null"); field.text = ""; return; }
Debug.Log("Added new def name: " + field.text);
Workspace.defNames.Add(field.text);
Initialize();
}
public void Initialize(Actor actor = null)
{
if (actor != null)
{ this.actor = actor; }
if (actor == null)
{ actor = this.actor; }
if (actor == null) return;
foreach (string defName in actor.defNames)
{
if (Workspace.defNames.Contains(defName) == false)
{ Workspace.defNames.Add(defName); }
}
Transform contentWindow = transform.FindDeepChild("Content");
Reset();
for (int i = 0; i < Workspace.defNames.Count; i++)
{
string defName = Workspace.defNames[i];
Transform _optionToggle = AddCloneObjectToParent(contentWindow).transform;
_optionToggle.Find("Text").GetComponent<Text>().text = defName;
Toggle toggleComp = _optionToggle.GetComponent<Toggle>();
toggleComp.isOn = actor.defNames.Contains(defName);
toggleComp.onValueChanged.AddListener(delegate {
if (toggleComp.isOn && actor.defNames.Contains(defName) == false)
{ actor.defNames.Add(defName); }
else if (toggleComp.isOn == false && actor.defNames.Contains(defName))
{ actor.defNames.Remove(defName); }
});
}
Transform _optionField = AddCloneObjectToParent(contentWindow, 1).transform;
_optionField.Find("Placeholder").GetComponent<Text>().text = "Enter new def name...";
InputField fieldComp = _optionField.GetComponent<InputField>();
fieldComp.onEndEdit.AddListener(delegate { AddDefName(fieldComp); });
}
public void Reset()
{
Transform contentWindow = transform.FindDeepChild("Content");
RemoveCloneObjectsFromParent(contentWindow);
}
}
}