2
0

MenuEntry.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // MenuEntry.cs
  4. //
  5. // XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Graphics;
  13. #endregion
  14. namespace HoneycombRush
  15. {
  16. /// <summary>
  17. /// Helper class represents a single entry in a MenuScreen. By default this
  18. /// just draws the entry text string, but it can be customized to display menu
  19. /// entries in different ways. This also provides an event that will be raised
  20. /// when the menu entry is selected.
  21. /// </summary>
  22. class MenuEntry
  23. {
  24. #region Fields
  25. /// <summary>
  26. /// The text rendered for this entry.
  27. /// </summary>
  28. string text;
  29. /// <summary>
  30. /// Tracks a fading selection effect on the entry.
  31. /// </summary>
  32. /// <remarks>
  33. /// The entries transition out of the selection effect when they are deselected.
  34. /// </remarks>
  35. float selectionFade;
  36. /// <summary>
  37. /// The position at which the entry is drawn. This is set by the MenuScreen
  38. /// each frame in Update.
  39. /// </summary>
  40. Vector2 position;
  41. Texture2D buttonTexture;
  42. #endregion
  43. #region Properties
  44. /// <summary>
  45. /// Gets or sets the text of this menu entry.
  46. /// </summary>
  47. public string Text
  48. {
  49. get { return text; }
  50. set { text = value; }
  51. }
  52. /// <summary>
  53. /// Gets or sets the position at which to draw this menu entry.
  54. /// </summary>
  55. public Vector2 Position
  56. {
  57. get { return position; }
  58. set { position = value; }
  59. }
  60. public Rectangle Bounds
  61. {
  62. get
  63. {
  64. return new Rectangle((int)position.X, (int)position.Y,
  65. buttonTexture.Width, buttonTexture.Height);
  66. }
  67. }
  68. public float Scale { get; set; }
  69. public float Rotation{ get; set; }
  70. #endregion
  71. #region Events
  72. /// <summary>
  73. /// Event raised when the menu entry is selected.
  74. /// </summary>
  75. public event EventHandler<PlayerIndexEventArgs> Selected;
  76. /// <summary>
  77. /// Method for raising the Selected event.
  78. /// </summary>
  79. protected internal virtual void OnSelectEntry(PlayerIndex playerIndex)
  80. {
  81. if (Selected != null)
  82. Selected(this, new PlayerIndexEventArgs(playerIndex));
  83. }
  84. #endregion
  85. #region Initialization
  86. /// <summary>
  87. /// Constructs a new menu entry with the specified text.
  88. /// </summary>
  89. public MenuEntry(string text)
  90. {
  91. this.text = text;
  92. Scale = 1f;
  93. Rotation = 0f;
  94. }
  95. #endregion
  96. #region Update and Draw
  97. /// <summary>
  98. /// Updates the menu entry.
  99. /// </summary>
  100. public virtual void Update(MenuScreen screen, bool isSelected, GameTime gameTime)
  101. {
  102. // there is no such thing as a selected item on Windows Phone, so we always
  103. // force isSelected to be false
  104. #if WINDOWS_PHONE
  105. isSelected = false;
  106. #endif
  107. // When the menu selection changes, entries gradually fade between
  108. // their selected and deselected appearance, rather than instantly
  109. // popping to the new state.
  110. float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;
  111. if (isSelected)
  112. selectionFade = Math.Min(selectionFade + fadeSpeed, 1);
  113. else
  114. selectionFade = Math.Max(selectionFade - fadeSpeed, 0);
  115. }
  116. /// <summary>
  117. /// Draws the menu entry. This can be overridden to customize the appearance.
  118. /// </summary>
  119. public virtual void Draw(MenuScreen screen, bool isSelected, GameTime gameTime)
  120. {
  121. Color textColor = isSelected ? Color.White : Color.Black;
  122. Color tintColor = isSelected ? Color.White : Color.Gray;
  123. #if WINDOWS_PHONE
  124. // there is no such thing as a selected item on Windows Phone, so we always
  125. // force isSelected to be false
  126. isSelected = false;
  127. tintColor = Color.White;
  128. textColor = Color.Black;
  129. #endif
  130. // Draw text, centered on the middle of each line.
  131. ScreenManager screenManager = screen.ScreenManager;
  132. SpriteBatch spriteBatch = screenManager.SpriteBatch;
  133. SpriteFont font = screenManager.Font;
  134. buttonTexture = screenManager.ButtonBackground;
  135. spriteBatch.Draw(buttonTexture, new Vector2((int)position.X, (int)position.Y), tintColor);
  136. spriteBatch.DrawString(screenManager.Font, text, getTextPosition(screen),
  137. textColor, Rotation, Vector2.Zero, Scale, SpriteEffects.None, 0);
  138. }
  139. /// <summary>
  140. /// Queries how much space this menu entry requires.
  141. /// </summary>
  142. public virtual int GetHeight(MenuScreen screen)
  143. {
  144. return (int)screen.ScreenManager.Font.MeasureString(Text).Y;
  145. }
  146. /// <summary>
  147. /// Queries how wide the entry is, used for centering on the screen.
  148. /// </summary>
  149. public virtual int GetWidth(MenuScreen screen)
  150. {
  151. return (int)screen.ScreenManager.Font.MeasureString(Text).X;
  152. }
  153. private Vector2 getTextPosition(MenuScreen screen)
  154. {
  155. Vector2 textPosition = Vector2.Zero;
  156. if (Scale == 1f)
  157. {
  158. textPosition = new Vector2((int)position.X + buttonTexture.Width / 2 - GetWidth(screen) / 2,
  159. (int)position.Y);
  160. }
  161. else
  162. {
  163. textPosition = new Vector2(
  164. (int)position.X + (buttonTexture.Width / 2 - ((GetWidth(screen) / 2)* Scale)),
  165. (int)position.Y + (GetHeight(screen) - GetHeight(screen) * Scale) / 2);
  166. }
  167. return textPosition;
  168. }
  169. #endregion
  170. }
  171. }