recrownedgtk/RecrownedAthenaeum/ScreenSystem/Screen.cs
2019-01-13 23:23:03 -06:00

163 lines
5.5 KiB
C#

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RecrownedAthenaeum.Camera;
using System.Collections.Generic;
namespace RecrownedAthenaeum.ScreenSystem
{
public enum ScreenState { EnterTransition, ExitTransition, Normal }
public class Screen
{
public List<ITransition> Transitions;
public bool UseRenderTargetForExitTransition;
public Color BackgroundColor;
public GraphicsDevice GraphicsDevice { get; private set; }
public Screen NextScreen { get; set; }
public Rectangle ScreenSize { get; private set; }
public bool Initiated { get; private set; }
public Camera2D Camera { get; private set; }
public Screen(bool useEnterTransition = false)
{
State = useEnterTransition ? ScreenState.EnterTransition : ScreenState.Normal;
Transitions = new List<ITransition>();
}
/// <summary>
/// Called only once after initialization to give required parameters.
/// </summary>
public virtual void Initiate(GraphicsDevice graphicsDevice, Rectangle screenSize, Camera2D camera)
{
this.Camera = camera;
this.ScreenSize = screenSize;
GraphicsDevice = graphicsDevice;
Initiated = true;
}
/// <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(SpriteBatch 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>
/// <param name="previousScreenExitFrame">The previous screen's frame. May be null.</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.EnteringTransition(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.ExitingTransition(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(ScreenSize);
}
}
/// <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)
{
}
public ScreenState State { get; private set; }
/// <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);
}
}
}
}