2
0

MenuEntry.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. using Microsoft.Xna.Framework.Input;
  14. #endregion
  15. namespace GameStateManagement
  16. {
  17. /// <summary>
  18. /// Helper class represents a single entry in a MenuScreen. By default this
  19. /// just draws the entry text string, but it can be customized to display menu
  20. /// entries in different ways. This also provides an event that will be raised
  21. /// when the menu entry is selected.
  22. /// </summary>
  23. class MenuEntry
  24. {
  25. #region Fields
  26. /// <summary>
  27. /// The text rendered for this entry.
  28. /// </summary>
  29. string text;
  30. /// <summary>
  31. /// Tracks a fading selection effect on the entry.
  32. /// </summary>
  33. /// <remarks>
  34. /// The entries transition out of the selection effect when they are deselected.
  35. /// </remarks>
  36. float selectionFade;
  37. /// <summary>
  38. /// The position at which the entry is drawn. This is set by the MenuScreen
  39. /// each frame in Update.
  40. /// </summary>
  41. Vector2 position;
  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. #endregion
  61. #region Events
  62. /// <summary>
  63. /// Event raised when the menu entry is selected.
  64. /// </summary>
  65. public event EventHandler<PlayerIndexEventArgs> Selected;
  66. /// <summary>
  67. /// Method for raising the Selected event.
  68. /// </summary>
  69. protected internal virtual void OnSelectEntry(PlayerIndex playerIndex)
  70. {
  71. if (Selected != null)
  72. Selected(this, new PlayerIndexEventArgs(playerIndex));
  73. }
  74. #endregion
  75. #region Initialization
  76. /// <summary>
  77. /// Constructs a new menu entry with the specified text.
  78. /// </summary>
  79. public MenuEntry(string text)
  80. {
  81. this.text = text;
  82. }
  83. #endregion
  84. #region Update and Draw
  85. /// <summary>
  86. /// Updates the menu entry.
  87. /// </summary>
  88. public virtual void Update(MenuScreen screen, bool isSelected, GameTime gameTime)
  89. {
  90. // there is no such thing as a selected item on Windows Phone, so we always
  91. // force isSelected to be false
  92. #if WINDOWS_PHONE
  93. isSelected = false;
  94. #endif
  95. // When the menu selection changes, entries gradually fade between
  96. // their selected and deselected appearance, rather than instantly
  97. // popping to the new state.
  98. float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;
  99. if (isSelected)
  100. selectionFade = Math.Min(selectionFade + fadeSpeed, 1);
  101. else
  102. selectionFade = Math.Max(selectionFade - fadeSpeed, 0);
  103. }
  104. /// <summary>
  105. /// Draws the menu entry. This can be overridden to customize the appearance.
  106. /// </summary>
  107. public virtual void Draw(MenuScreen screen, bool isSelected, GameTime gameTime)
  108. {
  109. // there is no such thing as a selected item on Windows Phone, so we always
  110. // force isSelected to be false
  111. #if WINDOWS_PHONE
  112. isSelected = false;
  113. #endif
  114. // Draw the selected entry in yellow, otherwise white.
  115. Color color = isSelected ? Color.White : Color.Yellow;
  116. // Pulsate the size of the selected menu entry.
  117. double time = gameTime.TotalGameTime.TotalSeconds;
  118. float pulsate = (float)Math.Sin(time * 6) + 1;
  119. float scale = 1 + pulsate * 0.05f * selectionFade;
  120. // Modify the alpha to fade text out during transitions.
  121. color = new Color(color.R, color.G, color.B) * screen.TransitionAlpha;
  122. // Draw text, centered on the middle of each line.
  123. ScreenManager screenManager = screen.ScreenManager;
  124. SpriteBatch spriteBatch = screenManager.SpriteBatch;
  125. SpriteFont font = screenManager.Font;
  126. Vector2 origin = new Vector2(0, font.LineSpacing / 2);
  127. spriteBatch.DrawString(font, text, position + new Vector2(4, 4), Color.Black, 0,
  128. origin, scale, SpriteEffects.None, 0);
  129. spriteBatch.DrawString(font, text, position, color, 0,
  130. origin, scale, SpriteEffects.None, 0);
  131. }
  132. /// <summary>
  133. /// Queries how much space this menu entry requires.
  134. /// </summary>
  135. public virtual int GetHeight(MenuScreen screen)
  136. {
  137. return screen.ScreenManager.Font.LineSpacing;
  138. }
  139. /// <summary>
  140. /// Queries how wide the entry is, used for centering on the screen.
  141. /// </summary>
  142. public virtual int GetWidth(MenuScreen screen)
  143. {
  144. return (int)screen.ScreenManager.Font.MeasureString(Text).X;
  145. }
  146. #endregion
  147. }
  148. }