Example #1
0
        public void CopyTo()
        {
            var blendGroup = new BlendGroup();

            ITimeline[] array = new ITimeline[0];
            Assert.That(() => ((IList <ITimeline>)blendGroup).CopyTo(null, 0), Throws.TypeOf <ArgumentNullException>());
            Assert.That(() => ((IList <ITimeline>)blendGroup).CopyTo(array, -1), Throws.TypeOf <ArgumentOutOfRangeException>());
            Assert.That(() => ((IList <ITimeline>)blendGroup).CopyTo(array, 1), Throws.ArgumentException);
            Assert.That(() => ((IList <ITimeline>)blendGroup).CopyTo(array, 0), Throws.Nothing);

            var animation0 = new SingleFromToByAnimation();
            var animation1 = new SingleFromToByAnimation();

            blendGroup = new BlendGroup {
                animation0, animation1
            };

            array = new ITimeline[1];
            Assert.That(() => ((IList <ITimeline>)blendGroup).CopyTo(array, 0), Throws.ArgumentException);

            array = new ITimeline[2];
            ((IList <ITimeline>)blendGroup).CopyTo(array, 0);
            Assert.AreEqual(animation0, array[0]);
            Assert.AreEqual(animation1, array[1]);

            array = new ITimeline[3];
            ((IList <ITimeline>)blendGroup).CopyTo(array, 1);
            Assert.AreEqual(null, array[0]);
            Assert.AreEqual(animation0, array[1]);
            Assert.AreEqual(animation1, array[2]);
        }
Example #2
0
        /// <summary>
        /// Creates an instance of the <see cref="BlendGroupInstance"/> class. (This method reuses a
        /// previously recycled instance or allocates a new instance if necessary.)
        /// </summary>
        /// <param name="blendGroup">The <see cref="BlendGroup"/> that should be played back.</param>
        /// <returns>
        /// A new or reusable instance of the <see cref="BlendGroupInstance"/> class.
        /// </returns>
        /// <remarks>
        /// <para>
        /// This method tries to obtain a previously recycled instance from a resource pool if resource
        /// pooling is enabled (see <see cref="ResourcePool.Enabled">ResourcePool.Enabled</see>). If no
        /// object is available, a new instance is automatically allocated on the heap.
        /// </para>
        /// <para>
        /// The owner of the object should call <see cref="Recycle"/> when the instance is no longer
        /// needed.
        /// </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="blendGroup"/> is <see langword="null"/>.
        /// </exception>
        public static BlendGroupInstance Create(BlendGroup blendGroup)
        {
            var blendGroupInstance = Pool.Obtain();

            blendGroupInstance.Initialize(blendGroup);
            return(blendGroupInstance);
        }
