Moved the ScreenSystem to under UI namespace.

This commit is contained in:
2019-06-01 15:36:31 -05:00
parent cc0099ee7d
commit 08f45bcfd4
8 changed files with 11 additions and 17 deletions

View File

@@ -0,0 +1,46 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RecrownedAthenaeum.Render;
namespace RecrownedAthenaeum.UI.ScreenSystem
{
/// <summary>
/// Contracts a transition that the <see cref="ScreenManager"/> can use.
/// </summary>
public interface ITransition
{
/// <summary>
/// Called once when the transition is needed.
/// <param name="dimensions">The dimensions of the screen.</param>
/// </summary>
void InitiateTransition(Rectangle dimensions);
/// <summary>
/// Called every frame if the state of the screen this transition is placed upon is in the enter transition phase.
/// </summary>
/// <param name="delta">The time passed in seconds since the last frame.</param>
/// <param name="waiting">Whether or not this transition is waiting on something. Usually the <see cref="ContentSystem"/>.</param>
/// <returns>If this returns true, then it is considered that this transition is complete.</returns>
bool UpdateEnteringTransition(double delta, bool waiting);
/// <summary>
/// Called every frame if the state of the screen this transition is placed upon is in the exit phase.
/// </summary>
/// <param name="delta">The time passed in seconds since the last frame.</param>
/// <param name="waiting">Whether or not this transition is waiting on something. Usually the <see cref="ContentSystem"/>.</param>
/// <returns>If this returns true, then it is considered that this transition is complete.</returns>
bool UpdateExitingTransition(double delta, bool waiting);
/// <summary>
/// Called once every frame while transition is active. Meant to draw transition.
/// </summary>
/// <param name="spriteBatch"></param>
void DrawTransition(ConsistentSpriteBatch spriteBatch);
/// <summary>
/// Updates if the previous screen uses a render target for exit transition.
/// </summary>
/// <param name="previousScreenFrame">The frame of the previous screen.</param>
void UpdatePreviousScreenFrame(RenderTarget2D previousScreenFrame);
}
}

View File

