public override void AtProgress(float value, PlaybackDirection direction) { Vector2 currentValue; switch (direction) { case PlaybackDirection.FORWARD: if (value < 1f) { currentValue = Vector2.Lerp(from, to, curve.Evaluate(value)); } else { currentValue = Vector2.Lerp(from, to, curve.Evaluate(1f)); isPlaying = false; } break; default: if (value < 1f) { currentValue = Vector2.Lerp(to, from, curve.Evaluate(value)); } else { currentValue = Vector2.Lerp(to, from, curve.Evaluate(1f)); isPlaying = false; } break; } SetSize(currentValue); }
public override void AtProgress(float value, PlaybackDirection direction) { Color currentColor; switch (direction) { case PlaybackDirection.FORWARD: if (value < 1f) { currentColor = Color.Lerp(from, to, curve.Evaluate(value)); } else { currentColor = Color.Lerp(from, to, curve.Evaluate(1f)); isPlaying = false; } break; default: if (value < 1f) { currentColor = Color.Lerp(to, from, curve.Evaluate(value)); } else { currentColor = Color.Lerp(to, from, curve.Evaluate(1f)); isPlaying = false; } break; } SetColor(currentColor); }
public void SetPlaybackDirection(PlaybackDirection dir) { for (int i = 0; i < recorders.Count; i++) { recorders[i].playbackDirection = dir; } }
private void FetchProperties() { if (_animation.SpeedRatio < 0d) { throw new ArgumentOutOfRangeException("SpeedRatio value should not be negative."); } if (_animation.Duration.TotalSeconds <= 0) { throw new InvalidOperationException("Duration value cannot be negative or zero."); } _easeFunc = _animation.Easing; _speedRatioConv = 1d / _animation.SpeedRatio; _initialDelay = _animation.Delay; _duration = _animation.Duration; _iterationDelay = _animation.DelayBetweenIterations; if (_animation.IterationCount.RepeatType == IterationType.Many) { _iterationCount = _animation.IterationCount.Value; } else { _iterationCount = null; } _playbackDirection = _animation.PlaybackDirection; _fillMode = _animation.FillMode; }
public override void AtProgress(float value, PlaybackDirection direction) { Vector3 currentPosition; switch (direction) { case PlaybackDirection.FORWARD: if (value < 1f) { currentPosition = GetBezierPoint(from, from + control, to, curve.Evaluate(value)); } else { currentPosition = GetBezierPoint(from, from + control, to, curve.Evaluate(1f)); isPlaying = false; } break; default: if (value < 1f) { currentPosition = GetBezierPoint(from, from + control, to, curve.Evaluate(1f - value)); } else { currentPosition = GetBezierPoint(from, from + control, to, curve.Evaluate(1f)); isPlaying = false; } break; } SetPosition(currentPosition); }
public KeyFrameAnimationInstance( IInterpolator <T> interpolator, ServerKeyFrame <T>[] keyFrames, PropertySetSnapshot snapshot, ExpressionVariant?finalValue, ServerObject target, AnimationDelayBehavior delayBehavior, TimeSpan delayTime, PlaybackDirection direction, TimeSpan duration, AnimationIterationBehavior iterationBehavior, int iterationCount, AnimationStopBehavior stopBehavior) : base(target, snapshot) { _interpolator = interpolator; _keyFrames = keyFrames; _finalValue = finalValue; _delayBehavior = delayBehavior; _delayTime = delayTime; _direction = direction; _duration = duration; _iterationBehavior = iterationBehavior; _iterationCount = iterationCount; _stopBehavior = stopBehavior; if (_iterationBehavior == AnimationIterationBehavior.Count) { _totalDuration = delayTime.Add(TimeSpan.FromTicks(iterationCount * _duration.Ticks)); } if (_keyFrames.Length == 0) { throw new InvalidOperationException("Animation has no key frames"); } if (_duration.Ticks <= 0) { throw new InvalidOperationException("Invalid animation duration"); } }
public virtual void Progress(Single value, PlaybackDirection direction) { Initialize(); onProgress?.Invoke((direction == PlaybackDirection.FORWARD) ? value : 1f - value); _onProgress?.Invoke((direction == PlaybackDirection.FORWARD) ? value : 1f - value); AtProgress(value, direction); if (value >= 1f) { switch (repeat) { case RepeatType.ZERO_TO_ONE: if (direction == PlaybackDirection.FORWARD) { PlayForward(true); } else { PlayBackward(true); } break; case RepeatType.PING_PONG: Toggle(true); break; } } }
private void HandleEndBehaviour() { // If we are looping, set the index to the farthest end of the direction we start from if (endBehaviour == PlaybackEndBehaviour.LOOP) { if (playbackDirection == PlaybackDirection.FORWARD) { keyframeIndex = 0; } else if (playbackDirection == PlaybackDirection.REVERSE) { keyframeIndex = keyframes.Count - 1; } } // If we are ping-ponging, reverse playback direction else if (endBehaviour == PlaybackEndBehaviour.PING_PONG) { if (playbackDirection == PlaybackDirection.FORWARD) { playbackDirection = PlaybackDirection.REVERSE; } else if (playbackDirection == PlaybackDirection.REVERSE) { playbackDirection = PlaybackDirection.FORWARD; } } // If we are stopping playback... stop playback else if (endBehaviour == PlaybackEndBehaviour.STOP) { playbackDirection = PlaybackDirection.STOP; } }
public AnimationInstance(Animation animation, Animatable control, Animator <T> animator, IClock baseClock, Action OnComplete, Func <double, T, T> Interpolator) { if (animation.SpeedRatio <= 0) { throw new InvalidOperationException("Speed ratio cannot be negative or zero."); } if (animation.Duration.TotalSeconds <= 0) { throw new InvalidOperationException("Duration cannot be negative or zero."); } _parent = animator; _easeFunc = animation.Easing; _targetControl = control; _neutralValue = (T)_targetControl.GetValue(_parent.Property); _speedRatio = animation.SpeedRatio; _delay = animation.Delay; _duration = animation.Duration; _iterationDelay = animation.DelayBetweenIterations; switch (animation.RepeatCount.RepeatType) { case RepeatType.None: _repeatCount = 1; break; case RepeatType.Loop: _isLooping = true; break; case RepeatType.Repeat: _repeatCount = (long)animation.RepeatCount.Value; break; } _animationDirection = animation.PlaybackDirection; _fillMode = animation.FillMode; _onCompleteAction = OnComplete; _interpolator = Interpolator; _baseClock = baseClock; }
/// <summary> /// Stops the current playing animation (if any) and plays the designated animation. /// </summary> /// <param name="name">The name of the animation to play</param> /// <param name="direction">Should the animation be played forward or backward?</param> /// <param name="fps">Speed at which the animation should be played</param> /// <returns></returns> private IEnumerator playAnimation(string name, PlaybackDirection direction, float fps = 1f) { stopAnimation(); SpriteAnimationFrame[] frames; try { frames = spriteType.getAnimation(name); } catch (System.Exception) { Debug.LogError(gameObject.name + " could not load sprite animation " + name); throw; } float currentTime = 0; int dir = (direction == PlaybackDirection.Forward) ? dir = 1 : dir = -1; while (true) { if (!isPlayingAnimation) { break; } currentTime += Time.deltaTime; if (currentTime < (fps * frames[frameNum].duration)) { continue; } mMaterial.mainTextureOffset = new Vector2(frames[frameNum].x * SpriteType.spriteSize, frames[frameNum].y * SpriteType.spriteSize); //Increment frame count frameNum += dir; if (frameNum >= frames.Length) //I think Length starts from 1, not 0. will confirm in morning, want to sleep now... { frameNum = 0; } currentTime = 0; yield return(null); } }
public virtual void PlayBackward(bool resetValue) { if (resetValue) { value = 0f; } else { if (value != 0f) { value = 1f - value; } } isPlaying = true; playbackDirection = PlaybackDirection.BACKWARD; Progress(value, playbackDirection); }
public override void AtProgress(float value, PlaybackDirection direction) { Vector3 currentValue; switch (direction) { case PlaybackDirection.FORWARD: if (value < 1f) { currentValue = Vector3.one + new Vector3(curve.Evaluate(value) * xAxisPower, curve.Evaluate(value) * yAxisPower); } else { currentValue = Vector3.one + new Vector3(curve.Evaluate(1f) * xAxisPower, curve.Evaluate(1f) * yAxisPower); isPlaying = false; } break; default: if (value < 1f) { currentValue = Vector3.one + new Vector3( curve.Evaluate(1f - value) * xAxisPower, curve.Evaluate(1f - value) * yAxisPower); } else { currentValue = Vector3.one + new Vector3(curve.Evaluate(0f) * xAxisPower, curve.Evaluate(0f) * yAxisPower); isPlaying = false; } break; } SetValue(currentValue); }
public AnimatorStateMachine(Animation animation, Animatable control, Animator <T> animator, Action onComplete) { _parent = animator; _targetAnimation = animation; _targetControl = control; _neutralValue = (T)_targetControl.GetValue(_parent.Property); _delayTotalFrameCount = (ulong)(animation.Delay.Ticks / Timing.FrameTick.Ticks); _durationTotalFrameCount = (ulong)(animation.Duration.Ticks / Timing.FrameTick.Ticks); switch (animation.RepeatCount.RepeatType) { case RepeatType.Loop: _isLooping = true; _checkLoopAndRepeat = true; break; case RepeatType.Repeat: _isRepeating = true; _checkLoopAndRepeat = true; _repeatCount = animation.RepeatCount.Value; break; } _isReversed = (animation.PlaybackDirection & PlaybackDirection.Reverse) != 0; _animationDirection = _targetAnimation.PlaybackDirection; _fillMode = _targetAnimation.FillMode; if (_durationTotalFrameCount > 0) { _currentState = KeyFramesStates.DoDelay; } else { _currentState = KeyFramesStates.DoRun; } _onComplete = onComplete; }
public ReplaySequenceResult UpdatePlayback(out ReplaySnapshot frame, PlaybackEndBehaviour endBehaviour, float deltaTime) { PlaybackDirection direction = ReplayTime.TimeScaleDirection; // Default to idle ReplaySequenceResult result = ReplaySequenceResult.SequenceIdle; if (last != null) { if (direction == PlaybackDirection.Forward) { // Calculatet the delta time ReplayTime.Delta = MapScale(playbackTime, last.TimeStamp, current.TimeStamp, 0, 1); } else { // Calculate the delta for reverse playback ReplayTime.Delta = -MapScale(playbackTime, current.TimeStamp, last.TimeStamp, 0, 1); } } else { if (current == null) { ReplayTime.Delta = 0; } else { ReplayTime.Delta = MapScale(playbackTime, 0, current.TimeStamp, 0, 1); } } // Clamp delta ReplayTime.Delta = Mathf.Clamp01(ReplayTime.Delta); float delta = Mathf.Abs(deltaTime * ReplayTime.TimeScale); //if (fixedTime == true) //{ // // Find the fixed time delta // delta = (Time.fixedDeltaTime * ReplayTime.TimeScale); //} //else //{ // // Find the time delta // delta = (Time.deltaTime * Mathf.Abs(ReplayTime.TimeScale)); //} // Advance our frame switch (direction) { case PlaybackDirection.Forward: { playbackTime += delta; } break; case PlaybackDirection.Backward: { playbackTime -= delta; } break; } switch (endBehaviour) { default: case PlaybackEndBehaviour.EndPlayback: { // Check for end of playback if (playbackTime >= target.Duration || playbackTime < 0) { frame = null; return(ReplaySequenceResult.SequenceEnd); } break; } case PlaybackEndBehaviour.LoopPlayback: { if (playbackTime >= target.Duration || playbackTime < 0) { playbackTime = (direction == PlaybackDirection.Forward) ? 0 : target.Duration; } break; } case PlaybackEndBehaviour.StopPlayback: { if (playbackTime >= target.Duration) { playbackTime = target.Duration; } else if (playbackTime < 0) { playbackTime = 0; } break; } } // Try to get the current frame ReplaySnapshot temp = target.RestoreSnapshot(playbackTime); // Check for valid frame if (temp != null) { // Check for sequence advancement if (current != temp) { // Snap to next frame ReplayTime.Delta = 0; // Set the result result = ReplaySequenceResult.SequenceAdvance; // Update last frame last = current; } // Update the current frame current = temp; } else { // Do nothing - We may be inbetween replay frames // Trigger sequence end //frame = null; //return ReplaySequenceResult.SequenceEnd; } // The sequencer only updated its timing values and there was no state change frame = current; return(result); }
public static void BeginPlayback(bool fromStart, PlaybackDirection direction) { // Make sure we have a manager if (Instance == null) { return; } if (instance.state == PlaybackState.Recording_Paused) { instance.state = PlaybackState.Recording_Paused_Playback; } else { // Check if we are recording if (IsRecording == true) { // Stop recording before playback StopRecording(); } } // Check if we are replaying if (IsReplaying == false) { // Inform the replay target that we are about to begin playback Instance.target.PrepareTarget(ReplayTargetTask.PrepareRead); // Enable replay mode - prepares scene for playback - Be sure to enable replay mode before settings the active replay frame Instance.scene.SetReplaySceneMode( ReplayScene.ReplaySceneMode.Playback, Instance.target.InitialStateBuffer); // Call the reset event ReplayBehaviour.Events.CallReplayResetEvents(); // Broadcast the replay start event ReplayBehaviour.Events.CallReplayStartEvents(); } // Check if we should seek to the start if (fromStart == true) { SetPlaybackFrame(0); } // Set the playback direction for the replay sequence if (direction == PlaybackDirection.Forward) { if (Time.timeScale < 0) { Time.timeScale = 1f; } } else if (direction == PlaybackDirection.Backward) { if (Time.timeScale > 0) { Time.timeScale = -1f; } } // Disable single playback Instance.singlePlayback = false; // Update our current state if (instance.state != PlaybackState.Recording_Paused_Playback) { Instance.state = PlaybackState.Playback; } }
public abstract void AtProgress(Single value, PlaybackDirection direction);
/// <summary> /// Stops the current playing animation (if any) and plays the designated animation. /// </summary> /// <param name="name">The name of the animation to play</param> /// <param name="direction">Should the animation be played forward or backward?</param> /// <param name="fps">Speed at which the animation should be played</param> /// <returns></returns> private IEnumerator playAnimation(string name, PlaybackDirection direction, float fps = 1f) { stopAnimation(); SpriteAnimationFrame[] frames; try { frames = spriteType.getAnimation( name ); } catch (System.Exception) { Debug.LogError( gameObject.name + " could not load sprite animation " + name ); throw; } float currentTime = 0; int dir = (direction == PlaybackDirection.Forward) ? dir = 1 : dir = -1; while (true) { if (!isPlayingAnimation) break; currentTime += Time.deltaTime; if (currentTime < (fps * frames[frameNum].duration)) continue; mMaterial.mainTextureOffset = new Vector2( frames[frameNum].x * SpriteType.spriteSize, frames[frameNum].y * SpriteType.spriteSize); //Increment frame count frameNum += dir; if (frameNum >= frames.Length) //I think Length starts from 1, not 0. will confirm in morning, want to sleep now... frameNum = 0; currentTime = 0; yield return null; } }