MenuScreen.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. Vector2 viewportSize = new Vector2(ScreenManager.BASE_BUFFER_WIDTH, ScreenManager.BASE_BUFFER_HEIGHT);
  87. Vector2 position = new Vector2(0f, viewportSize.Y * 0.65f);
  88. // Make the menu slide into place during transitions, using a
  89. // power curve to make things look more interesting (this makes
  90. // the movement slow down as it nears the end).
  91. float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
  92. if (ScreenState == ScreenState.TransitionOn)
  93. position.Y += transitionOffset * 256;
  94. else
  95. position.Y += transitionOffset * 512;
  96. // Draw each menu entry in turn.
  97. ScreenManager.SpriteBatch.Begin();
  98. for (int i = 0; i < menuEntries.Count; i++)
  99. {
  100. Color color = Color.White;
  101. float scale = 1.0f;
  102. if (IsActive && (i == selectedEntry))
  103. {
  104. // The selected entry is yellow, and has an animating size.
  105. double time = gameTime.TotalGameTime.TotalSeconds;
  106. float pulsate = (float)Math.Sin(time * 6f) + 1f;
  107. color = Color.Orange;
  108. scale += pulsate * 0.05f;
  109. }
  110. // Modify the alpha to fade text out during transitions.
  111. color = new Color(color.R, color.G, color.B, TransitionAlpha);
  112. // Draw text, centered on the middle of each line.
  113. Vector2 origin = new Vector2(0, ScreenManager.Font.LineSpacing / 2);
  114. Vector2 size = ScreenManager.Font.MeasureString(menuEntries[i]);
  115. position.X = viewportSize.X / 2f - size.X / 2f * scale;
  116. ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, menuEntries[i],
  117. position, color, 0, origin, scale,
  118. SpriteEffects.None, 0);
  119. position.Y += ScreenManager.Font.LineSpacing;
  120. }
  121. ScreenManager.SpriteBatch.End();
  122. }
  123. }
  124. }