2
0

MenuEntry.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. /// <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. #endregion
  42. #region Properties
  43. /// <summary>
  44. /// Gets or sets the text of this menu entry.
  45. /// </summary>
  46. public string Text
  47. {
  48. get { return text; }
  49. set { text = value; }
  50. }
  51. /// <summary>
  52. /// Gets or sets the position at which to draw this menu entry.
  53. /// </summary>
  54. public Vector2 Position
  55. {
  56. get { return position; }
  57. set { position = value; }
  58. }
  59. #endregion
  60. #region Events
  61. /// <summary>
  62. /// Event raised when the menu entry is selected.
  63. /// </summary>
  64. public event EventHandler<PlayerIndexEventArgs> Selected;
  65. /// <summary>
  66. /// Method for raising the Selected event.
  67. /// </summary>
  68. protected internal virtual void OnSelectEntry(PlayerIndex playerIndex)
  69. {
  70. if (Selected != null)
  71. Selected(this, new PlayerIndexEventArgs(playerIndex));
  72. }
  73. #endregion
  74. #region Initialization
  75. /// <summary>
  76. /// Constructs a new menu entry with the specified text.
  77. /// </summary>
  78. public MenuEntry(string text)
  79. {
  80. this.text = text;
  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
  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. // there is no such thing as a selected item on Windows Phone, so we always
  109. // force isSelected to be false
  110. #if WINDOWS_PHONE
  111. isSelected = false;
  112. #endif
  113. // Draw the selected entry in yellow, otherwise white.
  114. Color color = isSelected ? Color.Yellow : Color.White;
  115. // Pulsate the size of the selected menu entry.
  116. double time = gameTime.TotalGameTime.TotalSeconds;
  117. float pulsate = (float)Math.Sin(time * 6) + 1;
  118. float scale = 1 + pulsate * 0.05f * selectionFade;
  119. // Modify the alpha to fade text out during transitions.
  120. color *= screen.TransitionAlpha;
  121. // Draw text, centered on the middle of each line.
  122. ScreenManager screenManager = screen.ScreenManager;
  123. SpriteBatch spriteBatch = screenManager.SpriteBatch;
  124. SpriteFont font = screenManager.Font;
  125. Vector2 origin = new Vector2(0, font.LineSpacing / 2);
  126. spriteBatch.DrawString(font, text, position, color, 0,
  127. origin, scale, SpriteEffects.None, 0);
  128. }
  129. /// <summary>
  130. /// Queries how much space this menu entry requires.
  131. /// </summary>
  132. public virtual int GetHeight(MenuScreen screen)
  133. {
  134. return screen.ScreenManager.Font.LineSpacing;
  135. }
  136. /// <summary>
  137. /// Queries how wide the entry is, used for centering on the screen.
  138. /// </summary>
  139. public virtual int GetWidth(MenuScreen screen)
  140. {
  141. return (int)screen.ScreenManager.Font.MeasureString(Text).X;
  142. }
  143. #endregion
  144. }
  145. }