recrownedgtk/RecrownedAthenaeum/ScreenSystem/LoadingScreen.cs

135 lines
3.9 KiB
C#
Raw Normal View History


using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
namespace RecrownedAthenaeum.ScreenSystem
{
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;
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);
}
public override void Show()
{
game.IsMouseVisible = false;
base.Show();
}
public override void Hide()
{
game.IsMouseVisible = true;
base.Hide();
}
public void InitiateTransition(Rectangle dimensions)
{
color = Color.White;
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;
}
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, rotation, origin, SpriteEffects.None, 0f);
}
2019-01-14 05:23:03 +00:00
public bool EnteringTransition(double delta, bool waiting)
{
float deltaf = (float)delta;
if (rotate)
{
DoRotate(deltaf);
}
2019-01-14 05:23:03 +00:00
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;
}
2019-01-14 05:23:03 +00:00
public bool ExitingTransition(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;
}
}
}