rimworld-animation-studio/Assets/Scripts/Managers/ApplicationManager.cs

228 lines
9.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using System.IO;
using UnityEngine.UI;
using SFB;
namespace RimWorldAnimationStudio
{
public class ApplicationManager : Singleton<ApplicationManager>
{
public DialogBox exitDialog;
public SelectAnimationDialog selectAnimationDialog;
public void Start()
{
LoadCustomArrays();
/*AlienRaceDef human = new AlienRaceDef();
human.Initialize("Human");
human.SetHeadGraphicPath(Path.Combine(Application.streamingAssetsPath, "Textures/Humanlike/Heads/Head0.png"), CardinalDirection.North);
human.SetHeadGraphicPath(Path.Combine(Application.streamingAssetsPath, "Textures/Humanlike/Heads/Head1.png"), CardinalDirection.East);
human.SetHeadGraphicPath(Path.Combine(Application.streamingAssetsPath, "Textures/Humanlike/Heads/Head2.png"), CardinalDirection.South);
human.SetBodyTypeGraphicPath(Path.Combine(Application.streamingAssetsPath, "Textures/Humanlike/Bodies/Male0.png"), CardinalDirection.North, "Male");
human.SetBodyTypeGraphicPath(Path.Combine(Application.streamingAssetsPath, "Textures/Humanlike/Bodies/Male1.png"), CardinalDirection.East, "Male");
human.SetBodyTypeGraphicPath(Path.Combine(Application.streamingAssetsPath, "Textures/Humanlike/Bodies/Male2.png"), CardinalDirection.South, "Male");
human.SetBodyTypeGraphicPath(Path.Combine(Application.streamingAssetsPath, "Textures/Humanlike/Bodies/Female0.png"), CardinalDirection.North, "Female");
human.SetBodyTypeGraphicPath(Path.Combine(Application.streamingAssetsPath, "Textures/Humanlike/Bodies/Female1.png"), CardinalDirection.East, "Female");
human.SetBodyTypeGraphicPath(Path.Combine(Application.streamingAssetsPath, "Textures/Humanlike/Bodies/Female2.png"), CardinalDirection.South, "Female");
human.SetBodyTypeGraphicPath(Path.Combine(Application.streamingAssetsPath, "Textures/Humanlike/Bodies/Fat0.png"), CardinalDirection.North, "Fat");
human.SetBodyTypeGraphicPath(Path.Combine(Application.streamingAssetsPath, "Textures/Humanlike/Bodies/Fat1.png"), CardinalDirection.East, "Fat");
human.SetBodyTypeGraphicPath(Path.Combine(Application.streamingAssetsPath, "Textures/Humanlike/Bodies/Fat2.png"), CardinalDirection.South, "Fat");
human.SetBodyTypeGraphicPath(Path.Combine(Application.streamingAssetsPath, "Textures/Humanlike/Bodies/Hulk0.png"), CardinalDirection.North, "Hulk");
human.SetBodyTypeGraphicPath(Path.Combine(Application.streamingAssetsPath, "Textures/Humanlike/Bodies/Hulk1.png"), CardinalDirection.East, "Hulk");
human.SetBodyTypeGraphicPath(Path.Combine(Application.streamingAssetsPath, "Textures/Humanlike/Bodies/Hulk2.png"), CardinalDirection.South, "Hulk");
human.SetBodyTypeGraphicPath(Path.Combine(Application.streamingAssetsPath, "Textures/Humanlike/Bodies/Thin0.png"), CardinalDirection.North, "Thin");
human.SetBodyTypeGraphicPath(Path.Combine(Application.streamingAssetsPath, "Textures/Humanlike/Bodies/Thin1.png"), CardinalDirection.East, "Thin");
human.SetBodyTypeGraphicPath(Path.Combine(Application.streamingAssetsPath, "Textures/Humanlike/Bodies/Thin2.png"), CardinalDirection.South, "Thin");*/
//AlienRaceDefs.allDefs.Add(human);
LoadAlienRaceDefs();
}
public void TryToCloseApplication()
{
exitDialog.Pop();
}
public void CloseApplication()
{
Debug.Log("Exiting application");
Application.Quit();
}
public void TryToLoadAnimation()
{
var paths = StandaloneFileBrowser.OpenFilePanel("Open AnimationDef File", "", "xml", false);
if (paths == null || paths.Any() == false)
{ Debug.LogError("Selected file was null or invalid"); return; }
Defs defs = XmlUtility.ReadXML<Defs>(paths[0]);
if (defs?.animationDefs == null)
{ Debug.LogError("Selected file contains no animation data"); return; }
if (defs.animationDefs.Count == 1)
{ LoadAnimation(defs.animationDefs[0]); return; }
selectAnimationDialog.Initialize(defs);
selectAnimationDialog.Pop();
}
public void LoadAnimation(AnimationDef animationDef)
{
UpdateCustomArrays(animationDef);
RunPostLoadOperations(animationDef);
Debug.Log("Loaded AnimationDef: " + animationDef.defName);
Workspace.animationDef = animationDef;
animationDef.Initialize();
Workspace.Instance.ClearHistory();
Workspace.Instance.RecordEvent("AnimationDef loaded");
AnimationController.Instance.MakeDirty();
var animationDefCards = Resources.FindObjectsOfTypeAll(typeof(AnimationDefCard)) as AnimationDefCard[];
if (animationDefCards != null)
{
animationDefCards[0].Initialize();
animationDefCards[0].gameObject.SetActive(true);
}
}
public void RunPostLoadOperations(AnimationDef animationDef)
{
}
public void TrySaveAnimation()
{
if (Workspace.animationDef == null)
{ return; }
string defName = Workspace.animationDef.defName != null && Workspace.animationDef.defName != "" ? Workspace.animationDef.defName : "newAnimationDef";
var path = StandaloneFileBrowser.SaveFilePanel("Save AnimationDef File", "", defName, "xml");
if (path != null && path != "")
{ SaveAnimation(path); }
}
public void SaveAnimation(string path)
{
AnimationDef animationDef = Workspace.animationDef.Copy();
RunPreSaveOperations(animationDef);
Debug.Log("Saving AnimationDef: " + Workspace.animationDef.defName);
Defs defs = new Defs();
defs.animationDefs.Add(animationDef);
XmlUtility.WriteXML(defs, path);
}
public void RunPreSaveOperations(AnimationDef animationDef)
{
foreach (AnimationStage stage in animationDef.animationStages)
{
stage.ValidateData();
foreach (PawnAnimationClip clip in stage.animationClips)
{
clip.ValidateData();
foreach (PawnKeyframe keyframe in clip.keyframes)
{ keyframe.ValidateData(); }
}
}
}
public void NewAnimation()
{
var path = Path.Combine(Application.streamingAssetsPath, "AnimationDefs/newAnimationDef.xml");
Defs defs = XmlUtility.ReadXML<Defs>(path);
if (defs?.animationDefs == null)
{ Debug.LogError("Default animation def file contains no animation data"); return; }
LoadAnimation(defs.animationDefs[0]);
}
public void SaveCustomArrays()
{
var path = Path.Combine(Application.streamingAssetsPath, "customTags.xml");
CustomTagsHelper helper = new CustomTagsHelper();
helper.bodyParts = CustomTags.bodyParts;
helper.bodyDefTypes = CustomTags.bodyDefTypes;
helper.sexTypes = CustomTags.sexTypes;
helper.interactionDefTypes = CustomTags.interactionDefTypes;
helper.soundDefs = CustomTags.soundDefs;
XmlUtility.WriteXML(helper, path);
}
public void LoadCustomArrays()
{
var path = Path.Combine(Application.streamingAssetsPath, "customTags.xml");
if (File.Exists(path) == false)
{ SaveCustomArrays(); return; }
CustomTagsHelper helper = XmlUtility.ReadXML<CustomTagsHelper>(path);
CustomTags.bodyParts = helper.bodyParts;
CustomTags.bodyDefTypes = helper.bodyDefTypes;
CustomTags.sexTypes = helper.sexTypes;
CustomTags.interactionDefTypes = helper.interactionDefTypes;
CustomTags.soundDefs = helper.soundDefs;
}
public void UpdateCustomArrays(AnimationDef animationDef)
{
CustomTags.bodyParts.AddRangeDistinct(animationDef.actors.SelectMany(x => x.requiredGenitals).Except(Tags.bodyParts));
CustomTags.bodyDefTypes.AddRangeDistinct(animationDef.actors.SelectMany(x => x.bodyDefTypes).Except(Tags.bodyDefTypes));
CustomTags.sexTypes.AddRangeDistinct(animationDef.sexTypes.Except(Tags.sexTypes));
CustomTags.interactionDefTypes.AddRangeDistinct(animationDef.interactionDefTypes.Except(Tags.interactionDefTypes));
CustomTags.soundDefs.AddRangeDistinct(animationDef.animationStages.SelectMany(x => x.animationClips.SelectMany(y => y.keyframes.Select(z => z.soundEffect))).Except(Tags.soundDefs));
SaveCustomArrays();
}
public void LoadAlienRaceDefs()
{
var path = Path.Combine(Application.streamingAssetsPath, "alienRaceDefs.xml");
if (File.Exists(path) == false)
{ SaveAlienRaceDefs(); return; }
AlienRaceDefs.allDefs = XmlUtility.ReadXML<List<AlienRaceDef>>(path);
AlienRaceDefs.OnLoad();
}
public void SaveAlienRaceDefs()
{
var path = Path.Combine(Application.streamingAssetsPath, "alienRaceDefs.xml");
XmlUtility.WriteXML(AlienRaceDefs.allDefs, path);
}
}
}