improved transition system, target rendering portion tested with loading to main screen.

This commit is contained in:
2018-11-04 00:01:17 -05:00
parent 042fba4249
commit 99a8531b47
9 changed files with 329 additions and 225 deletions

View File

@@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.UI.Modular
{
class Text : Module
public class Text : Module
{
private SpriteFont font;
private float scale;

View File

@@ -1,102 +0,0 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Utilities.UI
{
public enum ScreenState { EnterTransition, ExitTransition, Normal }
public class Screen
{
public bool UseRenderTargetForExitTransition;
private GraphicsDevice GraphicsDevice;
public Screen(bool useEnterTransition = false)
{
State = useEnterTransition ? ScreenState.EnterTransition : ScreenState.Normal;
}
public void Initiate(Rectangle screenSize, GraphicsDevice graphicsDevice)
{
Initiated = true;
this.ScreenSize = screenSize;
this.GraphicsDevice = graphicsDevice;
RenderTarget = new RenderTarget2D(graphicsDevice, ScreenSize.Width, ScreenSize.Height);
}
public virtual void Update(GameTime gameTime)
{
}
public virtual void Draw(SpriteBatch spriteBatch)
{
}
public virtual void EnteringTransition(double delta, bool assetsLoaded, ref Color backgroundColor, RenderTarget2D previousScreenExitFrame)
{
}
public virtual bool ExitingTransition(double delta, bool assetsLoaded, ref Color backgroundColor)
{
return true;
}
public virtual void Show()
{
}
public virtual void Hide()
{
}
public virtual void AssetLoadStateChange(bool state)
{
}
public ScreenState State { get; private set; }
public void DoneEnterTransition()
{
State = ScreenState.Normal;
}
public void StartExitTransition(bool UseRenderTargetForExitTransition = false)
{
this.UseRenderTargetForExitTransition = UseRenderTargetForExitTransition;
State = ScreenState.ExitTransition;
}
public Screen NextScreen
{
get; protected set;
}
public Rectangle ScreenSize
{
get;
protected set;
//Remember to init in init func
}
public RenderTarget2D RenderTarget
{
get;
private set;
}
public bool Initiated
{
get;
private set;
}
}
}

View File

@@ -0,0 +1,48 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Utilities.UI.ScreenSystem
{
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="assetsLoaded">Whether or not if all assets have been loaded by the <see cref="ContentSystem"/>.</param>
/// <param name="previousScreenExitFrame">The frame being rendered by the screen as it exits. This can be null.</param>
/// <returns>If this returns true, then it is considered that this transition is complete.</returns>
bool EnteringTransition(double delta, bool assetsLoaded);
/// <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="assetsLoaded">Whether or not if all assets have been loaded by the <see cref="ContentSystem"/>.</param>
/// <returns>If this returns true, then it is considered that this transition is complete.</returns>
bool ExitingTransition(double delta, bool assetsLoaded);
/// <summary>
/// Called once every frame while transition is active. Meant to draw transition.
/// </summary>
/// <param name="spriteBatch"></param>
void DrawTransition(SpriteBatch 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

@@ -1,16 +1,14 @@
using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RhythmBullet.Zer01HD.Game.Screens;
using RhythmBullet.Zer01HD.Utilities.UI;
using System;
using System.Diagnostics;
namespace RhythmBullet.Zer01HD.Game
namespace RhythmBullet.Zer01HD.Utilities.UI.ScreenSystem
{
class LoadingScreen : Screen
class LoadingScreen : Screen, ITransition
{
private const float ENTER_RATE = 2f;
private const float EXIT_RATE = 1f;
private const float ENTER_TIME = 2f;
private const float EXIT_TIME = 1f;
readonly Texture2D texture;
Color color;
Rectangle textureBounds;
@@ -23,26 +21,40 @@ namespace RhythmBullet.Zer01HD.Game
readonly bool rotate;
Vector2 origin;
public LoadingScreen(Texture2D texture, float proportion, Rectangle dimensions, bool rotate = false) : base(true)
public LoadingScreen(Texture2D texture, float proportion, bool rotate = false) : base(true)
{
color = Color.White;
this.texture = texture;
this.proportion = proportion;
this.rotate = rotate;
textureBounds.Width = (int)(dimensions.Height * proportion);
textureBounds.Height = (int)(dimensions.Height * proportion);
textureBounds.X = (dimensions.Width) / 2;
textureBounds.Y = (dimensions.Height) / 2;
Transitions.Add(this);
}
void DoRotate(float deltaf)
{
rotation += (2f / 3f) * (float)Math.PI * deltaf;
if (rotation >= 2 * Math.PI)
{
rotation = 0;
}
}
public void DrawTransition(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, textureBounds, null, Color.White, rotation, origin, SpriteEffects.None, 0f);
}
public void InitiateTransition(Rectangle dimensions)
{
textureBounds.Width = (int)(ScreenSize.Height * proportion);
textureBounds.Height = (int)(ScreenSize.Height * proportion);
textureBounds.X = (ScreenSize.Width) / 2;
textureBounds.Y = (ScreenSize.Height) / 2;
origin.X = texture.Width / 2;
origin.Y = texture.Height / 2;
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
public override void EnteringTransition(double delta, bool assetsLoaded, ref Color BGColor, RenderTarget2D previousScreenExitFrame)
public bool EnteringTransition(double delta, bool assetsLoaded)
{
float deltaf = (float)delta;
if (rotate)
@@ -55,9 +67,9 @@ namespace RhythmBullet.Zer01HD.Game
{
if (!recorded)
{
rR = (Color.White.R - BGColor.R) / ENTER_RATE;
rG = (Color.White.G - BGColor.G) / ENTER_RATE;
rB = (Color.White.B - BGColor.B) / ENTER_RATE;
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;
@@ -66,19 +78,20 @@ namespace RhythmBullet.Zer01HD.Game
progG = Math.Min(progG, 254);
progB += rB * deltaf;
progB = Math.Min(progB, 254);
BGColor.R = (byte)progR;
BGColor.G = (byte)progG;
BGColor.B = (byte)progB;
BackgroundColor.R = (byte)progR;
BackgroundColor.G = (byte)progG;
BackgroundColor.B = (byte)progB;
}
else
{
DoneEnterTransition();
StartExitTransition(true);
return true;
}
}
return false;
}
public override bool ExitingTransition(double delta, bool assetsLoaded, ref Color backgroundColor)
public bool ExitingTransition(double delta, bool assetsLoaded)
{
float deltaf = (float)delta;
if (rotate)
@@ -87,7 +100,7 @@ namespace RhythmBullet.Zer01HD.Game
}
if (progC > 0)
{
float rate = deltaf * 255 / EXIT_RATE;
float rate = deltaf * 255 / EXIT_TIME;
progC -= rate;
progC = Math.Max(progC, 0);
color.R = (byte)progC;
@@ -102,20 +115,5 @@ namespace RhythmBullet.Zer01HD.Game
return false;
}
void DoRotate(float deltaf)
{
rotation += (2f / 3f) * (float)Math.PI * deltaf;
if (rotation >= 2 * Math.PI)
{
rotation = 0;
}
}
public override void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, textureBounds, null, color, rotation, origin, SpriteEffects.None, 0f);
base.Draw(spriteBatch);
}
}
}

View File

@@ -0,0 +1,142 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Utilities.UI.ScreenSystem
{
public enum ScreenState { EnterTransition, ExitTransition, Normal }
public class Screen
{
public List<ITransition> Transitions;
public bool UseRenderTargetForExitTransition;
public Color BackgroundColor;
public Screen(bool useEnterTransition = false)
{
State = useEnterTransition ? ScreenState.EnterTransition : ScreenState.Normal;
Transitions = new List<ITransition>();
}
public void Initiate(Rectangle screenSize)
{
Initiated = true;
this.ScreenSize = screenSize;
foreach (ITransition transition in Transitions)
{
transition.InitiateTransition(screenSize);
}
}
public virtual void Update(GameTime gameTime)
{
}
public virtual void Draw(SpriteBatch spriteBatch)
{
foreach (ITransition transition in Transitions)
{
transition.DrawTransition(spriteBatch);
}
}
/// <summary>
/// Updates the transition.
/// </summary>
/// <param name="delta">Time passed since last frame in seconds.</param>
/// <param name="assetsLoaded">If waiting on assets.</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 assetsLoaded)
{
switch (State)
{
case ScreenState.EnterTransition:
EnteringTransition(delta, assetsLoaded);
break;
case ScreenState.ExitTransition:
return ExitingTransition(delta, assetsLoaded);
}
return false;
}
private void EnteringTransition(double delta, bool assetsLoaded)
{
bool complete = true;
for (int transitionID = 0; transitionID < Transitions.Count; transitionID++)
{
ITransition transition = Transitions[transitionID];
if (!transition.EnteringTransition(delta, assetsLoaded))
{
complete = false;
}
}
if (complete && State != ScreenState.ExitTransition)
{
State = ScreenState.Normal;
}
}
private bool ExitingTransition(double delta, bool assetsLoaded)
{
bool complete = true;
foreach (ITransition transition in Transitions)
{
if (!transition.ExitingTransition(delta, assetsLoaded))
{
complete = false;
}
}
return complete;
}
public virtual void Show()
{
}
public virtual void Hide()
{
}
public virtual void AssetLoadStateChange(bool state)
{
}
public ScreenState State { get; private set; }
public void StartExitTransition(bool UseRenderTargetForExitTransition = false)
{
this.UseRenderTargetForExitTransition = UseRenderTargetForExitTransition;
State = ScreenState.ExitTransition;
}
public virtual void UpdatePreviousScreenFrame(RenderTarget2D previousScreenFrame)
{
foreach (ITransition transition in Transitions)
{
transition.UpdatePreviousScreenFrame(previousScreenFrame);
}
}
public Screen NextScreen
{ get; protected set; }
public Rectangle ScreenSize
{ get; protected set; }
public bool Initiated
{
get;
private set;
}
}
}