ScaleGameComponentAnimation.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // ScaleGameComponentAnimation.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Graphics;
  15. using System.Diagnostics;
  16. #endregion
  17. namespace CardsFramework
  18. {
  19. /// <summary>
  20. /// An animation which scales a component.
  21. /// </summary>
  22. public class ScaleGameComponentAnimation : AnimatedGameComponentAnimation
  23. {
  24. #region Fields
  25. float percent = 0;
  26. float beginFactor;
  27. float factorDelta;
  28. #endregion
  29. #region Initialzations
  30. /// <summary>
  31. /// Initializes a new instance of the class.
  32. /// </summary>
  33. /// <param name="beginFactor">The initial scale factor.</param>
  34. /// <param name="endFactor">The eventual scale factor.</param>
  35. public ScaleGameComponentAnimation(float beginFactor, float endFactor)
  36. {
  37. this.beginFactor = beginFactor;
  38. factorDelta = endFactor - beginFactor;
  39. }
  40. #endregion
  41. /// <summary>
  42. /// Runs the scaling animation.
  43. /// </summary>
  44. /// <param name="gameTime">Game time information.</param>
  45. public override void Run(GameTime gameTime)
  46. {
  47. Texture2D texture;
  48. if (IsStarted())
  49. {
  50. texture = Component.CurrentFrame;
  51. if (texture != null)
  52. {
  53. // Calculate the completion percent of animation
  54. percent += (float)(gameTime.ElapsedGameTime.TotalSeconds /
  55. Duration.TotalSeconds);
  56. // Inflate the component with an increasing delta. The eventual
  57. // delta will have the componenet scale to the specified target
  58. // scaling factor.
  59. Rectangle bounds = texture.Bounds;
  60. bounds.X = (int)Component.CurrentPosition.X;
  61. bounds.Y = (int)Component.CurrentPosition.Y;
  62. float currentFactor = beginFactor + factorDelta * percent - 1;
  63. bounds.Inflate((int)(bounds.Width * currentFactor),
  64. (int)(bounds.Height * currentFactor));
  65. Component.CurrentDestination = bounds;
  66. }
  67. }
  68. }
  69. }
  70. }