rhythmbullet/RhythmBullet/Zer01HD/Utilities/UI/ScreenSystem/LoadingScreen.cs

120 lines
3.7 KiB
C#
Raw Normal View History


using Microsoft.Xna.Framework;
2018-09-15 19:05:14 +00:00
using Microsoft.Xna.Framework.Graphics;
using System;
namespace RhythmBullet.Zer01HD.Utilities.UI.ScreenSystem
2018-09-15 19:05:14 +00:00
{
class LoadingScreen : Screen, ITransition
2018-09-15 19:05:14 +00:00
{
private const float ENTER_TIME = 2f;
private const float EXIT_TIME = 1f;
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;
public LoadingScreen(Texture2D texture, float proportion, bool rotate = false) : base(true)
2018-09-15 19:05:14 +00:00
{
color = Color.White;
this.texture = texture;
this.proportion = proportion;
this.rotate = rotate;
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 bool EnteringTransition(double delta, bool assetsLoaded)
{
float deltaf = (float)delta;
if (rotate)
{
DoRotate(deltaf);
}
if (assetsLoaded)
{
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;
}
public bool ExitingTransition(double delta, bool assetsLoaded)
{
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;
}
2018-09-15 19:05:14 +00:00
}
}