SaveLoadScreen.cs 16 KB

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