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

125 lines
4.2 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 KeyframeSlider : Slider
{
public AnimationTimeline timeline;
//public AnimationClip clip;
//public Keyframe keyframe;
public Transform ghostSliders;
public Slider ghostSliderPrefab;
public int maxGhosts = 4;
public int actorID;
public int keyframeID;
public void Initialize(AnimationTimeline timeline, int actorID, int keyframeID)
{
this.timeline = timeline;
//this.clip = clip;
//this.keyframe = keyframe;
this.actorID = actorID;
this.keyframeID = keyframeID;
PawnKeyframe keyframe = Workspace.Instance.GetPawnKeyframe(actorID, keyframeID);
Debug.Log(keyframe);
value = (float)keyframe.atTick / Workspace.Instance.GetCurrentStageLength();
OnValueChanged();
onValueChanged.AddListener(delegate (float value) { OnValueChanged(); });
}
public void OnValueChanged()
{
PawnKeyframe keyframe = Workspace.Instance.GetPawnKeyframe(actorID, keyframeID);
PawnAnimationClip clip = Workspace.Instance.GetPawnAnimationClip(actorID);
int stageLength = Workspace.Instance.GetCurrentStageLength();
int newTick = Mathf.RoundToInt(value * stageLength);
/*if (timeline.CanAddKeyFrameAtTick(newTick) == false)
{
int delta = keyframe.atTick > newTick ? 1 : -1;
while (timeline.CanAddKeyFrameAtTick(newTick) == false)
{
newTick += delta;
if (newTick == 1 || newTick == stageLength) { break; }
}
if (timeline.CanAddKeyFrameAtTick(newTick) == false)
{ value = (float)keyframe.atTick / stageLength; return; }
}*/
keyframe.atTick = newTick;
Debug.Log("Value changed: " + newTick);
//value = (float)keyframe.atTick / stageLength;
UpdateGhostFrames();
clip.BuildSimpleCurves();
}
// Ghost sliders are non-interactable slider handle
public void UpdateGhostFrames()
{
PawnKeyframe keyframe = Workspace.Instance.GetPawnKeyframe(actorID, keyframeID);
PawnAnimationClip clip = Workspace.Instance.GetPawnAnimationClip(actorID);
if (maxGhosts == 0)
{ return; }
int stageLength = Workspace.Instance.GetCurrentStageLength();
int nGhosts = GetGhostFramesRequired();
for (int i = 0; i < Mathf.Max(nGhosts, ghostSliders.childCount); i++)
{
if ((i - 1) * clip.duration + keyframe.atTick <= stageLength)
{
if (ghostSliders.childCount <= i)
{ Instantiate(ghostSliderPrefab, ghostSliders); }
GameObject ghostSliderObject = ghostSliders.GetChild(i).gameObject;
Debug.Log(ghostSliderObject);
ghostSliderObject.SetActive(true);
Slider ghostSlider = ghostSliderObject.GetComponent<Slider>();
Debug.Log(ghostSlider);
ghostSlider.value = (float)((i + 1) * clip.duration + keyframe.atTick) / stageLength;
float mult = 1f - Mathf.Pow((float)i / maxGhosts, 2);
ghostSlider.transform.FindDeepChild("Handle").GetComponent<Image>().color = new Color(0, 0.5f, 0.5f, 0.5f * mult);
}
if (i >= nGhosts)
{ transform.GetChild(i).gameObject.SetActive(false); }
}
}
public int GetGhostFramesRequired()
{
PawnAnimationClip clip = Workspace.Instance.GetPawnAnimationClip(actorID);
if (Workspace.animationDef.animationStages[Workspace.stageID].isLooping == false)
{ return 0; }
if (clip.duration <= 1)
{ return 0; }
return Math.Min(Mathf.CeilToInt((float)Workspace.Instance.GetCurrentStageLength() / clip.duration), maxGhosts);
}
}
}