rimworld-animation-studio/Assets/Scripts/GUI/DialogBoxes/SelectBodyPartsDialog.cs

88 lines
2.8 KiB
C#
Raw Normal View History

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 SelectBodyPartsDialog : DialogBox
{
private Actor actor;
public void Start()
{
Initialize();
}
public void AddBodyPart(InputField field)
{
Debug.Log("Attempting to add new body part");
if (field?.text == null || field.text == "")
{ Debug.LogWarning("Input field is null"); return; }
if (Workspace.bodyParts.Contains(field.text))
{ Debug.LogWarning("Body part is null"); field.text = ""; return; }
Debug.Log("Add new body part: " + field.text);
Workspace.bodyParts.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 bodyPart in actor.requiredGenitals)
{
if (Workspace.bodyParts.Contains(bodyPart) == false)
{ Workspace.bodyParts.Add(bodyPart); }
}
Transform contentWindow = transform.FindDeepChild("Content");
Reset();
for (int i = 0; i < Workspace.bodyParts.Count; i++)
{
string bodyPart = Workspace.bodyParts[i];
Transform _optionToggle = AddCloneObjectToParent(contentWindow).transform;
_optionToggle.Find("Text").GetComponent<Text>().text = bodyPart;
Toggle toggleComp = _optionToggle.GetComponent<Toggle>();
toggleComp.isOn = actor.requiredGenitals.Contains(bodyPart);
toggleComp.onValueChanged.AddListener(delegate {
if (toggleComp.isOn && actor.requiredGenitals.Contains(bodyPart) == false)
{ actor.requiredGenitals.Add(bodyPart); }
else if (toggleComp.isOn == false && actor.requiredGenitals.Contains(bodyPart))
{ actor.requiredGenitals.Remove(bodyPart); }
});
}
Transform _optionField = AddCloneObjectToParent(contentWindow, 1).transform;
_optionField.Find("Placeholder").GetComponent<Text>().text = "Enter new body part name...";
InputField fieldComp = _optionField.GetComponent<InputField>();
fieldComp.onEndEdit.AddListener(delegate { AddBodyPart(fieldComp); });
}
public void Reset()
{
Transform contentWindow = transform.FindDeepChild("Content");
RemoveCloneObjectsFromParent(contentWindow);
}
}
}