SaveLoadScreen.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. using System.IO;
  14. namespace RolePlaying
  15. {
  16. /// <summary>
  17. /// Displays a list of existing save games,
  18. /// allowing the user to save, load, or delete.
  19. /// </summary>
  20. class SaveLoadScreen : GameScreen
  21. {
  22. public enum SaveLoadScreenMode
  23. {
  24. Save,
  25. Load,
  26. };
  27. /// <summary>
  28. /// The mode of this screen.
  29. /// </summary>
  30. private SaveLoadScreenMode mode;
  31. /// <summary>
  32. /// The current selected slot.
  33. /// </summary>
  34. private int currentSlot;
  35. private Texture2D backgroundTexture;
  36. private Vector2 backgroundPosition;
  37. private Texture2D plankTexture;
  38. private Vector2 plankPosition;
  39. private Texture2D backTexture;
  40. private Vector2 backPosition;
  41. private Texture2D deleteTexture;
  42. private Vector2 deletePosition = new Vector2(400f, 610f);
  43. private Vector2 deleteTextPosition = new Vector2(410f, 615f);
  44. private Texture2D selectTexture;
  45. private Vector2 selectPosition;
  46. private Texture2D lineBorderTexture;
  47. private Vector2 lineBorderPosition;
  48. private Texture2D highlightTexture;
  49. private Texture2D arrowTexture;
  50. private Vector2 titleTextPosition;
  51. private Vector2 backTextPosition;
  52. private Vector2 selectTextPosition;
  53. /// <summary>
  54. /// Create a new SaveLoadScreen object.
  55. /// </summary>
  56. public SaveLoadScreen(SaveLoadScreenMode mode) : base()
  57. {
  58. this.mode = mode;
  59. // refresh the save game descriptions
  60. // TODO: Re-enable when Storage API returns in future MonoGame version
  61. // Session.RefreshSaveGameDescriptions();
  62. }
  63. /// <summary>
  64. /// Loads the graphics content for this screen.
  65. /// </summary>
  66. public override void LoadContent()
  67. {
  68. // load the textures
  69. ContentManager content = ScreenManager.Game.Content;
  70. backgroundTexture =
  71. content.Load<Texture2D>(Path.Combine("Textures", "MainMenu", "MainMenu"));
  72. plankTexture =
  73. content.Load<Texture2D>(Path.Combine("Textures", "MainMenu", "MainMenuPlank03"));
  74. backTexture =
  75. content.Load<Texture2D>(Path.Combine("Textures", "Buttons", "BButton"));
  76. selectTexture =
  77. content.Load<Texture2D>(Path.Combine("Textures", "Buttons", "AButton"));
  78. deleteTexture =
  79. content.Load<Texture2D>(Path.Combine("Textures", "Buttons", "XButton"));
  80. lineBorderTexture =
  81. content.Load<Texture2D>(Path.Combine("Textures", "GameScreens", "LineBorder"));
  82. highlightTexture =
  83. content.Load<Texture2D>(Path.Combine("Textures", "GameScreens", "HighlightLarge"));
  84. arrowTexture =
  85. content.Load<Texture2D>(Path.Combine("Textures", "GameScreens", "SelectionArrow"));
  86. // calculate the image positions
  87. backgroundPosition = new Vector2(
  88. (Session.BACK_BUFFER_WIDTH - backgroundTexture.Width) / 2,
  89. (Session.BACK_BUFFER_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.InputAction.Back))
  116. {
  117. ExitScreen();
  118. return;
  119. }
  120. // handle selecting a save game
  121. if (InputManager.IsActionTriggered(InputManager.InputAction.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.InputAction.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.InputAction.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.InputAction.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(SpriteSortMode.Deferred, null, null, null, null, null, ScreenManager.GlobalTransformation);
  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, (mode == SaveLoadScreenMode.Load ? "Load" : "Save"), titleTextPosition, Fonts.TitleColor, MathHelper.ToRadians(-3.0f), Vector2.Zero, 1.0f, SpriteEffects.None, 0f);
  282. if ((Session.SaveGameDescriptions != null))
  283. {
  284. for (int i = 0; i < Session.SaveGameDescriptions.Count; i++)
  285. {
  286. Vector2 descriptionTextPosition = new Vector2(295f,
  287. 200f + i * (Fonts.GearInfoFont.LineSpacing + 40f));
  288. Color descriptionTextColor = Color.Black;
  289. // if the save game is selected, draw the highlight color
  290. if (i == currentSlot)
  291. {
  292. descriptionTextColor = Fonts.HighlightColor;
  293. spriteBatch.Draw(highlightTexture,
  294. descriptionTextPosition + new Vector2(-100, -23),
  295. Color.White);
  296. spriteBatch.Draw(arrowTexture,
  297. descriptionTextPosition + new Vector2(-75, -15),
  298. Color.White);
  299. spriteBatch.Draw(deleteTexture, deletePosition,
  300. Color.White);
  301. spriteBatch.DrawString(Fonts.ButtonNamesFont, "Delete",
  302. deleteTextPosition, Color.White);
  303. spriteBatch.Draw(selectTexture, selectPosition, Color.White);
  304. spriteBatch.DrawString(Fonts.ButtonNamesFont, "Select",
  305. selectTextPosition, Color.White);
  306. }
  307. spriteBatch.DrawString(Fonts.GearInfoFont,
  308. Session.SaveGameDescriptions[i].ChapterName,
  309. descriptionTextPosition, descriptionTextColor);
  310. descriptionTextPosition.X = 650;
  311. spriteBatch.DrawString(Fonts.GearInfoFont,
  312. Session.SaveGameDescriptions[i].Description,
  313. descriptionTextPosition, descriptionTextColor);
  314. }
  315. // if there is space for one, add an empty entry
  316. if ((mode == SaveLoadScreenMode.Save) &&
  317. (Session.SaveGameDescriptions.Count <
  318. Session.MaximumSaveGameDescriptions))
  319. {
  320. int i = Session.SaveGameDescriptions.Count;
  321. Vector2 descriptionTextPosition = new Vector2(
  322. 295f,
  323. 200f + i * (Fonts.GearInfoFont.LineSpacing + 40f));
  324. Color descriptionTextColor = Color.Black;
  325. // if the save game is selected, draw the highlight color
  326. if (i == currentSlot)
  327. {
  328. descriptionTextColor = Fonts.HighlightColor;
  329. spriteBatch.Draw(highlightTexture,
  330. descriptionTextPosition + new Vector2(-100, -23),
  331. Color.White);
  332. spriteBatch.Draw(arrowTexture,
  333. descriptionTextPosition + new Vector2(-75, -15),
  334. Color.White);
  335. spriteBatch.Draw(selectTexture, selectPosition, Color.White);
  336. spriteBatch.DrawString(Fonts.ButtonNamesFont, "Select",
  337. selectTextPosition, Color.White);
  338. }
  339. spriteBatch.DrawString(Fonts.GearInfoFont, "-------empty------",
  340. descriptionTextPosition, descriptionTextColor);
  341. descriptionTextPosition.X = 650;
  342. spriteBatch.DrawString(Fonts.GearInfoFont, "-----",
  343. descriptionTextPosition, descriptionTextColor);
  344. }
  345. }
  346. // if there are no slots to load, report that
  347. if (Session.SaveGameDescriptions == null)
  348. {
  349. spriteBatch.DrawString(Fonts.GearInfoFont,
  350. "No Storage Device Available",
  351. new Vector2(295f, 200f), Color.Black);
  352. }
  353. else if ((mode == SaveLoadScreenMode.Load) &&
  354. (Session.SaveGameDescriptions.Count <= 0))
  355. {
  356. spriteBatch.DrawString(Fonts.GearInfoFont,
  357. "No Save Games Available",
  358. new Vector2(295f, 200f), Color.Black);
  359. }
  360. spriteBatch.End();
  361. }
  362. }
  363. }