Key snapping

This commit is contained in:
AbstractConcept 2022-10-03 21:45:52 -05:00
parent 5436410162
commit f275ed90ae
101 changed files with 253 additions and 117 deletions

View file

@ -103,7 +103,77 @@ namespace RimWorldAnimationStudio
return null;
}
public bool DoesPawnKeyframeExistAtTick(int stageID, int actorID, int atTick)
{
return animationDef.animationStages[stageID].animationClips[actorID].keyframes.Any(x => x.atTick == atTick);
}
public static void FindAdjacentKeyframes(int atTick, out PawnKeyframe prevKeyframe, out PawnKeyframe nextKeyframe, int actorID = -1, int excludedActorID = -1, bool ignoreSelf = true, int searchDistance = int.MaxValue)
{
prevKeyframe = null;
nextKeyframe = null;
List<PawnKeyframe> keyframesToCheck;
if (actorID >= 0)
{ keyframesToCheck = animationDef.animationStages[stageID].animationClips[actorID].keyframes; }
else
{
keyframesToCheck = animationDef.animationStages[stageID].animationClips.Where(x =>
animationDef.animationStages[stageID].animationClips.IndexOf(x) != excludedActorID).SelectMany(x => x.keyframes)?.ToList();
}
foreach (PawnKeyframe keyframe in keyframesToCheck)
{
if (keyframe.atTick <= atTick && atTick - keyframe.atTick <= searchDistance)
{
if (keyframe.atTick != atTick || ignoreSelf)
{ prevKeyframe = keyframe; }
else
{ prevKeyframe = null; }
}
if (nextKeyframe == null && keyframe.atTick > atTick && keyframe.atTick - atTick <= searchDistance)
{ nextKeyframe = keyframe; }
}
}
public static int FindClosestKeyFrameAtTick(int atTick, int searchDistance = int.MaxValue, int excludedActorID = -1)
{
List<PawnKeyframe> keyframesToCheck;
if (excludedActorID >= 0)
{
keyframesToCheck = animationDef.animationStages[stageID].animationClips.Where(x =>
animationDef.animationStages[stageID].animationClips.IndexOf(x) != excludedActorID).SelectMany(x => x.keyframes)?.ToList();
}
else
{ keyframesToCheck = animationDef.animationStages[stageID].animationClips.SelectMany(x => x.keyframes)?.ToList(); }
keyframesToCheck = keyframesToCheck.Where(x => Mathf.Abs(atTick - x.atTick.Value) <= searchDistance)?.ToList();
if (keyframesToCheck.NullOrEmpty())
{ return atTick; }
int minDist = int.MaxValue;
int bestAtTick = -1;
foreach (PawnKeyframe keyframe_ in keyframesToCheck)
{
if (Mathf.Abs(keyframe_.atTick.Value - atTick) < minDist)
{
minDist = Mathf.Abs(keyframe_.atTick.Value - atTick);
bestAtTick = keyframe_.atTick.Value;
}
}
return bestAtTick;
}
[SerializeField]
public LinkedList<HistoricRecord> pastSnapshots = new LinkedList<HistoricRecord>();
public LinkedList<HistoricRecord> futureSnapshots = new LinkedList<HistoricRecord>();