MenuEntry.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 HoneycombRush
  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. Texture2D buttonTexture;
  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. public Rectangle Bounds
  54. {
  55. get
  56. {
  57. return new Rectangle((int)position.X, (int)position.Y,
  58. buttonTexture.Width, buttonTexture.Height);
  59. }
  60. }
  61. public float Scale { get; set; }
  62. public float Rotation{ get; set; }
  63. /// <summary>
  64. /// Event raised when the menu entry is selected.
  65. /// </summary>
  66. public event EventHandler<PlayerIndexEventArgs> Selected;
  67. /// <summary>
  68. /// Method for raising the Selected event.
  69. /// </summary>
  70. protected internal virtual void OnSelectEntry(PlayerIndex playerIndex)
  71. {
  72. if (Selected != null)
  73. Selected(this, new PlayerIndexEventArgs(playerIndex));
  74. }
  75. /// <summary>
  76. /// Constructs a new menu entry with the specified text.
  77. /// </summary>
  78. public MenuEntry(string text)
  79. {
  80. this.text = text;
  81. Scale = 1f;
  82. Rotation = 0f;
  83. }
  84. /// <summary>
  85. /// Updates the menu entry.
  86. /// </summary>
  87. public virtual void Update(MenuScreen screen, bool isSelected, GameTime gameTime)
  88. {
  89. // there is no such thing as a selected item on Windows Phone, so we always
  90. // force isSelected to be false
  91. #if WINDOWS_PHONE
  92. isSelected = false;
  93. #endif
  94. // When the menu selection changes, entries gradually fade between
  95. // their selected and deselected appearance, rather than instantly
  96. // popping to the new state.
  97. float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;
  98. if (isSelected)
  99. selectionFade = Math.Min(selectionFade + fadeSpeed, 1);
  100. else
  101. selectionFade = Math.Max(selectionFade - fadeSpeed, 0);
  102. }
  103. /// <summary>
  104. /// Draws the menu entry. This can be overridden to customize the appearance.
  105. /// </summary>
  106. public virtual void Draw(MenuScreen screen, bool isSelected, GameTime gameTime)
  107. {
  108. Color textColor = isSelected ? Color.White : Color.Black;
  109. Color tintColor = isSelected ? Color.White : Color.Gray;
  110. #if WINDOWS_PHONE
  111. // there is no such thing as a selected item on Windows Phone, so we always
  112. // force isSelected to be false
  113. isSelected = false;
  114. tintColor = Color.White;
  115. textColor = Color.Black;
  116. #endif
  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. buttonTexture = screenManager.ButtonBackground;
  122. spriteBatch.Draw(buttonTexture, new Vector2((int)position.X, (int)position.Y), tintColor);
  123. spriteBatch.DrawString(screenManager.Font, text, getTextPosition(screen),
  124. textColor, Rotation, Vector2.Zero, Scale, SpriteEffects.None, 0);
  125. }
  126. /// <summary>
  127. /// Queries how much space this menu entry requires.
  128. /// </summary>
  129. public virtual int GetHeight(MenuScreen screen)
  130. {
  131. return (int)screen.ScreenManager.Font.MeasureString(Text).Y;
  132. }
  133. /// <summary>
  134. /// Queries how wide the entry is, used for centering on the screen.
  135. /// </summary>
  136. public virtual int GetWidth(MenuScreen screen)
  137. {
  138. return (int)screen.ScreenManager.Font.MeasureString(Text).X;
  139. }
  140. private Vector2 getTextPosition(MenuScreen screen)
  141. {
  142. Vector2 textPosition = Vector2.Zero;
  143. if (Scale == 1f)
  144. {
  145. textPosition = new Vector2((int)position.X + buttonTexture.Width / 2 - GetWidth(screen) / 2,
  146. (int)position.Y);
  147. }
  148. else
  149. {
  150. textPosition = new Vector2(
  151. (int)position.X + (buttonTexture.Width / 2 - ((GetWidth(screen) / 2)* Scale)),
  152. (int)position.Y + (GetHeight(screen) - GetHeight(screen) * Scale) / 2);
  153. }
  154. return textPosition;
  155. }
  156. }
  157. }