Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Copyrights
- /*
- Gibbo2D License - Version 1.0
- Copyright (c) 2013 - Gibbo2D Team
- Founders Joao Alves <joao.cpp.sca@gmail.com> & Luis Fernandes <luisapidcloud@hotmail.com>
- Permission is granted to use this software and associated documentation files (the "Software") free of charge,
- to any person or company. The code can be used, modified and merged without restrictions, but you cannot sell
- the software itself and parts where this license applies. Still, permission is granted for anyone to sell
- applications made using this software (for example, a game). This software cannot be claimed as your own,
- except for copyright holders. This license notes should also be available on any of the changed or added files.
- The software is provided "as is", without warranty of any kind, express or implied, including but not limited to
- the warranties of merchantability, fitness for a particular purpose and non-infrigement. In no event shall the
- authors or copyright holders be liable for any claim, damages or other liability.
- The license applies to all versions of the software, both newer and older than the one listed, unless a newer copy
- of the license is available, in which case the most recent copy of the license supercedes all others.
- */
- #endregion
- using System;
- using Gibbo.Library;
- using System.Threading.Tasks;
- using System.IO;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework;
- using System.Threading;
- namespace Samples
- {
- /// <summary>
- /// Sample controller.
- ///
- /// Notice:
- /// If you want the controller to be updated / drawn in the editor,
- /// implement ExtendedObjectComponent instead.
- /// </summary>
- class LoadingBar : ObjectComponent
- {
- #region fields
- private readonly float WINDOW_WIDTH = SceneManager.GraphicsDevice.Viewport.Width;
- private readonly float WINDOW_HEIGHT = SceneManager.GraphicsDevice.Viewport.Height;
- private const int BAR_WIDTH = 300;
- private const int BAR_HEIGHT = 50;
- private const int OFFSET_X = 2;
- private const int OFFSET_Y = 2;
- private Task _task;
- private CancellationTokenSource _cancellationTokenSource;
- private CancellationToken _cancellationToken;
- private Texture2D _textureBack, _textureLoading;
- private Color _colorLoadingBackground = Color.White;
- private Color _colorLoading = Color.CornflowerBlue;
- // Loading bar current width and height
- private int _currentWidth = 0;
- private int _currentHeight = BAR_HEIGHT - OFFSET_Y * 2; // height is fixed, since it's a horizontal loading bar
- // Loading percentage
- private float _percentage = 0.0f;
- private int _posX, _posY;
- #endregion
- #region Properties
- public Color ColorLoadingBackground
- {
- get { return _colorLoadingBackground; }
- set { _colorLoadingBackground = value; }
- }
- public Color ColorLoading
- {
- get { return _colorLoading; }
- set { _colorLoading = value; }
- }
- #endregion
- #region methods
- /// <summary>
- /// Initialize this instance.
- /// </summary>
- public override void Initialize()
- {
- // create back texture
- _textureBack = new Texture2D(SceneManager.ActiveScene.Graphics.GraphicsDevice, 1, 1);
- _textureBack.SetData<Color>(new Color[] { ColorLoadingBackground });
- // create loading texture
- _textureLoading = new Texture2D(SceneManager.ActiveScene.Graphics.GraphicsDevice, 1, 1);
- _textureLoading.SetData<Color>(new Color[] { ColorLoading });
- // calculate and store the position of the textures (center both horitontally and vertically)
- _posX = (int)(WINDOW_WIDTH / 2 - BAR_WIDTH / 2);
- _posY = (int)(WINDOW_HEIGHT / 2 - BAR_HEIGHT / 2);
- // subscribe to exiting event, in order to properly cancel the task created below if needed
- SceneManager.GameWindow.Exiting += GameWindow_Exiting;
- // create cancellation tokens in order to cancel the task created below
- _cancellationTokenSource = new CancellationTokenSource();
- _cancellationToken = _cancellationTokenSource.Token;
- // create and run task using the cancellation token above
- _task = Task.Factory.StartNew(() => LoadContent(), _cancellationToken);
- }
- /// <summary>
- /// Load Content to memory. Useful when loading heavy scenes.
- /// </summary>
- private void LoadContent()
- {
- // Retrieve every file from Content folder
- string[] files = Directory.GetFiles(SceneManager.GameProject.ProjectPath + @"\Content\", "*", SearchOption.AllDirectories);
- int done = 0;
- // iterate through every file
- foreach (var file in files)
- {
- // break the loop if cancellation was requested
- if (_cancellationToken.IsCancellationRequested)
- {
- break;
- }
- // Load file to memory
- TextureLoader.FromFile(file);
- // calculate percentage based on the loaded files
- _percentage = ++done * 100 / files.Length;
- Console.WriteLine(_percentage.ToString());
- }
- }
- public override void Update(GameTime gameTime)
- {
- // update bars width based on loading percentage
- _currentWidth = (int)(_percentage * (BAR_WIDTH - OFFSET_X * 2) / 100);
- }
- /// <summary>
- /// Component Draw
- /// </summary>
- /// <param name="gameTime">Game time.</param>
- /// <param name="spriteBatch">Sprite batch.</param>
- public override void Draw(Microsoft.Xna.Framework.GameTime gameTime, Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
- {
- spriteBatch.Begin();
- spriteBatch.Draw(_textureBack, new Rectangle(_posX, _posY, BAR_WIDTH, BAR_HEIGHT), Color.White);
- spriteBatch.Draw(_textureLoading, new Rectangle(_posX + OFFSET_X, _posY + OFFSET_Y, _currentWidth, _currentHeight), Color.White);
- spriteBatch.End();
- }
- void GameWindow_Exiting(object sender, EventArgs e)
- {
- if (!_task.IsCompleted)
- {
- Console.WriteLine("Cancel Called");
- _cancellationTokenSource.Cancel();
- }
- _cancellationTokenSource.Dispose();
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement