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

128 lines
4.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using SFB;
using System.IO;
namespace RimWorldAnimationStudio
{
public class RaceSettingsDialog : DialogBox
{
public Dropdown raceSelectDropdown;
public Transform raceSettingsWindow;
public Toggle isHumanoidToggle;
public override void Initialize(bool addedNewTag = false)
{
Reset();
string alienRaceDefName = raceSelectDropdown.value < raceSelectDropdown.options.Count ? raceSelectDropdown.options[raceSelectDropdown.value].text : "Human";
if (alienRaceDefName == null || alienRaceDefName == "") alienRaceDefName = "Human";
AlienRaceDef alienRaceDef = AlienRaceDefs.GetNamed(alienRaceDefName);
if (alienRaceDef == null) return;
isHumanoidToggle.isOn = alienRaceDef.isHumanoid;
Text bodyGraphicsTitle = AddCloneObjectToParent(raceSettingsWindow, 2).GetComponent<Text>();
bodyGraphicsTitle.text = "Body graphic filepaths";
foreach (string bodyType in Tags.bodyTypes)
{
string _bodyType = alienRaceDef.isHumanoid ? bodyType : "None";
if (alienRaceDef.isHumanoid)
{
Text bodyTypeTitle = AddCloneObjectToParent(raceSettingsWindow, 2).GetComponent<Text>();
bodyTypeTitle.text = bodyType;
}
for (int i = 2; i >= 0; i--)
{
CardinalDirection facing = (CardinalDirection)i;
GameObject filepath = AddCloneObjectToParent(raceSettingsWindow, 0);
filepath.GetComponent<Text>().text = facing.ToString();
filepath.transform.Find("FilepathButton").GetComponent<Button>().onClick.AddListener(delegate
{
SetBodyTypeGraphicPath(alienRaceDef, facing, _bodyType);
});
filepath.transform.FindDeepChild("FilepathLabel").GetComponent<Text>().text = alienRaceDef.GetBodyTypeGraphicPath(facing, _bodyType);
}
AddCloneObjectToParent(raceSettingsWindow, 3);
if (alienRaceDef.isHumanoid == false)
{ break; }
}
if (alienRaceDef.isHumanoid)
{
Text headGraphics = AddCloneObjectToParent(raceSettingsWindow, 2).GetComponent<Text>();
headGraphics.text = "Head graphic filepaths";
for (int i = 2; i >= 0; i--)
{
CardinalDirection facing = (CardinalDirection)i;
GameObject filepath = AddCloneObjectToParent(raceSettingsWindow, 0);
filepath.GetComponent<Text>().text = facing.ToString();
filepath.transform.Find("FilepathButton").GetComponent<Button>().onClick.AddListener(delegate
{
SetHeadGraphicPath(alienRaceDef, facing);
});
filepath.transform.FindDeepChild("FilepathLabel").GetComponent<Text>().text = alienRaceDef.GetHeadGraphicPath(facing);
}
AddCloneObjectToParent(raceSettingsWindow, 3);
}
}
public void Reset()
{
RemoveCloneObjectsFromParent(raceSettingsWindow);
}
public void SetIsHumanoid()
{
string alienRaceDefName = raceSelectDropdown.value < raceSelectDropdown.options.Count ? raceSelectDropdown.options[raceSelectDropdown.value].text : "Human";
if (alienRaceDefName == null || alienRaceDefName == "") alienRaceDefName = "Human";
AlienRaceDef alienRaceDef = AlienRaceDefs.GetNamed(alienRaceDefName);
if (alienRaceDef == null) return;
alienRaceDef.isHumanoid = isHumanoidToggle.isOn;
Initialize();
}
public void SetHeadGraphicPath(AlienRaceDef alienRaceDef, CardinalDirection direction)
{
var paths = StandaloneFileBrowser.OpenFilePanel("Select texture File", "", "png", false);
if (paths == null || paths.Any() == false || File.Exists(paths[0]) == false)
{ Debug.LogError("Selected file was null or invalid"); return; }
alienRaceDef.SetHeadGraphicPath(paths[0], direction);
Initialize();
}
public void SetBodyTypeGraphicPath(AlienRaceDef alienRaceDef, CardinalDirection direction, string bodyType)
{
var paths = StandaloneFileBrowser.OpenFilePanel("Select texture File", "", "png", false);
if (paths == null || paths.Any() == false || File.Exists(paths[0]) == false)
{ Debug.LogError("Selected file was null or invalid"); return; }
alienRaceDef.SetBodyTypeGraphicPath(paths[0], direction, bodyType);
Initialize();
}
}
}