MenuEntry.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 AlienGameSample
  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. /// Gets or sets the text of this menu entry.
  33. /// </summary>
  34. public string Text
  35. {
  36. get { return text; }
  37. set { text = value; }
  38. }
  39. /// <summary>
  40. /// Event raised when the menu entry is selected.
  41. /// </summary>
  42. public event EventHandler<EventArgs> Selected;
  43. /// <summary>
  44. /// Method for raising the Selected event.
  45. /// </summary>
  46. protected internal virtual void OnSelectEntry()
  47. {
  48. if (Selected != null)
  49. Selected(this, EventArgs.Empty);
  50. }
  51. /// <summary>
  52. /// Constructs a new menu entry with the specified text.
  53. /// </summary>
  54. public MenuEntry(string text)
  55. {
  56. this.text = text;
  57. }
  58. /// <summary>
  59. /// Updates the menu entry.
  60. /// </summary>
  61. public virtual void Update(MenuScreen screen, bool isSelected,
  62. GameTime gameTime)
  63. {
  64. // When the menu selection changes, entries gradually fade between
  65. // their selected and deselected appearance, rather than instantly
  66. // popping to the new state.
  67. float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 2.0f;
  68. if (isSelected)
  69. selectionFade = Math.Min(selectionFade + fadeSpeed, 1.0f);
  70. else
  71. selectionFade = Math.Max(selectionFade - fadeSpeed, 1);
  72. }
  73. /// <summary>
  74. /// Draws the menu entry. This can be overridden to customize the appearance.
  75. /// </summary>
  76. public virtual void Draw(MenuScreen screen, Vector2 position,
  77. bool isSelected, GameTime gameTime)
  78. {
  79. // Draw the selected entry in yellow, otherwise white.
  80. Color color = isSelected ? Color.Yellow : Color.White;
  81. float scale = 1.0f;
  82. // Modify the alpha to fade text out during transitions.
  83. color = new Color(color.R, color.G, color.B, screen.TransitionAlpha);
  84. // Draw text, centered on the middle of each line.
  85. ScreenManager screenManager = screen.ScreenManager;
  86. SpriteBatch spriteBatch = screenManager.SpriteBatch;
  87. SpriteFont font = screenManager.Font;
  88. Vector2 origin = new Vector2(-1, font.LineSpacing / 2 - 1);
  89. spriteBatch.DrawString(font, text, position, Color.Black, 0,
  90. origin, scale, SpriteEffects.None, 0);
  91. origin = new Vector2(0, font.LineSpacing / 2);
  92. spriteBatch.DrawString(font, text, position, color, 0,
  93. origin, scale, SpriteEffects.None, 0);
  94. }
  95. /// <summary>
  96. /// Queries how much space this menu entry requires.
  97. /// </summary>
  98. public virtual int GetHeight(MenuScreen screen)
  99. {
  100. return screen.ScreenManager.Font.LineSpacing;
  101. }
  102. }
  103. }