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 { class LoadingScreen : Screen { private const float ENTER_RATE = 2f; private const float EXIT_RATE = 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, Rectangle dimensions, 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; 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) { float deltaf = (float)delta; if (rotate) { DoRotate(deltaf); } if (assetsLoaded) { if (progR < 254 || progG < 254 || progB < 254) { 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; 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); BGColor.R = (byte)progR; BGColor.G = (byte)progG; BGColor.B = (byte)progB; } else { DoneEnterTransition(); StartExitTransition(true); } } } public override bool ExitingTransition(double delta, bool assetsLoaded, ref Color backgroundColor) { float deltaf = (float)delta; if (rotate) { DoRotate(deltaf); } if (progC > 0) { float rate = deltaf * 255 / EXIT_RATE; 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; } 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); } } }