rimworld-animation-studio/Assets/Scripts/GUI/AnimationTimeline.cs
2022-09-21 00:40:58 -05:00

104 lines
3.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
namespace RimWorldAnimationStudio
{
public class AnimationTimeline : MonoBehaviour
{
public int actorID;
public KeyframeSlider keyframeSliderPrefab;
public void Initialize(int actorID)
{
this.actorID = actorID;
Reset();
PawnAnimationClip clip = Workspace.Instance.GetPawnAnimationClip(actorID);
if (clip == null || clip.keyframes.NullOrEmpty())
{
Debug.Log("Clip was empty");
clip = new PawnAnimationClip();
clip.keyframes.Add(new PawnKeyframe());
clip.BuildSimpleCurves();
}
foreach (PawnKeyframe keyframe in clip.keyframes)
{
KeyframeSlider keyframeSlider = Instantiate(keyframeSliderPrefab, transform);
keyframeSlider.Initialize(this, actorID, keyframe.keyframeID);
}
}
public void Reset()
{
foreach(KeyframeSlider keyframeSlider in GetComponentsInChildren<KeyframeSlider>())
{ Destroy(keyframeSlider.gameObject); }
}
public void AddPawnKeyFrame(int keyframeID)
{
KeyframeSlider keyframeSlider = Instantiate(keyframeSliderPrefab, transform);
keyframeSlider.Initialize(this, actorID, keyframeID);
}
public void RemovePawnKeyFrame(int keyframeID)
{
KeyframeSlider keyframeSlider = GetComponentsInChildren<KeyframeSlider>().FirstOrDefault(x => x.keyframeID == keyframeID);
Destroy(keyframeSlider?.gameObject);
}
public void Update()
{
if (Workspace.actorID == actorID)
{ GetComponent<Image>().color = Constants.ColorGoldYellow; }
else
{ GetComponent<Image>().color = Constants.ColorGrey; }
}
public void InitiateUpdateOfGhostFrames()
{
BroadcastMessage("UpdateGhostFrames");
}
public void OnMoveTimeline(int delta)
{
int? siblingIndex = transform.parent.GetComponentsInChildren<AnimationTimeline>()?.ToList()?.IndexOf(this);
int? siblingCount = transform.parent.GetComponentsInChildren<AnimationTimeline>()?.ToList()?.Count();
if (siblingIndex != null && siblingCount != null && MoveAnimationTimeline(siblingIndex.Value, delta))
{
//siblingIndex = Mathf.Clamp(siblingCount.Value + delta, 0, siblingCount.Value - 1);
transform.SetSiblingIndex(transform.GetSiblingIndex() + delta);
}
}
public bool MoveAnimationTimeline(int startIndex, int delta)
{
if (startIndex + delta < 0 || startIndex + delta >= Workspace.animationDef.animationStages[Workspace.stageID].animationClips.Count)
{ return false; }
Actor actor = Workspace.animationDef.actors[startIndex];
Workspace.animationDef.actors[startIndex] = Workspace.animationDef.actors[startIndex + delta];
Workspace.animationDef.actors[startIndex + delta] = actor;
PawnAnimationClip clip = Workspace.Instance.GetPawnAnimationClip(startIndex);
Workspace.animationDef.animationStages[Workspace.stageID].animationClips[startIndex] = Workspace.animationDef.animationStages[Workspace.stageID].animationClips[startIndex + delta];
Workspace.animationDef.animationStages[Workspace.stageID].animationClips[startIndex + delta] = clip;
Workspace.actorID = startIndex + delta;
Workspace.Instance.RecordEvent("Timeline move");
return true;
}
}
}