2
0

ImageComponent.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #region Using Statements
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. #endregion
  5. namespace RockRainIphone.Core
  6. {
  7. /// <summary>
  8. /// This is a game component that draw a image.
  9. /// </summary>
  10. public class ImageComponent : DrawableGameComponent
  11. {
  12. public enum DrawMode
  13. {
  14. Center = 1,
  15. Stretch,
  16. } ;
  17. // Texture to draw
  18. protected readonly Texture2D texture;
  19. // Draw Mode
  20. protected readonly DrawMode drawMode;
  21. // Spritebatch
  22. protected SpriteBatch spriteBatch = null;
  23. // Image Rectangle
  24. protected Rectangle imageRect;
  25. /// <summary>
  26. /// Default constructor
  27. /// </summary>
  28. /// <param name="game">The game object</param>
  29. /// <param name="texture">Texture to Draw</param>
  30. /// <param name="drawMode">Draw Mode</param>
  31. public ImageComponent(Game game, Texture2D texture, DrawMode drawMode)
  32. : base(game)
  33. {
  34. this.texture = texture;
  35. this.drawMode = drawMode;
  36. // Get the current spritebatch
  37. spriteBatch = (SpriteBatch)
  38. Game.Services.GetService(typeof (SpriteBatch));
  39. // Create a rectangle with the size and position of the image
  40. switch (drawMode)
  41. {
  42. case DrawMode.Center:
  43. imageRect = new Rectangle((Game.Window.ClientBounds.Width -
  44. texture.Width)/2,(Game.Window.ClientBounds.Height -
  45. texture.Height)/2,texture.Width, texture.Height);
  46. break;
  47. case DrawMode.Stretch:
  48. imageRect = new Rectangle(0, 0, Game.Window.ClientBounds.Width,
  49. Game.Window.ClientBounds.Height);
  50. break;
  51. }
  52. }
  53. /// <summary>
  54. /// Allows the game component to draw itself.
  55. /// </summary>
  56. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  57. public override void Draw(GameTime gameTime)
  58. {
  59. spriteBatch.Draw(texture, imageRect, Color.White);
  60. base.Draw(gameTime);
  61. }
  62. }
  63. }