MenuEntry.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 GameStateManagement
  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. Rectangle destination;
  37. #endregion
  38. #region Properties
  39. /// <summary>
  40. /// Gets or sets the text of this menu entry.
  41. /// </summary>
  42. public string Text
  43. {
  44. get { return text; }
  45. set { text = value; }
  46. }
  47. /// <summary>
  48. /// Gets or sets the position at which to draw this menu entry.
  49. /// </summary>
  50. public Rectangle Destination
  51. {
  52. get { return destination; }
  53. set { destination = value; }
  54. }
  55. public float Scale { get; set; }
  56. public float Rotation { get; set; }
  57. #endregion
  58. #region Events
  59. /// <summary>
  60. /// Event raised when the menu entry is selected.
  61. /// </summary>
  62. public event EventHandler<PlayerIndexEventArgs> Selected;
  63. /// <summary>
  64. /// Method for raising the Selected event.
  65. /// </summary>
  66. protected internal virtual void OnSelectEntry(PlayerIndex playerIndex)
  67. {
  68. if (Selected != null)
  69. Selected(this, new PlayerIndexEventArgs(playerIndex));
  70. }
  71. #endregion
  72. #region Initialization
  73. /// <summary>
  74. /// Constructs a new menu entry with the specified text.
  75. /// </summary>
  76. public MenuEntry(string text)
  77. {
  78. this.text = text;
  79. Scale = 1f;
  80. Rotation = 0f;
  81. }
  82. #endregion
  83. #region Update and Draw
  84. /// <summary>
  85. /// Updates the menu entry.
  86. /// </summary>
  87. public virtual void Update(MenuScreen screen, bool isSelected, GameTime gameTime)
  88. {
  89. // there is no such thing as a selected item on Windows Phone, so we always
  90. // force isSelected to be false
  91. #if WINDOWS_PHONE || IOS || ANDROID
  92. isSelected = false;
  93. #endif
  94. // When the menu selection changes, entries gradually fade between
  95. // their selected and deselected appearance, rather than instantly
  96. // popping to the new state.
  97. float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;
  98. if (isSelected)
  99. selectionFade = Math.Min(selectionFade + fadeSpeed, 1);
  100. else
  101. selectionFade = Math.Max(selectionFade - fadeSpeed, 0);
  102. }
  103. /// <summary>
  104. /// Draws the menu entry. This can be overridden to customize the appearance.
  105. /// </summary>
  106. public virtual void Draw(MenuScreen screen, bool isSelected, GameTime gameTime)
  107. {
  108. Color textColor = isSelected ? Color.White : Color.Black;
  109. Color tintColor = isSelected ? Color.White : Color.Gray;
  110. #if WINDOWS_PHONE || IOS || ANDROID
  111. // there is no such thing as a selected item on Windows Phone, so we always
  112. // force isSelected to be false
  113. isSelected = false;
  114. tintColor = Color.White;
  115. textColor = Color.Black;
  116. #endif
  117. // Draw text, centered on the middle of each line.
  118. ScreenManager screenManager = screen.ScreenManager;
  119. SpriteBatch spriteBatch = screenManager.SpriteBatch;
  120. SpriteFont font = screenManager.Font;
  121. spriteBatch.Draw(screenManager.ButtonBackground, destination, tintColor);
  122. spriteBatch.DrawString(screenManager.Font, text, getTextPosition(screen),
  123. textColor, Rotation, Vector2.Zero, Scale, SpriteEffects.None, 0);
  124. }
  125. /// <summary>
  126. /// Queries how much space this menu entry requires.
  127. /// </summary>
  128. public virtual int GetHeight(MenuScreen screen)
  129. {
  130. return screen.ScreenManager.Font.LineSpacing;
  131. }
  132. /// <summary>
  133. /// Queries how wide the entry is, used for centering on the screen.
  134. /// </summary>
  135. public virtual int GetWidth(MenuScreen screen)
  136. {
  137. return (int)screen.ScreenManager.Font.MeasureString(Text).X;
  138. }
  139. private Vector2 getTextPosition(MenuScreen screen)
  140. {
  141. Vector2 textPosition = Vector2.Zero;
  142. if (Scale == 1f)
  143. {
  144. textPosition = new Vector2((int)destination.X + destination.Width / 2 - GetWidth(screen) / 2,
  145. (int)destination.Y);
  146. }
  147. else
  148. {
  149. textPosition = new Vector2(
  150. (int)destination.X + (destination.Width / 2 - ((GetWidth(screen) / 2) * Scale)),
  151. (int)destination.Y + (GetHeight(screen) - GetHeight(screen) * Scale) / 2);
  152. }
  153. return textPosition;
  154. }
  155. #endregion
  156. }
  157. }