Checkbox.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 On and Off content.
  17. /// Has a state of IsChecked that is switched by click.
  18. /// Draws content according to state.
  19. /// </summary>
  20. public class Checkbox : Clickable
  21. {
  22. #region Fields
  23. readonly string asset;
  24. Texture2D textureOn;
  25. bool isChecked;
  26. #region Public accessors
  27. public bool IsChecked { get { return isChecked; } }
  28. #endregion
  29. #endregion
  30. #region Initialization
  31. /// <summary>
  32. ///
  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="isChecked">Initial state of the checkbox</param>
  38. public Checkbox(Graphics3DSampleGame game, string textureName, Rectangle targetRectangle, bool isChecked)
  39. : base(game, targetRectangle)
  40. {
  41. asset = textureName;
  42. this.isChecked = isChecked;
  43. }
  44. /// <summary>
  45. /// Load the texture
  46. /// </summary>
  47. protected override void LoadContent()
  48. {
  49. textureOn = 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. isChecked = IsClicked ? !isChecked : isChecked;
  62. base.Update(gameTime);
  63. }
  64. /// <summary>
  65. /// Allows the game component to update itself.
  66. /// </summary>
  67. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  68. public override void Draw(GameTime gameTime)
  69. {
  70. Game.SpriteBatch.Begin();
  71. Game.SpriteBatch.Draw(textureOn, Rectangle,
  72. IsChecked ? Color.Yellow : Color.White);
  73. Game.SpriteBatch.End();
  74. base.Draw(gameTime);
  75. }
  76. #endregion
  77. }
  78. }