MenuScreen.cs 5.1 KB

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