Beispiel #1
0
        public static void StartScaleAnimation(this UIElement element, Vector3?from = null, Vector3?to = null,
                                               double duration = 800, int delay = 0, CompositionEasingFunction easing = null, Action completed = null,
                                               AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            CompositionScopedBatch batch = null;

            var visual     = element.Visual();
            var compositor = visual.Compositor;

            if (completed != null)
            {
                batch            = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
                batch.Completed += (s, e) => completed();
            }

            if (to == null)
            {
                to = Vector3.One;
            }

            visual.StartAnimation("Scale",
                                  compositor.CreateVector3KeyFrameAnimation(from, to.Value, duration, delay, easing, iterationBehavior));

            batch?.End();
        }
Beispiel #2
0
 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");
     }
 }
Beispiel #3
0
        public static Task StartScaleAnimationAsync(this UIElement element, Vector3?from = null, Vector3?to = null,
                                                    double duration = 800, int delay = 0, CompositionEasingFunction easing = null,
                                                    AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            CompositionScopedBatch batch;

            var visual     = element.Visual();
            var compositor = visual.Compositor;

            var taskSource = new TaskCompletionSource <bool>();

            void Completed(object o, CompositionBatchCompletedEventArgs e)
            {
                batch.Completed -= Completed;
                taskSource.SetResult(true);
            }

            batch            = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
            batch.Completed += Completed;

            if (to == null)
            {
                to = Vector3.One;
            }

            visual.StartAnimation("Scale",
                                  compositor.CreateVector3KeyFrameAnimation(from, to.Value, duration, delay, easing, iterationBehavior));

            batch.End();

            return(taskSource.Task);
        }
Beispiel #4
0
        public static Task StartScaleAnimationAsync(this Visual visual, AnimationAxis axis, float?from = null, float to = 0,
                                                    double duration = 800, int delay = 0, CompositionEasingFunction easing = null,
                                                    AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            CompositionScopedBatch batch;
            var compositor = visual.Compositor;

            var taskSource = new TaskCompletionSource <bool>();

            void Completed(object o, CompositionBatchCompletedEventArgs e)
            {
                batch.Completed -= Completed;
                taskSource.SetResult(true);
            }

            batch            = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
            batch.Completed += Completed;


            visual.StartAnimation($"Scale.{axis}",
                                  compositor.CreateScalarKeyFrameAnimation(from, to, duration, delay, easing, iterationBehavior));

            batch.End();

            return(taskSource.Task);
        }
        public static void StartRotationAnimation(this Visual visual, Vector3 rotationAxis, Vector3?centerPoint = null,
                                                  float?from = null, float to = 0, double duration = 800, int delay = 0, CompositionEasingFunction easing = null, Action completed = null,
                                                  AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            CompositionScopedBatch batch = null;
            var compositor = visual.Compositor;

            if (centerPoint == null)
            {
                centerPoint = new Vector3(visual.Size / 2, 0.0f);
            }
            visual.CenterPoint = centerPoint.Value;

            if (completed != null)
            {
                batch            = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
                batch.Completed += (s, e) => completed();
            }

            visual.RotationAxis = rotationAxis;

            visual.StartAnimation("RotationAngleInDegrees",
                                  compositor.CreateScalarKeyFrameAnimation(from, to, duration, delay, easing, iterationBehavior));

            batch?.End();
        }
Beispiel #6
0
        public static void StartRotationAnimation(this UIElement element, Vector3 rotationAxis, float?from = null, float to = 0,
                                                  double duration = 800, int delay = 0, CompositionEasingFunction easing = null, Action completed = null,
                                                  AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            CompositionScopedBatch batch = null;

            var visual     = element.Visual();
            var compositor = visual.Compositor;

            if (completed != null)
            {
                batch            = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
                batch.Completed += (s, e) => completed();
            }

            visual.RotationAxis = rotationAxis;

            visual.StartAnimation("RotationAngleInDegrees",
                                  compositor.CreateScalarKeyFrameAnimation(from, to, duration, delay, easing, iterationBehavior));

            if (batch != null)
            {
                batch.End();
            }
        }
        public static Task StartRotationAnimationAsync(this Visual visual, Vector3 rotationAxis, Vector3?centerPoint = null,
                                                       float?from = null, float to = 0, double duration = 800, int delay = 0, CompositionEasingFunction easing = null,
                                                       AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            CompositionScopedBatch batch;
            var compositor = visual.Compositor;

            if (centerPoint == null)
            {
                centerPoint = new Vector3(visual.Size / 2, 0.0f);
            }
            visual.CenterPoint = centerPoint.Value;

            var taskSource = new TaskCompletionSource <bool>();

            void Completed(object o, CompositionBatchCompletedEventArgs e)
            {
                batch.Completed -= Completed;
                taskSource.SetResult(true);
            }

            batch            = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
            batch.Completed += Completed;


            visual.RotationAxis = rotationAxis;

            visual.StartAnimation("RotationAngleInDegrees",
                                  compositor.CreateScalarKeyFrameAnimation(from, to, duration, delay, easing, iterationBehavior));

            batch.End();

            return(taskSource.Task);
        }
