示例#1
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);
        }
示例#2
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);
        }
示例#3
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));
        }
示例#4
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);
        }
示例#5
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);
        }
示例#6
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();
    }
示例#7
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));
        }
示例#8
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);
        }
示例#9
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();
        }