SaveLoadScreen.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. backgroundPosition = new Vector2(
  87. (Session.BACK_BUFFER_WIDTH - backgroundTexture.Width) / 2,
  88. (Session.BACK_BUFFER_HEIGHT - backgroundTexture.Height) / 2);
  89. plankPosition = backgroundPosition + new Vector2(
  90. backgroundTexture.Width / 2 - plankTexture.Width / 2,
  91. 60f);
  92. backPosition = backgroundPosition + new Vector2(225, 610);
  93. selectPosition = backgroundPosition + new Vector2(1120, 610);
  94. lineBorderPosition = backgroundPosition + new Vector2(200, 570);
  95. // calculate the text positions
  96. titleTextPosition = backgroundPosition + new Vector2(
  97. plankPosition.X + (plankTexture.Width -
  98. Fonts.HeaderFont.MeasureString("Load").X) / 2,
  99. plankPosition.Y + (plankTexture.Height -
  100. Fonts.HeaderFont.MeasureString("Load").Y) / 2);
  101. backTextPosition = new Vector2(backPosition.X + 55, backPosition.Y + 5);
  102. deleteTextPosition.X += deleteTexture.Width;
  103. selectTextPosition = new Vector2(
  104. selectPosition.X - Fonts.ButtonNamesFont.MeasureString("Select").X - 5,
  105. selectPosition.Y + 5);
  106. base.LoadContent();
  107. }
  108. /// <summary>
  109. /// Respond to user input.
  110. /// </summary>
  111. public override void HandleInput()
  112. {
  113. // handle exiting the screen
  114. if (InputManager.IsActionTriggered(InputManager.Action.Back))
  115. {
  116. ExitScreen();
  117. return;
  118. }
  119. // handle selecting a save game
  120. if (InputManager.IsActionTriggered(InputManager.Action.Ok) &&
  121. (Session.SaveGameDescriptions != null))
  122. {
  123. switch (mode)
  124. {
  125. case SaveLoadScreenMode.Load:
  126. if ((currentSlot >= 0) &&
  127. (currentSlot < Session.SaveGameDescriptions.Count) &&
  128. (Session.SaveGameDescriptions[currentSlot] != null))
  129. {
  130. if (Session.IsActive)
  131. {
  132. MessageBoxScreen messageBoxScreen = new MessageBoxScreen(
  133. "Are you sure you want to load this game?");
  134. messageBoxScreen.Accepted +=
  135. ConfirmLoadMessageBoxAccepted;
  136. ScreenManager.AddScreen(messageBoxScreen);
  137. }
  138. else
  139. {
  140. ConfirmLoadMessageBoxAccepted(null, EventArgs.Empty);
  141. }
  142. }
  143. break;
  144. case SaveLoadScreenMode.Save:
  145. if ((currentSlot >= 0) &&
  146. (currentSlot <= Session.SaveGameDescriptions.Count))
  147. {
  148. if (currentSlot == Session.SaveGameDescriptions.Count)
  149. {
  150. ConfirmSaveMessageBoxAccepted(null, EventArgs.Empty);
  151. }
  152. else
  153. {
  154. MessageBoxScreen messageBoxScreen = new MessageBoxScreen(
  155. "Are you sure you want to overwrite this save game?");
  156. messageBoxScreen.Accepted +=
  157. ConfirmSaveMessageBoxAccepted;
  158. ScreenManager.AddScreen(messageBoxScreen);
  159. }
  160. }
  161. break;
  162. }
  163. }
  164. // handle deletion
  165. else if (InputManager.IsActionTriggered(InputManager.Action.DropUnEquip) &&
  166. (Session.SaveGameDescriptions != null))
  167. {
  168. if ((currentSlot >= 0) &&
  169. (currentSlot < Session.SaveGameDescriptions.Count) &&
  170. (Session.SaveGameDescriptions[currentSlot] != null))
  171. {
  172. MessageBoxScreen messageBoxScreen = new MessageBoxScreen(
  173. "Are you sure you want to delete this save game?");
  174. messageBoxScreen.Accepted += ConfirmDeleteMessageBoxAccepted;
  175. ScreenManager.AddScreen(messageBoxScreen);
  176. }
  177. }
  178. // handle cursor-down
  179. else if (InputManager.IsActionTriggered(InputManager.Action.CursorDown) &&
  180. (Session.SaveGameDescriptions != null))
  181. {
  182. int maximumSlot = Session.SaveGameDescriptions.Count;
  183. if (mode == SaveLoadScreenMode.Save)
  184. {
  185. maximumSlot = Math.Min(maximumSlot + 1,
  186. Session.MaximumSaveGameDescriptions);
  187. }
  188. if (currentSlot < maximumSlot - 1)
  189. {
  190. currentSlot++;
  191. }
  192. }
  193. // handle cursor-up
  194. else if (InputManager.IsActionTriggered(InputManager.Action.CursorUp) &&
  195. (Session.SaveGameDescriptions != null))
  196. {
  197. if (currentSlot >= 1)
  198. {
  199. currentSlot--;
  200. }
  201. }
  202. }
  203. /// <summary>
  204. /// Callback for the Save Game confirmation message box.
  205. /// </summary>
  206. void ConfirmSaveMessageBoxAccepted(object sender, EventArgs e)
  207. {
  208. if ((currentSlot >= 0) &&
  209. (currentSlot <= Session.SaveGameDescriptions.Count))
  210. {
  211. if (currentSlot == Session.SaveGameDescriptions.Count)
  212. {
  213. Session.SaveSession(null);
  214. }
  215. else
  216. {
  217. Session.SaveSession(Session.SaveGameDescriptions[currentSlot]);
  218. }
  219. ExitScreen();
  220. }
  221. }
  222. /// <summary>
  223. /// Delegate type for the save-game-selected-to-load event.
  224. /// </summary>
  225. /// <param name="saveGameDescription">
  226. /// The description of the file to load.
  227. /// </param>
  228. public delegate void LoadingSaveGameHandler(
  229. SaveGameDescription saveGameDescription);
  230. /// <summary>
  231. /// Fired when a save game is selected to load.
  232. /// </summary>
  233. /// <remarks>
  234. /// Loading save games exits multiple screens,
  235. /// so we use events to move backwards.
  236. /// </remarks>
  237. public event LoadingSaveGameHandler LoadingSaveGame;
  238. /// <summary>
  239. /// Callback for the Load Game confirmation message box.
  240. /// </summary>
  241. void ConfirmLoadMessageBoxAccepted(object sender, EventArgs e)
  242. {
  243. if ((Session.SaveGameDescriptions != null) && (currentSlot >= 0) &&
  244. (currentSlot < Session.SaveGameDescriptions.Count) &&
  245. (Session.SaveGameDescriptions[currentSlot] != null))
  246. {
  247. ExitScreen();
  248. if (LoadingSaveGame != null)
  249. {
  250. LoadingSaveGame(Session.SaveGameDescriptions[currentSlot]);
  251. }
  252. }
  253. }
  254. /// <summary>
  255. /// Callback for the Delete Game confirmation message box.
  256. /// </summary>
  257. void ConfirmDeleteMessageBoxAccepted(object sender, EventArgs e)
  258. {
  259. if ((Session.SaveGameDescriptions != null) && (currentSlot >= 0) &&
  260. (currentSlot < Session.SaveGameDescriptions.Count) &&
  261. (Session.SaveGameDescriptions[currentSlot] != null))
  262. {
  263. // TODO: Re-enable when Storage API returns in future MonoGame version
  264. // Session.DeleteSaveGame(Session.SaveGameDescriptions[currentSlot]);
  265. }
  266. }
  267. /// <summary>
  268. /// Draws the screen.
  269. /// </summary>
  270. public override void Draw(GameTime gameTime)
  271. {
  272. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  273. spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, ScreenManager.GlobalTransformation);
  274. spriteBatch.Draw(backgroundTexture, backgroundPosition, Color.White);
  275. spriteBatch.Draw(plankTexture, plankPosition, Color.White);
  276. spriteBatch.Draw(lineBorderTexture, lineBorderPosition, Color.White);
  277. spriteBatch.Draw(backTexture, backPosition, Color.White);
  278. spriteBatch.DrawString(Fonts.ButtonNamesFont, "Back",
  279. backTextPosition, Color.White);
  280. spriteBatch.DrawString(Fonts.HeaderFont, (mode == SaveLoadScreenMode.Load ? "Load" : "Save"), titleTextPosition, Fonts.TitleColor, MathHelper.ToRadians(-3.0f), Vector2.Zero, 1.0f, SpriteEffects.None, 0f);
  281. if ((Session.SaveGameDescriptions != null))
  282. {
  283. for (int i = 0; i < Session.SaveGameDescriptions.Count; i++)
  284. {
  285. Vector2 descriptionTextPosition = new Vector2(295f,
  286. 200f + i * (Fonts.GearInfoFont.LineSpacing + 40f));
  287. Color descriptionTextColor = Color.Black;
  288. // if the save game is selected, draw the highlight color
  289. if (i == currentSlot)
  290. {
  291. descriptionTextColor = Fonts.HighlightColor;
  292. spriteBatch.Draw(highlightTexture,
  293. descriptionTextPosition + new Vector2(-100, -23),
  294. Color.White);
  295. spriteBatch.Draw(arrowTexture,
  296. descriptionTextPosition + new Vector2(-75, -15),
  297. Color.White);
  298. spriteBatch.Draw(deleteTexture, deletePosition,
  299. Color.White);
  300. spriteBatch.DrawString(Fonts.ButtonNamesFont, "Delete",
  301. deleteTextPosition, Color.White);
  302. spriteBatch.Draw(selectTexture, selectPosition, Color.White);
  303. spriteBatch.DrawString(Fonts.ButtonNamesFont, "Select",
  304. selectTextPosition, Color.White);
  305. }
  306. spriteBatch.DrawString(Fonts.GearInfoFont,
  307. Session.SaveGameDescriptions[i].ChapterName,
  308. descriptionTextPosition, descriptionTextColor);
  309. descriptionTextPosition.X = 650;
  310. spriteBatch.DrawString(Fonts.GearInfoFont,
  311. Session.SaveGameDescriptions[i].Description,
  312. descriptionTextPosition, descriptionTextColor);
  313. }
  314. // if there is space for one, add an empty entry
  315. if ((mode == SaveLoadScreenMode.Save) &&
  316. (Session.SaveGameDescriptions.Count <
  317. Session.MaximumSaveGameDescriptions))
  318. {
  319. int i = Session.SaveGameDescriptions.Count;
  320. Vector2 descriptionTextPosition = new Vector2(
  321. 295f,
  322. 200f + i * (Fonts.GearInfoFont.LineSpacing + 40f));
  323. Color descriptionTextColor = Color.Black;
  324. // if the save game is selected, draw the highlight color
  325. if (i == currentSlot)
  326. {
  327. descriptionTextColor = Fonts.HighlightColor;
  328. spriteBatch.Draw(highlightTexture,
  329. descriptionTextPosition + new Vector2(-100, -23),
  330. Color.White);
  331. spriteBatch.Draw(arrowTexture,
  332. descriptionTextPosition + new Vector2(-75, -15),
  333. Color.White);
  334. spriteBatch.Draw(selectTexture, selectPosition, Color.White);
  335. spriteBatch.DrawString(Fonts.ButtonNamesFont, "Select",
  336. selectTextPosition, Color.White);
  337. }
  338. spriteBatch.DrawString(Fonts.GearInfoFont, "-------empty------",
  339. descriptionTextPosition, descriptionTextColor);
  340. descriptionTextPosition.X = 650;
  341. spriteBatch.DrawString(Fonts.GearInfoFont, "-----",
  342. descriptionTextPosition, descriptionTextColor);
  343. }
  344. }
  345. // if there are no slots to load, report that
  346. if (Session.SaveGameDescriptions == null)
  347. {
  348. spriteBatch.DrawString(Fonts.GearInfoFont,
  349. "No Storage Device Available",
  350. new Vector2(295f, 200f), Color.Black);
  351. }
  352. else if ((mode == SaveLoadScreenMode.Load) &&
  353. (Session.SaveGameDescriptions.Count <= 0))
  354. {
  355. spriteBatch.DrawString(Fonts.GearInfoFont,
  356. "No Save Games Available",
  357. new Vector2(295f, 200f), Color.Black);
  358. }
  359. spriteBatch.End();
  360. }
  361. }
  362. }