Example #3
0
        public void BlendGroupWithTwoAnimationsSynchronized()
        {
            var property1 = new AnimatableProperty <float> {
                Value = 123.45f
            };

            var blendGroup = new BlendGroup {
                FillBehavior = FillBehavior.Stop
            };
            var animation1 = new SingleFromToByAnimation {
                From = 0, To = 100, Duration = TimeSpan.FromSeconds(1.0)
            };
            var animation2 = new SingleFromToByAnimation {
                From = 100, To = 300, Duration = TimeSpan.FromSeconds(2.0)
            };

            blendGroup.Add(animation1);
            blendGroup.Add(animation2);
            Assert.AreEqual(1.0f, blendGroup.GetWeight(0));
            Assert.AreEqual(1.0f, blendGroup.GetWeight(1));
            Assert.AreEqual(TimeSpan.FromSeconds(2.0), blendGroup.GetTotalDuration());

            blendGroup.SynchronizeDurations();
            Assert.AreEqual(TimeSpan.FromSeconds(1.5), blendGroup.GetTotalDuration());

            var manager    = new AnimationManager();
            var controller = manager.StartAnimation(blendGroup, property1);

            controller.UpdateAndApply();
            Assert.AreEqual(50.0f, property1.Value);

            manager.Update(TimeSpan.FromSeconds(0.75)); // t = 0.75
            manager.ApplyAnimations();
            Assert.AreEqual(TimeSpan.FromSeconds(1.5), blendGroup.GetTotalDuration());
            Assert.AreEqual(0.5f * 50.0f + 0.5f * 200.0f, property1.Value);

            blendGroup.SetWeight(0, 0);
            Assert.AreEqual(TimeSpan.FromSeconds(2.0), blendGroup.GetTotalDuration());
            manager.Update(TimeSpan.FromSeconds(0.25)); // t = 1.0
            manager.ApplyAnimations();
            Assert.AreEqual(200.0f, property1.Value);

            blendGroup.SetWeight(0, 10);
            blendGroup.SetWeight(1, 0);
            Assert.AreEqual(TimeSpan.FromSeconds(1.0), blendGroup.GetTotalDuration());
            manager.Update(TimeSpan.Zero); // t = 1.0
            manager.ApplyAnimations();
            Assert.AreEqual(100.0f, property1.Value);

            blendGroup.SetWeight(0, 10);
            blendGroup.SetWeight(1, 1);
            Assert.AreEqual(new TimeSpan((long)((1.0f * 10.0f / 11.0f + 2.0f * 1.0f / 11.0f) * TimeSpan.TicksPerSecond)), blendGroup.GetTotalDuration());
            manager.Update(TimeSpan.FromSeconds(0.5)); // t = 1.5
            manager.ApplyAnimations();
            Assert.AreEqual(123.45f, property1.Value);
            Assert.AreEqual(AnimationState.Stopped, controller.State);
        }
        //--------------------------------------------------------------
        #region Creation & Cleanup
        //--------------------------------------------------------------
        #endregion


        //--------------------------------------------------------------
        #region Methods
        //--------------------------------------------------------------

        /// <inheritdoc/>
        internal override void Initialize(BlendGroup blendGroup, string targetProperty)
        {
            base.Initialize(blendGroup, targetProperty);

            var numberOfAnimations = blendGroup.Count;

            for (int i = 0; i < numberOfAnimations; i++)
            {
                _animations.Add(null);
            }
        }
Example #5
0
        public void BlendGroupWithOneAnimation()
        {
            var property1 = new AnimatableProperty <float> {
                Value = 123.45f
            };

            var blendGroup = new BlendGroup {
                FillBehavior = FillBehavior.Stop
            };
            var animation = new SingleFromToByAnimation {
                From = 0, To = 100, Duration = TimeSpan.FromSeconds(1.0)
            };

            blendGroup.Add(animation);
            Assert.AreEqual(1.0f, blendGroup.GetWeight(0));
            Assert.AreEqual(1.0f, blendGroup.GetWeight(animation));

            blendGroup.SetWeight(0, 10.0f);
            Assert.AreEqual(10.0f, blendGroup.GetWeight(0));
            Assert.AreEqual(10.0f, blendGroup.GetWeight(animation));

            blendGroup.SetWeight(animation, 0.5f);
            Assert.AreEqual(0.5f, blendGroup.GetWeight(0));
            Assert.AreEqual(0.5f, blendGroup.GetWeight(animation));

            var manager    = new AnimationManager();
            var controller = manager.StartAnimation(blendGroup, property1);

            controller.UpdateAndApply();
            Assert.AreEqual(0.0f, property1.Value);

            manager.Update(TimeSpan.FromSeconds(0.25));
            manager.ApplyAnimations();
            Assert.AreEqual(25.0f, property1.Value);

            manager.Update(TimeSpan.FromSeconds(0.25));
            manager.ApplyAnimations();
            Assert.AreEqual(50.0f, property1.Value);

            manager.Update(TimeSpan.FromSeconds(0.25));
            manager.ApplyAnimations();
            Assert.AreEqual(75.0f, property1.Value);

            manager.Update(TimeSpan.FromSeconds(0.25));
            manager.ApplyAnimations();
            Assert.AreEqual(100.0f, property1.Value);

            manager.Update(TimeSpan.FromSeconds(0.25));
            manager.ApplyAnimations();
            Assert.AreEqual(123.45f, property1.Value);
            Assert.AreEqual(AnimationState.Stopped, controller.State);
        }
Example #6
0
        public void BlendGroupWithAnimationAndTimelineGroups()
        {
            var testObject = new AnimatableObject("TestObject");
              var property1 = new AnimatableProperty<float> { Value = 123.45f };
              testObject.Properties.Add("Property1", property1);

              var blendGroup = new BlendGroup
              {
            new SingleFromToByAnimation { From = 0, To = 100, TargetProperty = "Property1" },
            new TimelineGroup { new SingleFromToByAnimation { From = 100, To = 300, Duration = TimeSpan.FromSeconds(2.0), TargetProperty = "Property1" }, },
              };
              blendGroup.SynchronizeDurations();
              Assert.AreEqual(TimeSpan.FromSeconds(1.5), blendGroup.GetTotalDuration());

              var manager = new AnimationManager();
              var controller = manager.StartAnimation(blendGroup, testObject);
              controller.UpdateAndApply();
              Assert.AreEqual(50.0f, property1.Value);

              manager.Update(TimeSpan.FromSeconds(0.75));      // t = 0.75
              manager.ApplyAnimations();
              Assert.AreEqual(TimeSpan.FromSeconds(1.5), blendGroup.GetTotalDuration());
              Assert.AreEqual(0.5f * 50.0f + 0.5f * 200.0f, property1.Value);

              blendGroup.SetWeight(0, 0);
              Assert.AreEqual(TimeSpan.FromSeconds(2.0), blendGroup.GetTotalDuration());
              manager.Update(TimeSpan.FromSeconds(0.25));       // t = 1.0
              manager.ApplyAnimations();
              Assert.AreEqual(200.0f, property1.Value);

              blendGroup.SetWeight(0, 10);
              blendGroup.SetWeight(1, 0);
              Assert.AreEqual(TimeSpan.FromSeconds(1.0), blendGroup.GetTotalDuration());
              manager.Update(TimeSpan.Zero);       // t = 1.0
              manager.ApplyAnimations();
              Assert.AreEqual(100.0f, property1.Value);

              blendGroup.SetWeight(0, 10);
              blendGroup.SetWeight(1, 1);
              Assert.AreEqual(new TimeSpan((long)((1.0f * 10.0f / 11.0f + 2.0f * 1.0f / 11.0f) * TimeSpan.TicksPerSecond)), blendGroup.GetTotalDuration());
              manager.Update(TimeSpan.FromSeconds(0.5));       // t = 1.5
              manager.ApplyAnimations();
              Assert.AreEqual(AnimationState.Filling, controller.State);
              Assert.IsTrue(Numeric.AreEqual(100.0f * 10.0f / 11.0f + 300.0f * 1.0f / 11.0f, property1.Value));

              controller.Stop();
              controller.UpdateAndApply();
              Assert.AreEqual(AnimationState.Stopped, controller.State);
              Assert.AreEqual(123.45f, property1.Value);
        }
Example #7
0
        public void EnumeratorTest()
        {
            var blendGroup = new BlendGroup();

            Assert.AreEqual(0, blendGroup.Count());

            var animation0 = new SingleFromToByAnimation();
            var animation1 = new SingleFromToByAnimation();

            blendGroup = new BlendGroup {
                animation0, animation1
            };
            Assert.AreEqual(2, blendGroup.Count());
            Assert.AreEqual(animation0, blendGroup.ElementAt(0));
            Assert.AreEqual(animation1, blendGroup.ElementAt(1));
        }
