Basic actor timelines and keyframe tweaking

This commit is contained in:
AbstractConcept 2022-09-16 09:18:06 -05:00
parent 0364322d46
commit dfa564759b
246 changed files with 1918 additions and 150 deletions

View file

@ -0,0 +1,56 @@
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;
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 AddKeyFrame(int atTick)
{
}
public bool CanAddKeyFrameAtTick(int atTick)
{
foreach (Transform child in transform)
{
KeyframeSlider keyframeSlider = child.GetComponent<KeyframeSlider>();
if (keyframeSlider != null && Workspace.Instance.GetPawnKeyframe(keyframeSlider.actorID, keyframeSlider.keyframeID).atTick == atTick)
{ return false; }
}
return true;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b3dd6b099b5c67744b84a5ec7283277b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,124 @@
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);
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9c733f5218a2b4a449e3115b2bef26f2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: