MainMenu.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //-----------------------------------------------------------------------------
  2. // MainMenu.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using System;
  10. using System.Collections;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using RacingGame.GameLogic;
  14. using RacingGame.Graphics;
  15. using RacingGame.Helpers;
  16. using RacingGame.Sounds;
  17. namespace RacingGame.GameScreens
  18. {
  19. /// <summary>
  20. /// Main menu
  21. /// </summary>
  22. class MainMenu : IGameScreen
  23. {
  24. static readonly Rectangle[] ButtonRects = new Rectangle[]
  25. {
  26. UIRenderer.MenuButtonPlayGfxRect,
  27. UIRenderer.MenuButtonHighscoresGfxRect,
  28. UIRenderer.MenuButtonOptionsGfxRect,
  29. UIRenderer.MenuButtonHelpGfxRect,
  30. UIRenderer.MenuButtonQuitGfxRect,
  31. };
  32. static readonly Rectangle[] TextRects = new Rectangle[]
  33. {
  34. UIRenderer.MenuTextPlayGfxRect,
  35. UIRenderer.MenuTextHighscoresGfxRect,
  36. UIRenderer.MenuTextOptionsGfxRect,
  37. UIRenderer.MenuTextHelpGfxRect,
  38. UIRenderer.MenuTextQuitGfxRect,
  39. };
  40. const int NumberOfButtons = 5,
  41. ActiveButtonWidth = 132,
  42. InactiveButtonWidth = 108,
  43. DistanceBetweenButtons = 14;
  44. /// <summary>
  45. /// The amount of time idle at the menu before returning to the splash screen
  46. /// </summary>
  47. const float TimeOutMenu = 60000.0f;
  48. /// <summary>
  49. /// Start with button 0 being selected (play game)
  50. /// </summary>
  51. int selectedButton = 0;
  52. private int SelectedButton
  53. {
  54. get
  55. {
  56. return selectedButton;
  57. }
  58. set
  59. {
  60. selectedButton = value;
  61. idleTime = 0.0f;
  62. }
  63. }
  64. /// <summary>
  65. /// Current button sizes for scaling up/down smooth effect.
  66. /// </summary>
  67. float[] currentButtonSizes =
  68. new float[NumberOfButtons] { 1, 0, 0, 0, 0 };
  69. /// <summary>
  70. /// Ignore the mouse unless it moves;
  71. /// this is so the mouse does not disrupt game pads and keyboard
  72. /// </summary>
  73. bool ignoreMouse = true;
  74. float idleTime = 0.0f;
  75. bool musicHasStarted = false;
  76. public MainMenu()
  77. {
  78. }
  79. /// <summary>
  80. /// Handle starting the menu music.
  81. /// </summary>
  82. /// <param name="gameTime"></param>
  83. public void Update(GameTime gameTime)
  84. {
  85. // Start playing the menu music
  86. if (!musicHasStarted)
  87. {
  88. Sound.Play(Sound.Sounds.MenuMusic);
  89. musicHasStarted = true;
  90. }
  91. }
  92. /// <summary>
  93. /// Interpolate rectangle
  94. /// </summary>
  95. /// <param name="rect1">Rectangle 1</param>
  96. /// <param name="rect2">Rectangle 2</param>
  97. /// <param name="interpolation">Interpolation</param>
  98. /// <returns>Rectangle</returns>
  99. internal static Rectangle InterpolateRect(
  100. Rectangle rect1, Rectangle rect2,
  101. float interpolation)
  102. {
  103. return new Rectangle(
  104. (int)Math.Round(
  105. rect1.X * interpolation + rect2.X * (1 - interpolation)),
  106. (int)Math.Round(
  107. rect1.Y * interpolation + rect2.Y * (1 - interpolation)),
  108. (int)Math.Round(
  109. rect1.Width * interpolation + rect2.Width * (1 - interpolation)),
  110. (int)Math.Round(
  111. rect1.Height * interpolation + rect2.Height * (1 - interpolation)));
  112. }
  113. float pressedLeftMs = 0;
  114. float pressedRightMs = 0;
  115. /// <summary>
  116. /// Render
  117. /// </summary>
  118. /// <returns>Bool</returns>
  119. public bool Render()
  120. {
  121. // This starts both menu and in game post screen shader!
  122. if(BaseGame.UsePostScreenShaders)
  123. BaseGame.UI.PostScreenMenuShader.Start();
  124. // Render background and black bar
  125. BaseGame.UI.RenderMenuBackground();
  126. BaseGame.UI.RenderBlackBar(280, 192);
  127. // Show logos
  128. // Little helper to keep track if mouse is actually over a button.
  129. // Required because buttons are selected even when not hovering over
  130. // them for GamePad support, but we still want the mouse only to
  131. // be apply when we are actually over the button.
  132. int mouseIsOverButton = -1;
  133. // If the user manipulated the mouse, stop ignoring the mouse
  134. // This allows the mouse to override the game pad or keyboard selection
  135. if (Input.HasMouseMoved || Input.MouseLeftButtonJustPressed)
  136. ignoreMouse = false;
  137. // Show buttons
  138. // Part 1: Calculate global variables for our buttons
  139. Rectangle activeRect = BaseGame.CalcRectangleCenteredWithGivenHeight(
  140. 0, 0, ActiveButtonWidth, ButtonRects[0]);
  141. Rectangle inactiveRect = BaseGame.CalcRectangleCenteredWithGivenHeight(
  142. 0, 0, InactiveButtonWidth, ButtonRects[0]);
  143. int totalWidth = activeRect.Width +
  144. (NumberOfButtons - 1) * inactiveRect.Width +
  145. (NumberOfButtons - 1) * BaseGame.XToRes(DistanceBetweenButtons);
  146. int xPos = BaseGame.XToRes(512) - totalWidth / 2;
  147. int yPos = BaseGame.YToRes(316);
  148. for (int num = 0; num < NumberOfButtons; num++)
  149. {
  150. // Is this button currently selected?
  151. bool selected = num == SelectedButton;
  152. // Increase size if selected, decrease otherwise
  153. currentButtonSizes[num] +=
  154. (selected ? 1 : -1) * BaseGame.MoveFactorPerSecond * 2;
  155. if (currentButtonSizes[num] < 0)
  156. currentButtonSizes[num] = 0;
  157. if (currentButtonSizes[num] > 1)
  158. currentButtonSizes[num] = 1;
  159. // Use this size to build rect
  160. Rectangle thisRect =
  161. InterpolateRect(activeRect, inactiveRect, currentButtonSizes[num]);
  162. Rectangle renderRect = new Rectangle(
  163. xPos, yPos - (thisRect.Height - inactiveRect.Height) / 2,
  164. thisRect.Width, thisRect.Height);
  165. BaseGame.UI.Buttons.RenderOnScreen(renderRect, ButtonRects[num],
  166. selected ? Color.White : new Color(192, 192, 192, 192));
  167. // Add border effect if selected
  168. if (selected)
  169. BaseGame.UI.Buttons.RenderOnScreen(renderRect,
  170. UIRenderer.MenuButtonSelectionGfxRect);
  171. // Also add text below button
  172. Rectangle textRenderRect = new Rectangle(
  173. xPos, renderRect.Bottom + BaseGame.YToRes(5),
  174. renderRect.Width,
  175. renderRect.Height * TextRects[0].Height / ButtonRects[0].Height);
  176. if (selected)
  177. BaseGame.UI.Buttons.RenderOnScreen(textRenderRect, TextRects[num],
  178. selected ? Color.White : new Color(192, 192, 192, 192));
  179. // Also check if the user hovers with the mouse over this button
  180. if (Input.MouseInBox(renderRect))
  181. mouseIsOverButton = num;
  182. xPos += thisRect.Width + BaseGame.XToRes(DistanceBetweenButtons);
  183. }
  184. if (!ignoreMouse && mouseIsOverButton >= 0)
  185. SelectedButton = mouseIsOverButton;
  186. // Handle input, we have 2 modes: Immediate response if one of the
  187. // keys is pressed to move our selected menu entry and after a timeout we
  188. // move even more. Controlling feels more natural this way.
  189. if (Input.KeyboardLeftPressed ||
  190. Input.GamePadLeftPressed)
  191. pressedLeftMs += BaseGame.ElapsedTimeThisFrameInMilliseconds;
  192. else
  193. pressedLeftMs = 0;
  194. if (Input.KeyboardRightPressed ||
  195. Input.GamePadRightPressed)
  196. pressedRightMs += BaseGame.ElapsedTimeThisFrameInMilliseconds;
  197. else
  198. pressedRightMs = 0;
  199. // Handle GamePad input, and also allow keyboard input
  200. if (Input.GamePadLeftJustPressed ||
  201. Input.KeyboardLeftJustPressed ||
  202. (pressedLeftMs > 250 &&
  203. (Input.KeyboardLeftPressed ||
  204. Input.GamePadLeftPressed)))
  205. {
  206. pressedLeftMs -= 250;
  207. Sound.Play(Sound.Sounds.Highlight);
  208. SelectedButton =
  209. (SelectedButton + NumberOfButtons - 1) % NumberOfButtons;
  210. ignoreMouse = true;
  211. }
  212. else if (Input.GamePadRightJustPressed ||
  213. Input.KeyboardRightJustPressed ||
  214. (pressedRightMs > 250 &&
  215. (Input.KeyboardRightPressed ||
  216. Input.GamePadRightPressed)))
  217. {
  218. pressedRightMs -= 250;
  219. Sound.Play(Sound.Sounds.Highlight);
  220. SelectedButton = (SelectedButton + 1) % NumberOfButtons;
  221. ignoreMouse = true;
  222. }
  223. // If user presses the mouse button or the game pad A or Space,
  224. // start the game screen for the currently selected game part.
  225. if ((mouseIsOverButton >= 0 && Input.MouseLeftButtonJustPressed) ||
  226. Input.GamePadAJustPressed ||
  227. Input.KeyboardSpaceJustPressed)
  228. {
  229. idleTime = 0.0f;
  230. // Start game screen
  231. switch (SelectedButton)
  232. {
  233. case 0:
  234. RacingGameManager.AddGameScreen(new CarSelection());
  235. break;
  236. case 1:
  237. RacingGameManager.AddGameScreen(new Highscores());
  238. break;
  239. case 2:
  240. RacingGameManager.AddGameScreen(new Options());
  241. break;
  242. case 3:
  243. RacingGameManager.AddGameScreen(new Help());
  244. break;
  245. case 4:
  246. // Exit
  247. return true;
  248. }
  249. }
  250. if (Input.KeyboardEscapeJustPressed ||
  251. Input.GamePadBackJustPressed)
  252. return true;
  253. // If the game sits idle at the menu for too long,
  254. // return to the splash screen
  255. idleTime += BaseGame.ElapsedTimeThisFrameInMilliseconds;
  256. if (idleTime > TimeOutMenu)
  257. {
  258. idleTime = 0.0f;
  259. RacingGameManager.AddGameScreen(new SplashScreen());
  260. }
  261. return false;
  262. }
  263. }
  264. }