@@ -0,0 +1,166 @@

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RecrownedAthenaeum.Render;
using System;
namespace RecrownedAthenaeum.UI.ScreenSystem
{
/// <summary>
/// A screen specifically meant to fill in loading times.
/// </summary>
public class LoadingScreen : Screen, ITransition
{
private const float ENTER_TIME = 2f;
private const float EXIT_TIME = 1f;
Game game;
readonly Texture2D texture;
Color color;
Rectangle textureBounds;
readonly float proportion;
bool recorded;
float rR, rG, rB;
float progR, progG, progB;
float progC = 254;
float rotation;
readonly bool rotate;
Vector2 origin;
/// <summary>
/// Constructs a loading screen.
/// </summary>
/// <param name="game">The game itself to adjust mouse settings and such.</param>
/// <param name="screenImage"></param>
/// <param name="proportion"></param>
/// <param name="rotate"></param>
public LoadingScreen(Game game, Texture2D screenImage, float proportion, bool rotate = false) : base(true)
{
this.game = game;
this.texture = screenImage;
this.proportion = proportion;
this.rotate = rotate;
Transitions.Add(this);
}
/// <inheritdoc />
public override void Show()
{
game.IsMouseVisible = false;
base.Show();
}
/// <inheritdoc />
public override void Hide()
{
game.IsMouseVisible = true;
base.Hide();
}
/// <summary>
/// Sets things to correct values for start of transition.
/// </summary>
/// <param name="dimensions">The window dimensions.</param>
public void InitiateTransition(Rectangle dimensions)
{
color = Color.White;
textureBounds.Width = (int)(height * proportion);
textureBounds.Height = (int)(height * proportion);
textureBounds.X = (width) / 2;
textureBounds.Y = (height) / 2;
origin.X = texture.Width / 2f;
origin.Y = texture.Height / 2f;
}
void DoRotate(float deltaf)
{
rotation += (2f / 3f) * (float)Math.PI * deltaf;
if (rotation >= 2 * Math.PI)
{
rotation = 0;
}
}
/// <summary>
/// Draws the transition.
/// </summary>
/// <param name="spriteBatch">Batch to use.</param>
public void DrawTransition(ConsistentSpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, textureBounds, null, color, rotation, origin, SpriteEffects.None, 0f);
}
/// <summary>
/// Updates the entering transition.
/// </summary>
/// <param name="delta">Time passed between frames.</param>
/// <param name="waiting">Whether or not this transition should be waiting.</param>
/// <returns>Whether or not transition is complete.</returns>
public bool UpdateEnteringTransition(double delta, bool waiting)
{
float deltaf = (float)delta;
if (rotate)
{
DoRotate(deltaf);
}
if (waiting)
{
if (progR < 254 || progG < 254 || progB < 254)
{
if (!recorded)
{
rR = (Color.White.R - BackgroundColor.R) / ENTER_TIME;
rG = (Color.White.G - BackgroundColor.G) / ENTER_TIME;
rB = (Color.White.B - BackgroundColor.B) / ENTER_TIME;
recorded = true;
}
progR += rR * deltaf;
progR = Math.Min(progR, 254);
progG += rG * deltaf;
progG = Math.Min(progG, 254);
progB += rB * deltaf;
progB = Math.Min(progB, 254);
BackgroundColor.R = (byte)progR;
BackgroundColor.G = (byte)progG;
BackgroundColor.B = (byte)progB;
}
else
{
StartExitTransition(true);
return true;
}
}
return false;
}
/// <summary>
/// Updates the exiting transition.
/// </summary>
/// <param name="delta">Time passed between frames.</param>
/// <param name="waiting">Whether or not this transition should be waiting.</param>
/// <returns>Whether or not transition is complete.</returns>
public bool UpdateExitingTransition(double delta, bool waiting)
{
float deltaf = (float)delta;
if (rotate)
{
DoRotate(deltaf);
}
if (progC > 0)
{
float rate = deltaf * 255 / EXIT_TIME;
progC -= rate;
progC = Math.Max(progC, 0);
color.R = (byte)progC;
color.G = (byte)progC;
color.B = (byte)progC;
color.A = (byte)progC;
}
else
{
return true;
}
return false;
}
}
}

View File

