Button.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11. using Microsoft.Xna.Framework.Input.Touch;
  12. namespace Graphics3DSample
  13. {
  14. /// <summary>
  15. /// A game component, inherits to Clickable.
  16. /// Has associated content.
  17. /// Has an integer Value that is incremented by click.
  18. /// Draws content.
  19. /// </summary>
  20. public class Button : Clickable
  21. {
  22. #region Fields
  23. readonly string asset;
  24. Texture2D texture;
  25. int value;
  26. #region Public accessors
  27. public int Value { get { return value; } }
  28. #endregion
  29. #endregion
  30. #region Initialization
  31. /// <summary>
  32. /// Constructor
  33. /// </summary>
  34. /// <param name="game">The Game object</param>
  35. /// <param name="textureName">Texture Name</param>
  36. /// <param name="targetRectangle">Position of the component on the screen</param>
  37. /// <param name="initialValue">Initial value</param>
  38. public Button (Graphics3DSampleGame game, string textureName, Rectangle targetRectangle, int initialValue)
  39. : base(game, targetRectangle)
  40. {
  41. asset = textureName;
  42. value = initialValue;
  43. }
  44. /// <summary>
  45. /// Load the button's texture
  46. /// </summary>
  47. protected override void LoadContent()
  48. {
  49. texture = Game.Content.Load<Texture2D>(asset);
  50. base.LoadContent();
  51. }
  52. #endregion
  53. #region Update and render
  54. /// <summary>
  55. /// Allows the game component to update itself
  56. /// </summary>
  57. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  58. public override void Update(GameTime gameTime)
  59. {
  60. HandleInput();
  61. if (IsClicked)
  62. ++value;
  63. base.Update(gameTime);
  64. }
  65. /// <summary>
  66. /// Allows the game component to update itself.
  67. /// </summary>
  68. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  69. public override void Draw(GameTime gameTime)
  70. {
  71. var color = IsTouching ? Color.Wheat : Color.White;
  72. Game.SpriteBatch.Begin();
  73. Game.SpriteBatch.Draw(texture, Rectangle, color);
  74. Game.SpriteBatch.End();
  75. base.Draw(gameTime);
  76. }
  77. #endregion
  78. }
  79. }