Beispiel #8
0
        public static void StartClipAnimation(this Visual visual, ClipAnimationDirection direction, float to,
                                              double duration = 800, int delay = 0, CompositionEasingFunction easing = null, Action completed = null,
                                              AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            CompositionScopedBatch batch = null;

            if (visual.Size.X.Equals(0) || visual.Size.Y.Equals(0))
            {
                throw new ArgumentException("The visual is not properly sized.");
            }

            var compositor = visual.Compositor;

            if (visual.Clip == null)
            {
                var clip = compositor.CreateInsetClip();
                visual.Clip = clip;
            }

            if (completed != null)
            {
                batch            = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
                batch.Completed += (s, e) => completed();
            }

            visual.Clip.StartAnimation($"{direction}Inset",
                                       compositor.CreateScalarKeyFrameAnimation(null, to, duration, delay, easing, iterationBehavior));

            batch?.End();
        }
        public static void StartTranslationAnimation(this UIElement element, Vector2?from = null, Vector2?to = null,
                                                     double duration = 800, int delay = 0, CompositionEasingFunction easing = null, Action completed = null,
                                                     AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            CompositionScopedBatch batch = null;

            ElementCompositionPreview.SetIsTranslationEnabled(element, true);
            var visual     = element.Visual();
            var compositor = visual.Compositor;

            if (completed != null)
            {
                batch            = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
                batch.Completed += (s, e) => completed();
            }

            if (to == null)
            {
                to = Vector2.Zero;
            }

            visual.StartAnimation("Translation.XY", compositor.CreateVector2KeyFrameAnimation(from, to.Value, duration, delay, easing, iterationBehavior));

            batch?.End();
        }
Beispiel #10
0
        public static void StartClipAnimation(this FrameworkElement element, ClipAnimationDirection direction, float to,
                                              double duration = 800, int delay = 0, CompositionEasingFunction easing = null, Action completed = null,
                                              AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            CompositionScopedBatch batch = null;

            var visual = element.Visual();

            // After we get the Visual of the View, we need to SIZE it 'cause by design the
            // Size is (0,0). Without doing this, clipping will not work.
            visual.Size = element.RenderSize.ToVector2();
            var compositor = visual.Compositor;

            if (visual.Clip == null)
            {
                var clip = compositor.CreateInsetClip();
                visual.Clip = clip;
            }

            if (completed != null)
            {
                batch            = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
                batch.Completed += (s, e) => completed();
            }

            visual.Clip.StartAnimation($"{direction}Inset",
                                       compositor.CreateScalarKeyFrameAnimation(null, to, duration, delay, easing, iterationBehavior));

            batch?.End();
        }
Beispiel #11
0
        public static void StartScaleAnimation(this Visual visual, AnimationAxis axis, float?from = null, float to = 0,
                                               double duration = 800, int delay = 0, CompositionEasingFunction easing = null, Action completed = null,
                                               AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            CompositionScopedBatch batch = null;
            var compositor = visual.Compositor;

            if (completed != null)
            {
                batch            = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
                batch.Completed += (s, e) => completed();
            }

            visual.StartAnimation($"Scale.{axis}",
                                  compositor.CreateScalarKeyFrameAnimation(from, to, duration, delay, easing, iterationBehavior));

            batch?.End();
        }
