MenuEntry.cs 4.7 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. namespace NetworkStateManagement
  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. // When the menu selection changes, entries gradually fade between
  77. // their selected and deselected appearance, rather than instantly
  78. // popping to the new state.
  79. float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;
  80. if (isSelected)
  81. selectionFade = Math.Min(selectionFade + fadeSpeed, 1);
  82. else
  83. selectionFade = Math.Max(selectionFade - fadeSpeed, 0);
  84. }
  85. /// <summary>
  86. /// Draws the menu entry. This can be overridden to customize the appearance.
  87. /// </summary>
  88. public virtual void Draw(MenuScreen screen, bool isSelected, GameTime gameTime)
  89. {
  90. // Draw the selected entry in yellow, otherwise white.
  91. Color color = isSelected ? Color.Yellow : Color.White;
  92. // Pulsate the size of the selected menu entry.
  93. double time = gameTime.TotalGameTime.TotalSeconds;
  94. float pulsate = (float)Math.Sin(time * 6) + 1;
  95. float scale = 1 + pulsate * 0.05f * selectionFade;
  96. // Modify the alpha to fade text out during transitions.
  97. color *= screen.TransitionAlpha;
  98. // Draw text, centered on the middle of each line.
  99. ScreenManager screenManager = screen.ScreenManager;
  100. SpriteBatch spriteBatch = screenManager.SpriteBatch;
  101. SpriteFont font = screenManager.Font;
  102. Vector2 origin = new Vector2(0, font.LineSpacing / 2);
  103. spriteBatch.DrawString(font, text, position, color, 0,
  104. origin, scale, SpriteEffects.None, 0);
  105. }
  106. /// <summary>
  107. /// Queries how much space this menu entry requires.
  108. /// </summary>
  109. public virtual int GetHeight(MenuScreen screen)
  110. {
  111. return screen.ScreenManager.Font.LineSpacing;
  112. }
  113. /// <summary>
  114. /// Queries how wide the entry is, used for centering on the screen.
  115. /// </summary>
  116. public virtual int GetWidth(MenuScreen screen)
  117. {
  118. return (int)screen.ScreenManager.Font.MeasureString(Text).X;
  119. }
  120. }
  121. }