Button.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #region File Information
  2. //-----------------------------------------------------------------------------
  3. // Button.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using Microsoft.Xna.Framework.Graphics;
  15. using Microsoft.Xna.Framework;
  16. using Microsoft.Xna.Framework.Content;
  17. using Microsoft.Xna.Framework.Input.Touch;
  18. #endregion
  19. namespace DynamicMenu.Controls
  20. {
  21. /// <summary>
  22. /// A control which reads touch input within its bounds and fires a tapped event when touched
  23. /// </summary>
  24. public class Button : TextControl
  25. {
  26. #region Fields
  27. //Keep a button pressed for this long in seconds before executing the event
  28. private const double PRESS_TIME_SECONDS = .2;
  29. private double pressStartTime = 0;
  30. #endregion
  31. #region Events
  32. /// <summary>
  33. /// Indicates that the button was tapped
  34. /// </summary>
  35. public event EventHandler Tapped;
  36. #endregion
  37. #region Properties
  38. /// <summary>
  39. /// The name of the texture that is shown when this button is pressed
  40. /// </summary>
  41. [ContentSerializer(Optional = true)]
  42. public string PressedTextureName { get; set; }
  43. /// <summary>
  44. /// The texture that is shown when this button is pressed
  45. /// </summary>
  46. [ContentSerializerIgnore]
  47. public Texture2D PressedTexture { get; set; }
  48. /// <summary>
  49. /// Whether the button is pressed
  50. /// </summary>
  51. [ContentSerializerIgnore]
  52. public bool Pressed { get; set; }
  53. #endregion
  54. #region Initialization
  55. /// <summary>
  56. /// Loads this control's content
  57. /// </summary>
  58. public override void LoadContent(GraphicsDevice _graphics, ContentManager _content)
  59. {
  60. base.LoadContent(_graphics, _content);
  61. if (!string.IsNullOrEmpty(PressedTextureName))
  62. {
  63. PressedTexture = _content.Load<Texture2D>(PressedTextureName);
  64. }
  65. }
  66. #endregion
  67. #region Update
  68. /// <summary>
  69. /// Updates this control
  70. /// </summary>
  71. public override void Update(GameTime gameTime, List<GestureSample> gestures)
  72. {
  73. base.Update(gameTime, gestures);
  74. foreach (GestureSample sample in gestures)
  75. {
  76. if (sample.GestureType != GestureType.Tap) continue;
  77. if (ContainsPos(sample.Position))
  78. {
  79. Pressed = true;
  80. pressStartTime = gameTime.TotalGameTime.TotalSeconds;
  81. HandlePressed();
  82. break;
  83. }
  84. }
  85. if (Pressed && (gameTime.TotalGameTime.TotalSeconds > pressStartTime + PRESS_TIME_SECONDS))
  86. {
  87. Pressed = false;
  88. if (Tapped != null)
  89. {
  90. Tapped(this, new EventArgs());
  91. }
  92. }
  93. }
  94. #endregion
  95. #region Draw
  96. /// <summary>
  97. /// Draws this control
  98. /// </summary>
  99. public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
  100. {
  101. base.Draw(gameTime, spriteBatch);
  102. DrawCenteredText(spriteBatch, Font, GetAbsoluteRect(), Text, TextColor);
  103. }
  104. #endregion
  105. #region Methods
  106. /// <summary>
  107. /// Gets the current texture based on whether the button is pressed
  108. /// </summary>
  109. public override Texture2D GetCurrTexture()
  110. {
  111. if (Pressed && PressedTexture != null)
  112. {
  113. return PressedTexture;
  114. }
  115. return base.GetCurrTexture();
  116. }
  117. protected virtual void HandlePressed()
  118. {
  119. // Can be overriden to do special processing when this is clicked
  120. }
  121. #endregion
  122. }
  123. }