Skip to content

Commit

Permalink
Backward compatibility fix; setting Animation while stop and then pla…
Browse files Browse the repository at this point in the history
…ying will not use the set animation
  • Loading branch information
acidbubbles committed Oct 16, 2021
1 parent 93849d0 commit 33304c5
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions src/AtomPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class AtomPlugin : MVRScript, IAtomPlugin
private bool _scrubbing;

private bool _restoring;
private string _legacyAnimationNext;
private FreeControllerV3Hook _freeControllerHook;
public Logger logger { get; private set; }
public OperationsFactory operations => new OperationsFactory(containingAtom, animation, animationEditContext.current, peers);
Expand Down Expand Up @@ -164,6 +165,7 @@ public void Update()

private const float _physicsResetTimeoutSeconds = 0.5f;
private float _physicsResetTimeout;

private bool IsPhysicsReset()
{
if (containingAtom.physicsSimulators.Length == 0) return false;
Expand Down Expand Up @@ -260,6 +262,7 @@ public void InitStorables()
{
if (string.IsNullOrEmpty(val)) return;
if (logger.triggers) logger.Log(logger.triggersCategory, $"Triggered '{StorableNames.Animation}' = '{val}'");
_legacyAnimationNext = val;
var clip = animation.clips.FirstOrDefault(c => c.animationName == val);
if (clip == null) return;
if (animationEditContext.current != clip)
Expand Down Expand Up @@ -334,25 +337,13 @@ public void InitStorables()
};
RegisterFloat(_timeJSON);

_playJSON = new JSONStorableAction(StorableNames.Play, () =>
{
if (logger.triggers) logger.Log(logger.triggersCategory, $"Triggered '{StorableNames.Play}'");
if (animation.paused) { animation.paused = false; return; }
var selected = string.IsNullOrEmpty(_animationLegacyJSON.val) ? animation.GetDefaultClip() : animation.GetClips(_animationLegacyJSON.val).FirstOrDefault();
animation.PlayOneAndOtherMainsInLayers(selected);
});
_playJSON = new JSONStorableAction(StorableNames.Play, () => StorablePlay(StorableNames.Play));
RegisterAction(_playJSON);

_playIfNotPlayingJSON = new JSONStorableAction(StorableNames.PlayIfNotPlaying, () =>
{
if (logger.triggers) logger.Log(logger.triggersCategory, $"Triggered '{StorableNames.PlayIfNotPlaying}'");
if (animation.paused) { animation.paused = false; return; }
var selected = string.IsNullOrEmpty(_animationLegacyJSON.val) ? animation.GetDefaultClip() : animation.GetClips(_animationLegacyJSON.val).FirstOrDefault();
if (selected == null) return;
if (!animation.isPlaying)
animation.PlayOneAndOtherMainsInLayers(selected);
else if (!selected.playbackEnabled)
animation.PlayClip(selected, true);
if (animation.isPlaying) return;
StorablePlay(StorableNames.PlayIfNotPlaying);
});
RegisterAction(_playIfNotPlayingJSON);

Expand Down Expand Up @@ -430,6 +421,36 @@ public void InitStorables()
RegisterBool(_pausedJSON);
}

private void StorablePlay(string storableName)
{
if (animation.paused)
{
animation.paused = false;
if (logger.triggers) logger.Log(logger.triggersCategory, $"Triggered '{storableName}' (unpause)");
return;
}

AtomAnimationClip selected;
if (string.IsNullOrEmpty(_legacyAnimationNext))
{
selected = animation.GetDefaultClip();
if (logger.triggers) logger.Log(logger.triggersCategory, $"Triggered '{storableName}' (Using default clip)");
}
else
{
selected = animation.GetClips(_legacyAnimationNext).FirstOrDefault();
if (logger.triggers) logger.Log(logger.triggersCategory, $"Triggered '{storableName}' (Using 'Animation' = '{_legacyAnimationNext}')");
if (selected == null)
{
SuperController.LogError($"Timeline: Atom '{containingAtom.uid}' failed to play animation '{_legacyAnimationNext}' specified in Animations storable: no animation found with that name.");
_legacyAnimationNext = null;
return;
}
}

animation.PlayOneAndOtherMainsInLayers(selected);
}

private IEnumerator DeferredInit()
{
yield return new WaitForEndOfFrame();
Expand Down

0 comments on commit 33304c5

Please sign in to comment.