MenuEntry.cs 5.3 KB

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