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;
|
2022-10-27 05:56:04 +00:00
|
|
|
|
public PawnKeyframe pivotKeyframe;
|
2022-10-14 05:52:07 +00:00
|
|
|
|
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-10-27 05:56:04 +00:00
|
|
|
|
this.clip = Workspace.GetPawnAnimationClip(actorID);
|
|
|
|
|
this.keyframe = Workspace.GetPawnKeyframe(actorID, keyframeID);
|
2022-09-16 14:18:06 +00:00
|
|
|
|
|
|
|
|
|
this.actorID = actorID;
|
|
|
|
|
this.keyframeID = keyframeID;
|
|
|
|
|
|
2022-10-27 05:56:04 +00:00
|
|
|
|
PawnKeyframe keyframe = Workspace.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.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()
|
|
|
|
|
{
|
2022-10-27 05:56:04 +00:00
|
|
|
|
if (Workspace.animationDef.AnimationStages[Workspace.StageID].IsLooping == false)
|
2022-09-16 14:18:06 +00:00
|
|
|
|
{ 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-10-27 05:56:04 +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-10-27 05:56:04 +00:00
|
|
|
|
{ Workspace.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-10-27 05:56:04 +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-27 05:56:04 +00:00
|
|
|
|
List<PawnKeyframe> selectedKeyframes = Workspace.GetPawnKeyframesByID(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())
|
|
|
|
|
{
|
2022-10-15 06:00:47 +00:00
|
|
|
|
pivotKeyframe = keyframe.atTick <= selectedKeyframes.Min(x => x.atTick) ?
|
|
|
|
|
selectedKeyframes.FirstOrDefault(x => x.atTick >= selectedKeyframes.Max(y => y.atTick)) :
|
|
|
|
|
selectedKeyframes.FirstOrDefault(x => x.atTick <= selectedKeyframes.Min(y => y.atTick));
|
|
|
|
|
|
2022-10-14 05:52:07 +00:00
|
|
|
|
foreach (PawnKeyframe selectedKeyframe in selectedKeyframes)
|
|
|
|
|
{
|
|
|
|
|
KeyframeSlider unlinkedSlider = selectedKeyframe.GetKeyframeSlider();
|
|
|
|
|
|
|
|
|
|
if (unlinkedSlider != null)
|
|
|
|
|
{
|
2022-10-15 06:00:47 +00:00
|
|
|
|
if (AnimationController.Instance.stretchKeyframesToggle.isOn && unlinkedSlider.keyframe.atTick == pivotKeyframe.atTick) continue;
|
|
|
|
|
|
2022-10-14 05:52:07 +00:00
|
|
|
|
unlinkedSlider.linkedSlider = this;
|
|
|
|
|
unlinkedSlider.linkedOffset = unlinkedSlider.keyframe.atTick.Value - keyframe.atTick.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
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-27 05:56:04 +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);
|
2022-10-27 05:56:04 +00:00
|
|
|
|
if (Input.GetKey(KeyCode.LeftShift) && Workspace.DoesPawnKeyframeExistAtTick(Workspace.StageID, actorID, targetTick) == false)
|
2022-10-04 02:45:52 +00:00
|
|
|
|
{ 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; }
|
|
|
|
|
|
2022-10-27 05:56:04 +00:00
|
|
|
|
List<PawnKeyframe> keyframesToCheck = Workspace.GetAllPawnKeyframesAtTick(actorID, keyframe.atTick.Value);
|
2022-10-19 02:57:43 +00:00
|
|
|
|
if (keyframesToCheck.NotNullOrEmpty())
|
2022-10-14 05:52:07 +00:00
|
|
|
|
{
|
2022-10-19 02:57:43 +00:00
|
|
|
|
foreach (PawnKeyframe _keyframe in keyframesToCheck)
|
|
|
|
|
{
|
|
|
|
|
if (_keyframe != keyframe)
|
2022-10-27 05:56:04 +00:00
|
|
|
|
{ Workspace.GetCurrentPawnAnimationClip().RemovePawnKeyframe(actorID, _keyframe.keyframeID); }
|
2022-10-19 02:57:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (Selectable selectable in Selectable.allSelectablesArray)
|
|
|
|
|
{
|
|
|
|
|
if (selectable is KeyframeSlider)
|
|
|
|
|
{
|
|
|
|
|
KeyframeSlider linkedSlider = selectable.GetComponent<KeyframeSlider>();
|
|
|
|
|
PawnKeyframe linkedKeyframe = linkedSlider.keyframe;
|
|
|
|
|
|
|
|
|
|
if (linkedSlider.linkedSlider != null)
|
|
|
|
|
{
|
2022-10-27 05:56:04 +00:00
|
|
|
|
keyframesToCheck = Workspace.GetAllPawnKeyframesAtTick(actorID, linkedKeyframe.atTick.Value);
|
2022-10-19 02:57:43 +00:00
|
|
|
|
|
|
|
|
|
if (keyframesToCheck.NotNullOrEmpty() && keyframesToCheck.Count > 1)
|
|
|
|
|
{
|
|
|
|
|
foreach (PawnKeyframe _keyframe in keyframesToCheck)
|
|
|
|
|
{
|
|
|
|
|
if (_keyframe.keyframeID != linkedKeyframe.keyframeID)
|
2022-10-27 05:56:04 +00:00
|
|
|
|
{ Workspace.GetCurrentPawnAnimationClip().RemovePawnKeyframe(actorID, _keyframe.keyframeID); Debug.Log("delete"); }
|
2022-10-19 02:57:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
linkedSlider.linkedSlider = null;
|
|
|
|
|
linkedSlider.pivotKeyframe = null;
|
2022-10-15 06:00:47 +00:00
|
|
|
|
}
|
2022-10-14 05:52:07 +00:00
|
|
|
|
}
|
2022-09-19 00:07:44 +00:00
|
|
|
|
|
2022-10-02 05:42:07 +00:00
|
|
|
|
interactable = false;
|
2022-10-15 06:00:47 +00:00
|
|
|
|
|
2022-10-27 05:56:04 +00:00
|
|
|
|
Workspace.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; }
|
|
|
|
|
|
2022-10-15 06:00:47 +00:00
|
|
|
|
else if (AnimationController.Instance.stretchKeyframesToggle.isOn && linkedSlider != null)
|
2022-10-14 19:00:57 +00:00
|
|
|
|
{ value = Mathf.CeilToInt(linkedSlider.keyframe.atTick.Value + linkedOffset * linkedSlider.ScaledOffsetFromPivot()); }
|
2022-10-14 05:52:07 +00:00
|
|
|
|
|
|
|
|
|
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-27 05:56:04 +00:00
|
|
|
|
if (keyframe.atTick.HasValue && Workspace.keyframeID.Contains(keyframeID) && Workspace.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-27 05:56:04 +00:00
|
|
|
|
else if (Workspace.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-10-27 05:56:04 +00:00
|
|
|
|
string soundDef = Workspace.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 (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);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-27 05:56:04 +00:00
|
|
|
|
public bool IsPivotKeyframe(PawnKeyframe otherKeyframe)
|
2022-10-14 05:52:07 +00:00
|
|
|
|
{
|
|
|
|
|
return pivotKeyframe == otherKeyframe;
|
|
|
|
|
}
|
2022-09-16 14:18:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|