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

151 lines
5.5 KiB
C#
Raw Normal View History

2022-09-24 07:17:40 +00:00
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;
2022-09-24 19:12:18 +00:00
public InputField scaleField;
2022-09-24 07:17:40 +00:00
2022-10-31 05:44:53 +00:00
protected override void OnEnable()
{
raceSelectDropdown.ClearOptions();
raceSelectDropdown.AddOptions(PawnRaceDefs.allDefs.Select(x => x.defName).ToList());
base.OnEnable();
}
2022-09-24 07:17:40 +00:00
public override void Initialize(bool addedNewTag = false)
{
Reset();
2022-09-24 19:12:18 +00:00
2022-10-27 05:56:04 +00:00
PawnRaceDef pawnRaceDef = GetCurrentRaceDef();
if (pawnRaceDef == null) return;
2022-09-24 07:17:40 +00:00
2022-10-27 05:56:04 +00:00
isHumanoidToggle.SetIsOnWithoutNotify(pawnRaceDef.isHumanoid);
2022-09-24 07:17:40 +00:00
Text bodyGraphicsTitle = AddCloneObjectToParent(raceSettingsWindow, 2).GetComponent<Text>();
bodyGraphicsTitle.text = "Body graphic filepaths";
2022-10-27 05:56:04 +00:00
List<string> allTags = pawnRaceDef.isHumanoid ? DefaultTags.bodyTypes : new List<string>() { "None" };
foreach (string bodyType in allTags)
2022-09-24 07:17:40 +00:00
{
string _bodyType = bodyType;
2022-09-24 07:17:40 +00:00
2022-10-27 05:56:04 +00:00
if (pawnRaceDef.isHumanoid)
2022-09-24 07:17:40 +00:00
{
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
{
2022-10-27 05:56:04 +00:00
SetBodyTypeGraphicPath(pawnRaceDef, facing, _bodyType);
2022-09-24 07:17:40 +00:00
});
2022-10-27 05:56:04 +00:00
filepath.transform.FindDeepChild("FilepathLabel").GetComponent<Text>().text = pawnRaceDef.GetBodyTypeGraphicPath(facing, _bodyType);
2022-09-24 07:17:40 +00:00
}
AddCloneObjectToParent(raceSettingsWindow, 3);
}
2022-10-27 05:56:04 +00:00
if (pawnRaceDef.isHumanoid)
2022-09-24 07:17:40 +00:00
{
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
{
2022-10-27 05:56:04 +00:00
SetHeadGraphicPath(pawnRaceDef, facing);
2022-09-24 07:17:40 +00:00
});
2022-10-27 05:56:04 +00:00
filepath.transform.FindDeepChild("FilepathLabel").GetComponent<Text>().text = pawnRaceDef.GetHeadGraphicPath(facing);
2022-09-24 07:17:40 +00:00
}
AddCloneObjectToParent(raceSettingsWindow, 3);
}
2022-09-24 19:12:18 +00:00
2022-10-31 05:44:53 +00:00
scaleField.text = string.Format("{0:0.000}", pawnRaceDef.scale);
2022-09-24 07:17:40 +00:00
}
public void Reset()
{
2022-10-31 05:44:53 +00:00
RemoveCloneObjectsFromParent(raceSettingsWindow);
2022-09-24 07:17:40 +00:00
}
public void SetIsHumanoid()
{
2022-10-27 05:56:04 +00:00
PawnRaceDef pawnRaceDef = GetCurrentRaceDef();
if (pawnRaceDef == null) return;
2022-09-24 07:17:40 +00:00
2022-10-27 05:56:04 +00:00
pawnRaceDef.isHumanoid = isHumanoidToggle.isOn;
2022-09-24 07:17:40 +00:00
Initialize();
}
2022-10-27 05:56:04 +00:00
public void SetHeadGraphicPath(PawnRaceDef pawnRaceDef, CardinalDirection direction)
2022-09-24 07:17:40 +00:00
{
var paths = StandaloneFileBrowser.OpenFilePanel("Select texture File", "", "png", false);
if (paths == null || paths.Any() == false || File.Exists(paths[0]) == false)
2022-09-26 04:10:41 +00:00
{ Debug.LogWarning("Selected file was null or invalid"); return; }
2022-09-24 07:17:40 +00:00
2022-10-27 05:56:04 +00:00
pawnRaceDef.SetHeadGraphicPath(paths[0], direction);
2022-09-24 07:17:40 +00:00
Initialize();
}
2022-10-27 05:56:04 +00:00
public void SetBodyTypeGraphicPath(PawnRaceDef pawnRaceDef, CardinalDirection direction, string bodyType)
2022-09-24 07:17:40 +00:00
{
var paths = StandaloneFileBrowser.OpenFilePanel("Select texture File", "", "png", false);
if (paths == null || paths.Any() == false || File.Exists(paths[0]) == false)
2022-09-26 04:10:41 +00:00
{ Debug.LogWarning("Selected file was null or invalid"); return; }
2022-09-24 07:17:40 +00:00
2022-10-27 05:56:04 +00:00
pawnRaceDef.SetBodyTypeGraphicPath(paths[0], direction, bodyType);
2022-09-24 07:17:40 +00:00
Initialize();
}
2022-09-24 19:12:18 +00:00
2022-10-27 05:56:04 +00:00
public PawnRaceDef GetCurrentRaceDef()
2022-09-24 19:12:18 +00:00
{
2022-10-27 05:56:04 +00:00
string pawnRaceDefName = raceSelectDropdown.value < raceSelectDropdown.options.Count ? raceSelectDropdown.options[raceSelectDropdown.value].text : "Human";
if (pawnRaceDefName == null || pawnRaceDefName == "") pawnRaceDefName = "Human";
2022-09-24 19:12:18 +00:00
2022-10-27 05:56:04 +00:00
return PawnRaceDefs.GetNamed(pawnRaceDefName);
2022-09-24 19:12:18 +00:00
}
public void SetRaceScale()
{
2022-10-27 05:56:04 +00:00
PawnRaceDef pawnRaceDef = GetCurrentRaceDef();
if (pawnRaceDef == null) return;
2022-09-24 19:12:18 +00:00
float scale = float.Parse(scaleField.text);
2022-10-31 05:44:53 +00:00
pawnRaceDef.scale = Mathf.Clamp(scale, 0.01f, 100f);
scaleField.text = string.Format("{0:0.000}", pawnRaceDef.scale);
2022-09-24 19:12:18 +00:00
}
2022-09-24 07:17:40 +00:00
}
}