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

142 lines
5.2 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 InputField scaleField;
public override void Initialize(bool addedNewTag = false)
{
Reset();
AlienRaceDef alienRaceDef = GetCurrentRaceDef();
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);
}
scaleField.text = string.Format("{0:0.000}", alienRaceDef.scale.ToString());
}
public void Reset()
{
RemoveCloneObjectsFromParent(raceSettingsWindow);
}
public void SetIsHumanoid()
{
AlienRaceDef alienRaceDef = GetCurrentRaceDef();
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.LogWarning("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.LogWarning("Selected file was null or invalid"); return; }
alienRaceDef.SetBodyTypeGraphicPath(paths[0], direction, bodyType);
Initialize();
}
public AlienRaceDef GetCurrentRaceDef()
{
string alienRaceDefName = raceSelectDropdown.value < raceSelectDropdown.options.Count ? raceSelectDropdown.options[raceSelectDropdown.value].text : "Human";
if (alienRaceDefName == null || alienRaceDefName == "") alienRaceDefName = "Human";
return AlienRaceDefs.GetNamed(alienRaceDefName);
}
public void SetRaceScale()
{
AlienRaceDef alienRaceDef = GetCurrentRaceDef();
if (alienRaceDef == null) return;
float scale = float.Parse(scaleField.text);
alienRaceDef.scale = Mathf.Clamp(scale, 0.05f, 100f);
}
}
}