MenuEntry.cs 6.8 KB

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