Beispiel #12
0
        public static void StartOpacityAnimation(this UIElement element, float?from = null, float to = 1.0f,
                                                 double duration = 800, int delay = 0, CompositionEasingFunction easing = null, Action completed = null,
                                                 AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            CompositionScopedBatch batch = null;

            var visual     = element.Visual();
            var compositor = visual.Compositor;

            if (completed != null)
            {
                batch            = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
                batch.Completed += (s, e) => completed();
            }

            visual.StartAnimation("Opacity",
                                  compositor.CreateScalarKeyFrameAnimation(from, to, duration, delay, easing, iterationBehavior));

            batch?.End();
        }
        public static void StartTranslationAnimation(this UIElement element, AnimationAxis axis, float?from = null, float to = 0,
                                                     double duration = 800, int delay = 0, CompositionEasingFunction easing = null, Action completed = null,
                                                     AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            CompositionScopedBatch batch = null;

            ElementCompositionPreview.SetIsTranslationEnabled(element, true);
            var visual     = element.Visual();
            var compositor = visual.Compositor;

            if (completed != null)
            {
                batch            = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
                batch.Completed += (s, e) => completed();
            }

            visual.StartAnimation($"Translation.{axis}", compositor.CreateScalarKeyFrameAnimation(from, to, duration, delay, easing, iterationBehavior));

            batch?.End();
        }
        public static Task StartClipAnimationAsync(this FrameworkElement element, ClipAnimationDirection direction, float to,
                                                   double duration = 800, int delay = 0, CompositionEasingFunction easing = null,
                                                   AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            CompositionScopedBatch batch;

            var visual = element.Visual();

            // After we get the Visual of the View, we need to SIZE it 'cause by design the
            // Size is (0,0). Without doing this, clipping will not work.
            visual.Size = element.RenderSize.ToVector2();
            var compositor = visual.Compositor;

            if (visual.Clip == null)
            {
                var clip = compositor.CreateInsetClip();
                visual.Clip = clip;
            }

            var taskSource = new TaskCompletionSource <bool>();

            void Completed(object o, CompositionBatchCompletedEventArgs e)
            {
                batch.Completed -= Completed;
                taskSource.SetResult(true);
            }

            batch            = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
            batch.Completed += Completed;

            visual.Clip.StartAnimation($"{direction}Inset",
                                       compositor.CreateScalarKeyFrameAnimation(null, to, duration, delay, easing, iterationBehavior));

            batch.End();

            return(taskSource.Task);
        }
        public static Task StartClipAnimationAsync(this Visual visual, ClipAnimationDirection direction, float to,
                                                   double duration = 800, int delay = 0, CompositionEasingFunction easing = null,
                                                   AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            CompositionScopedBatch batch;

            if (visual.Size.X.Equals(0) || visual.Size.Y.Equals(0))
            {
                throw new ArgumentException("The visual is not properly sized.");
            }

            var compositor = visual.Compositor;

            if (visual.Clip == null)
            {
                var clip = compositor.CreateInsetClip();
                visual.Clip = clip;
            }

            var taskSource = new TaskCompletionSource <bool>();

            void Completed(object o, CompositionBatchCompletedEventArgs e)
            {
                batch.Completed -= Completed;
                taskSource.SetResult(true);
            }

            batch            = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
            batch.Completed += Completed;

            visual.Clip.StartAnimation($"{direction}Inset",
                                       compositor.CreateScalarKeyFrameAnimation(null, to, duration, delay, easing, iterationBehavior));

            batch.End();

            return(taskSource.Task);
        }
Beispiel #16
0
        public static Vector3KeyFrameAnimation CreateVector3KeyFrameAnimation(this Compositor compositor, Vector3?from, Vector3 to,
                                                                              double duration, double delay, CompositionEasingFunction easing, AnimationIterationBehavior iterationBehavior)
        {
            var animation = compositor.CreateVector3KeyFrameAnimation();

            animation.Duration  = TimeSpan.FromMilliseconds(duration);
            animation.DelayTime = TimeSpan.FromMilliseconds(delay);
            if (from.HasValue)
            {
                animation.InsertKeyFrame(0.0f, from.Value, easing);
            }
            animation.InsertKeyFrame(1.0f, to, easing);
            animation.IterationBehavior = iterationBehavior;

            return(animation);
        }
        public static ScalarKeyFrameAnimation CreateScalarKeyFrameAnimation(this Compositor compositor, float?from, float to,
                                                                            double duration, double delay, CompositionEasingFunction easing = null, AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            var animation = compositor.CreateScalarKeyFrameAnimation();

            animation.Duration = TimeSpan.FromMilliseconds(duration);
            if (!delay.Equals(0))
            {
                animation.DelayTime = TimeSpan.FromMilliseconds(delay);
            }
            if (from.HasValue)
            {
                animation.InsertKeyFrame(0.0f, from.Value, easing);
            }
            animation.InsertKeyFrame(1.0f, to, easing);
            animation.IterationBehavior = iterationBehavior;

            return(animation);
        }
 public static T SetIterationBehavior<T>(this T animation, AnimationIterationBehavior iterationBehavior) where T : KeyFrameAnimation
 {
     animation.IterationBehavior = iterationBehavior;
     return animation;
 }