Button.cs 2.4 KB

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