Example #8
0
        public void BlendGroupWithOneAnimation()
        {
            var property1 = new AnimatableProperty<float> { Value = 123.45f };

              var blendGroup = new BlendGroup { FillBehavior = FillBehavior.Stop };
              var animation = new SingleFromToByAnimation { From = 0, To = 100, Duration = TimeSpan.FromSeconds(1.0) };
              blendGroup.Add(animation);
              Assert.AreEqual(1.0f, blendGroup.GetWeight(0));
              Assert.AreEqual(1.0f, blendGroup.GetWeight(animation));

              blendGroup.SetWeight(0, 10.0f);
              Assert.AreEqual(10.0f, blendGroup.GetWeight(0));
              Assert.AreEqual(10.0f, blendGroup.GetWeight(animation));

              blendGroup.SetWeight(animation, 0.5f);
              Assert.AreEqual(0.5f, blendGroup.GetWeight(0));
              Assert.AreEqual(0.5f, blendGroup.GetWeight(animation));

              var manager = new AnimationManager();
              var controller = manager.StartAnimation(blendGroup, property1);
              controller.UpdateAndApply();
              Assert.AreEqual(0.0f, property1.Value);

              manager.Update(TimeSpan.FromSeconds(0.25));
              manager.ApplyAnimations();
              Assert.AreEqual(25.0f, property1.Value);

              manager.Update(TimeSpan.FromSeconds(0.25));
              manager.ApplyAnimations();
              Assert.AreEqual(50.0f, property1.Value);

              manager.Update(TimeSpan.FromSeconds(0.25));
              manager.ApplyAnimations();
              Assert.AreEqual(75.0f, property1.Value);

              manager.Update(TimeSpan.FromSeconds(0.25));
              manager.ApplyAnimations();
              Assert.AreEqual(100.0f, property1.Value);

              manager.Update(TimeSpan.FromSeconds(0.25));
              manager.ApplyAnimations();
              Assert.AreEqual(123.45f, property1.Value);
              Assert.AreEqual(AnimationState.Stopped, controller.State);
        }
Example #9
0
        public void ShouldDoNothingWhenWeightsAreZero()
        {
            var blendGroup = new BlendGroup {
                FillBehavior = FillBehavior.Stop
            };
            var animation1 = new SingleFromToByAnimation {
                From = 0, To = 100, Duration = TimeSpan.FromSeconds(1.0)
            };
            var animation2 = new SingleFromToByAnimation {
                From = 100, To = 300, Duration = TimeSpan.FromSeconds(2.0)
            };

            blendGroup.Add(animation1);
            blendGroup.Add(animation2);
            blendGroup.SetWeight(0, 0);
            blendGroup.SetWeight(1, 0);

            Assert.That(() => blendGroup.SynchronizeDurations(), Throws.Nothing);
            blendGroup.Update();
            Assert.AreEqual(0.0f, blendGroup.GetNormalizedWeight(0));
            Assert.AreEqual(0.0f, blendGroup.GetNormalizedWeight(1));
        }
Example #10
0
        public void EmptyBlendGroup()
        {
            var property1  = new AnimatableProperty <float>();
            var blendGroup = new BlendGroup();

            Assert.That(() => blendGroup.GetWeight(0), Throws.TypeOf <ArgumentOutOfRangeException>());
            Assert.That(() => blendGroup.SetWeight(0, 1.0f), Throws.TypeOf <ArgumentOutOfRangeException>());

            var manager    = new AnimationManager();
            var controller = manager.StartAnimation(blendGroup, property1);

            manager.Update(TimeSpan.Zero);
            manager.ApplyAnimations();
            Assert.AreEqual(AnimationState.Stopped, controller.State);

            blendGroup.SynchronizeDurations();
            blendGroup.Clear();
            controller = manager.StartAnimation(blendGroup, property1);
            manager.Update(TimeSpan.Zero);
            manager.ApplyAnimations();
            Assert.AreEqual(AnimationState.Stopped, controller.State);
        }
Example #11
0
        public void ShouldDoNothingWhenWeightsAreZero()
        {
            var blendGroup = new BlendGroup { FillBehavior = FillBehavior.Stop };
              var animation1 = new SingleFromToByAnimation { From = 0, To = 100, Duration = TimeSpan.FromSeconds(1.0) };
              var animation2 = new SingleFromToByAnimation { From = 100, To = 300, Duration = TimeSpan.FromSeconds(2.0) };
              blendGroup.Add(animation1);
              blendGroup.Add(animation2);
              blendGroup.SetWeight(0, 0);
              blendGroup.SetWeight(1, 0);

              Assert.That(() => blendGroup.SynchronizeDurations(), Throws.Nothing);
              blendGroup.Update();
              Assert.AreEqual(0.0f, blendGroup.GetNormalizedWeight(0));
              Assert.AreEqual(0.0f, blendGroup.GetNormalizedWeight(1));
        }
