TrackSelection.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //-----------------------------------------------------------------------------
  2. // TrackSelection.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 System.Text;
  10. using RacingGame.GameLogic;
  11. using RacingGame.Graphics;
  12. using RacingGame.Helpers;
  13. using RacingGame.Properties;
  14. using Microsoft.Xna.Framework;
  15. using Microsoft.Xna.Framework.Graphics;
  16. using RacingGame.Sounds;
  17. using RacingGame.Landscapes;
  18. namespace RacingGame.GameScreens
  19. {
  20. /// <summary>
  21. /// Track selection screen
  22. /// </summary>
  23. /// <returns>IGame screen</returns>
  24. class TrackSelection : IGameScreen
  25. {
  26. static readonly Rectangle[] ButtonRects = new Rectangle[]
  27. {
  28. UIRenderer.TrackButtonBeginnerGfxRect,
  29. UIRenderer.TrackButtonAdvancedGfxRect,
  30. UIRenderer.TrackButtonExpertGfxRect,
  31. };
  32. static readonly Rectangle[] TextRects = new Rectangle[]
  33. {
  34. UIRenderer.TrackTextBeginnerGfxRect,
  35. UIRenderer.TrackTextAdvancedGfxRect,
  36. UIRenderer.TrackTextExpertGfxRect,
  37. };
  38. const int NumberOfButtons = 3,
  39. ActiveButtonWidth = 132,
  40. InactiveButtonWidth = 108,
  41. DistanceBetweenButtons = 32;
  42. /// <summary>
  43. /// Unimplemented
  44. /// </summary>
  45. /// <param name="gameTime"></param>
  46. public void Update(GameTime gameTime)
  47. {
  48. }
  49. /// <summary>
  50. /// Start with button 0 being selected (beginner track)
  51. /// Update: Now use advanced track as default, looks better in replays.
  52. /// </summary>
  53. static int selectedButton = 1;
  54. /// <summary>
  55. /// Selected track number
  56. /// </summary>
  57. /// <returns>Int</returns>
  58. static public int SelectedTrackNumber
  59. {
  60. get
  61. {
  62. return selectedButton;
  63. }
  64. }
  65. /// <summary>
  66. /// Selected track
  67. /// </summary>
  68. /// <returns>Track level</returns>
  69. static public RacingGameManager.Level SelectedTrack
  70. {
  71. get
  72. {
  73. return (RacingGameManager.Level)selectedButton;
  74. }
  75. }
  76. /// <summary>
  77. /// Current button sizes for scaling up/down smooth effect.
  78. /// </summary>
  79. float[] currentButtonSizes =
  80. new float[NumberOfButtons] { 1, 0, 0 };
  81. /// <summary>
  82. /// Ignore the mouse unless it moves;
  83. /// this is so the mouse does not disrupt game pads and keyboard
  84. /// </summary>
  85. bool ignoreMouse = true;
  86. /// <summary>
  87. /// Render game screen. Called each frame.
  88. /// </summary>
  89. public bool Render()
  90. {
  91. // This starts both menu and in game post screen shader!
  92. if(BaseGame.UsePostScreenShaders)
  93. BaseGame.UI.PostScreenMenuShader.Start();
  94. // Render background and black bar
  95. BaseGame.UI.RenderMenuBackground();
  96. BaseGame.UI.RenderBlackBar(220, 280);
  97. // Track header
  98. int posX = 10;
  99. int posY = 18;
  100. // UWP COMMENT OUT
  101. //if (Environment.OSVersion.Platform != PlatformID.Win32NT)
  102. //{
  103. // posX += 36;
  104. // posY += 26;
  105. //}
  106. BaseGame.UI.Headers.RenderOnScreenRelative1600(
  107. posX, posY, UIRenderer.HeaderSelectTrackGfxRect);
  108. // Little helper to keep track if mouse is actually over a button.
  109. // Required because buttons are selected even when not hovering over
  110. // them for GamePad support, but we still want the mouse only to
  111. // be apply when we are actually over the button.
  112. int mouseIsOverButton = -1;
  113. // If the user manipulated the mouse, stop ignoring the mouse
  114. // This allows the mouse to override the game pad or keyboard selection
  115. if (Input.HasMouseMoved || Input.MouseLeftButtonJustPressed)
  116. ignoreMouse = false;
  117. // Show buttons
  118. // Part 1: Calculate global variables for our buttons
  119. Rectangle activeRect = BaseGame.CalcRectangleCenteredWithGivenHeight(
  120. 0, 0,
  121. ActiveButtonWidth * ButtonRects[0].Height / ButtonRects[0].Width,
  122. ButtonRects[0]);
  123. Rectangle inactiveRect = BaseGame.CalcRectangleCenteredWithGivenHeight(
  124. 0, 0,
  125. InactiveButtonWidth * ButtonRects[0].Height / ButtonRects[0].Width,
  126. ButtonRects[0]);
  127. int totalWidth = activeRect.Width +
  128. 2 * inactiveRect.Width +
  129. 2 * BaseGame.XToRes(DistanceBetweenButtons);
  130. int xPos = BaseGame.XToRes(512) - totalWidth / 2;
  131. int yPos = BaseGame.YToRes(258);
  132. for (int num = 0; num < NumberOfButtons; num++)
  133. {
  134. // Is this button currently selected?
  135. bool selected = num == selectedButton;
  136. // Increase size if selected, decrease otherwise
  137. currentButtonSizes[num] +=
  138. (selected ? 1 : -1) * BaseGame.MoveFactorPerSecond * 2;
  139. if (currentButtonSizes[num] < 0)
  140. currentButtonSizes[num] = 0;
  141. if (currentButtonSizes[num] > 1)
  142. currentButtonSizes[num] = 1;
  143. Rectangle thisRect = MainMenu.
  144. InterpolateRect(activeRect, inactiveRect, currentButtonSizes[num]);
  145. Rectangle renderRect = new Rectangle(
  146. xPos, yPos - (thisRect.Height - inactiveRect.Height) / 2,
  147. thisRect.Width, thisRect.Height);
  148. BaseGame.UI.Buttons.RenderOnScreen(renderRect, ButtonRects[num],
  149. selected ? Color.White : new Color(192, 192, 192, 192));
  150. // Add border effect if selected
  151. if (selected)
  152. BaseGame.UI.Buttons.RenderOnScreen(renderRect,
  153. UIRenderer.TrackButtonSelectionGfxRect);
  154. // Also add text below button
  155. Rectangle textRenderRect = new Rectangle(
  156. xPos, renderRect.Bottom + BaseGame.YToRes(5),
  157. renderRect.Width,
  158. renderRect.Height * TextRects[0].Height / ButtonRects[0].Height);
  159. if (selected)
  160. BaseGame.UI.Buttons.RenderOnScreen(textRenderRect, TextRects[num],
  161. selected ? Color.White : Color.Gray);
  162. // Also check if the user hovers with the mouse over this button
  163. if (Input.MouseInBox(renderRect))
  164. mouseIsOverButton = num;
  165. xPos += thisRect.Width + BaseGame.XToRes(DistanceBetweenButtons);
  166. }
  167. if (!ignoreMouse && mouseIsOverButton >= 0)
  168. selectedButton = mouseIsOverButton;
  169. // Handle GamePad input, and also allow keyboard input
  170. if (Input.GamePadLeftJustPressed ||
  171. Input.KeyboardLeftJustPressed)
  172. {
  173. Sound.Play(Sound.Sounds.ButtonClick);
  174. selectedButton =
  175. (selectedButton + NumberOfButtons - 1) % NumberOfButtons;
  176. ignoreMouse = true;
  177. }
  178. else if (Input.GamePadRightJustPressed ||
  179. Input.KeyboardRightJustPressed)
  180. {
  181. Sound.Play(Sound.Sounds.ButtonClick);
  182. selectedButton = (selectedButton + 1) % NumberOfButtons;
  183. ignoreMouse = true;
  184. }
  185. bool aButtonPressed = BaseGame.UI.RenderBottomButtons(false);
  186. // If user presses the mouse button or the game pad A or Space,
  187. // start the game screen for the currently selected game part.
  188. if ((mouseIsOverButton >= 0 && Input.MouseLeftButtonJustPressed) ||
  189. aButtonPressed ||
  190. Input.GamePadAJustPressed ||
  191. Input.KeyboardSpaceJustPressed)
  192. {
  193. // Track selection is handled through SelectedTrackNumber
  194. RacingGameManager.AddGameScreen(new GameScreen());
  195. }
  196. if (Input.KeyboardEscapeJustPressed ||
  197. Input.GamePadBJustPressed ||
  198. Input.GamePadBackJustPressed ||
  199. BaseGame.UI.backButtonPressed)
  200. return true;
  201. return false;
  202. }
  203. }
  204. }