MenuEntry.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 UserInterfaceSample
  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. #region Fields
  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. #endregion
  38. #region Properties
  39. /// <summary>
  40. /// Gets or sets the text of this menu entry.
  41. /// </summary>
  42. public string Text
  43. {
  44. get { return text; }
  45. set { text = value; }
  46. }
  47. /// <summary>
  48. /// Gets or sets the position at which to draw this menu entry.
  49. /// </summary>
  50. public Vector2 Position
  51. {
  52. get { return position; }
  53. set { position = value; }
  54. }
  55. #endregion
  56. #region Events
  57. /// <summary>
  58. /// Event raised when the menu entry is selected.
  59. /// </summary>
  60. public event EventHandler<PlayerIndexEventArgs> Selected;
  61. /// <summary>
  62. /// Method for raising the Selected event.
  63. /// </summary>
  64. protected internal virtual void OnSelectEntry(PlayerIndex playerIndex)
  65. {
  66. if (Selected != null)
  67. Selected(this, new PlayerIndexEventArgs(playerIndex));
  68. }
  69. #endregion
  70. #region Initialization
  71. /// <summary>
  72. /// Constructs a new menu entry with the specified text.
  73. /// </summary>
  74. public MenuEntry(string text)
  75. {
  76. this.text = text;
  77. }
  78. #endregion
  79. #region Update and Draw
  80. /// <summary>
  81. /// Updates the menu entry.
  82. /// </summary>
  83. public virtual void Update(MenuScreen screen, bool isSelected, GameTime gameTime)
  84. {
  85. // there is no such thing as a selected item on Windows Phone, so we always
  86. // force isSelected to be false
  87. #if WINDOWS_PHONE
  88. isSelected = false;
  89. #endif
  90. // When the menu selection changes, entries gradually fade between
  91. // their selected and deselected appearance, rather than instantly
  92. // popping to the new state.
  93. float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;
  94. if (isSelected)
  95. selectionFade = Math.Min(selectionFade + fadeSpeed, 1);
  96. else
  97. selectionFade = Math.Max(selectionFade - fadeSpeed, 0);
  98. }
  99. /// <summary>
  100. /// Draws the menu entry. This can be overridden to customize the appearance.
  101. /// </summary>
  102. public virtual void Draw(MenuScreen screen, bool isSelected, GameTime gameTime)
  103. {
  104. // there is no such thing as a selected item on Windows Phone, so we always
  105. // force isSelected to be false
  106. #if WINDOWS_PHONE
  107. isSelected = false;
  108. #endif
  109. // Draw the selected entry in yellow, otherwise white.
  110. Color color = isSelected ? Color.Yellow : Color.White;
  111. // Pulsate the size of the selected menu entry.
  112. double time = gameTime.TotalGameTime.TotalSeconds;
  113. float pulsate = (float)Math.Sin(time * 6) + 1;
  114. float scale = 1 + pulsate * 0.05f * selectionFade;
  115. // Modify the alpha to fade text out during transitions.
  116. color = new Color(color.R, color.G, color.B) * screen.TransitionAlpha;
  117. // Draw text, centered on the middle of each line.
  118. ScreenManager screenManager = screen.ScreenManager;
  119. SpriteBatch spriteBatch = screenManager.SpriteBatch;
  120. SpriteFont font = screenManager.Font;
  121. Vector2 origin = new Vector2(0, font.LineSpacing / 2);
  122. spriteBatch.DrawString(font, text, position, color, 0,
  123. origin, scale, SpriteEffects.None, 0);
  124. }
  125. /// <summary>
  126. /// Queries how much space this menu entry requires.
  127. /// </summary>
  128. public virtual int GetHeight(MenuScreen screen)
  129. {
  130. return screen.ScreenManager.Font.LineSpacing;
  131. }
  132. /// <summary>
  133. /// Queries how wide the entry is, used for centering on the screen.
  134. /// </summary>
  135. public virtual int GetWidth(MenuScreen screen)
  136. {
  137. return (int)screen.ScreenManager.Font.MeasureString(Text).X;
  138. }
  139. #endregion
  140. }
  141. }