MenuEntry.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 Blackjack;
  11. using CardsFramework;
  12. namespace GameStateManagement
  13. {
  14. /// <summary>
  15. /// Helper class represents a single entry in a MenuScreen. By default this
  16. /// just draws the entry text string, but it can be customized to display menu
  17. /// entries in different ways. This also provides an event that will be raised
  18. /// when the menu entry is selected.
  19. /// </summary>
  20. class MenuEntry
  21. {
  22. /// <summary>
  23. /// The text rendered for this entry.
  24. /// </summary>
  25. string text;
  26. /// <summary>
  27. /// Tracks a fading selection effect on the entry.
  28. /// </summary>
  29. /// <remarks>
  30. /// The entries transition out of the selection effect when they are deselected.
  31. /// </remarks>
  32. float selectionFade;
  33. Rectangle destination;
  34. /// <summary>
  35. /// Gets or sets the text of this menu entry.
  36. /// </summary>
  37. public string Text
  38. {
  39. get { return text; }
  40. set { text = value; }
  41. }
  42. /// <summary>
  43. /// Gets or sets the position at which to draw this menu entry.
  44. /// </summary>
  45. public Rectangle Destination
  46. {
  47. get { return destination; }
  48. set { destination = value; }
  49. }
  50. public float Scale { get; set; }
  51. public float Rotation { get; set; }
  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. Scale = 1f;
  71. Rotation = 0f;
  72. }
  73. /// <summary>
  74. /// Updates the menu entry.
  75. /// </summary>
  76. public virtual void Update(MenuScreen screen, bool isSelected, GameTime gameTime)
  77. {
  78. // there is no such thing as a selected item on Windows Phone, so we always
  79. // force isSelected to be false
  80. if (UIUtility.IsMobile)
  81. {
  82. isSelected = false;
  83. }
  84. // When the menu selection changes, entries gradually fade between
  85. // their selected and deselected appearance, rather than instantly
  86. // popping to the new state.
  87. float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;
  88. if (isSelected)
  89. selectionFade = Math.Min(selectionFade + fadeSpeed, 1);
  90. else
  91. selectionFade = Math.Max(selectionFade - fadeSpeed, 0);
  92. }
  93. /// <summary>
  94. /// Draws the menu entry. This can be overridden to customize the appearance.
  95. /// </summary>
  96. public virtual void Draw(MenuScreen screen, bool isSelected, GameTime gameTime)
  97. {
  98. Color textColor = isSelected ? Color.White : Color.Black;
  99. Color tintColor = isSelected ? Color.White : Color.Gray;
  100. if (UIUtility.IsMobile)
  101. {
  102. // there is no such thing as a selected item on Windows Phone, so we always
  103. // force isSelected to be false
  104. isSelected = false;
  105. tintColor = Color.White;
  106. textColor = Color.Black;
  107. }
  108. // Draw text, centered on the middle of each line.
  109. ScreenManager screenManager = screen.ScreenManager;
  110. SpriteBatch spriteBatch = screenManager.SpriteBatch;
  111. SpriteFont font = screenManager.Font;
  112. spriteBatch.Draw(screenManager.ButtonBackground, destination, tintColor);
  113. spriteBatch.DrawString(screenManager.Font, text, getTextPosition(screen),
  114. textColor, Rotation, Vector2.Zero, Scale, SpriteEffects.None, 0);
  115. }
  116. /// <summary>
  117. /// Queries how much space this menu entry requires.
  118. /// </summary>
  119. public virtual int GetHeight(MenuScreen screen)
  120. {
  121. return screen.ScreenManager.Font.LineSpacing;
  122. }
  123. /// <summary>
  124. /// Queries how wide the entry is, used for centering on the screen.
  125. /// </summary>
  126. public virtual int GetWidth(MenuScreen screen)
  127. {
  128. return (int)screen.ScreenManager.Font.MeasureString(Text).X;
  129. }
  130. private Vector2 getTextPosition(MenuScreen screen)
  131. {
  132. Vector2 textPosition = Vector2.Zero;
  133. if (Scale == 1f)
  134. {
  135. textPosition = new Vector2((int)destination.X + destination.Width / 2 - GetWidth(screen) / 2,
  136. (int)destination.Y);
  137. }
  138. else
  139. {
  140. textPosition = new Vector2(
  141. (int)destination.X + (destination.Width / 2 - ((GetWidth(screen) / 2) * Scale)),
  142. (int)destination.Y + (GetHeight(screen) - GetHeight(screen) * Scale) / 2);
  143. }
  144. return textPosition;
  145. }
  146. }
  147. }