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

119 lines
4.4 KiB
C#
Raw Normal View History

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;
2022-10-27 05:56:04 +00:00
PawnAnimationClip clip = Workspace.GetPawnAnimationClip(actorID);
2022-10-15 06:00:47 +00:00
clip.BuildSimpleCurves();
2022-10-15 06:00:47 +00:00
foreach (KeyframeSlider slider in GetComponentsInChildren<KeyframeSlider>())
{ RemovePawnKeyFrame(slider.keyframeID);}
2022-10-27 05:56:04 +00:00
foreach (PawnKeyframe keyframe in clip.Keyframes)
2022-10-15 06:00:47 +00:00
{ AddPawnKeyFrame(keyframe.keyframeID); }
2022-10-15 06:00:47 +00:00
/*int keyframeCount = clip.keyframes.Count;
2022-10-14 19:00:57 +00:00
int childCount = GetComponentsInChildren<KeyframeSlider>().Count();
for (int i = 0; i < Mathf.Max(keyframeCount, childCount); i++)
{
// Add new keyframe sliders as required
if (i >= childCount)
2022-10-15 06:00:47 +00:00
{ AddPawnKeyFrame(clip.keyframes[i].keyframeID); }
2022-10-14 19:00:57 +00:00
// 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
2022-10-15 06:00:47 +00:00
{ 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);
}
2022-09-18 05:01:12 +00:00
public void Update()
{
2022-10-27 05:56:04 +00:00
if (Workspace.ActorID == actorID)
2022-09-18 05:01:12 +00:00
{ GetComponent<Image>().color = Constants.ColorGoldYellow; }
else
2022-10-12 05:22:29 +00:00
{ GetComponent<Image>().color = Constants.ColorMidGrey; }
2022-09-18 05:01:12 +00:00
}
2022-09-19 00:07:44 +00:00
public void InitiateUpdateOfGhostFrames()
{
2022-10-14 19:00:57 +00:00
if (AnimationController.Instance.IsTimelineDirty()) return;
2022-09-19 00:07:44 +00:00
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))
2022-10-14 19:00:57 +00:00
{ AnimationController.Instance.InitializeAnimationTimeline(); }
}
public bool MoveAnimationTimeline(int startIndex, int delta)
{
2022-10-27 05:56:04 +00:00
if (startIndex + delta < 0 || startIndex + delta >= Workspace.GetCurrentAnimationStage().AnimationClips.Count)
2022-10-02 05:42:07 +00:00
{ Debug.Log("Cannot move animation timeline - movement would exceed bounds"); return false; }
2022-10-27 05:56:04 +00:00
Actor actor = Workspace.animationDef.Actors[startIndex];
Workspace.animationDef.Actors[startIndex] = Workspace.animationDef.Actors[startIndex + delta];
Workspace.animationDef.Actors[startIndex + delta] = actor;
2022-10-27 05:56:04 +00:00
PawnAnimationClip clip = Workspace.GetPawnAnimationClip(startIndex);
Workspace.GetCurrentAnimationStage().AnimationClips[startIndex] = Workspace.GetCurrentAnimationStage().AnimationClips[startIndex + delta];
Workspace.GetCurrentAnimationStage().AnimationClips[startIndex + delta] = clip;
2022-10-27 05:56:04 +00:00
Workspace.ActorID = startIndex + delta;
2022-10-27 05:56:04 +00:00
Workspace.RecordEvent("Timeline move");
return true;
}
public void OnPointerClick(PointerEventData eventData)
{
2022-10-27 05:56:04 +00:00
Workspace.ActorID = actorID;
Workspace.keyframeID.Clear();
}
}
}