@@ -0,0 +1,205 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RecrownedAthenaeum.Render;
using System.Collections.Generic;
namespace RecrownedAthenaeum.UI.ScreenSystem
{
/// <summary>
/// Represents one of the poosible states a screen can be in.
/// </summary>
public enum ScreenState {
/// <summary>
/// Screen is transitioning in.
/// </summary>
EnterTransition,
/// <summary>
/// Screen is transitioning out.
/// </summary>
ExitTransition,
/// <summary>
/// Screen is currently displayed normally without transition.
/// </summary>
Normal
}
/// <summary>
/// A screen represents a virtual system of management that controls an system of items to be displayed.
/// </summary>
public class Screen
{
/// <summary>
/// Transitions to apply.
/// </summary>
public readonly List<ITransition> Transitions;
/// <summary>
/// Whether or not to continue rendering this screen onto a buffer for the benefit of the next screen to use as a transition.
/// </summary>
public bool UseRenderTargetForExitTransition { get; private set; }
/// <summary>
/// The background color to be used to clear the screen.
/// </summary>
public Color BackgroundColor;
/// <summary>
/// The next screen to be displayed after exit transition finishes. May be null, leading to transition to previous screen or loading screen.
/// </summary>
public Screen NextScreen { get; set; }
/// <summary>
/// The current window dimensions.
/// </summary>
public int width, height;
/// <summary>
/// Current state of the screen.
/// </summary>
public ScreenState State { get; private set; }
/// <summary>
/// Creates a new screen.
/// </summary>
/// <param name="useEnterTransition">True to start in entering transition state.</param>
public Screen(bool useEnterTransition = false)
{
State = useEnterTransition ? ScreenState.EnterTransition : ScreenState.Normal;
Transitions = new List<ITransition>();
}
/// <summary>
/// Called when screen size is set.
/// </summary>
/// <param name="width">The width of the screen.</param>
/// <param name="height">The height of the screen.</param>
public virtual void ApplySize(int width, int height)
{
this.width = width;
this.height = height;
}
/// <summary>
/// Called to update the screen.
/// </summary>
/// <param name="gameTime">Game time information.</param>
public virtual void Update(GameTime gameTime)
{
}
/// <summary>
/// Called to draw this screen.
/// </summary>
/// <param name="spriteBatch">SpriteBatch to use.</param>
public virtual void Draw(ConsistentSpriteBatch spriteBatch)
{
foreach (ITransition transition in Transitions)
{
transition.DrawTransition(spriteBatch);
}
}
/// <summary>
/// Updates the transition based on the current state of the screen.
/// </summary>
/// <param name="delta">Time passed since last frame in seconds.</param>
/// <param name="waiting">If the this transition should wait.</param>
/// <returns>Only returns true if exit transition is complete. Returns false otherwise.</returns>
public bool UpdateTransition(double delta, bool waiting)
{
switch (State)
{
case ScreenState.EnterTransition:
EnteringTransition(delta, waiting);
break;
case ScreenState.ExitTransition:
return ExitingTransition(delta, waiting);
}
return false;
}
private void EnteringTransition(double delta, bool waiting)
{
bool complete = true;
for (int transitionID = 0; transitionID < Transitions.Count; transitionID++)
{
ITransition transition = Transitions[transitionID];
if (!transition.UpdateEnteringTransition(delta, waiting))
{
complete = false;
}
}
if (complete && State != ScreenState.ExitTransition)
{
State = ScreenState.Normal;
}
}
private bool ExitingTransition(double delta, bool waiting)
{
bool complete = true;
foreach (ITransition transition in Transitions)
{
if (!transition.UpdateExitingTransition(delta, waiting))
{
complete = false;
}
}
return complete;
}
/// <summary>
/// Called when the screen is shown.
/// </summary>
public virtual void Show()
{
foreach (ITransition transition in Transitions)
{
transition.InitiateTransition(new Rectangle(0, 0, width, height));
}
}
/// <summary>
/// Called when this screen is no longer the displayed screen.
/// </summary>
public virtual void Hide()
{
}
/// <summary>
/// Called whenever the status of assets changes from being loaded to unloaded or unloaded to loaded.
/// </summary>
/// <param name="state">True for loaded, false for unloaded.</param>
public virtual void AssetLoadStateChange(bool state)
{
}
/// <summary>
/// Call this to begin exit transition.
/// </summary>
/// <param name="UseRenderTargetForExitTransition">Whether or not to use a render target for the next screen to use.</param>
public void StartExitTransition(bool UseRenderTargetForExitTransition = false)
{
this.UseRenderTargetForExitTransition = UseRenderTargetForExitTransition;
State = ScreenState.ExitTransition;
}
/// <summary>
/// Called everytime the previous screen frame buffer updates. Used for transition purposes.
/// </summary>
/// <param name="previousScreenFrame">The previous screen's render buffer.</param>
public virtual void UpdatePreviousScreenFrame(RenderTarget2D previousScreenFrame)
{
foreach (ITransition transition in Transitions)
{
transition.UpdatePreviousScreenFrame(previousScreenFrame);
}
}
}
}

View File

