SaveLoadScreen.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // SaveLoadScreen.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Graphics;
  14. using Microsoft.Xna.Framework.Content;
  15. using RolePlayingGameData;
  16. #endregion
  17. namespace RolePlaying
  18. {
  19. /// <summary>
  20. /// Displays a list of existing save games,
  21. /// allowing the user to save, load, or delete.
  22. /// </summary>
  23. class SaveLoadScreen : GameScreen
  24. {
  25. public enum SaveLoadScreenMode
  26. {
  27. Save,
  28. Load,
  29. };
  30. /// <summary>
  31. /// The mode of this screen.
  32. /// </summary>
  33. private SaveLoadScreenMode mode;
  34. /// <summary>
  35. /// The current selected slot.
  36. /// </summary>
  37. private int currentSlot;
  38. #region Graphics Data
  39. private Texture2D backgroundTexture;
  40. private Vector2 backgroundPosition;
  41. private Texture2D plankTexture;
  42. private Vector2 plankPosition;
  43. private Texture2D backTexture;
  44. private Vector2 backPosition;
  45. private Texture2D deleteTexture;
  46. private Vector2 deletePosition = new Vector2(400f, 610f);
  47. private Vector2 deleteTextPosition = new Vector2(410f, 615f);
  48. private Texture2D selectTexture;
  49. private Vector2 selectPosition;
  50. private Texture2D lineBorderTexture;
  51. private Vector2 lineBorderPosition;
  52. private Texture2D highlightTexture;
  53. private Texture2D arrowTexture;
  54. private Vector2 titleTextPosition;
  55. private Vector2 backTextPosition;
  56. private Vector2 selectTextPosition;
  57. #endregion
  58. #region Initialization
  59. /// <summary>
  60. /// Create a new SaveLoadScreen object.
  61. /// </summary>
  62. public SaveLoadScreen(SaveLoadScreenMode mode) : base()
  63. {
  64. this.mode = mode;
  65. // refresh the save game descriptions
  66. Session.RefreshSaveGameDescriptions();
  67. }
  68. /// <summary>
  69. /// Loads the graphics content for this screen.
  70. /// </summary>
  71. public override void LoadContent()
  72. {
  73. // load the textures
  74. ContentManager content = ScreenManager.Game.Content;
  75. backgroundTexture =
  76. content.Load<Texture2D>(@"Textures\MainMenu\MainMenu");
  77. plankTexture =
  78. content.Load<Texture2D>(@"Textures\MainMenu\MainMenuPlank03");
  79. backTexture =
  80. content.Load<Texture2D>(@"Textures\Buttons\BButton");
  81. selectTexture =
  82. content.Load<Texture2D>(@"Textures\Buttons\AButton");
  83. deleteTexture =
  84. content.Load<Texture2D>(@"Textures\Buttons\XButton");
  85. lineBorderTexture =
  86. content.Load<Texture2D>(@"Textures\GameScreens\LineBorder");
  87. highlightTexture =
  88. content.Load<Texture2D>(@"Textures\GameScreens\HighlightLarge");
  89. arrowTexture =
  90. content.Load<Texture2D>(@"Textures\GameScreens\SelectionArrow");
  91. // calculate the image positions
  92. Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
  93. backgroundPosition = new Vector2(
  94. (viewport.Width - backgroundTexture.Width) / 2,
  95. (viewport.Height - backgroundTexture.Height) / 2);
  96. plankPosition = backgroundPosition + new Vector2(
  97. backgroundTexture.Width / 2 - plankTexture.Width / 2,
  98. 60f);
  99. backPosition = backgroundPosition + new Vector2(225, 610);
  100. selectPosition = backgroundPosition + new Vector2(1120, 610);
  101. lineBorderPosition = backgroundPosition + new Vector2(200, 570);
  102. // calculate the text positions
  103. titleTextPosition = backgroundPosition + new Vector2(
  104. plankPosition.X + (plankTexture.Width -
  105. Fonts.HeaderFont.MeasureString("Load").X) / 2,
  106. plankPosition.Y + (plankTexture.Height -
  107. Fonts.HeaderFont.MeasureString("Load").Y) / 2);
  108. backTextPosition = new Vector2(backPosition.X + 55, backPosition.Y + 5);
  109. deleteTextPosition.X += deleteTexture.Width;
  110. selectTextPosition = new Vector2(
  111. selectPosition.X - Fonts.ButtonNamesFont.MeasureString("Select").X - 5,
  112. selectPosition.Y + 5);
  113. base.LoadContent();
  114. }
  115. #endregion
  116. #region Handle Input
  117. /// <summary>
  118. /// Respond to user input.
  119. /// </summary>
  120. public override void HandleInput()
  121. {
  122. // handle exiting the screen
  123. if (InputManager.IsActionTriggered(InputManager.Action.Back))
  124. {
  125. ExitScreen();
  126. return;
  127. }
  128. // handle selecting a save game
  129. if (InputManager.IsActionTriggered(InputManager.Action.Ok) &&
  130. (Session.SaveGameDescriptions != null))
  131. {
  132. switch (mode)
  133. {
  134. case SaveLoadScreenMode.Load:
  135. if ((currentSlot >= 0) &&
  136. (currentSlot < Session.SaveGameDescriptions.Count) &&
  137. (Session.SaveGameDescriptions[currentSlot] != null))
  138. {
  139. if (Session.IsActive)
  140. {
  141. MessageBoxScreen messageBoxScreen = new MessageBoxScreen(
  142. "Are you sure you want to load this game?");
  143. messageBoxScreen.Accepted +=
  144. ConfirmLoadMessageBoxAccepted;
  145. ScreenManager.AddScreen(messageBoxScreen);
  146. }
  147. else
  148. {
  149. ConfirmLoadMessageBoxAccepted(null, EventArgs.Empty);
  150. }
  151. }
  152. break;
  153. case SaveLoadScreenMode.Save:
  154. if ((currentSlot >= 0) &&
  155. (currentSlot <= Session.SaveGameDescriptions.Count))
  156. {
  157. if (currentSlot == Session.SaveGameDescriptions.Count)
  158. {
  159. ConfirmSaveMessageBoxAccepted(null, EventArgs.Empty);
  160. }
  161. else
  162. {
  163. MessageBoxScreen messageBoxScreen = new MessageBoxScreen(
  164. "Are you sure you want to overwrite this save game?");
  165. messageBoxScreen.Accepted +=
  166. ConfirmSaveMessageBoxAccepted;
  167. ScreenManager.AddScreen(messageBoxScreen);
  168. }
  169. }
  170. break;
  171. }
  172. }
  173. // handle deletion
  174. else if (InputManager.IsActionTriggered(InputManager.Action.DropUnEquip) &&
  175. (Session.SaveGameDescriptions != null))
  176. {
  177. if ((currentSlot >= 0) &&
  178. (currentSlot < Session.SaveGameDescriptions.Count) &&
  179. (Session.SaveGameDescriptions[currentSlot] != null))
  180. {
  181. MessageBoxScreen messageBoxScreen = new MessageBoxScreen(
  182. "Are you sure you want to delete this save game?");
  183. messageBoxScreen.Accepted += ConfirmDeleteMessageBoxAccepted;
  184. ScreenManager.AddScreen(messageBoxScreen);
  185. }
  186. }
  187. // handle cursor-down
  188. else if (InputManager.IsActionTriggered(InputManager.Action.CursorDown) &&
  189. (Session.SaveGameDescriptions != null))
  190. {
  191. int maximumSlot = Session.SaveGameDescriptions.Count;
  192. if (mode == SaveLoadScreenMode.Save)
  193. {
  194. maximumSlot = Math.Min(maximumSlot + 1,
  195. Session.MaximumSaveGameDescriptions);
  196. }
  197. if (currentSlot < maximumSlot - 1)
  198. {
  199. currentSlot++;
  200. }
  201. }
  202. // handle cursor-up
  203. else if (InputManager.IsActionTriggered(InputManager.Action.CursorUp) &&
  204. (Session.SaveGameDescriptions != null))
  205. {
  206. if (currentSlot >= 1)
  207. {
  208. currentSlot--;
  209. }
  210. }
  211. }
  212. /// <summary>
  213. /// Callback for the Save Game confirmation message box.
  214. /// </summary>
  215. void ConfirmSaveMessageBoxAccepted(object sender, EventArgs e)
  216. {
  217. if ((currentSlot >= 0) &&
  218. (currentSlot <= Session.SaveGameDescriptions.Count))
  219. {
  220. if (currentSlot == Session.SaveGameDescriptions.Count)
  221. {
  222. Session.SaveSession(null);
  223. }
  224. else
  225. {
  226. Session.SaveSession(Session.SaveGameDescriptions[currentSlot]);
  227. }
  228. ExitScreen();
  229. }
  230. }
  231. /// <summary>
  232. /// Delegate type for the save-game-selected-to-load event.
  233. /// </summary>
  234. /// <param name="saveGameDescription">
  235. /// The description of the file to load.
  236. /// </param>
  237. public delegate void LoadingSaveGameHandler(
  238. SaveGameDescription saveGameDescription);
  239. /// <summary>
  240. /// Fired when a save game is selected to load.
  241. /// </summary>
  242. /// <remarks>
  243. /// Loading save games exits multiple screens,
  244. /// so we use events to move backwards.
  245. /// </remarks>
  246. public event LoadingSaveGameHandler LoadingSaveGame;
  247. /// <summary>
  248. /// Callback for the Load Game confirmation message box.
  249. /// </summary>
  250. void ConfirmLoadMessageBoxAccepted(object sender, EventArgs e)
  251. {
  252. if ((Session.SaveGameDescriptions != null) && (currentSlot >= 0) &&
  253. (currentSlot < Session.SaveGameDescriptions.Count) &&
  254. (Session.SaveGameDescriptions[currentSlot] != null))
  255. {
  256. ExitScreen();
  257. if (LoadingSaveGame != null)
  258. {
  259. LoadingSaveGame(Session.SaveGameDescriptions[currentSlot]);
  260. }
  261. }
  262. }
  263. /// <summary>
  264. /// Callback for the Delete Game confirmation message box.
  265. /// </summary>
  266. void ConfirmDeleteMessageBoxAccepted(object sender, EventArgs e)
  267. {
  268. if ((Session.SaveGameDescriptions != null) && (currentSlot >= 0) &&
  269. (currentSlot < Session.SaveGameDescriptions.Count) &&
  270. (Session.SaveGameDescriptions[currentSlot] != null))
  271. {
  272. Session.DeleteSaveGame(Session.SaveGameDescriptions[currentSlot]);
  273. }
  274. }
  275. #endregion
  276. #region Drawing
  277. /// <summary>
  278. /// Draws the screen.
  279. /// </summary>
  280. public override void Draw(GameTime gameTime)
  281. {
  282. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  283. spriteBatch.Begin();
  284. spriteBatch.Draw(backgroundTexture, backgroundPosition, Color.White);
  285. spriteBatch.Draw(plankTexture, plankPosition, Color.White);
  286. spriteBatch.Draw(lineBorderTexture, lineBorderPosition, Color.White);
  287. spriteBatch.Draw(backTexture, backPosition, Color.White);
  288. spriteBatch.DrawString(Fonts.ButtonNamesFont, "Back",
  289. backTextPosition, Color.White);
  290. spriteBatch.DrawString(Fonts.HeaderFont,
  291. (mode == SaveLoadScreenMode.Load ? "Load" : "Save"),
  292. titleTextPosition, Fonts.TitleColor);
  293. if ((Session.SaveGameDescriptions != null))
  294. {
  295. for (int i = 0; i < Session.SaveGameDescriptions.Count; i++)
  296. {
  297. Vector2 descriptionTextPosition = new Vector2(295f,
  298. 200f + i * (Fonts.GearInfoFont.LineSpacing + 40f));
  299. Color descriptionTextColor = Color.Black;
  300. // if the save game is selected, draw the highlight color
  301. if (i == currentSlot)
  302. {
  303. descriptionTextColor = Fonts.HighlightColor;
  304. spriteBatch.Draw(highlightTexture,
  305. descriptionTextPosition + new Vector2(-100, -23),
  306. Color.White);
  307. spriteBatch.Draw(arrowTexture,
  308. descriptionTextPosition + new Vector2(-75, -15),
  309. Color.White);
  310. spriteBatch.Draw(deleteTexture, deletePosition,
  311. Color.White);
  312. spriteBatch.DrawString(Fonts.ButtonNamesFont, "Delete",
  313. deleteTextPosition, Color.White);
  314. spriteBatch.Draw(selectTexture, selectPosition, Color.White);
  315. spriteBatch.DrawString(Fonts.ButtonNamesFont, "Select",
  316. selectTextPosition, Color.White);
  317. }
  318. spriteBatch.DrawString(Fonts.GearInfoFont,
  319. Session.SaveGameDescriptions[i].ChapterName,
  320. descriptionTextPosition, descriptionTextColor);
  321. descriptionTextPosition.X = 650;
  322. spriteBatch.DrawString(Fonts.GearInfoFont,
  323. Session.SaveGameDescriptions[i].Description,
  324. descriptionTextPosition, descriptionTextColor);
  325. }
  326. // if there is space for one, add an empty entry
  327. if ((mode == SaveLoadScreenMode.Save) &&
  328. (Session.SaveGameDescriptions.Count <
  329. Session.MaximumSaveGameDescriptions))
  330. {
  331. int i = Session.SaveGameDescriptions.Count;
  332. Vector2 descriptionTextPosition = new Vector2(
  333. 295f,
  334. 200f + i * (Fonts.GearInfoFont.LineSpacing + 40f));
  335. Color descriptionTextColor = Color.Black;
  336. // if the save game is selected, draw the highlight color
  337. if (i == currentSlot)
  338. {
  339. descriptionTextColor = Fonts.HighlightColor;
  340. spriteBatch.Draw(highlightTexture,
  341. descriptionTextPosition + new Vector2(-100, -23),
  342. Color.White);
  343. spriteBatch.Draw(arrowTexture,
  344. descriptionTextPosition + new Vector2(-75, -15),
  345. Color.White);
  346. spriteBatch.Draw(selectTexture, selectPosition, Color.White);
  347. spriteBatch.DrawString(Fonts.ButtonNamesFont, "Select",
  348. selectTextPosition, Color.White);
  349. }
  350. spriteBatch.DrawString(Fonts.GearInfoFont, "-------empty------",
  351. descriptionTextPosition, descriptionTextColor);
  352. descriptionTextPosition.X = 650;
  353. spriteBatch.DrawString(Fonts.GearInfoFont, "-----",
  354. descriptionTextPosition, descriptionTextColor);
  355. }
  356. }
  357. // if there are no slots to load, report that
  358. if (Session.SaveGameDescriptions == null)
  359. {
  360. spriteBatch.DrawString(Fonts.GearInfoFont,
  361. "No Storage Device Available",
  362. new Vector2(295f, 200f), Color.Black);
  363. }
  364. else if ((mode == SaveLoadScreenMode.Load) &&
  365. (Session.SaveGameDescriptions.Count <= 0))
  366. {
  367. spriteBatch.DrawString(Fonts.GearInfoFont,
  368. "No Save Games Available",
  369. new Vector2(295f, 200f), Color.Black);
  370. }
  371. spriteBatch.End();
  372. }
  373. #endregion
  374. }
  375. }