From ced3d03bdb3ce866d832e03fb212865140905a9a Mon Sep 17 00:00:00 2001 From: Thomas Leyh Date: Sun, 24 Jul 2016 08:14:18 +0200 Subject: Add project files. --- V3/Screens/LoadScreen.cs | 124 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 V3/Screens/LoadScreen.cs (limited to 'V3/Screens/LoadScreen.cs') diff --git a/V3/Screens/LoadScreen.cs b/V3/Screens/LoadScreen.cs new file mode 100644 index 0000000..0e58147 --- /dev/null +++ b/V3/Screens/LoadScreen.cs @@ -0,0 +1,124 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; +using Ninject; +using System.Collections.Generic; +using V3.Data; +using V3.Input; +using V3.Widgets; + +namespace V3.Screens +{ + /// + /// The screen for the load menu. + /// + // ReSharper disable once ClassNeverInstantiated.Global + public sealed class LoadScreen : AbstractScreen, IInitializable + { + private readonly ContentManager mContentManager; + private readonly IMenu mMenu; + private readonly ISaveGameManager mSaveGameManager; + private readonly MenuActions mMenuActions; + private readonly WidgetFactory mWidgetFactory; + + private Texture2D mRectangle; + private Button mButtonBack; + private SelectButton mButtonSaveGame; + private Button mButtonLoad; + private List mSaveGames; + + /// + /// Creates a new load screen. + /// + public LoadScreen(ContentManager contentManager, FormMenu menu, + ISaveGameManager saveGameManager, MenuActions menuActions, + WidgetFactory widgetFactory) + : base(false, true) + { + mContentManager = contentManager; + mMenu = menu; + mSaveGameManager = saveGameManager; + mMenuActions = menuActions; + mWidgetFactory = widgetFactory; + } + + public void Initialize() + { + mRectangle = mContentManager.Load("Sprites/WhiteRectangle"); + + mButtonBack = mWidgetFactory.CreateButton("Zurück"); + mButtonLoad = mWidgetFactory.CreateButton("Laden"); + mButtonSaveGame = mWidgetFactory.CreateSelectButton(); + mSaveGames = mSaveGameManager.GetSaveGames(); + foreach (var saveGame in mSaveGames) + { + mButtonSaveGame.Values.Add(GetSaveGameString(saveGame)); + } + mButtonSaveGame.SelectedIndex = mSaveGames.Count - 1; + + mMenu.Widgets.Add(mWidgetFactory.CreateEmptyWidget()); + mMenu.Widgets.Add(mButtonBack); + mMenu.Widgets.Add(mWidgetFactory.CreateLabel("Spielstand")); + mMenu.Widgets.Add(mButtonSaveGame); + mMenu.Widgets.Add(mWidgetFactory.CreateEmptyWidget()); + mMenu.Widgets.Add(mButtonLoad); + } + + public override bool HandleKeyEvent(IKeyEvent keyEvent) + { + if (keyEvent.KeyState == KeyState.Down && keyEvent.Key == Keys.Escape) + mMenuActions.Close(this); + + return true; + } + + public override bool HandleMouseEvent(IMouseEvent mouseEvent) + { + mMenu.HandleMouseEvent(mouseEvent); + return true; + } + + /// + /// Draws this object using the given sprite batch. + /// + /// a snapshot of the game time + /// the sprite batch to use for drawing + /// this object + public override void Draw(GameTime gameTime, SpriteBatch spriteBatch) + { + var backgroundRectangle = new Rectangle((int)mMenu.Position.X, + (int)mMenu.Position.Y, (int)mMenu.Size.X, (int)mMenu.Size.Y); + backgroundRectangle.X -= 30; + backgroundRectangle.Y -= 30; + backgroundRectangle.Width += 60; + backgroundRectangle.Height += 60; + + spriteBatch.Begin(); + spriteBatch.Draw(mRectangle, backgroundRectangle, Color.LightGray); + spriteBatch.End(); + + mMenu.Draw(spriteBatch); + } + + public override void Update(GameTime gameTime) + { + if (mButtonBack.IsClicked) + { + mMenuActions.Close(this); + } + else if (mButtonLoad.IsClicked) + { + var saveGame = mSaveGames[mButtonSaveGame.SelectedIndex]; + mMenuActions.LoadGame(saveGame); + } + + mMenu.Update(); + } + + private static string GetSaveGameString(ISaveGame saveGame) + { + return saveGame.Timestamp.ToString("s"); + } + } +} -- cgit v1.2.1