@@ -0,0 +1,212 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RecrownedAthenaeum.Render;
using System;
using System.Diagnostics;
namespace RecrownedAthenaeum.UI.ScreenSystem
{
/// <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 NeedNextScreen(Screen screen);
/// <summary>
/// A manager for screens. Helps with transitions and updating screens as well as resizes.
/// </summary>
public class ScreenManager : IDisposable
{
bool disposed = false;
/// <summary>
/// Called when the first loading screen is done, and needs to show the landing screen.
/// </summary>
public event NeedNextScreen NeedNextScreen;
/// <summary>
/// The settings this manager will use to begin a sprite batch.
/// </summary>
private GraphicsDeviceManager graphics;
private Screen previousScreen;
private RenderTarget2D previousScreenRenderTarget;
private Screen currentScreen;
private bool firstScreenChangeComplete;
private bool resizing;
/// <summary>
/// Currently displayed screen.
/// </summary>
public Screen Screen
{
get
{
return currentScreen;
}
set
{
previousScreen = currentScreen;
currentScreen = value;
if (Screen.width != graphics.PreferredBackBufferWidth || Screen.height != graphics.PreferredBackBufferHeight)
{
Screen.ApplySize(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
}
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();
}
}
/// <summary>
/// Creates a screen manager that helps update, draw, and manage multiple screens and their transitions. Uses its own <see cref="SpriteBatch"/>.
/// </summary>
/// <param name="graphicsDeviceManager">The graphics device manager to be used.</param>
public ScreenManager(GraphicsDeviceManager graphicsDeviceManager)
{
graphics = graphicsDeviceManager ?? throw new ArgumentNullException("Graphics device manager argument cannot be null.");
}
/// <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)
{
waiting = !waiting;
switch (Screen.State)
{
case ScreenState.EnterTransition:
if (previousScreen != null && previousScreen.UseRenderTargetForExitTransition)
{
previousScreen.UpdateTransition(gameTime.ElapsedGameTime.TotalSeconds, waiting);
}
Screen.UpdateTransition(gameTime.ElapsedGameTime.TotalSeconds, waiting);
break;
case ScreenState.ExitTransition:
if (Screen.UseRenderTargetForExitTransition || Screen.UpdateTransition(gameTime.ElapsedGameTime.TotalSeconds, waiting))
{
if (!firstScreenChangeComplete)
{
firstScreenChangeComplete = true;
OnNeedNextScreen(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);
}
/// <summary>
/// Renders screen into window.
/// Starts and ends the given <paramref name="consistentSpriteBatch"/>.
/// </summary>
/// <param name="consistentSpriteBatch">The consistent sprite batch to use. Needs to be consistent as changing render targets requires ending and beginning the sprite batch.</param>
public void DrawScreen(ConsistentSpriteBatch consistentSpriteBatch)
{
if (consistentSpriteBatch == null)
{
throw new ArgumentNullException(nameof(consistentSpriteBatch));
}
if (Screen == null) return;
graphics.GraphicsDevice.Clear(Screen.BackgroundColor);
if (Screen.State == ScreenState.EnterTransition && previousScreen != null && previousScreen.UseRenderTargetForExitTransition)
{
graphics.GraphicsDevice.SetRenderTarget(previousScreenRenderTarget);
graphics.GraphicsDevice.Clear(previousScreen.BackgroundColor);
consistentSpriteBatch.Begin();
previousScreen.Draw(consistentSpriteBatch);
consistentSpriteBatch.End();
graphics.GraphicsDevice.SetRenderTarget(null);
Screen.UpdatePreviousScreenFrame(previousScreenRenderTarget);
}
consistentSpriteBatch.Begin();
Screen.Draw(consistentSpriteBatch);
consistentSpriteBatch.End();
}
/// <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)
{
if (resizing) throw new InvalidOperationException("Already resizing.");
resizing = true;
Screen.AssetLoadStateChange(false);
Screen = loadingScreen;
previousScreenRenderTarget.Dispose();
previousScreenRenderTarget = null;
}
/// <summary>
/// Notifies all screen that assets have completed being loaded after a resize.
/// </summary>
public void PostResize()
{
if (!resizing) throw new InvalidOperationException("Was never resizing.");
Screen.AssetLoadStateChange(true);
}
private void OnNeedNextScreen(Screen screen)
{
NeedNextScreen?.Invoke(screen);
}
/// <summary>
/// Disposes this.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// An overridable dispose.
/// </summary>
/// <param name="disposing">True of user invoked dispose called.</param>
public virtual void Dispose(bool disposing)
{
if (disposed) throw new ObjectDisposedException(GetType().Name);
if (disposing)
{
previousScreenRenderTarget?.Dispose();
}
disposed = true;
}
/// <summary>
/// Destructor.
/// </summary>
~ScreenManager()
{
Dispose(false);
}
}
}