2018-11-13 01:40:27 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
using RhythmBullet.Zer01HD.Utilities.Camera;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace RhythmBullet.Zer01HD.Utilities.ScreenSystem
|
|
|
|
|
{
|
2018-11-18 05:27:05 +00:00
|
|
|
|
public delegate void FirstScreenChange(Screen screen);
|
2018-11-13 01:40:27 +00:00
|
|
|
|
|
|
|
|
|
public class ScreenManager : IDisposable
|
|
|
|
|
{
|
2018-11-18 05:27:05 +00:00
|
|
|
|
public event FirstScreenChange RequireNextScreenEvent;
|
2018-11-13 01:40:27 +00:00
|
|
|
|
private GraphicsDeviceManager graphics;
|
|
|
|
|
private Screen previousScreen;
|
|
|
|
|
private RenderTarget2D previousScreenRenderTarget;
|
|
|
|
|
private Screen currentScreen;
|
|
|
|
|
private Camera2D camera;
|
2018-11-18 05:27:05 +00:00
|
|
|
|
private bool firstScreenChangeComplete;
|
2018-11-13 01:40:27 +00:00
|
|
|
|
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();
|
2018-11-18 06:17:23 +00:00
|
|
|
|
previousScreen?.Hide();
|
2018-11-13 01:40:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ScreenManager(GraphicsDeviceManager graphicsDeviceManager, Camera2D camera)
|
|
|
|
|
{
|
|
|
|
|
this.graphics = graphicsDeviceManager;
|
|
|
|
|
this.camera = camera;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-13 01:45:57 +00:00
|
|
|
|
public void UpdateCurrentScreen(GameTime gameTime, bool assetsDone)
|
2018-11-13 01:40:27 +00:00
|
|
|
|
{
|
|
|
|
|
switch (Screen.State)
|
|
|
|
|
{
|
|
|
|
|
case ScreenState.EnterTransition:
|
|
|
|
|
if (previousScreen != null && previousScreen.UseRenderTargetForExitTransition)
|
|
|
|
|
{
|
|
|
|
|
previousScreen.UpdateTransition(gameTime.ElapsedGameTime.TotalSeconds, assetsDone);
|
|
|
|
|
}
|
|
|
|
|
Screen.UpdateTransition(gameTime.ElapsedGameTime.TotalSeconds, assetsDone);
|
|
|
|
|
break;
|
|
|
|
|
case ScreenState.ExitTransition:
|
|
|
|
|
if (Screen.UseRenderTargetForExitTransition || Screen.UpdateTransition(gameTime.ElapsedGameTime.TotalSeconds, assetsDone))
|
|
|
|
|
{
|
2018-11-18 05:27:05 +00:00
|
|
|
|
if (!firstScreenChangeComplete)
|
2018-11-13 01:43:03 +00:00
|
|
|
|
{
|
2018-11-18 05:27:05 +00:00
|
|
|
|
firstScreenChangeComplete = true;
|
|
|
|
|
OnFirstScreenChange(Screen);
|
2018-11-13 01:43:03 +00:00
|
|
|
|
}
|
2018-11-13 01:40:27 +00:00
|
|
|
|
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;
|
2018-11-13 01:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2018-11-13 01:40:27 +00:00
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("No next screen provided and no previous screen to return to.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-11-15 07:22:19 +00:00
|
|
|
|
Screen.Update(gameTime);
|
2018-11-13 01:40:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-13 01:45:57 +00:00
|
|
|
|
public void DrawCurrentScreen(SpriteBatch spriteBatch)
|
2018-11-13 01:40:27 +00:00
|
|
|
|
{
|
|
|
|
|
graphics.GraphicsDevice.Clear(Screen.BackgroundColor);
|
|
|
|
|
if (Screen.State == ScreenState.EnterTransition && previousScreen != null && previousScreen.UseRenderTargetForExitTransition)
|
|
|
|
|
{
|
|
|
|
|
graphics.GraphicsDevice.SetRenderTarget(previousScreenRenderTarget);
|
|
|
|
|
graphics.GraphicsDevice.Clear(previousScreen.BackgroundColor);
|
|
|
|
|
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, camera.TransformMatrix);
|
|
|
|
|
previousScreen.Draw(spriteBatch);
|
|
|
|
|
spriteBatch.End();
|
|
|
|
|
graphics.GraphicsDevice.SetRenderTarget(null);
|
|
|
|
|
Screen.UpdatePreviousScreenFrame(previousScreenRenderTarget);
|
|
|
|
|
}
|
|
|
|
|
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, camera.TransformMatrix);
|
|
|
|
|
Screen.Draw(spriteBatch);
|
|
|
|
|
spriteBatch.End();
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-18 05:27:05 +00:00
|
|
|
|
public void Resize(LoadingScreen loadingScreen)
|
2018-11-13 01:40:27 +00:00
|
|
|
|
{
|
|
|
|
|
Screen.AssetLoadStateChange(true);
|
2018-11-18 05:27:05 +00:00
|
|
|
|
Screen = loadingScreen;
|
2018-11-13 01:40:27 +00:00
|
|
|
|
previousScreenRenderTarget.Dispose();
|
|
|
|
|
previousScreenRenderTarget = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void PostResize()
|
|
|
|
|
{
|
|
|
|
|
Screen.AssetLoadStateChange(false);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-18 05:27:05 +00:00
|
|
|
|
public void OnFirstScreenChange(Screen screen)
|
2018-11-13 01:40:27 +00:00
|
|
|
|
{
|
|
|
|
|
RequireNextScreenEvent?.Invoke(screen);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
previousScreenRenderTarget?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|