2
0

MenuEntry.cs 5.0 KB

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