Example #12
0
        public void BlendGroupWithAnimationAndTimelineGroups()
        {
            var testObject = new AnimatableObject("TestObject");
            var property1  = new AnimatableProperty <float> {
                Value = 123.45f
            };

            testObject.Properties.Add("Property1", property1);

            var blendGroup = new BlendGroup
            {
                new SingleFromToByAnimation {
                    From = 0, To = 100, TargetProperty = "Property1"
                },
                new TimelineGroup {
                    new SingleFromToByAnimation {
                        From = 100, To = 300, Duration = TimeSpan.FromSeconds(2.0), TargetProperty = "Property1"
                    },
                },
            };

            blendGroup.SynchronizeDurations();
            Assert.AreEqual(TimeSpan.FromSeconds(1.5), blendGroup.GetTotalDuration());

            var manager    = new AnimationManager();
            var controller = manager.StartAnimation(blendGroup, testObject);

            controller.UpdateAndApply();
            Assert.AreEqual(50.0f, property1.Value);

            manager.Update(TimeSpan.FromSeconds(0.75)); // t = 0.75
            manager.ApplyAnimations();
            Assert.AreEqual(TimeSpan.FromSeconds(1.5), blendGroup.GetTotalDuration());
            Assert.AreEqual(0.5f * 50.0f + 0.5f * 200.0f, property1.Value);

            blendGroup.SetWeight(0, 0);
            Assert.AreEqual(TimeSpan.FromSeconds(2.0), blendGroup.GetTotalDuration());
            manager.Update(TimeSpan.FromSeconds(0.25)); // t = 1.0
            manager.ApplyAnimations();
            Assert.AreEqual(200.0f, property1.Value);

            blendGroup.SetWeight(0, 10);
            blendGroup.SetWeight(1, 0);
            Assert.AreEqual(TimeSpan.FromSeconds(1.0), blendGroup.GetTotalDuration());
            manager.Update(TimeSpan.Zero); // t = 1.0
            manager.ApplyAnimations();
            Assert.AreEqual(100.0f, property1.Value);

            blendGroup.SetWeight(0, 10);
            blendGroup.SetWeight(1, 1);
            Assert.AreEqual(new TimeSpan((long)((1.0f * 10.0f / 11.0f + 2.0f * 1.0f / 11.0f) * TimeSpan.TicksPerSecond)), blendGroup.GetTotalDuration());
            manager.Update(TimeSpan.FromSeconds(0.5)); // t = 1.5
            manager.ApplyAnimations();
            Assert.AreEqual(AnimationState.Filling, controller.State);
            Assert.IsTrue(Numeric.AreEqual(100.0f * 10.0f / 11.0f + 300.0f * 1.0f / 11.0f, property1.Value));

            controller.Stop();
            controller.UpdateAndApply();
            Assert.AreEqual(AnimationState.Stopped, controller.State);
            Assert.AreEqual(123.45f, property1.Value);
        }
Example #13
0
 /// <summary>
 /// Initializes the <see cref="BlendAnimation{T}"/> for specified number of animation.
 /// </summary>
 /// <param name="blendGroup">The <see cref="BlendGroup"/>.</param>
 /// <param name="targetProperty">The property to which the animation is applied.</param>
 internal virtual void Initialize(BlendGroup blendGroup, string targetProperty)
 {
     Group          = blendGroup;
     TargetProperty = targetProperty;
 }
