MenuScreen.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // MenuScreen.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Graphics;
  14. #endregion
  15. namespace NetRumble
  16. {
  17. /// <summary>
  18. /// Base public class for screens that contain a menu of options. The user can
  19. /// move up and down to select an entry, or cancel to back out of the screen.
  20. /// </summary>
  21. /// <remarks>
  22. /// This public class is similar to one in the GameStateManagement sample.
  23. /// </remarks>
  24. abstract public class MenuScreen : GameScreen
  25. {
  26. #region Fields
  27. List<string> menuEntries = new List<string>();
  28. int selectedEntry = 0;
  29. #endregion
  30. #region Properties
  31. /// <summary>
  32. /// Gets the list of menu entry strings, so derived classes can add
  33. /// or change the menu contents.
  34. /// </summary>
  35. protected IList<string> MenuEntries
  36. {
  37. get { return menuEntries; }
  38. }
  39. #endregion
  40. #region Initialization
  41. /// <summary>
  42. /// Constructor.
  43. /// </summary>
  44. protected MenuScreen()
  45. {
  46. TransitionOnTime = TimeSpan.FromSeconds(1.0);
  47. TransitionOffTime = TimeSpan.FromSeconds(1.0);
  48. }
  49. #endregion
  50. #region Handle Input
  51. /// <summary>
  52. /// Responds to user input, changing the selected entry and accepting
  53. /// or cancelling the menu.
  54. /// </summary>
  55. public override void HandleInput(InputState input)
  56. {
  57. // Move to the previous menu entry?
  58. if (input.MenuUp)
  59. {
  60. selectedEntry--;
  61. if (selectedEntry < 0)
  62. selectedEntry = menuEntries.Count - 1;
  63. AudioManager.PlaySoundEffect("menu_scroll");
  64. }
  65. // Move to the next menu entry?
  66. if (input.MenuDown)
  67. {
  68. selectedEntry++;
  69. if (selectedEntry >= menuEntries.Count)
  70. selectedEntry = 0;
  71. AudioManager.PlaySoundEffect("menu_scroll");
  72. }
  73. // Accept or cancel the menu?
  74. if (input.MenuSelect)
  75. {
  76. AudioManager.PlaySoundEffect("menu_select");
  77. OnSelectEntry(selectedEntry);
  78. }
  79. else if (input.MenuCancel)
  80. {
  81. OnCancel();
  82. }
  83. }
  84. /// <summary>
  85. /// Notifies derived classes that a menu entry has been chosen.
  86. /// </summary>
  87. protected abstract void OnSelectEntry(int entryIndex);
  88. /// <summary>
  89. /// Notifies derived classes that the menu has been cancelled.
  90. /// </summary>
  91. protected abstract void OnCancel();
  92. #endregion
  93. #region Draw
  94. /// <summary>
  95. /// Draws the menu.
  96. /// </summary>
  97. public override void Draw(GameTime gameTime)
  98. {
  99. Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
  100. Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height);
  101. Vector2 position = new Vector2(0f, viewportSize.Y * 0.65f);
  102. // Make the menu slide into place during transitions, using a
  103. // power curve to make things look more interesting (this makes
  104. // the movement slow down as it nears the end).
  105. float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
  106. if (ScreenState == ScreenState.TransitionOn)
  107. position.Y += transitionOffset * 256;
  108. else
  109. position.Y += transitionOffset * 512;
  110. // Draw each menu entry in turn.
  111. ScreenManager.SpriteBatch.Begin();
  112. for (int i = 0; i < menuEntries.Count; i++)
  113. {
  114. Color color = Color.White;
  115. float scale = 1.0f;
  116. if (IsActive && (i == selectedEntry))
  117. {
  118. // The selected entry is yellow, and has an animating size.
  119. double time = gameTime.TotalGameTime.TotalSeconds;
  120. float pulsate = (float)Math.Sin(time * 6f) + 1f;
  121. color = Color.Orange;
  122. scale += pulsate * 0.05f;
  123. }
  124. // Modify the alpha to fade text out during transitions.
  125. color = new Color(color.R, color.G, color.B, TransitionAlpha);
  126. // Draw text, centered on the middle of each line.
  127. Vector2 origin = new Vector2(0, ScreenManager.Font.LineSpacing / 2);
  128. Vector2 size = ScreenManager.Font.MeasureString(menuEntries[i]);
  129. position.X = viewportSize.X / 2f - size.X / 2f * scale;
  130. ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, menuEntries[i],
  131. position, color, 0, origin, scale,
  132. SpriteEffects.None, 0);
  133. position.Y += ScreenManager.Font.LineSpacing;
  134. }
  135. ScreenManager.SpriteBatch.End();
  136. }
  137. #endregion
  138. }
  139. }