Skip to content

Commit

Permalink
[WIP] Screen manager
Browse files Browse the repository at this point in the history
For issue #3.
  • Loading branch information
ARMmaster17 committed Feb 1, 2016
1 parent 49b1c8a commit eb109ef
Showing 1 changed file with 52 additions and 11 deletions.
63 changes: 52 additions & 11 deletions war-of-katan/war-of-katan/ScreenManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Katan.Graphics;

namespace Katan
Expand All @@ -12,27 +13,33 @@ class ScreenManager
{
private Dictionary<string, Screen> screenList;
private string currentScreen;
private List<Task> runningOperations;
private bool switchOperationRequested;
private string switchOperationArgument;

/// <summary>
/// Default constructor for ScreenManager object.
/// </summary>
public ScreenManager()
{

screenList = new Dictionary<string, Screen>();
currentScreen = "";
runningOperations = new List<Task>();
switchOperationRequested = false;
}
/// <summary>
/// Runs update method for currently selected Screen object.
/// </summary>
public void Update()
{

screenList[currentScreen].Update();
}
/// <summary>
/// Runs draw method for currently selected Screen object.
/// </summary>
public void Draw()
{

screenList[currentScreen].Draw();
}
/// <summary>
/// Adds a Screen object to private screen collection.
Expand All @@ -43,6 +50,12 @@ public void AddScreen(string _name, Screen _screen)
{
screenList.Add(_name, _screen);
}
/// <summary>
/// Switches display context to a new screen by unloading the current
/// screen and loading the new screen. Can also suspend/resume if the
/// screen object inherits the ISuspendable interface.
/// </summary>
/// <param name="_name">Name of screen to switch to.</param>
public void SwitchScreen(string _name)
{
// Check to see if screen exists
Expand All @@ -54,24 +67,52 @@ public void SwitchScreen(string _name)
if (currentScreen != "")
{
// There is a screen already taking up memory, unload it
unloadScreen(_name);
runningOperations.Add(new Task(() => unloadScreen(_name)));
runningOperations[runningOperations.Count - 1].Start();
}
// Load the new screen

loadScreen(_name);
currentScreen = _name;
}
public void SwitchPreloadedScreen(string _name)
{
// Check to see if screen exists
if (!screenList.ContainsKey(_name))
{
throw new ScreenNotFoundException();
}
// Check if there is already an active screen
if (currentScreen != "")
{
// There is a screen already taking up memory, unload it
runningOperations.Add(new Task(() => unloadScreen(_name)));
runningOperations[runningOperations.Count - 1].Start();
}
// Load the new screen
currentScreen = _name;
}
/// <summary>
/// Spawns a task that will asyncronously load a new screen into memory.
/// </summary>
/// <param name="_name">Name of Screen object to load.</param>
public Task AsyncPreloadScreen(string _name)
{
Task result = new Task(() => loadScreen(_name));
return result;
}
/// <summary>
/// Unloads a Screen object from memory.
/// </summary>
/// <param name="_name">Name of Screen object to unload.</param>
private void unloadScreen(string _name)
{
if (screenList[currentScreen] is ISuspendable)
if (screenList[_name] is ISuspendable)
{
(screenList[currentScreen] as ISuspendable).Suspend();
(screenList[_name] as ISuspendable).Suspend();
}
else
{
screenList[currentScreen].UnloadContent();
screenList[_name].UnloadContent();
}
}
/// <summary>
Expand All @@ -80,13 +121,13 @@ private void unloadScreen(string _name)
/// <param name="_name">Name of Screen object to load.</param>
private void loadScreen(string _name)
{
if (screenList[currentScreen] is ISuspendable)
if (screenList[_name] is ISuspendable)
{
(screenList[currentScreen] as ISuspendable).Resume();
(screenList[_name] as ISuspendable).Resume();
}
else
{
screenList[currentScreen].LoadContent();
screenList[_name].LoadContent();
}
}
}
Expand Down

0 comments on commit eb109ef

Please sign in to comment.