using System; using System.IO; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace Foresight.Engine.Scenes { public class LoadingScene : Scene { protected const string ContentPath = "resmenu\\loading"; private static readonly Vector2 LoadingBarPosition = new Vector2(284, 580); private static readonly Vector2 MapLoadingBarPosition = new Vector2(450, 675); // Textures private readonly Texture2D _barTexture; private readonly Texture2D _mainTexture; public bool Is3D() => false; private readonly Game _game; private readonly bool _isMap; private Vector2 BarPosition => _isMap ? MapLoadingBarPosition : LoadingBarPosition; private int _progress; private Rectangle _barBoundingRectangle; public LoadingScene(Game game, string mapName = null) { _game = game; _isMap = mapName != null; var loadingTextureLocation = Path.Combine(ContentPath, _isMap ? mapName : "NowLoading"); _mainTexture = game.Content.Load(loadingTextureLocation); var barTextureLocation = Path.Combine(ContentPath, _isMap ? "NowLoadingTextColor" : "ProgressBar"); if (_isMap) { var origBarTexture = game.Content.Load(barTextureLocation); // crop out the texture to only the text var newBarRect = new Rectangle(450, 0, 550, 64); _barTexture = new Texture2D(origBarTexture.GraphicsDevice, 550, 64); var data = new Color[550 * 64]; origBarTexture.GetData(0, newBarRect, data, 0, 550 * 64); _barTexture.SetData(data); } else if (!_isMap) { _barTexture = game.Content.Load(barTextureLocation); } Update(0); } public void Update(double percent) { if (percent > 1) percent = 1; if (percent < 0) percent = 0; var percentF = decimal.ToDouble(decimal.Round((decimal) percent, 2, MidpointRounding.AwayFromZero)); var barWidth = (int)((_isMap ? 550 : _barTexture.Width) * percentF); _barBoundingRectangle = new Rectangle(0, 0, barWidth, _barTexture.Height); } public override void Draw(SpriteBatch spriteBatch) { spriteBatch.Draw(_mainTexture, Vector2.Zero, Color.White); spriteBatch.Draw(_barTexture, BarPosition, _barBoundingRectangle, Color.White); } public override void Dispose() { _barTexture?.Dispose(); _mainTexture?.Dispose(); } } }