MenuEntry.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //-----------------------------------------------------------------------------
  2. // MenuEntry.cs
  3. //
  4. // XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using Microsoft.Xna.Framework;
  9. using Microsoft.Xna.Framework.Graphics;
  10. namespace GameStateManagement
  11. {
  12. /// <summary>
  13. /// Helper class represents a single entry in a MenuScreen. By default this
  14. /// just draws the entry text string, but it can be customized to display menu
  15. /// entries in different ways. This also provides an event that will be raised
  16. /// when the menu entry is selected.
  17. /// </summary>
  18. class MenuEntry
  19. {
  20. /// <summary>
  21. /// The text rendered for this entry.
  22. /// </summary>
  23. string text;
  24. /// <summary>
  25. /// Tracks a fading selection effect on the entry.
  26. /// </summary>
  27. /// <remarks>
  28. /// The entries transition out of the selection effect when they are deselected.
  29. /// </remarks>
  30. float selectionFade;
  31. /// <summary>
  32. /// The position at which the entry is drawn. This is set by the MenuScreen
  33. /// each frame in Update.
  34. /// </summary>
  35. Vector2 position;
  36. /// <summary>
  37. /// Gets or sets the text of this menu entry.
  38. /// </summary>
  39. public string Text
  40. {
  41. get { return text; }
  42. set { text = value; }
  43. }
  44. /// <summary>
  45. /// Gets or sets the position at which to draw this menu entry.
  46. /// </summary>
  47. public Vector2 Position
  48. {
  49. get { return position; }
  50. set { position = value; }
  51. }
  52. /// <summary>
  53. /// Event raised when the menu entry is selected.
  54. /// </summary>
  55. public event EventHandler<PlayerIndexEventArgs> Selected;
  56. /// <summary>
  57. /// Method for raising the Selected event.
  58. /// </summary>
  59. protected internal virtual void OnSelectEntry(PlayerIndex playerIndex)
  60. {
  61. if (Selected != null)
  62. Selected(this, new PlayerIndexEventArgs(playerIndex));
  63. }
  64. /// <summary>
  65. /// Constructs a new menu entry with the specified text.
  66. /// </summary>
  67. public MenuEntry(string text)
  68. {
  69. this.text = text;
  70. }
  71. /// <summary>
  72. /// Updates the menu entry.
  73. /// </summary>
  74. public virtual void Update(MenuScreen screen, bool isSelected, GameTime gameTime)
  75. {
  76. // there is no such thing as a selected item on Windows Phone, so we always
  77. // force isSelected to be false
  78. #if WINDOWS_PHONE
  79. isSelected = false;
  80. #endif
  81. // When the menu selection changes, entries gradually fade between
  82. // their selected and deselected appearance, rather than instantly
  83. // popping to the new state.
  84. float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;
  85. if (isSelected)
  86. selectionFade = Math.Min(selectionFade + fadeSpeed, 1);
  87. else
  88. selectionFade = Math.Max(selectionFade - fadeSpeed, 0);
  89. }
  90. /// <summary>
  91. /// Draws the menu entry. This can be overridden to customize the appearance.
  92. /// </summary>
  93. public virtual void Draw(MenuScreen screen, bool isSelected, GameTime gameTime)
  94. {
  95. // there is no such thing as a selected item on Windows Phone, so we always
  96. // force isSelected to be false
  97. #if WINDOWS_PHONE
  98. isSelected = false;
  99. #endif
  100. // Draw the selected entry in yellow, otherwise white.
  101. Color color = isSelected ? Color.Yellow : Color.White;
  102. // Pulsate the size of the selected menu entry.
  103. double time = gameTime.TotalGameTime.TotalSeconds;
  104. float pulsate = (float)Math.Sin(time * 6) + 1;
  105. float scale = 1 + pulsate * 0.05f * selectionFade;
  106. // Modify the alpha to fade text out during transitions.
  107. color *= screen.TransitionAlpha;
  108. // Draw text, centered on the middle of each line.
  109. ScreenManager screenManager = screen.ScreenManager;
  110. SpriteBatch spriteBatch = screenManager.SpriteBatch;
  111. SpriteFont font = screenManager.Font;
  112. Vector2 origin = new Vector2(0, font.LineSpacing / 2);
  113. spriteBatch.DrawString(font, text, position, color, 0,
  114. origin, scale, SpriteEffects.None, 0);
  115. }
  116. /// <summary>
  117. /// Queries how much space this menu entry requires.
  118. /// </summary>
  119. public virtual int GetHeight(MenuScreen screen)
  120. {
  121. return screen.ScreenManager.Font.LineSpacing;
  122. }
  123. /// <summary>
  124. /// Queries how wide the entry is, used for centering on the screen.
  125. /// </summary>
  126. public virtual int GetWidth(MenuScreen screen)
  127. {
  128. return (int)screen.ScreenManager.Font.MeasureString(Text).X;
  129. }
  130. }
  131. }