Example #14
0
        public void BlendGroupWithTwoAnimationsSynchronized()
        {
            var property1 = new AnimatableProperty<float> { Value = 123.45f };

              var blendGroup = new BlendGroup { FillBehavior = FillBehavior.Stop };
              var animation1 = new SingleFromToByAnimation { From = 0, To = 100, Duration = TimeSpan.FromSeconds(1.0) };
              var animation2 = new SingleFromToByAnimation { From = 100, To = 300, Duration = TimeSpan.FromSeconds(2.0) };
              blendGroup.Add(animation1);
              blendGroup.Add(animation2);
              Assert.AreEqual(1.0f, blendGroup.GetWeight(0));
              Assert.AreEqual(1.0f, blendGroup.GetWeight(1));
              Assert.AreEqual(TimeSpan.FromSeconds(2.0), blendGroup.GetTotalDuration());

              blendGroup.SynchronizeDurations();
              Assert.AreEqual(TimeSpan.FromSeconds(1.5), blendGroup.GetTotalDuration());

              var manager = new AnimationManager();
              var controller = manager.StartAnimation(blendGroup, property1);
              controller.UpdateAndApply();
              Assert.AreEqual(50.0f, property1.Value);

              manager.Update(TimeSpan.FromSeconds(0.75));      // t = 0.75
              manager.ApplyAnimations();
              Assert.AreEqual(TimeSpan.FromSeconds(1.5), blendGroup.GetTotalDuration());
              Assert.AreEqual(0.5f * 50.0f + 0.5f * 200.0f, property1.Value);

              blendGroup.SetWeight(0, 0);
              Assert.AreEqual(TimeSpan.FromSeconds(2.0), blendGroup.GetTotalDuration());
              manager.Update(TimeSpan.FromSeconds(0.25));       // t = 1.0
              manager.ApplyAnimations();
              Assert.AreEqual(200.0f, property1.Value);

              blendGroup.SetWeight(0, 10);
              blendGroup.SetWeight(1, 0);
              Assert.AreEqual(TimeSpan.FromSeconds(1.0), blendGroup.GetTotalDuration());
              manager.Update(TimeSpan.Zero);       // t = 1.0
              manager.ApplyAnimations();
              Assert.AreEqual(100.0f, property1.Value);

              blendGroup.SetWeight(0, 10);
              blendGroup.SetWeight(1, 1);
              Assert.AreEqual(new TimeSpan((long)((1.0f * 10.0f / 11.0f + 2.0f * 1.0f / 11.0f) * TimeSpan.TicksPerSecond)), blendGroup.GetTotalDuration());
              manager.Update(TimeSpan.FromSeconds(0.5));       // t = 1.5
              manager.ApplyAnimations();
              Assert.AreEqual(123.45f, property1.Value);
              Assert.AreEqual(AnimationState.Stopped, controller.State);
        }
        public BlendedAnimationSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            Rectangle bounds = GraphicsService.GraphicsDevice.Viewport.TitleSafeArea;

            // Create the animatable object.
            _animatableSprite = new AnimatableSprite("SpriteA", SpriteBatch, Logo)
            {
                Position = new Vector2(bounds.Center.X, bounds.Center.Y / 2.0f),
                Color    = Color.Red,
            };

            Vector2FromToByAnimation slowLeftRightAnimation = new Vector2FromToByAnimation
            {
                TargetProperty = "Position",
                From           = new Vector2(bounds.Left + 100, bounds.Top + 200),
                To             = new Vector2(bounds.Right - 100, bounds.Top + 200),
                Duration       = TimeSpan.FromSeconds(4),
                EasingFunction = new HermiteEase {
                    Mode = EasingMode.EaseInOut
                },
            };

            ColorKeyFrameAnimation redGreenAnimation = new ColorKeyFrameAnimation
            {
                TargetProperty      = "Color",
                EnableInterpolation = true,
            };

            redGreenAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(0), Color.Red));
            redGreenAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(4), Color.Green));

            TimelineGroup animationA = new TimelineGroup
            {
                slowLeftRightAnimation,
                redGreenAnimation,
            };

            Vector2FromToByAnimation fastLeftRightAnimation = new Vector2FromToByAnimation
            {
                TargetProperty = "Position",
                From           = new Vector2(bounds.Left + 100, bounds.Bottom - 200),
                To             = new Vector2(bounds.Right - 100, bounds.Bottom - 200),
                Duration       = TimeSpan.FromSeconds(1),
                EasingFunction = new HermiteEase {
                    Mode = EasingMode.EaseInOut
                },
            };

            ColorKeyFrameAnimation blackWhiteAnimation = new ColorKeyFrameAnimation
            {
                TargetProperty      = "Color",
                EnableInterpolation = true,
            };

            blackWhiteAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(0), Color.Black));
            blackWhiteAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(1), Color.White));

            TimelineGroup animationB = new TimelineGroup
            {
                fastLeftRightAnimation,
                blackWhiteAnimation,
            };

            // Create a BlendGroup that blends animationA and animationB.
            // The BlendGroup uses the TargetProperty values of the contained animations to
            // to match the animations that should be blended:
            //   slowLeftRightAnimation with fastLeftRightAnimation
            //   redGreenAnimation with blackWhiteAnimation
            _blendedAnimation = new BlendGroup
            {
                LoopBehavior = LoopBehavior.Oscillate,
                Duration     = TimeSpan.MaxValue,
            };
            _blendedAnimation.Add(animationA, 1);
            _blendedAnimation.Add(animationB, 0);
            _blendedAnimation.SynchronizeDurations();

            // Start blended animation.
            AnimationService.StartAnimation(_blendedAnimation, _animatableSprite).UpdateAndApply();
        }
