MenuEntry.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #if IPHONE
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Graphics;
  14. #else
  15. using Microsoft.Xna.Framework;
  16. using Microsoft.Xna.Framework.Graphics;
  17. #endif
  18. #endregion
  19. namespace Microsoft.Xna.Samples.GameStateManagement
  20. {
  21. /// <summary>
  22. /// Helper class represents a single entry in a MenuScreen. By default this
  23. /// just draws the entry text string, but it can be customized to display menu
  24. /// entries in different ways. This also provides an event that will be raised
  25. /// when the menu entry is selected.
  26. /// </summary>
  27. class MenuEntry
  28. {
  29. #region Fields
  30. /// <summary>
  31. /// The text rendered for this entry.
  32. /// </summary>
  33. string text;
  34. /// <summary>
  35. /// Tracks a fading selection effect on the entry.
  36. /// </summary>
  37. /// <remarks>
  38. /// The entries transition out of the selection effect when they are deselected.
  39. /// </remarks>
  40. float selectionFade;
  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. #endregion
  52. #region Events
  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. #endregion
  66. #region Initialization
  67. /// <summary>
  68. /// Constructs a new menu entry with the specified text.
  69. /// </summary>
  70. public MenuEntry(string text)
  71. {
  72. this.text = text;
  73. }
  74. #endregion
  75. #region Update and Draw
  76. /// <summary>
  77. /// Updates the menu entry.
  78. /// </summary>
  79. public virtual void Update(MenuScreen screen, bool isSelected,
  80. GameTime gameTime)
  81. {
  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, Vector2 position,
  95. bool isSelected, GameTime gameTime)
  96. {
  97. // Draw the selected entry in yellow, otherwise white.
  98. Color color = isSelected ? Color.Yellow : Color.White;
  99. // Pulsate the size of the selected menu entry.
  100. double time = gameTime.TotalGameTime.TotalSeconds;
  101. float pulsate = (float)Math.Sin(time * 6) + 1;
  102. float scale = 1 + pulsate * 0.05f * selectionFade;
  103. // Modify the alpha to fade text out during transitions.
  104. color = new Color(color.R, color.G, color.B, screen.TransitionAlpha);
  105. // Draw text, centered on the middle of each line.
  106. ScreenManager screenManager = screen.ScreenManager;
  107. SpriteBatch spriteBatch = screenManager.SpriteBatch;
  108. SpriteFont font = screenManager.Font;
  109. Vector2 origin = new Vector2(0, font.LineSpacing / 2);
  110. spriteBatch.DrawString(font, text, position, color, 0,
  111. origin, scale, SpriteEffects.None, 0);
  112. }
  113. /// <summary>
  114. /// Queries how much space this menu entry requires.
  115. /// </summary>
  116. public virtual int GetHeight(MenuScreen screen)
  117. {
  118. return screen.ScreenManager.Font.LineSpacing;
  119. }
  120. #endregion
  121. }
  122. }