123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- #region File Description
- //-----------------------------------------------------------------------------
- // SaveLoadScreen.cs
- //
- // Microsoft XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- #endregion
- #region Using Statements
- using System;
- using System.Collections.Generic;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Content;
- using RolePlayingGameData;
- #endregion
- namespace RolePlaying
- {
- /// <summary>
- /// Displays a list of existing save games,
- /// allowing the user to save, load, or delete.
- /// </summary>
- class SaveLoadScreen : GameScreen
- {
- public enum SaveLoadScreenMode
- {
- Save,
- Load,
- };
- /// <summary>
- /// The mode of this screen.
- /// </summary>
- private SaveLoadScreenMode mode;
- /// <summary>
- /// The current selected slot.
- /// </summary>
- private int currentSlot;
- #region Graphics Data
- private Texture2D backgroundTexture;
- private Vector2 backgroundPosition;
- private Texture2D plankTexture;
- private Vector2 plankPosition;
- private Texture2D backTexture;
- private Vector2 backPosition;
- private Texture2D deleteTexture;
- private Vector2 deletePosition = new Vector2(400f, 610f);
- private Vector2 deleteTextPosition = new Vector2(410f, 615f);
- private Texture2D selectTexture;
- private Vector2 selectPosition;
- private Texture2D lineBorderTexture;
- private Vector2 lineBorderPosition;
- private Texture2D highlightTexture;
- private Texture2D arrowTexture;
- private Vector2 titleTextPosition;
- private Vector2 backTextPosition;
- private Vector2 selectTextPosition;
- #endregion
- #region Initialization
- /// <summary>
- /// Create a new SaveLoadScreen object.
- /// </summary>
- public SaveLoadScreen(SaveLoadScreenMode mode) : base()
- {
- this.mode = mode;
- // refresh the save game descriptions
- Session.RefreshSaveGameDescriptions();
- }
- /// <summary>
- /// Loads the graphics content for this screen.
- /// </summary>
- public override void LoadContent()
- {
- // load the textures
- ContentManager content = ScreenManager.Game.Content;
- backgroundTexture =
- content.Load<Texture2D>(@"Textures\MainMenu\MainMenu");
- plankTexture =
- content.Load<Texture2D>(@"Textures\MainMenu\MainMenuPlank03");
- backTexture =
- content.Load<Texture2D>(@"Textures\Buttons\BButton");
- selectTexture =
- content.Load<Texture2D>(@"Textures\Buttons\AButton");
- deleteTexture =
- content.Load<Texture2D>(@"Textures\Buttons\XButton");
- lineBorderTexture =
- content.Load<Texture2D>(@"Textures\GameScreens\LineBorder");
- highlightTexture =
- content.Load<Texture2D>(@"Textures\GameScreens\HighlightLarge");
- arrowTexture =
- content.Load<Texture2D>(@"Textures\GameScreens\SelectionArrow");
- // calculate the image positions
- Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
- backgroundPosition = new Vector2(
- (viewport.Width - backgroundTexture.Width) / 2,
- (viewport.Height - backgroundTexture.Height) / 2);
- plankPosition = backgroundPosition + new Vector2(
- backgroundTexture.Width / 2 - plankTexture.Width / 2,
- 60f);
- backPosition = backgroundPosition + new Vector2(225, 610);
- selectPosition = backgroundPosition + new Vector2(1120, 610);
- lineBorderPosition = backgroundPosition + new Vector2(200, 570);
- // calculate the text positions
- titleTextPosition = backgroundPosition + new Vector2(
- plankPosition.X + (plankTexture.Width -
- Fonts.HeaderFont.MeasureString("Load").X) / 2,
- plankPosition.Y + (plankTexture.Height -
- Fonts.HeaderFont.MeasureString("Load").Y) / 2);
- backTextPosition = new Vector2(backPosition.X + 55, backPosition.Y + 5);
- deleteTextPosition.X += deleteTexture.Width;
- selectTextPosition = new Vector2(
- selectPosition.X - Fonts.ButtonNamesFont.MeasureString("Select").X - 5,
- selectPosition.Y + 5);
- base.LoadContent();
- }
- #endregion
- #region Handle Input
- /// <summary>
- /// Respond to user input.
- /// </summary>
- public override void HandleInput()
- {
- // handle exiting the screen
- if (InputManager.IsActionTriggered(InputManager.Action.Back))
- {
- ExitScreen();
- return;
- }
-
- // handle selecting a save game
- if (InputManager.IsActionTriggered(InputManager.Action.Ok) &&
- (Session.SaveGameDescriptions != null))
- {
- switch (mode)
- {
- case SaveLoadScreenMode.Load:
- if ((currentSlot >= 0) &&
- (currentSlot < Session.SaveGameDescriptions.Count) &&
- (Session.SaveGameDescriptions[currentSlot] != null))
- {
- if (Session.IsActive)
- {
- MessageBoxScreen messageBoxScreen = new MessageBoxScreen(
- "Are you sure you want to load this game?");
- messageBoxScreen.Accepted +=
- ConfirmLoadMessageBoxAccepted;
- ScreenManager.AddScreen(messageBoxScreen);
- }
- else
- {
- ConfirmLoadMessageBoxAccepted(null, EventArgs.Empty);
- }
- }
- break;
- case SaveLoadScreenMode.Save:
- if ((currentSlot >= 0) &&
- (currentSlot <= Session.SaveGameDescriptions.Count))
- {
- if (currentSlot == Session.SaveGameDescriptions.Count)
- {
- ConfirmSaveMessageBoxAccepted(null, EventArgs.Empty);
- }
- else
- {
- MessageBoxScreen messageBoxScreen = new MessageBoxScreen(
- "Are you sure you want to overwrite this save game?");
- messageBoxScreen.Accepted +=
- ConfirmSaveMessageBoxAccepted;
- ScreenManager.AddScreen(messageBoxScreen);
- }
- }
- break;
- }
- }
- // handle deletion
- else if (InputManager.IsActionTriggered(InputManager.Action.DropUnEquip) &&
- (Session.SaveGameDescriptions != null))
- {
- if ((currentSlot >= 0) &&
- (currentSlot < Session.SaveGameDescriptions.Count) &&
- (Session.SaveGameDescriptions[currentSlot] != null))
- {
- MessageBoxScreen messageBoxScreen = new MessageBoxScreen(
- "Are you sure you want to delete this save game?");
- messageBoxScreen.Accepted += ConfirmDeleteMessageBoxAccepted;
- ScreenManager.AddScreen(messageBoxScreen);
- }
- }
- // handle cursor-down
- else if (InputManager.IsActionTriggered(InputManager.Action.CursorDown) &&
- (Session.SaveGameDescriptions != null))
- {
- int maximumSlot = Session.SaveGameDescriptions.Count;
- if (mode == SaveLoadScreenMode.Save)
- {
- maximumSlot = Math.Min(maximumSlot + 1,
- Session.MaximumSaveGameDescriptions);
- }
- if (currentSlot < maximumSlot - 1)
- {
- currentSlot++;
- }
- }
- // handle cursor-up
- else if (InputManager.IsActionTriggered(InputManager.Action.CursorUp) &&
- (Session.SaveGameDescriptions != null))
- {
- if (currentSlot >= 1)
- {
- currentSlot--;
- }
- }
- }
- /// <summary>
- /// Callback for the Save Game confirmation message box.
- /// </summary>
- void ConfirmSaveMessageBoxAccepted(object sender, EventArgs e)
- {
- if ((currentSlot >= 0) &&
- (currentSlot <= Session.SaveGameDescriptions.Count))
- {
- if (currentSlot == Session.SaveGameDescriptions.Count)
- {
- Session.SaveSession(null);
- }
- else
- {
- Session.SaveSession(Session.SaveGameDescriptions[currentSlot]);
- }
- ExitScreen();
- }
- }
- /// <summary>
- /// Delegate type for the save-game-selected-to-load event.
- /// </summary>
- /// <param name="saveGameDescription">
- /// The description of the file to load.
- /// </param>
- public delegate void LoadingSaveGameHandler(
- SaveGameDescription saveGameDescription);
- /// <summary>
- /// Fired when a save game is selected to load.
- /// </summary>
- /// <remarks>
- /// Loading save games exits multiple screens,
- /// so we use events to move backwards.
- /// </remarks>
- public event LoadingSaveGameHandler LoadingSaveGame;
- /// <summary>
- /// Callback for the Load Game confirmation message box.
- /// </summary>
- void ConfirmLoadMessageBoxAccepted(object sender, EventArgs e)
- {
- if ((Session.SaveGameDescriptions != null) && (currentSlot >= 0) &&
- (currentSlot < Session.SaveGameDescriptions.Count) &&
- (Session.SaveGameDescriptions[currentSlot] != null))
- {
- ExitScreen();
- if (LoadingSaveGame != null)
- {
- LoadingSaveGame(Session.SaveGameDescriptions[currentSlot]);
- }
- }
- }
- /// <summary>
- /// Callback for the Delete Game confirmation message box.
- /// </summary>
- void ConfirmDeleteMessageBoxAccepted(object sender, EventArgs e)
- {
- if ((Session.SaveGameDescriptions != null) && (currentSlot >= 0) &&
- (currentSlot < Session.SaveGameDescriptions.Count) &&
- (Session.SaveGameDescriptions[currentSlot] != null))
- {
- Session.DeleteSaveGame(Session.SaveGameDescriptions[currentSlot]);
- }
- }
- #endregion
- #region Drawing
- /// <summary>
- /// Draws the screen.
- /// </summary>
- public override void Draw(GameTime gameTime)
- {
- SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
- spriteBatch.Begin();
- spriteBatch.Draw(backgroundTexture, backgroundPosition, Color.White);
- spriteBatch.Draw(plankTexture, plankPosition, Color.White);
- spriteBatch.Draw(lineBorderTexture, lineBorderPosition, Color.White);
- spriteBatch.Draw(backTexture, backPosition, Color.White);
- spriteBatch.DrawString(Fonts.ButtonNamesFont, "Back",
- backTextPosition, Color.White);
- spriteBatch.DrawString(Fonts.HeaderFont,
- (mode == SaveLoadScreenMode.Load ? "Load" : "Save"),
- titleTextPosition, Fonts.TitleColor);
- if ((Session.SaveGameDescriptions != null))
- {
- for (int i = 0; i < Session.SaveGameDescriptions.Count; i++)
- {
- Vector2 descriptionTextPosition = new Vector2(295f,
- 200f + i * (Fonts.GearInfoFont.LineSpacing + 40f));
- Color descriptionTextColor = Color.Black;
- // if the save game is selected, draw the highlight color
- if (i == currentSlot)
- {
- descriptionTextColor = Fonts.HighlightColor;
- spriteBatch.Draw(highlightTexture,
- descriptionTextPosition + new Vector2(-100, -23),
- Color.White);
- spriteBatch.Draw(arrowTexture,
- descriptionTextPosition + new Vector2(-75, -15),
- Color.White);
- spriteBatch.Draw(deleteTexture, deletePosition,
- Color.White);
- spriteBatch.DrawString(Fonts.ButtonNamesFont, "Delete",
- deleteTextPosition, Color.White);
- spriteBatch.Draw(selectTexture, selectPosition, Color.White);
- spriteBatch.DrawString(Fonts.ButtonNamesFont, "Select",
- selectTextPosition, Color.White);
- }
- spriteBatch.DrawString(Fonts.GearInfoFont,
- Session.SaveGameDescriptions[i].ChapterName,
- descriptionTextPosition, descriptionTextColor);
- descriptionTextPosition.X = 650;
- spriteBatch.DrawString(Fonts.GearInfoFont,
- Session.SaveGameDescriptions[i].Description,
- descriptionTextPosition, descriptionTextColor);
- }
- // if there is space for one, add an empty entry
- if ((mode == SaveLoadScreenMode.Save) &&
- (Session.SaveGameDescriptions.Count <
- Session.MaximumSaveGameDescriptions))
- {
- int i = Session.SaveGameDescriptions.Count;
- Vector2 descriptionTextPosition = new Vector2(
- 295f,
- 200f + i * (Fonts.GearInfoFont.LineSpacing + 40f));
- Color descriptionTextColor = Color.Black;
- // if the save game is selected, draw the highlight color
- if (i == currentSlot)
- {
- descriptionTextColor = Fonts.HighlightColor;
- spriteBatch.Draw(highlightTexture,
- descriptionTextPosition + new Vector2(-100, -23),
- Color.White);
- spriteBatch.Draw(arrowTexture,
- descriptionTextPosition + new Vector2(-75, -15),
- Color.White);
- spriteBatch.Draw(selectTexture, selectPosition, Color.White);
- spriteBatch.DrawString(Fonts.ButtonNamesFont, "Select",
- selectTextPosition, Color.White);
- }
- spriteBatch.DrawString(Fonts.GearInfoFont, "-------empty------",
- descriptionTextPosition, descriptionTextColor);
- descriptionTextPosition.X = 650;
- spriteBatch.DrawString(Fonts.GearInfoFont, "-----",
- descriptionTextPosition, descriptionTextColor);
- }
- }
- // if there are no slots to load, report that
- if (Session.SaveGameDescriptions == null)
- {
- spriteBatch.DrawString(Fonts.GearInfoFont,
- "No Storage Device Available",
- new Vector2(295f, 200f), Color.Black);
- }
- else if ((mode == SaveLoadScreenMode.Load) &&
- (Session.SaveGameDescriptions.Count <= 0))
- {
- spriteBatch.DrawString(Fonts.GearInfoFont,
- "No Save Games Available",
- new Vector2(295f, 200f), Color.Black);
- }
- spriteBatch.End();
- }
- #endregion
- }
- }
|