Multi-drag and stretching for keyframes

This commit is contained in:
AbstractConcept 2022-10-14 00:52:07 -05:00
parent f449a9b4e2
commit ab5a2a4c02
151 changed files with 195 additions and 85 deletions

View file

@ -23,9 +23,15 @@ namespace RimWorldAnimationStudio
private PawnAnimationClip clip;
private PawnKeyframe keyframe;
private float dragTimeStart = -1f;
public void Initialize(AnimationTimeline timeline, int actorID, int keyframeID)
private float dragTimeStart = -1f;
private int dragTickStart = -1;
public KeyframeSlider linkedSlider;
public Keyframe pivotKeyframe;
public int linkedOffset;
public void Initialize(AnimationTimeline timeline, int actorID, int keyframeID)
{
this.timeline = timeline;
this.clip = Workspace.Instance.GetPawnAnimationClip(actorID);
@ -48,8 +54,6 @@ namespace RimWorldAnimationStudio
keyframe.atTick = (int)value;
clip.BuildSimpleCurves();
//AnimationController.Instance.stageTick = keyframe.atTick.Value;
timeline.InitiateUpdateOfGhostFrames();
}
@ -99,52 +103,76 @@ namespace RimWorldAnimationStudio
if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.LeftCommand))
{ Workspace.keyframeID.Add(keyframeID); }
else
else if (Workspace.keyframeID.NullOrEmpty() || Workspace.keyframeID.Contains(keyframeID) == false)
{ Workspace.keyframeID = new List<int> { keyframeID }; }
if (eventData.clickCount >= 2)
{ AnimationController.Instance.stageTick = keyframe.atTick.Value; }
//Workspace.Instance.RecordEvent("Keyframe selected");
}
public void OnBeginDrag(PointerEventData eventData)
{
//AnimationController.Instance.stageTick = keyframe.atTick.Value;
Workspace.actorID = actorID;
Workspace.keyframeID = new List<int> { keyframeID };
dragTimeStart = Time.unscaledTime;
dragTickStart = keyframe.atTick.Value;
if (Workspace.keyframeID.NullOrEmpty() || Workspace.keyframeID.Contains(keyframeID) == false)
{ Workspace.keyframeID = new List<int> { keyframeID }; }
List<PawnKeyframe> selectedKeyframes = Workspace.Instance.GetPawnKeyframes(Workspace.keyframeID).Except(new List<PawnKeyframe>() { keyframe })?.ToList();
// Link other slected keyframes to the movement of this one
if (selectedKeyframes.NotNullOrEmpty())
{
foreach (PawnKeyframe selectedKeyframe in selectedKeyframes)
{
KeyframeSlider unlinkedSlider = selectedKeyframe.GetKeyframeSlider();
if (unlinkedSlider != null)
{
unlinkedSlider.linkedSlider = this;
unlinkedSlider.linkedOffset = unlinkedSlider.keyframe.atTick.Value - keyframe.atTick.Value;
}
}
pivotKeyframe = keyframe.atTick < selectedKeyframes[0].atTick ? selectedKeyframes.Last() : selectedKeyframes.First();
}
}
public override void OnDrag(PointerEventData eventData)
{
if (keyframe.atTick == 1)
{ value = 1; return; }
Workspace.actorID = actorID;
// The first keyframe can't be moved
if (keyframe.atTick == Constants.minTick)
{ value = Constants.minTick; return; }
// Sticky drag
if (Time.unscaledTime - dragTimeStart < 0.05f) return;
interactable = true;
base.OnDrag(eventData);
// Snap to nearest keyframe (on another timeline)
int targetTick = Workspace.FindClosestKeyFrameAtTick(keyframe.atTick.Value, Mathf.CeilToInt(Workspace.StageWindowSize * 0.01f), actorID);
if (Input.GetKey(KeyCode.LeftShift) && Workspace.Instance.DoesPawnKeyframeExistAtTick(Workspace.stageID, actorID, targetTick) == false)
{ value = (float)targetTick; }
// Prevent frames from being moved to tick 1
if (value == 1)
{ value = 2; }
//AnimationController.Instance.stageTick = keyframe.atTick.Value;
Workspace.actorID = actorID;
// Prevent other frames from being moved to the first keyframe
if (value == Constants.minTick)
{ value = Constants.minTick + 1; }
}
public void OnEndDrag(PointerEventData eventData)
{
if (keyframe.atTick == 1)
{ value = 1; return; }
if (keyframe.atTick == Constants.minTick)
{ value = Constants.minTick; return; }
foreach (Selectable otherSlider in Selectable.allSelectablesArray)
{
if (otherSlider is KeyframeSlider)
{ Debug.Log("unlinked keyframes"); (otherSlider as KeyframeSlider).linkedSlider = null; }
}
interactable = false;
Workspace.Instance.RecordEvent("Keyframe move");
@ -154,25 +182,60 @@ namespace RimWorldAnimationStudio
{
base.Update();
// Update outdated values
if (Workspace.keyframeID.NullOrEmpty() || Workspace.keyframeID.Contains(keyframeID) == false)
{ linkedSlider = null; }
else if (AnimationController.Instance.stretchKeyframesToggle.isOn && linkedSlider != null && linkedSlider.IsPivotKeyframe(keyframe) == false)
{
//int minTick = linkedSlider.pivotKeyframe.atTick.Value + GetIndexAmongstSelectedKeyframes();
//value = Mathf.Clamp(Mathf.CeilToInt(linkedSlider.keyframe.atTick.Value + linkedOffset * linkedSlider.ScaledOffsetFromPivot()), minTick, Workspace.StageWindowSize);
value = Mathf.CeilToInt(linkedSlider.keyframe.atTick.Value + linkedOffset * linkedSlider.ScaledOffsetFromPivot());
}
else if (AnimationController.Instance.stretchKeyframesToggle.isOn == false && linkedSlider != null)
{ value = Mathf.Clamp(linkedSlider.keyframe.atTick.Value + linkedOffset, Constants.minTick + 1, Workspace.StageWindowSize); }
else if (keyframe.atTick.Value != value)
{ value = keyframe.atTick.Value; }
// Update key color
if (keyframe.atTick.HasValue && Workspace.keyframeID.Contains(keyframeID) && AnimationController.Instance.stageTick == keyframe.atTick.Value)
{ handleImage.color = Constants.ColorPurple; }
else if (Workspace.keyframeID.Contains(keyframeID))
{ handleImage.color = Constants.ColorCyan; }
else if (keyframe.atTick.HasValue && AnimationController.Instance.stageTick == keyframe.atTick.Value)
else if (AnimationController.Instance.stageTick == keyframe.atTick.Value)
{ handleImage.color = Constants.ColorPink; }
else
{ handleImage.color = Constants.ColorGrey; }
// Show sound symbol
string soundDef = Workspace.Instance.GetPawnKeyframe(actorID, keyframeID)?.soundEffect;
soundIcon.SetActive(soundDef != null && soundDef != "" && soundDef != "None");
}
if (soundDef != null && soundDef != "" && soundDef != "None")
{ soundIcon.SetActive(true); }
public float ScaledOffsetFromPivot()
{
//if (IsPivotKeyframe(keyframe)) return 1f;
if (dragTickStart == pivotKeyframe.atTick.Value) return 0f;
else
{ soundIcon.SetActive(false); }
return (float)(keyframe.atTick.Value - pivotKeyframe.atTick.Value) / (dragTickStart - pivotKeyframe.atTick.Value);
}
public bool IsPivotKeyframe(Keyframe otherKeyframe)
{
return pivotKeyframe == otherKeyframe;
}
public int GetIndexAmongstSelectedKeyframes()
{
List<PawnKeyframe> selectedKeyframes = Workspace.Instance.GetPawnKeyframes(Workspace.keyframeID).OrderBy(x => x.atTick)?.ToList();
if (selectedKeyframes.NullOrEmpty() || selectedKeyframes.Contains(keyframe) == false) return -1;
return selectedKeyframes.IndexOf(keyframe);
}
}
}

View file

@ -64,7 +64,7 @@ namespace RimWorldAnimationStudio
if (Workspace.stageID != transform.GetSiblingIndex())
{
AnimationController.Instance.stageTick = 1;
AnimationController.Instance.stageTick = Constants.minTick;
Workspace.Instance.RecordEvent("Stage selected");
}