ControlsScreen.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. //-----------------------------------------------------------------------------
  2. // ControlsScreen.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 Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Graphics;
  12. using Microsoft.Xna.Framework.Content;
  13. using RolePlaying.Data;
  14. namespace RolePlaying
  15. {
  16. /// <summary>
  17. /// Displays the in-game controls to the user.
  18. /// </summary>
  19. /// <remarks>One possible extension would be to enable control remapping.</remarks>
  20. class ControlsScreen : GameScreen
  21. {
  22. /// <summary>
  23. /// Holds the GamePad control info to display
  24. /// </summary>
  25. private struct GamePadInfo
  26. {
  27. public string text;
  28. public Vector2 textPosition;
  29. }
  30. /// <summary>
  31. /// Holds the Keyboard control info to display
  32. /// </summary>
  33. private struct KeyboardInfo
  34. {
  35. public InputManager.ActionMap[] totalActionList;
  36. public int selectedIndex;
  37. }
  38. private Texture2D backgroundTexture;
  39. private Texture2D plankTexture;
  40. private Vector2 plankPosition;
  41. private Vector2 titlePosition;
  42. private Vector2 actionPosition;
  43. private Vector2 key1Position;
  44. private Vector2 key2Position;
  45. private Texture2D baseBorderTexture;
  46. private Vector2 baseBorderPosition = new Vector2(200, 570);
  47. private Texture2D scrollUpTexture;
  48. private Texture2D scrollDownTexture;
  49. private Vector2 scrollUpPosition = new Vector2(990, 235);
  50. private Vector2 scrollDownPosition = new Vector2(990, 490);
  51. private Texture2D rightTriggerButton;
  52. private Texture2D leftTriggerButton;
  53. private Vector2 rightTriggerPosition;
  54. private Vector2 leftTriggerPosition;
  55. private Texture2D controlPadTexture;
  56. private Vector2 controlPosition = new Vector2(450, 180);
  57. private Texture2D keyboardTexture;
  58. private Vector2 keyboardPosition = new Vector2(305, 185);
  59. private float chartLine1Position;
  60. private float chartLine2Position;
  61. private float chartLine3Position;
  62. private float chartLine4Position;
  63. private Texture2D backTexture;
  64. private readonly Vector2 backPosition = new Vector2(225, 610);
  65. private bool isShowControlPad;
  66. private GamePadInfo[] leftStrings = new GamePadInfo[7];
  67. private GamePadInfo[] rightStrings = new GamePadInfo[6];
  68. private KeyboardInfo keyboardInfo;
  69. private int startIndex = 0;
  70. private const int maxActionDisplay = 6;
  71. /// <summary>
  72. /// Creates a new ControlsScreen object.
  73. /// </summary>
  74. public ControlsScreen()
  75. : base()
  76. {
  77. TransitionOnTime = TimeSpan.FromSeconds(0.5);
  78. chartLine1Position = keyboardPosition.X + 30;
  79. chartLine2Position = keyboardPosition.X + 340;
  80. chartLine3Position = keyboardPosition.X + 510;
  81. chartLine4Position = keyboardPosition.X + 670;
  82. isShowControlPad = true;
  83. }
  84. /// <summary>
  85. /// Loads the graphics content required for this screen.
  86. /// </summary>
  87. public override void LoadContent()
  88. {
  89. keyboardInfo.totalActionList = InputManager.ActionMaps;
  90. keyboardInfo.selectedIndex = 0;
  91. const int leftStringsPosition = 450;
  92. const int rightStringPosition = 818;
  93. float height = Fonts.DescriptionFont.LineSpacing - 5;
  94. // Set the data for gamepad control to display
  95. leftStrings[0].text = "Page Left";
  96. leftStrings[0].textPosition = new Vector2(leftStringsPosition -
  97. Fonts.DescriptionFont.MeasureString(leftStrings[0].text).X, 170);
  98. leftStrings[1].text = "N/A";
  99. leftStrings[1].textPosition = new Vector2(leftStringsPosition -
  100. Fonts.DescriptionFont.MeasureString(leftStrings[1].text).X, 220);
  101. leftStrings[2].text = "Main Menu";
  102. leftStrings[2].textPosition = new Vector2(leftStringsPosition -
  103. Fonts.DescriptionFont.MeasureString(leftStrings[2].text).X, 290);
  104. leftStrings[3].text = "Exit Game";
  105. leftStrings[3].textPosition = new Vector2(leftStringsPosition -
  106. Fonts.DescriptionFont.MeasureString(leftStrings[3].text).X, 340);
  107. leftStrings[4].text = "Navigation";
  108. leftStrings[4].textPosition = new Vector2(leftStringsPosition -
  109. Fonts.DescriptionFont.MeasureString(leftStrings[4].text).X, 400);
  110. leftStrings[5].text = "Navigation";
  111. leftStrings[5].textPosition = new Vector2(leftStringsPosition -
  112. Fonts.DescriptionFont.MeasureString(leftStrings[5].text).X, 455);
  113. leftStrings[6].text = "N/A";
  114. leftStrings[6].textPosition = new Vector2(leftStringsPosition -
  115. Fonts.DescriptionFont.MeasureString(leftStrings[6].text).X, 510);
  116. rightStrings[0].text = "Page Right";
  117. rightStrings[0].textPosition = new Vector2(rightStringPosition, 170);
  118. rightStrings[1].text = "N/A";
  119. rightStrings[1].textPosition = new Vector2(rightStringPosition, 230);
  120. rightStrings[2].text = "Character Management";
  121. rightStrings[2].textPosition = new Vector2(rightStringPosition, 295);
  122. rightStrings[3].text = "Back";
  123. rightStrings[3].textPosition = new Vector2(rightStringPosition, 355);
  124. rightStrings[4].text = "OK";
  125. rightStrings[4].textPosition = new Vector2(rightStringPosition, 435);
  126. rightStrings[5].text = "Drop Gear";
  127. rightStrings[5].textPosition = new Vector2(rightStringPosition, 510);
  128. ContentManager content = ScreenManager.Game.Content;
  129. backgroundTexture =
  130. content.Load<Texture2D>(@"Textures\MainMenu\MainMenu");
  131. keyboardTexture =
  132. content.Load<Texture2D>(@"Textures\MainMenu\KeyboardBkgd");
  133. plankTexture =
  134. content.Load<Texture2D>(@"Textures\MainMenu\MainMenuPlank03");
  135. backTexture =
  136. content.Load<Texture2D>(@"Textures\Buttons\BButton");
  137. baseBorderTexture =
  138. content.Load<Texture2D>(@"Textures\GameScreens\LineBorder");
  139. controlPadTexture =
  140. content.Load<Texture2D>(@"Textures\MainMenu\ControlJoystick");
  141. scrollUpTexture =
  142. content.Load<Texture2D>(@"Textures\GameScreens\ScrollUp");
  143. scrollDownTexture =
  144. content.Load<Texture2D>(@"Textures\GameScreens\ScrollDown");
  145. rightTriggerButton =
  146. content.Load<Texture2D>(@"Textures\Buttons\RightTriggerButton");
  147. leftTriggerButton =
  148. content.Load<Texture2D>(@"Textures\Buttons\LeftTriggerButton");
  149. plankPosition.X = backgroundTexture.Width / 2 - plankTexture.Width / 2;
  150. plankPosition.Y = 60;
  151. rightTriggerPosition.X = 900;
  152. rightTriggerPosition.Y = 50;
  153. leftTriggerPosition.X = 320;
  154. leftTriggerPosition.Y = 50;
  155. base.LoadContent();
  156. }
  157. /// <summary>
  158. /// Handles user input.
  159. /// </summary>
  160. public override void HandleInput()
  161. {
  162. // exit the screen
  163. if (InputManager.IsActionTriggered(InputManager.Action.Back))
  164. {
  165. ExitScreen();
  166. }
  167. #if !XBOX
  168. // toggle between keyboard and gamepad controls
  169. else if (InputManager.IsActionTriggered(InputManager.Action.PageLeft) ||
  170. InputManager.IsActionTriggered(InputManager.Action.PageRight))
  171. {
  172. isShowControlPad = !isShowControlPad;
  173. }
  174. // scroll through the keyboard controls
  175. if (isShowControlPad == false)
  176. {
  177. if (InputManager.IsActionTriggered(InputManager.Action.CursorDown))
  178. {
  179. if (startIndex < keyboardInfo.totalActionList.Length -
  180. maxActionDisplay)
  181. {
  182. startIndex++;
  183. keyboardInfo.selectedIndex++;
  184. }
  185. }
  186. if (InputManager.IsActionTriggered(InputManager.Action.CursorUp))
  187. {
  188. if (startIndex > 0)
  189. {
  190. startIndex--;
  191. keyboardInfo.selectedIndex--;
  192. }
  193. }
  194. }
  195. #endif
  196. }
  197. /// <summary>
  198. /// Draws the control screen
  199. /// </summary>
  200. /// <param name="gameTime">Provides a snapshot of timing values</param>
  201. public override void Draw(GameTime gameTime)
  202. {
  203. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  204. Vector2 textPosition = Vector2.Zero;
  205. spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, ScreenManager.GlobalTransformation);
  206. // Draw the background texture
  207. spriteBatch.Draw(backgroundTexture, Vector2.Zero, Color.White);
  208. // Draw the back icon and text
  209. spriteBatch.Draw(backTexture, backPosition, Color.White);
  210. spriteBatch.DrawString(Fonts.ButtonNamesFont, "Back",
  211. new Vector2(backPosition.X + 55, backPosition.Y + 5), Color.White);
  212. // Draw the plank
  213. spriteBatch.Draw(plankTexture, plankPosition, Color.White);
  214. #if !XBOX
  215. // Draw the trigger buttons
  216. spriteBatch.Draw(leftTriggerButton, leftTriggerPosition, Color.White);
  217. spriteBatch.Draw(rightTriggerButton, rightTriggerPosition, Color.White);
  218. #endif
  219. // Draw the base border
  220. spriteBatch.Draw(baseBorderTexture, baseBorderPosition, Color.White);
  221. // draw the control pad screen
  222. if (isShowControlPad)
  223. {
  224. spriteBatch.Draw(controlPadTexture, controlPosition,
  225. Color.White);
  226. for (int i = 0; i < leftStrings.Length; i++)
  227. {
  228. spriteBatch.DrawString(Fonts.DescriptionFont, leftStrings[i].text,
  229. leftStrings[i].textPosition, Color.Black);
  230. }
  231. for (int i = 0; i < rightStrings.Length; i++)
  232. {
  233. spriteBatch.DrawString(Fonts.DescriptionFont, rightStrings[i].text,
  234. rightStrings[i].textPosition, Color.Black);
  235. }
  236. // Near left trigger
  237. spriteBatch.DrawString(Fonts.PlayerStatisticsFont, "Keyboard",
  238. new Vector2(leftTriggerPosition.X + (leftTriggerButton.Width -
  239. Fonts.PlayerStatisticsFont.MeasureString("Keyboard").X) / 2,
  240. rightTriggerPosition.Y + 85),
  241. Color.Black);
  242. // Near right trigger
  243. spriteBatch.DrawString(Fonts.PlayerStatisticsFont, "Keyboard",
  244. new Vector2(rightTriggerPosition.X + (rightTriggerButton.Width -
  245. Fonts.PlayerStatisticsFont.MeasureString("Keyboard").X) / 2,
  246. rightTriggerPosition.Y + 85),
  247. Color.Black);
  248. // Draw the title text
  249. titlePosition.X = plankPosition.X + (plankTexture.Width -
  250. Fonts.HeaderFont.MeasureString("Gamepad").X) / 2;
  251. titlePosition.Y = plankPosition.Y + (plankTexture.Height -
  252. Fonts.HeaderFont.MeasureString("Gamepad").Y) / 2;
  253. spriteBatch.DrawString(Fonts.HeaderFont, "Gamepad", titlePosition, Fonts.TitleColor, MathHelper.ToRadians(-3.0f), Vector2.Zero, 1.0f, SpriteEffects.None, 0f);
  254. }
  255. else // draws the keyboard screen
  256. {
  257. const float spacing = 47;
  258. string keyboardString;
  259. spriteBatch.Draw(keyboardTexture, keyboardPosition, Color.White);
  260. for (int j = 0, i = startIndex; i < startIndex + maxActionDisplay;
  261. i++, j++)
  262. {
  263. keyboardString = InputManager.GetActionName((InputManager.Action)i);
  264. textPosition.X = chartLine1Position +
  265. ((chartLine2Position - chartLine1Position) -
  266. Fonts.DescriptionFont.MeasureString(keyboardString).X) / 2;
  267. textPosition.Y = 253 + (spacing * j);
  268. // Draw the action
  269. spriteBatch.DrawString(Fonts.DescriptionFont, keyboardString,
  270. textPosition, Color.Black);
  271. // Draw the key one
  272. keyboardString =
  273. keyboardInfo.totalActionList[i].keyboardKeys[0].ToString();
  274. textPosition.X = chartLine2Position +
  275. ((chartLine3Position - chartLine2Position) -
  276. Fonts.DescriptionFont.MeasureString(keyboardString).X) / 2;
  277. spriteBatch.DrawString(Fonts.DescriptionFont, keyboardString,
  278. textPosition, Color.Black);
  279. // Draw the key two
  280. if (keyboardInfo.totalActionList[i].keyboardKeys.Count > 1)
  281. {
  282. keyboardString = keyboardInfo.totalActionList[i].
  283. keyboardKeys[1].ToString();
  284. textPosition.X = chartLine3Position +
  285. ((chartLine4Position - chartLine3Position) -
  286. Fonts.DescriptionFont.MeasureString(keyboardString).X) / 2;
  287. spriteBatch.DrawString(Fonts.DescriptionFont, keyboardString,
  288. textPosition, Color.Black);
  289. }
  290. else
  291. {
  292. textPosition.X = chartLine3Position +
  293. ((chartLine4Position - chartLine3Position) -
  294. Fonts.DescriptionFont.MeasureString("---").X) / 2;
  295. spriteBatch.DrawString(Fonts.DescriptionFont, "---",
  296. textPosition, Color.Black);
  297. }
  298. }
  299. // Draw the Action
  300. actionPosition.X = chartLine1Position +
  301. ((chartLine2Position - chartLine1Position) -
  302. Fonts.CaptionFont.MeasureString("Action").X) / 2;
  303. actionPosition.Y = 200;
  304. spriteBatch.DrawString(Fonts.CaptionFont, "Action", actionPosition,
  305. Fonts.CaptionColor);
  306. // Draw the Key 1
  307. key1Position.X = chartLine2Position +
  308. ((chartLine3Position - chartLine2Position) -
  309. Fonts.CaptionFont.MeasureString("Key 1").X) / 2;
  310. key1Position.Y = 200;
  311. spriteBatch.DrawString(Fonts.CaptionFont, "Key 1", key1Position,
  312. Fonts.CaptionColor);
  313. // Draw the Key 2
  314. key2Position.X = chartLine3Position +
  315. ((chartLine4Position - chartLine3Position) -
  316. Fonts.CaptionFont.MeasureString("Key 2").X) / 2;
  317. key2Position.Y = 200;
  318. spriteBatch.DrawString(Fonts.CaptionFont, "Key 2", key2Position,
  319. Fonts.CaptionColor);
  320. // Near left trigger
  321. spriteBatch.DrawString(Fonts.PlayerStatisticsFont, "Gamepad",
  322. new Vector2(leftTriggerPosition.X + (leftTriggerButton.Width -
  323. Fonts.PlayerStatisticsFont.MeasureString("Gamepad").X) / 2,
  324. rightTriggerPosition.Y + 85), Color.Black);
  325. // Near right trigger
  326. spriteBatch.DrawString(Fonts.PlayerStatisticsFont, "Gamepad",
  327. new Vector2(rightTriggerPosition.X + (rightTriggerButton.Width -
  328. Fonts.PlayerStatisticsFont.MeasureString("Gamepad").X) / 2,
  329. rightTriggerPosition.Y + 85), Color.Black);
  330. // Draw the title text
  331. titlePosition.X = plankPosition.X + (plankTexture.Width -
  332. Fonts.HeaderFont.MeasureString("Keyboard").X) / 2;
  333. titlePosition.Y = plankPosition.Y + (plankTexture.Height -
  334. Fonts.HeaderFont.MeasureString("Keyboard").Y) / 2;
  335. spriteBatch.DrawString(Fonts.HeaderFont, "Keyboard", titlePosition, Fonts.TitleColor, MathHelper.ToRadians(-3.0f), Vector2.Zero, 1.0f, SpriteEffects.None, 0f);
  336. // Draw the scroll textures
  337. spriteBatch.Draw(scrollUpTexture, scrollUpPosition, Color.White);
  338. spriteBatch.Draw(scrollDownTexture, scrollDownPosition, Color.White);
  339. }
  340. spriteBatch.End();
  341. }
  342. }
  343. }