Example #16
0
        public void CopyTo()
        {
            var blendGroup = new BlendGroup();
              ITimeline[] array = new ITimeline[0];
              Assert.That(() => ((IList<ITimeline>)blendGroup).CopyTo(null, 0), Throws.TypeOf<ArgumentNullException>());
              Assert.That(() => ((IList<ITimeline>)blendGroup).CopyTo(array, -1), Throws.TypeOf<ArgumentOutOfRangeException>());
              Assert.That(() => ((IList<ITimeline>)blendGroup).CopyTo(array, 1), Throws.ArgumentException);
              Assert.That(() => ((IList<ITimeline>)blendGroup).CopyTo(array, 0), Throws.Nothing);

              var animation0 = new SingleFromToByAnimation();
              var animation1 = new SingleFromToByAnimation();
              blendGroup = new BlendGroup { animation0, animation1 };

              array = new ITimeline[1];
              Assert.That(() => ((IList<ITimeline>)blendGroup).CopyTo(array, 0), Throws.ArgumentException);

              array = new ITimeline[2];
              ((IList<ITimeline>)blendGroup).CopyTo(array, 0);
              Assert.AreEqual(animation0, array[0]);
              Assert.AreEqual(animation1, array[1]);

              array = new ITimeline[3];
              ((IList<ITimeline>)blendGroup).CopyTo(array, 1);
              Assert.AreEqual(null, array[0]);
              Assert.AreEqual(animation0, array[1]);
              Assert.AreEqual(animation1, array[2]);
        }
Example #17
0
    public BlendedAnimationSample(Microsoft.Xna.Framework.Game game)
      : base(game)
    {
      Rectangle bounds = GraphicsService.GraphicsDevice.Viewport.TitleSafeArea;

      // Create the animatable object.
      _animatableSprite = new AnimatableSprite("SpriteA", SpriteBatch, Logo)
      {
        Position = new Vector2(bounds.Center.X, bounds.Center.Y / 2.0f),
        Color = Color.Red,
      };

      Vector2FromToByAnimation slowLeftRightAnimation = new Vector2FromToByAnimation
      {
        TargetProperty = "Position",
        From = new Vector2(bounds.Left + 100, bounds.Top + 200),
        To = new Vector2(bounds.Right - 100, bounds.Top + 200),
        Duration = TimeSpan.FromSeconds(4),
        EasingFunction = new HermiteEase { Mode = EasingMode.EaseInOut },
      };

      ColorKeyFrameAnimation redGreenAnimation = new ColorKeyFrameAnimation
      {
        TargetProperty = "Color",
        EnableInterpolation = true,
      };
      redGreenAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(0), Color.Red));
      redGreenAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(4), Color.Green));

      TimelineGroup animationA = new TimelineGroup
      {
        slowLeftRightAnimation,
        redGreenAnimation,
      };

      Vector2FromToByAnimation fastLeftRightAnimation = new Vector2FromToByAnimation
      {
        TargetProperty = "Position",
        From = new Vector2(bounds.Left + 100, bounds.Bottom - 200),
        To = new Vector2(bounds.Right - 100, bounds.Bottom - 200),
        Duration = TimeSpan.FromSeconds(1),
        EasingFunction = new HermiteEase { Mode = EasingMode.EaseInOut },
      };

      ColorKeyFrameAnimation blackWhiteAnimation = new ColorKeyFrameAnimation
      {
        TargetProperty = "Color",
        EnableInterpolation = true,
      };
      blackWhiteAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(0), Color.Black));
      blackWhiteAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(1), Color.White));

      TimelineGroup animationB = new TimelineGroup
      {
        fastLeftRightAnimation,
        blackWhiteAnimation,
      };

      // Create a BlendGroup that blends animationA and animationB. 
      // The BlendGroup uses the TargetProperty values of the contained animations to 
      // to match the animations that should be blended:
      //   slowLeftRightAnimation with fastLeftRightAnimation
      //   redGreenAnimation with blackWhiteAnimation
      _blendedAnimation = new BlendGroup
      {
        LoopBehavior = LoopBehavior.Oscillate,
        Duration = TimeSpan.MaxValue,
      };
      _blendedAnimation.Add(animationA, 1);
      _blendedAnimation.Add(animationB, 0);
      _blendedAnimation.SynchronizeDurations();

      // Start blended animation.
      AnimationService.StartAnimation(_blendedAnimation, _animatableSprite).UpdateAndApply();
    }
