2024-04-21 22:38:26 +08:00
|
|
|
using UnityEngine;
|
|
|
|
using System.Linq;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using System.IO;
|
|
|
|
using System;
|
|
|
|
|
|
|
|
public class UIPoseContainer : MonoBehaviour
|
|
|
|
{
|
|
|
|
public DataType Type;
|
|
|
|
public string PoseName;
|
|
|
|
public KeyframeData KeyframeData;
|
|
|
|
public TMPro.TMP_InputField NameLabel;
|
|
|
|
|
2024-05-10 15:56:39 +08:00
|
|
|
static UISaveLoadPanel SaveLoadPanel => ModelViewerInterface.GetInstance().CanvasContainer.SaveLoadPanel;
|
2024-04-21 22:38:26 +08:00
|
|
|
static ModelViewerMain Main => ModelViewerMain.GetInstance();
|
|
|
|
|
|
|
|
public static UIPoseContainer CreateNew(string poseName, KeyframeData poseData, bool forceOverride)
|
|
|
|
{
|
|
|
|
if (poseData == null) return null;
|
|
|
|
if (string.IsNullOrEmpty(poseName))
|
|
|
|
{
|
|
|
|
Error.Log(Color.red, "Name the pose first");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-05-10 15:56:39 +08:00
|
|
|
if (SaveLoadPanel.GetPoseByName(poseName, out var savedPose))
|
2024-04-21 22:38:26 +08:00
|
|
|
{
|
|
|
|
if (!forceOverride)
|
|
|
|
{
|
|
|
|
Error.Log(Color.red, "Preset with this name already exists");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-05-10 15:56:39 +08:00
|
|
|
savedPose = Instantiate(SharedResources.Instance.UIPoseContainer, SaveLoadPanel.ScrollPose.content);
|
|
|
|
SaveLoadPanel.AddPose(savedPose);
|
2024-04-21 22:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
savedPose.Init(poseName, poseData);
|
|
|
|
|
|
|
|
return savedPose;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Init(string poseName, KeyframeData poseData)
|
|
|
|
{
|
|
|
|
NameLabel.text = poseName;
|
|
|
|
NameLabel.interactable = false;
|
|
|
|
KeyframeData = poseData;
|
|
|
|
transform.SetAsFirstSibling();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SaveNew()
|
|
|
|
{
|
|
|
|
var poseName = NameLabel.text;
|
|
|
|
|
2024-05-10 15:56:39 +08:00
|
|
|
if (SaveLoadPanel.GetPoseByName(poseName, out var savedPose))
|
2024-04-21 22:38:26 +08:00
|
|
|
{
|
|
|
|
Error.Log(Color.red, "Preset with this name already exists");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-05-10 15:56:39 +08:00
|
|
|
savedPose = Instantiate(SharedResources.Instance.UIPoseContainer, SaveLoadPanel.ScrollPose.content);
|
2024-04-21 22:38:26 +08:00
|
|
|
savedPose.NameLabel.text = poseName;
|
2024-05-10 15:56:39 +08:00
|
|
|
SaveLoadPanel.AddPose(savedPose);
|
2024-04-21 22:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
savedPose.Save();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Save()
|
|
|
|
{
|
|
|
|
Error.Log(Color.blue, $"Saving pose...");
|
|
|
|
|
|
|
|
var poseData = Main.SelectedObject.SerializeFrame();
|
|
|
|
string poseName = NameLabel.text;
|
2024-05-10 15:56:39 +08:00
|
|
|
string fileName = Path.GetFullPath(Path.Combine(UISaveLoadPanel.SavePoseDirectory, $"{poseName}.pose"));
|
2024-04-21 22:38:26 +08:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var settings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto, DefaultValueHandling = DefaultValueHandling.Ignore };
|
2024-05-10 15:56:39 +08:00
|
|
|
var json = JsonConvert.SerializeObject(poseData, typeof(KeyframeData), Formatting.Indented, settings);
|
2024-04-21 22:38:26 +08:00
|
|
|
File.WriteAllText(fileName, json);
|
|
|
|
Error.Log(Color.green, $"Pose saved to {fileName}");
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Error.Log(Color.red, ex.Message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Init(poseName, poseData);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Load()
|
|
|
|
{
|
|
|
|
if (Main.SelectedObject == null) return;
|
|
|
|
|
|
|
|
if (Input.GetKey(KeyCode.LeftShift))
|
|
|
|
{
|
|
|
|
LoadAdvanced();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Main.SelectedObject.PastePose(KeyframeData, new PoseLoadOptions(true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void LoadAdvanced()
|
|
|
|
{
|
|
|
|
if (Main.SelectedObject == null) return;
|
|
|
|
|
|
|
|
UIPopupPastePanel.Create(
|
|
|
|
(pasteOption) => { Main.SelectedObject.PastePose(KeyframeData, pasteOption); Debug.Log(pasteOption.Root); },
|
|
|
|
() => { });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Delete()
|
|
|
|
{
|
|
|
|
Destroy(this.gameObject);
|
|
|
|
|
2024-05-10 15:56:39 +08:00
|
|
|
string path = Path.GetFullPath(Path.Combine(UISaveLoadPanel.SavePoseDirectory, $"{NameLabel.text}.pose"));
|
2024-04-21 22:38:26 +08:00
|
|
|
if (File.Exists(path))
|
|
|
|
File.Delete(path);
|
|
|
|
|
|
|
|
Error.Log(Color.yellow, "Deleted " + path);
|
|
|
|
}
|
|
|
|
}
|