2
0

MenuEntry.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. namespace FarseerPhysics.SamplesFramework
  5. {
  6. public enum EntryType
  7. {
  8. Screen,
  9. Separator,
  10. ExitItem
  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. public sealed class MenuEntry
  19. {
  20. private float _alpha;
  21. private Vector2 _baseOrigin;
  22. private float _height;
  23. private MenuScreen _menu;
  24. /// <summary>
  25. /// The position at which the entry is drawn. This is set by the MenuScreen
  26. /// each frame in Update.
  27. /// </summary>
  28. private Vector2 _position;
  29. private float _scale;
  30. private GameScreen _screen;
  31. /// <summary>
  32. /// Tracks a fading selection effect on the entry.
  33. /// </summary>
  34. /// <remarks>
  35. /// The entries transition out of the selection effect when they are deselected.
  36. /// </remarks>
  37. private float _selectionFade;
  38. /// <summary>
  39. /// The text rendered for this entry.
  40. /// </summary>
  41. private string _text;
  42. private EntryType _type;
  43. private float _width;
  44. /// <summary>
  45. /// Constructs a new menu entry with the specified text.
  46. /// </summary>
  47. public MenuEntry(MenuScreen menu, string text, EntryType type, GameScreen screen)
  48. {
  49. _text = text;
  50. _screen = screen;
  51. _type = type;
  52. _menu = menu;
  53. _scale = 0.9f;
  54. _alpha = 1.0f;
  55. }
  56. /// <summary>
  57. /// Gets or sets the text of this menu entry.
  58. /// </summary>
  59. public string Text
  60. {
  61. get { return _text; }
  62. set { _text = value; }
  63. }
  64. /// <summary>
  65. /// Gets or sets the position at which to draw this menu entry.
  66. /// </summary>
  67. public Vector2 Position
  68. {
  69. get { return _position; }
  70. set { _position = value; }
  71. }
  72. public float Alpha
  73. {
  74. get { return _alpha; }
  75. set { _alpha = value; }
  76. }
  77. public GameScreen Screen
  78. {
  79. get { return _screen; }
  80. }
  81. public void Initialize()
  82. {
  83. SpriteFont font = _menu.ScreenManager.Fonts.MenuSpriteFont;
  84. _baseOrigin = new Vector2(font.MeasureString(Text).X, font.MeasureString("M").Y) * 0.5f;
  85. _width = font.MeasureString(Text).X * 0.8f;
  86. _height = font.MeasureString("M").Y * 0.8f;
  87. }
  88. public bool IsExitItem()
  89. {
  90. return _type == EntryType.ExitItem;
  91. }
  92. public bool IsSelectable()
  93. {
  94. return _type != EntryType.Separator;
  95. }
  96. /// <summary>
  97. /// Updates the menu entry.
  98. /// </summary>
  99. public void Update(bool isSelected, GameTime gameTime)
  100. {
  101. // there is no such thing as a selected item on Windows Phone, so we always
  102. // force isSelected to be false
  103. #if WINDOWS_PHONE
  104. isSelected = false;
  105. #endif
  106. // When the menu selection changes, entries gradually fade between
  107. // their selected and deselected appearance, rather than instantly
  108. // popping to the new state.
  109. if (_type != EntryType.Separator)
  110. {
  111. float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;
  112. if (isSelected)
  113. {
  114. _selectionFade = Math.Min(_selectionFade + fadeSpeed, 1f);
  115. }
  116. else
  117. {
  118. _selectionFade = Math.Max(_selectionFade - fadeSpeed, 0f);
  119. }
  120. _scale = 0.7f + 0.1f * _selectionFade;
  121. }
  122. }
  123. /// <summary>
  124. /// Draws the menu entry. This can be overridden to customize the appearance.
  125. /// </summary>
  126. public void Draw()
  127. {
  128. SpriteFont font = _menu.ScreenManager.Fonts.MenuSpriteFont;
  129. SpriteBatch batch = _menu.ScreenManager.SpriteBatch;
  130. Color color;
  131. if (_type == EntryType.Separator)
  132. {
  133. color = Color.DarkOrange;
  134. }
  135. else
  136. {
  137. // Draw the selected entry in yellow, otherwise white
  138. color = Color.Lerp(Color.White, new Color(255, 210, 0), _selectionFade);
  139. }
  140. color *= _alpha;
  141. // Draw text, centered on the middle of each line.
  142. batch.DrawString(font, _text, _position - _baseOrigin * _scale + Vector2.One,
  143. Color.DarkSlateGray * _alpha * _alpha, 0, Vector2.Zero, _scale, SpriteEffects.None, 0);
  144. batch.DrawString(font, _text, _position - _baseOrigin * _scale, color, 0, Vector2.Zero, _scale,
  145. SpriteEffects.None, 0);
  146. }
  147. /// <summary>
  148. /// Queries how much space this menu entry requires.
  149. /// </summary>
  150. public int GetHeight()
  151. {
  152. return (int)_height;
  153. }
  154. /// <summary>
  155. /// Queries how wide the entry is, used for centering on the screen.
  156. /// </summary>
  157. public int GetWidth()
  158. {
  159. return (int)_width;
  160. }
  161. }
  162. }