Button.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Button.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. using System;
  10. using GameStateManagement;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Graphics;
  13. namespace GameStateManagementSample
  14. {
  15. /// <summary>
  16. /// A special button that handles toggling between "On" and "Off"
  17. /// </summary>
  18. class BooleanButton : Button
  19. {
  20. private string option;
  21. private bool value;
  22. /// <summary>
  23. /// Creates a new BooleanButton.
  24. /// </summary>
  25. /// <param name="option">The string text to display for the option.</param>
  26. /// <param name="value">The initial value of the button.</param>
  27. public BooleanButton(string option, bool value)
  28. : base(option)
  29. {
  30. this.option = option;
  31. this.value = value;
  32. GenerateText();
  33. }
  34. protected override void OnTapped()
  35. {
  36. // When tapped we need to toggle the value and regenerate the text
  37. value = !value;
  38. GenerateText();
  39. base.OnTapped();
  40. }
  41. /// <summary>
  42. /// Helper that generates the actual Text value the base class uses for drawing.
  43. /// </summary>
  44. private void GenerateText()
  45. {
  46. Text = string.Format("{0}: {1}", option, value ? "On" : "Off");
  47. }
  48. }
  49. /// <summary>
  50. /// Represents a touchable button.
  51. /// </summary>
  52. class Button
  53. {
  54. /// <summary>
  55. /// The text displayed in the button.
  56. /// </summary>
  57. public string Text = "Button";
  58. /// <summary>
  59. /// The position of the top-left corner of the button.
  60. /// </summary>
  61. public Vector2 Position = Vector2.Zero;
  62. /// <summary>
  63. /// The size of the button.
  64. /// </summary>
  65. public Vector2 Size = new Vector2(250, 75);
  66. /// <summary>
  67. /// The thickness of the border drawn for the button.
  68. /// </summary>
  69. public int BorderThickness = 4;
  70. /// <summary>
  71. /// The color of the button border.
  72. /// </summary>
  73. public Color BorderColor = new Color(200, 200, 200);
  74. /// <summary>
  75. /// The color of the button background.
  76. /// </summary>
  77. public Color FillColor = new Color(100, 100, 100) * .75f;
  78. /// <summary>
  79. /// The color of the text.
  80. /// </summary>
  81. public Color TextColor = Color.White;
  82. /// <summary>
  83. /// The opacity of the button.
  84. /// </summary>
  85. public float Alpha = 0f;
  86. /// <summary>
  87. /// Invoked when the button is tapped.
  88. /// </summary>
  89. public event EventHandler<EventArgs> Tapped;
  90. /// <summary>
  91. /// Creates a new Button.
  92. /// </summary>
  93. /// <param name="text">The text to display in the button.</param>
  94. public Button(string text)
  95. {
  96. Text = text;
  97. }
  98. /// <summary>
  99. /// Invokes the Tapped event and allows subclasses to perform actions when tapped.
  100. /// </summary>
  101. protected virtual void OnTapped()
  102. {
  103. if (Tapped != null)
  104. Tapped(this, EventArgs.Empty);
  105. }
  106. /// <summary>
  107. /// Passes a tap location to the button for handling.
  108. /// </summary>
  109. /// <param name="tap">The location of the tap.</param>
  110. /// <returns>True if the button was tapped, false otherwise.</returns>
  111. public bool HandleTap(Vector2 tap)
  112. {
  113. if (tap.X >= Position.X &&
  114. tap.Y >= Position.Y &&
  115. tap.X <= Position.X + Size.X &&
  116. tap.Y <= Position.Y + Size.Y)
  117. {
  118. OnTapped();
  119. return true;
  120. }
  121. return false;
  122. }
  123. /// <summary>
  124. /// Draws the button
  125. /// </summary>
  126. /// <param name="screen">The screen drawing the button</param>
  127. public void Draw(GameScreen screen)
  128. {
  129. // Grab some common items from the ScreenManager
  130. SpriteBatch spriteBatch = screen.ScreenManager.SpriteBatch;
  131. SpriteFont font = screen.ScreenManager.Font;
  132. Texture2D blank = screen.ScreenManager.BlankTexture;
  133. // Compute the button's rectangle
  134. Rectangle r = new Rectangle(
  135. (int)Position.X,
  136. (int)Position.Y,
  137. (int)Size.X,
  138. (int)Size.Y);
  139. // Fill the button
  140. spriteBatch.Draw(blank, r, FillColor * Alpha);
  141. // Draw the border
  142. spriteBatch.Draw(
  143. blank,
  144. new Rectangle(r.Left, r.Top, r.Width, BorderThickness),
  145. BorderColor * Alpha);
  146. spriteBatch.Draw(
  147. blank,
  148. new Rectangle(r.Left, r.Top, BorderThickness, r.Height),
  149. BorderColor * Alpha);
  150. spriteBatch.Draw(
  151. blank,
  152. new Rectangle(r.Right - BorderThickness, r.Top, BorderThickness, r.Height),
  153. BorderColor * Alpha);
  154. spriteBatch.Draw(
  155. blank,
  156. new Rectangle(r.Left, r.Bottom - BorderThickness, r.Width, BorderThickness),
  157. BorderColor * Alpha);
  158. // Draw the text centered in the button
  159. Vector2 textSize = font.MeasureString(Text);
  160. Vector2 textPosition = new Vector2(r.Center.X, r.Center.Y) - textSize / 2f;
  161. textPosition.X = (int)textPosition.X;
  162. textPosition.Y = (int)textPosition.Y;
  163. spriteBatch.DrawString(font, Text, textPosition, TextColor * Alpha);
  164. }
  165. }
  166. }