recrownedathenaeum/RecrownedAthenaeum/ScreenSystem/ScreenManager.cs

183 lines
7.4 KiB
C#
Raw Normal View History

2019-01-13 20:22:53 +00:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RecrownedAthenaeum.Camera;
using System;
using System.Diagnostics;
namespace RecrownedAthenaeum.ScreenSystem
{
2019-01-14 05:23:03 +00:00
/// <summary>
/// Called when the first screen is being shown.
/// </summary>
/// <param name="screen">The screen to show after the loading screen.</param>
public delegate void ShowFirstScreen(Screen screen);
public class ScreenManager : IDisposable
{
bool disposed = false;
2019-01-14 05:23:03 +00:00
/// <summary>
/// Called when the first loading screen is done, and needs to show the landing screen.
/// </summary>
public event ShowFirstScreen ShowFirstScreenEvent;
private GraphicsDeviceManager graphics;
private Screen previousScreen;
private RenderTarget2D previousScreenRenderTarget;
private Screen currentScreen;
private Camera2D camera;
private bool firstScreenChangeComplete;
2019-01-14 05:23:03 +00:00
private bool resizing;
/// <summary>
/// Currently displayed screen.
/// </summary>
public Screen Screen
{
get
{
return currentScreen;
}
set
{
previousScreen = currentScreen;
currentScreen = value;
if (!Screen.Initiated)
{
Screen.Initiate(graphics.GraphicsDevice, new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight), camera);
}
if (previousScreen != null && previousScreenRenderTarget == null && previousScreen.UseRenderTargetForExitTransition)
{
previousScreenRenderTarget = new RenderTarget2D(graphics.GraphicsDevice, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
}
graphics.GraphicsDevice.SetRenderTarget(previousScreenRenderTarget);
graphics.GraphicsDevice.Clear(Color.Black);
Debug.WriteLine("Showing " + value.GetType().Name);
Screen.Show();
previousScreen?.Hide();
}
}
2019-01-14 05:23:03 +00:00
/// <summary>
/// Creates a screen manager that helps manage multiple screens and their transitions.
/// </summary>
/// <param name="graphicsDeviceManager">The graphics device manager to be used.</param>
/// <param name="camera">The camera to be used to perform the correct translations and transformations.</param>
public ScreenManager(GraphicsDeviceManager graphicsDeviceManager, Camera2D camera)
{
this.graphics = graphicsDeviceManager;
this.camera = camera;
}
2019-01-14 05:23:03 +00:00
/// <summary>
/// Updates the screens. Should be called once every frame.
/// </summary>
/// <param name="gameTime">Contains the time that has passed from the last frame.</param>
/// <param name="waiting">Whether or not there is something a transition should be waiting for. Usually used to wait for assets to complete loading.</param>
public void UpdateCurrentScreen(GameTime gameTime, bool waiting)
{
switch (Screen.State)
{
case ScreenState.EnterTransition:
if (previousScreen != null && previousScreen.UseRenderTargetForExitTransition)
{
2019-01-14 05:23:03 +00:00
previousScreen.UpdateTransition(gameTime.ElapsedGameTime.TotalSeconds, waiting);
}
2019-01-14 05:23:03 +00:00
Screen.UpdateTransition(gameTime.ElapsedGameTime.TotalSeconds, waiting);
break;
case ScreenState.ExitTransition:
2019-01-14 05:23:03 +00:00
if (Screen.UseRenderTargetForExitTransition || Screen.UpdateTransition(gameTime.ElapsedGameTime.TotalSeconds, waiting))
{
if (!firstScreenChangeComplete)
{
firstScreenChangeComplete = true;
OnFirstScreenChange(Screen);
}
if (Screen.NextScreen != null)
{
Debug.WriteLine("Changing to the next given screen.");
Screen = Screen.NextScreen;
}
else if (previousScreen != null)
{
Debug.WriteLine("Changing to previous screen.");
Screen = previousScreen;
}
else
{
throw new InvalidOperationException("No next screen provided and no previous screen to return to.");
}
}
break;
}
Screen.Update(gameTime);
}
2019-01-14 05:23:03 +00:00
/// <summary>
/// Renders screen into window.
/// </summary>
/// <param name="spriteBatch">Uses this batch to render.</param>
public void DrawCurrentScreen(SpriteBatch spriteBatch)
{
graphics.GraphicsDevice.Clear(Screen.BackgroundColor);
if (Screen.State == ScreenState.EnterTransition && previousScreen != null && previousScreen.UseRenderTargetForExitTransition)
{
graphics.GraphicsDevice.SetRenderTarget(previousScreenRenderTarget);
graphics.GraphicsDevice.Clear(previousScreen.BackgroundColor);
2019-01-12 04:09:42 +00:00
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, camera.Matrix);
previousScreen.Draw(spriteBatch);
spriteBatch.End();
graphics.GraphicsDevice.SetRenderTarget(null);
Screen.UpdatePreviousScreenFrame(previousScreenRenderTarget);
}
2019-01-12 04:09:42 +00:00
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, camera.Matrix);
Screen.Draw(spriteBatch);
spriteBatch.End();
}
2019-01-14 05:23:03 +00:00
/// <summary>
/// Should be called when resize is occurring to change to a loading screen.
/// This will notify the screen of the status of the assets, change the screen to a loading screen, and dispose of the previous screen buffer.
/// </summary>
/// <param name="loadingScreen">The loading screen to change to.</param>
public void Resize(LoadingScreen loadingScreen)
{
2019-01-14 05:23:03 +00:00
if (resizing) throw new InvalidOperationException("Already resizing.");
resizing = true;
Screen.AssetLoadStateChange(false);
Screen = loadingScreen;
previousScreenRenderTarget.Dispose();
previousScreenRenderTarget = null;
}
2019-01-14 05:23:03 +00:00
/// <summary>
/// Notifies all screen that assets have completed being loaded after a resize.
/// </summary>
public void PostResize()
{
2019-01-14 05:23:03 +00:00
if (!resizing) throw new InvalidOperationException("Was never resizing.");
Screen.AssetLoadStateChange(true);
}
2019-01-14 05:23:03 +00:00
private void OnFirstScreenChange(Screen screen)
{
2019-01-14 05:23:03 +00:00
ShowFirstScreenEvent?.Invoke(screen);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public virtual void Dispose(bool disposing)
{
if (disposed) return;
if (disposing)
{
previousScreenRenderTarget?.Dispose();
}
disposing = true;
}
}
}