Example #18
0
 /// <summary>
 /// Creates an instance of the <see cref="BlendGroupInstance"/> class. (This method reuses a
 /// previously recycled instance or allocates a new instance if necessary.)
 /// </summary>
 /// <param name="blendGroup">The <see cref="BlendGroup"/> that should be played back.</param>
 /// <returns>
 /// A new or reusable instance of the <see cref="BlendGroupInstance"/> class.
 /// </returns>
 /// <remarks>
 /// <para>
 /// This method tries to obtain a previously recycled instance from a resource pool if resource
 /// pooling is enabled (see <see cref="ResourcePool.Enabled">ResourcePool.Enabled</see>). If no
 /// object is available, a new instance is automatically allocated on the heap. 
 /// </para>
 /// <para>
 /// The owner of the object should call <see cref="Recycle"/> when the instance is no longer 
 /// needed.
 /// </para>
 /// </remarks>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="blendGroup"/> is <see langword="null"/>.
 /// </exception>
 public static BlendGroupInstance Create(BlendGroup blendGroup)
 {
     var blendGroupInstance = Pool.Obtain();
       blendGroupInstance.Initialize(blendGroup);
       return blendGroupInstance;
 }
Example #19
0
        public void EmptyBlendGroup()
        {
            var property1 = new AnimatableProperty<float>();
              var blendGroup = new BlendGroup();

              Assert.That(() => blendGroup.GetWeight(0), Throws.TypeOf<ArgumentOutOfRangeException>());
              Assert.That(() => blendGroup.SetWeight(0, 1.0f), Throws.TypeOf<ArgumentOutOfRangeException>());

              var manager = new AnimationManager();
              var controller = manager.StartAnimation(blendGroup, property1);
              manager.Update(TimeSpan.Zero);
              manager.ApplyAnimations();
              Assert.AreEqual(AnimationState.Stopped, controller.State);

              blendGroup.SynchronizeDurations();
              blendGroup.Clear();
              controller = manager.StartAnimation(blendGroup, property1);
              manager.Update(TimeSpan.Zero);
              manager.ApplyAnimations();
              Assert.AreEqual(AnimationState.Stopped, controller.State);
        }
Example #20
0
        public void EnumeratorTest()
        {
            var blendGroup = new BlendGroup();
              Assert.AreEqual(0, blendGroup.Count());

              var animation0 = new SingleFromToByAnimation();
              var animation1 = new SingleFromToByAnimation();
              blendGroup = new BlendGroup { animation0, animation1 };
              Assert.AreEqual(2, blendGroup.Count());
              Assert.AreEqual(animation0, blendGroup.ElementAt(0));
              Assert.AreEqual(animation1, blendGroup.ElementAt(1));
        }
Example #21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AnimatableBlendWeight"/> class.
 /// </summary>
 /// <param name="blendGroup">The blend group.</param>
 /// <param name="weight">The blend weight.</param>
 public AnimatableBlendWeight(BlendGroup blendGroup, float weight)
 {
     Debug.Assert(blendGroup != null, "BlendGroup is null.");
     _blendGroup = blendGroup;
     _baseValue  = weight;
 }
Example #22
0
 //--------------------------------------------------------------
 /// <summary>
 /// Initializes the <see cref="BlendAnimation{T}"/> for specified number of animation.
 /// </summary>
 /// <param name="blendGroup">The <see cref="BlendGroup"/>.</param>
 /// <param name="targetProperty">The property to which the animation is applied.</param>
 internal virtual void Initialize(BlendGroup blendGroup, string targetProperty)
 {
     Group = blendGroup;
       TargetProperty = targetProperty;
 }