Checkbox.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 On and Off content.
  16. /// Has a state of IsChecked that is switched by click.
  17. /// Draws content according to state.
  18. /// </summary>
  19. public class Checkbox : Clickable
  20. {
  21. readonly string asset;
  22. Texture2D textureOn;
  23. bool isChecked;
  24. public bool IsChecked
  25. {
  26. get { return isChecked; }
  27. set { isChecked = value; }
  28. }
  29. public Rectangle Bounds { get { return Rectangle; } }
  30. /// <summary>
  31. ///
  32. /// </summary>
  33. /// <param name="game">The Game object</param>
  34. /// <param name="textureName">Texture name</param>
  35. /// <param name="targetRectangle">Position of the component on the screen</param>
  36. /// <param name="isChecked">Initial state of the checkbox</param>
  37. public Checkbox(Graphics3DSampleGame game, string textureName, Rectangle targetRectangle, bool isChecked)
  38. : base(game, targetRectangle)
  39. {
  40. asset = textureName;
  41. this.isChecked = isChecked;
  42. }
  43. /// <summary>
  44. /// Load the texture
  45. /// </summary>
  46. protected override void LoadContent()
  47. {
  48. textureOn = Game.Content.Load<Texture2D>(asset);
  49. base.LoadContent();
  50. }
  51. /// <summary>
  52. /// Allows the game component to update itself.
  53. /// </summary>
  54. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  55. public override void Update(GameTime gameTime)
  56. {
  57. HandleInput();
  58. isChecked = IsClicked ? !isChecked : isChecked;
  59. base.Update(gameTime);
  60. }
  61. /// <summary>
  62. /// Allows the game component to update itself.
  63. /// </summary>
  64. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  65. public override void Draw(GameTime gameTime)
  66. {
  67. Game.SpriteBatch.Begin();
  68. Game.SpriteBatch.Draw(textureOn, Rectangle,
  69. IsChecked ? Color.Yellow : Color.White);
  70. Game.SpriteBatch.End();
  71. base.Draw(gameTime);
  72. }
  73. }
  74. }