MenuEntry.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. using Microsoft.Xna.Framework.Net;
  13. namespace GameStateManagement
  14. {
  15. /// <summary>
  16. /// MenuEntry subclass for displaying AvailableNetworkSession info.
  17. /// </summary>
  18. class AvailableSessionMenuEntry : MenuEntry
  19. {
  20. public AvailableNetworkSession AvailableSession { get; }
  21. public AvailableSessionMenuEntry(AvailableNetworkSession session)
  22. : base(GetMenuItemText(session))
  23. {
  24. AvailableSession = session;
  25. }
  26. static string GetMenuItemText(AvailableNetworkSession session)
  27. {
  28. int totalSlots = session.CurrentGamerCount + session.OpenPublicGamerSlots;
  29. return $"{session.HostGamertag} ({session.CurrentGamerCount}/{totalSlots})";
  30. }
  31. }
  32. /// <summary>
  33. /// Helper class represents a single entry in a MenuScreen. By default this
  34. /// just draws the entry text string, but it can be customized to display menu
  35. /// entries in different ways. This also provides an event that will be raised
  36. /// when the menu entry is selected.
  37. /// </summary>
  38. class MenuEntry
  39. {
  40. /// <summary>
  41. /// The text rendered for this entry.
  42. /// </summary>
  43. string text;
  44. /// <summary>
  45. /// Tracks a fading selection effect on the entry.
  46. /// </summary>
  47. /// <remarks>
  48. /// The entries transition out of the selection effect when they are deselected.
  49. /// </remarks>
  50. float selectionFade;
  51. Rectangle destination;
  52. /// <summary>
  53. /// Gets or sets the text of this menu entry.
  54. /// </summary>
  55. public string Text
  56. {
  57. get { return text; }
  58. set { text = value; }
  59. }
  60. /// <summary>
  61. /// Gets or sets the position at which to draw this menu entry.
  62. /// </summary>
  63. public Rectangle Destination
  64. {
  65. get { return destination; }
  66. set { destination = value; }
  67. }
  68. public float Scale { get; set; }
  69. public float Rotation { get; set; }
  70. /// <summary>
  71. /// Optional tag for storing custom data (e.g., session reference).
  72. /// </summary>
  73. public object Tag { get; set; }
  74. /// <summary>
  75. /// Event raised when the menu entry is selected.
  76. /// </summary>
  77. public event EventHandler<PlayerIndexEventArgs> Selected;
  78. /// <summary>
  79. /// Method for raising the Selected event.
  80. /// </summary>
  81. protected internal virtual void OnSelectEntry(PlayerIndex playerIndex)
  82. {
  83. if (Selected != null)
  84. Selected(this, new PlayerIndexEventArgs(playerIndex));
  85. }
  86. /// <summary>
  87. /// Constructs a new menu entry with the specified text.
  88. /// </summary>
  89. public MenuEntry(string text)
  90. {
  91. this.text = text;
  92. Scale = 1f;
  93. Rotation = 0f;
  94. }
  95. /// <summary>
  96. /// Updates the menu entry.
  97. /// </summary>
  98. public virtual void Update(MenuScreen screen, bool isSelected, GameTime gameTime)
  99. {
  100. // there is no such thing as a selected item on Windows Phone, so we always
  101. // force isSelected to be false
  102. if (UIUtility.IsMobile)
  103. {
  104. isSelected = false;
  105. }
  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. float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;
  110. if (isSelected)
  111. selectionFade = Math.Min(selectionFade + fadeSpeed, 1);
  112. else
  113. selectionFade = Math.Max(selectionFade - fadeSpeed, 0);
  114. }
  115. /// <summary>
  116. /// Draws the menu entry. This can be overridden to customize the appearance.
  117. /// </summary>
  118. public virtual void Draw(MenuScreen screen, bool isSelected, GameTime gameTime)
  119. {
  120. Color textColor = isSelected ? Color.White : Color.Black;
  121. // Check if this menu entry is currently being pressed
  122. bool isPressed = screen.IsMouseDown && isSelected;
  123. if (UIUtility.IsMobile)
  124. {
  125. // there is no such thing as a selected item on Windows Phone, so we always
  126. // force isSelected to be false
  127. isSelected = false;
  128. isPressed = false;
  129. textColor = Color.Black;
  130. }
  131. // Draw text, centered on the middle of each line.
  132. ScreenManager screenManager = screen.ScreenManager;
  133. SpriteBatch spriteBatch = screenManager.SpriteBatch;
  134. SpriteFont font = screenManager.Font;
  135. // Use pressed texture when button is being pressed, otherwise use regular texture
  136. Texture2D buttonTexture = isPressed ? screenManager.ButtonPressed : screenManager.ButtonBackground;
  137. spriteBatch.Draw(buttonTexture, destination, Color.White);
  138. spriteBatch.DrawString(screenManager.Font, text, getTextPosition(screen),
  139. textColor, Rotation, Vector2.Zero, Scale, SpriteEffects.None, 0);
  140. }
  141. /// <summary>
  142. /// Queries how much space this menu entry requires.
  143. /// </summary>
  144. public virtual int GetHeight(MenuScreen screen)
  145. {
  146. return screen.ScreenManager.Font.LineSpacing;
  147. }
  148. /// <summary>
  149. /// Queries how wide the entry is, used for centering on the screen.
  150. /// </summary>
  151. public virtual int GetWidth(MenuScreen screen)
  152. {
  153. return (int)screen.ScreenManager.Font.MeasureString(Text).X;
  154. }
  155. private Vector2 getTextPosition(MenuScreen screen)
  156. {
  157. Vector2 textPosition = Vector2.Zero;
  158. // Calculate horizontal centering
  159. float centeredX = destination.X + (destination.Width / 2.0f) - (GetWidth(screen) / 2.0f) * Scale;
  160. // Calculate vertical centering
  161. float centeredY = destination.Y + (destination.Height / 2.0f) - (GetHeight(screen) / 2.0f) * Scale;
  162. textPosition = new Vector2(centeredX, centeredY);
  163. return textPosition;
  164. }
  165. }
  166. }