rimworld-animation-studio/Assets/Scripts/GUI/AnimationTimeline.cs

119 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
namespace RimWorldAnimationStudio
{
public class AnimationTimeline : MonoBehaviour, IPointerClickHandler
{
public int actorID;
public KeyframeSlider keyframeSliderPrefab;
private Transform anchorTransform;
public void Initialize(int actorID)
{
anchorTransform = transform.parent;
this.actorID = actorID;
PawnAnimationClip clip = Workspace.GetPawnAnimationClip(actorID);
clip.BuildSimpleCurves();
foreach (KeyframeSlider slider in GetComponentsInChildren<KeyframeSlider>())
{ RemovePawnKeyFrame(slider.keyframeID);}
foreach (PawnKeyframe keyframe in clip.Keyframes)
{ AddPawnKeyFrame(keyframe.keyframeID); }
/*int keyframeCount = clip.keyframes.Count;
int childCount = GetComponentsInChildren<KeyframeSlider>().Count();
for (int i = 0; i < Mathf.Max(keyframeCount, childCount); i++)
{
// Add new keyframe sliders as required
if (i >= childCount)
{ AddPawnKeyFrame(clip.keyframes[i].keyframeID); }
// Get objects to update
KeyframeSlider keyframeSlider = GetComponentsInChildren<KeyframeSlider>()[i];
// Update values
if (i < keyframeCount)
{ keyframeSlider.Initialize(this, actorID, clip.keyframes[i].keyframeID); }
// Remove excess objects as required
else
{ RemovePawnKeyFrame(GetComponentsInChildren<KeyframeSlider>()[i].keyframeID); }
}*/
}
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.ColorMidGrey; }
}
public void InitiateUpdateOfGhostFrames()
{
if (AnimationController.Instance.IsTimelineDirty()) return;
BroadcastMessage("UpdateGhostFrames");
}
public void OnMoveTimeline(int delta)
{
int? siblingIndex = anchorTransform.parent.GetComponentsInChildren<AnimationTimeline>()?.ToList()?.IndexOf(this);
int? siblingCount = anchorTransform.parent.GetComponentsInChildren<AnimationTimeline>()?.ToList()?.Count();
if (siblingIndex != null && siblingCount != null && MoveAnimationTimeline(siblingIndex.Value, delta))
{ AnimationController.Instance.InitializeAnimationTimeline(); }
}
public bool MoveAnimationTimeline(int startIndex, int delta)
{
if (startIndex + delta < 0 || startIndex + delta >= Workspace.GetCurrentAnimationStage().AnimationClips.Count)
{ Debug.Log("Cannot move animation timeline - movement would exceed bounds"); 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.GetPawnAnimationClip(startIndex);
Workspace.GetCurrentAnimationStage().AnimationClips[startIndex] = Workspace.GetCurrentAnimationStage().AnimationClips[startIndex + delta];
Workspace.GetCurrentAnimationStage().AnimationClips[startIndex + delta] = clip;
Workspace.ActorID = startIndex + delta;
Workspace.RecordEvent("Timeline move");
return true;
}
public void OnPointerClick(PointerEventData eventData)
{
Workspace.ActorID = actorID;
Workspace.keyframeID.Clear();
}
}
}