2022-09-16 14:18:06 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using UnityEngine;
|
2022-09-16 22:50:15 +00:00
|
|
|
|
using UnityEngine.EventSystems;
|
2022-09-16 14:18:06 +00:00
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
|
|
namespace RimWorldAnimationStudio
|
|
|
|
|
{
|
2022-09-16 22:50:15 +00:00
|
|
|
|
public class KeyframeSlider : Slider, IPointerClickHandler, IBeginDragHandler, IEndDragHandler
|
2022-09-16 14:18:06 +00:00
|
|
|
|
{
|
|
|
|
|
public AnimationTimeline timeline;
|
|
|
|
|
public Transform ghostSliders;
|
|
|
|
|
public Slider ghostSliderPrefab;
|
2022-09-20 06:03:55 +00:00
|
|
|
|
public Image handleImage;
|
|
|
|
|
public GameObject soundIcon;
|
2022-09-16 14:18:06 +00:00
|
|
|
|
public int maxGhosts = 4;
|
|
|
|
|
|
|
|
|
|
public int actorID;
|
|
|
|
|
public int keyframeID;
|
|
|
|
|
|
2022-09-18 00:06:33 +00:00
|
|
|
|
private PawnAnimationClip clip;
|
|
|
|
|
private PawnKeyframe keyframe;
|
2022-10-14 05:52:07 +00:00
|
|
|
|
|
2022-10-02 05:42:07 +00:00
|
|
|
|
private float dragTimeStart = -1f;
|
2022-10-14 05:52:07 +00:00
|
|
|
|
private int dragTickStart = -1;
|
|
|
|
|
|
|
|
|
|
public KeyframeSlider linkedSlider;
|
|
|
|
|
public Keyframe pivotKeyframe;
|
|
|
|
|
public int linkedOffset;
|
2022-09-18 00:06:33 +00:00
|
|
|
|
|
2022-10-14 05:52:07 +00:00
|
|
|
|
public void Initialize(AnimationTimeline timeline, int actorID, int keyframeID)
|
2022-09-16 14:18:06 +00:00
|
|
|
|
{
|
|
|
|
|
this.timeline = timeline;
|
2022-09-18 00:06:33 +00:00
|
|
|
|
this.clip = Workspace.Instance.GetPawnAnimationClip(actorID);
|
|
|
|
|
this.keyframe = Workspace.Instance.GetPawnKeyframe(actorID, keyframeID);
|
2022-09-16 14:18:06 +00:00
|
|
|
|
|
|
|
|
|
this.actorID = actorID;
|
|
|
|
|
this.keyframeID = keyframeID;
|
|
|
|
|
|
|
|
|
|
PawnKeyframe keyframe = Workspace.Instance.GetPawnKeyframe(actorID, keyframeID);
|
2022-09-18 05:01:12 +00:00
|
|
|
|
maxValue = Workspace.StageWindowSize;
|
2022-09-18 00:06:33 +00:00
|
|
|
|
value = keyframe.atTick.Value;
|
|
|
|
|
|
2022-09-16 14:18:06 +00:00
|
|
|
|
OnValueChanged();
|
|
|
|
|
|
|
|
|
|
onValueChanged.AddListener(delegate (float value) { OnValueChanged(); });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnValueChanged()
|
|
|
|
|
{
|
2022-09-18 00:06:33 +00:00
|
|
|
|
keyframe.atTick = (int)value;
|
2022-09-16 14:18:06 +00:00
|
|
|
|
clip.BuildSimpleCurves();
|
2022-09-16 22:50:15 +00:00
|
|
|
|
|
2022-09-19 00:07:44 +00:00
|
|
|
|
timeline.InitiateUpdateOfGhostFrames();
|
2022-09-16 14:18:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ghost sliders are non-interactable slider handle
|
|
|
|
|
public void UpdateGhostFrames()
|
|
|
|
|
{
|
|
|
|
|
if (maxGhosts == 0)
|
|
|
|
|
{ return; }
|
|
|
|
|
|
2022-09-19 00:07:44 +00:00
|
|
|
|
int requiredGhosts = GetGhostFramesRequired();
|
|
|
|
|
int currentGhostCount = ghostSliders.childCount;
|
2022-09-16 14:18:06 +00:00
|
|
|
|
|
2022-09-19 00:07:44 +00:00
|
|
|
|
for (int i = 0; i < Mathf.Max(requiredGhosts, currentGhostCount); i++)
|
2022-09-16 14:18:06 +00:00
|
|
|
|
{
|
2022-09-19 00:07:44 +00:00
|
|
|
|
int targetTick = (int)(i * clip.duration + keyframe.atTick);
|
2022-09-16 14:18:06 +00:00
|
|
|
|
|
2022-09-19 00:07:44 +00:00
|
|
|
|
if (ghostSliders.childCount <= i)
|
|
|
|
|
{ Instantiate(ghostSliderPrefab, ghostSliders); }
|
2022-09-16 14:18:06 +00:00
|
|
|
|
|
2022-09-19 00:07:44 +00:00
|
|
|
|
GameObject ghostSliderObject = ghostSliders.GetChild(i).gameObject;
|
|
|
|
|
ghostSliderObject.SetActive(i < requiredGhosts);
|
2022-09-16 14:18:06 +00:00
|
|
|
|
|
2022-09-19 00:07:44 +00:00
|
|
|
|
Slider ghostSlider = ghostSliderObject.GetComponent<Slider>();
|
|
|
|
|
ghostSlider.maxValue = Workspace.StageWindowSize;
|
|
|
|
|
ghostSlider.value = targetTick;
|
|
|
|
|
|
|
|
|
|
if (targetTick > ghostSlider.maxValue)
|
|
|
|
|
{ ghostSlider.gameObject.SetActive(false); }
|
2022-09-16 14:18:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int GetGhostFramesRequired()
|
|
|
|
|
{
|
|
|
|
|
if (Workspace.animationDef.animationStages[Workspace.stageID].isLooping == false)
|
|
|
|
|
{ return 0; }
|
|
|
|
|
|
|
|
|
|
if (clip.duration <= 1)
|
|
|
|
|
{ return 0; }
|
|
|
|
|
|
2022-09-18 05:01:12 +00:00
|
|
|
|
return Math.Min(Mathf.CeilToInt((float)Workspace.StageWindowSize / clip.duration), maxGhosts);
|
2022-09-16 22:50:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnPointerClick(PointerEventData eventData)
|
|
|
|
|
{
|
2022-09-18 06:52:54 +00:00
|
|
|
|
Workspace.actorID = actorID;
|
2022-10-02 22:39:03 +00:00
|
|
|
|
|
|
|
|
|
if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.LeftCommand))
|
|
|
|
|
{ Workspace.keyframeID.Add(keyframeID); }
|
|
|
|
|
|
2022-10-14 05:52:07 +00:00
|
|
|
|
else if (Workspace.keyframeID.NullOrEmpty() || Workspace.keyframeID.Contains(keyframeID) == false)
|
2022-10-02 22:39:03 +00:00
|
|
|
|
{ Workspace.keyframeID = new List<int> { keyframeID }; }
|
2022-09-18 06:52:54 +00:00
|
|
|
|
|
|
|
|
|
if (eventData.clickCount >= 2)
|
2022-09-19 00:07:44 +00:00
|
|
|
|
{ AnimationController.Instance.stageTick = keyframe.atTick.Value; }
|
2022-09-18 00:06:33 +00:00
|
|
|
|
}
|
2022-09-16 22:50:15 +00:00
|
|
|
|
|
2022-09-18 00:06:33 +00:00
|
|
|
|
public void OnBeginDrag(PointerEventData eventData)
|
|
|
|
|
{
|
2022-09-18 06:52:54 +00:00
|
|
|
|
Workspace.actorID = actorID;
|
2022-10-14 05:52:07 +00:00
|
|
|
|
dragTimeStart = Time.unscaledTime;
|
|
|
|
|
dragTickStart = keyframe.atTick.Value;
|
|
|
|
|
|
|
|
|
|
if (Workspace.keyframeID.NullOrEmpty() || Workspace.keyframeID.Contains(keyframeID) == false)
|
|
|
|
|
{ Workspace.keyframeID = new List<int> { keyframeID }; }
|
2022-10-02 22:39:03 +00:00
|
|
|
|
|
2022-10-14 05:52:07 +00:00
|
|
|
|
List<PawnKeyframe> selectedKeyframes = Workspace.Instance.GetPawnKeyframes(Workspace.keyframeID).Except(new List<PawnKeyframe>() { keyframe })?.ToList();
|
2022-10-02 05:42:07 +00:00
|
|
|
|
|
2022-10-14 05:52:07 +00:00
|
|
|
|
// 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();
|
|
|
|
|
}
|
2022-09-16 22:50:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-18 00:06:33 +00:00
|
|
|
|
public override void OnDrag(PointerEventData eventData)
|
2022-09-16 22:50:15 +00:00
|
|
|
|
{
|
2022-10-14 05:52:07 +00:00
|
|
|
|
Workspace.actorID = actorID;
|
2022-09-26 05:50:26 +00:00
|
|
|
|
|
2022-10-14 05:52:07 +00:00
|
|
|
|
// The first keyframe can't be moved
|
|
|
|
|
if (keyframe.atTick == Constants.minTick)
|
|
|
|
|
{ value = Constants.minTick; return; }
|
|
|
|
|
|
|
|
|
|
// Sticky drag
|
2022-10-02 05:42:07 +00:00
|
|
|
|
if (Time.unscaledTime - dragTimeStart < 0.05f) return;
|
|
|
|
|
|
|
|
|
|
interactable = true;
|
2022-09-18 00:06:33 +00:00
|
|
|
|
base.OnDrag(eventData);
|
|
|
|
|
|
2022-10-14 05:52:07 +00:00
|
|
|
|
// Snap to nearest keyframe (on another timeline)
|
2022-10-04 02:45:52 +00:00
|
|
|
|
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; }
|
|
|
|
|
|
2022-10-14 05:52:07 +00:00
|
|
|
|
// Prevent other frames from being moved to the first keyframe
|
|
|
|
|
if (value == Constants.minTick)
|
|
|
|
|
{ value = Constants.minTick + 1; }
|
2022-09-16 22:50:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnEndDrag(PointerEventData eventData)
|
|
|
|
|
{
|
2022-10-14 05:52:07 +00:00
|
|
|
|
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; }
|
|
|
|
|
}
|
2022-09-19 00:07:44 +00:00
|
|
|
|
|
2022-10-02 05:42:07 +00:00
|
|
|
|
interactable = false;
|
2022-10-02 22:39:03 +00:00
|
|
|
|
Workspace.Instance.RecordEvent("Keyframe move");
|
2022-09-16 14:18:06 +00:00
|
|
|
|
}
|
2022-09-18 00:06:33 +00:00
|
|
|
|
|
2022-09-18 06:52:54 +00:00
|
|
|
|
protected override void Update()
|
2022-09-18 00:06:33 +00:00
|
|
|
|
{
|
2022-09-18 06:52:54 +00:00
|
|
|
|
base.Update();
|
2022-09-18 00:06:33 +00:00
|
|
|
|
|
2022-10-14 05:52:07 +00:00
|
|
|
|
// 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
|
2022-10-02 22:39:03 +00:00
|
|
|
|
if (keyframe.atTick.HasValue && Workspace.keyframeID.Contains(keyframeID) && AnimationController.Instance.stageTick == keyframe.atTick.Value)
|
2022-09-20 06:03:55 +00:00
|
|
|
|
{ handleImage.color = Constants.ColorPurple; }
|
2022-09-18 00:06:33 +00:00
|
|
|
|
|
2022-10-02 22:39:03 +00:00
|
|
|
|
else if (Workspace.keyframeID.Contains(keyframeID))
|
2022-09-20 06:03:55 +00:00
|
|
|
|
{ handleImage.color = Constants.ColorCyan; }
|
2022-09-18 00:06:33 +00:00
|
|
|
|
|
2022-10-14 05:52:07 +00:00
|
|
|
|
else if (AnimationController.Instance.stageTick == keyframe.atTick.Value)
|
2022-09-20 06:03:55 +00:00
|
|
|
|
{ handleImage.color = Constants.ColorPink; }
|
2022-09-18 06:52:54 +00:00
|
|
|
|
|
|
|
|
|
else
|
2022-10-12 05:22:29 +00:00
|
|
|
|
{ handleImage.color = Constants.ColorGrey; }
|
2022-09-20 06:03:55 +00:00
|
|
|
|
|
2022-10-14 05:52:07 +00:00
|
|
|
|
// Show sound symbol
|
2022-09-21 05:40:58 +00:00
|
|
|
|
string soundDef = Workspace.Instance.GetPawnKeyframe(actorID, keyframeID)?.soundEffect;
|
2022-10-14 05:52:07 +00:00
|
|
|
|
soundIcon.SetActive(soundDef != null && soundDef != "" && soundDef != "None");
|
|
|
|
|
}
|
2022-09-20 06:03:55 +00:00
|
|
|
|
|
2022-10-14 05:52:07 +00:00
|
|
|
|
public float ScaledOffsetFromPivot()
|
|
|
|
|
{
|
|
|
|
|
//if (IsPivotKeyframe(keyframe)) return 1f;
|
|
|
|
|
if (dragTickStart == pivotKeyframe.atTick.Value) return 0f;
|
2022-09-20 06:03:55 +00:00
|
|
|
|
|
2022-10-14 05:52:07 +00:00
|
|
|
|
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);
|
2022-09-18 00:06:33 +00:00
|
|
|
|
}
|
2022-09-16 14:18:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|