ProgressBar.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #region File Information
  2. //-----------------------------------------------------------------------------
  3. // ProgressBar.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using Microsoft.Xna.Framework.Graphics;
  15. using Microsoft.Xna.Framework.Content;
  16. using Microsoft.Xna.Framework;
  17. #endregion
  18. namespace DynamicMenu.Controls
  19. {
  20. /// <summary>
  21. /// A control which shows a progress indicator
  22. /// </summary>
  23. public class ProgressBar : Control
  24. {
  25. #region Properties
  26. /// <summary>
  27. /// The name of the texture to use for the left side of the control
  28. /// </summary>
  29. public string LeftTextureName { get; set; }
  30. /// <summary>
  31. /// The loaded texture for the left side of the control
  32. /// </summary>
  33. [ContentSerializerIgnore]
  34. public Texture2D LeftTexture { get; set; }
  35. /// <summary>
  36. /// The name of the texture to use for the right side of the control
  37. /// </summary>
  38. public string RightTextureName { get; set; }
  39. /// <summary>
  40. /// The loaded texture for the right side of the control
  41. /// </summary>
  42. [ContentSerializerIgnore]
  43. public Texture2D RightTexture { get; set; }
  44. /// <summary>
  45. /// The current position for the progress indicator
  46. /// </summary>
  47. [ContentSerializer(Optional = true)]
  48. public int Position { get; set; }
  49. /// <summary>
  50. /// The maximum value for the progress bar
  51. /// </summary>
  52. [ContentSerializer(Optional = true)]
  53. public int MaxValue { get; set; }
  54. /// <summary>
  55. /// The color to apply to the left side of the control
  56. /// </summary>
  57. [ContentSerializer(Optional = true)]
  58. public Color LeftColor { get; set; }
  59. /// <summary>
  60. /// The color to apply to the right side of the control
  61. /// </summary>
  62. [ContentSerializer(Optional = true)]
  63. public Color RightColor { get; set; }
  64. /// <summary>
  65. /// The width of the border around the progress bar
  66. /// </summary>
  67. [ContentSerializer(Optional = true)]
  68. public int BorderWidth { get; set; }
  69. #endregion
  70. #region Initialization
  71. public ProgressBar()
  72. {
  73. LeftColor = Color.White;
  74. RightColor = Color.White;
  75. Position = 0;
  76. MaxValue = 100;
  77. BorderWidth = 0;
  78. }
  79. /// <summary>
  80. /// Loads the control content
  81. /// </summary>
  82. public override void LoadContent(GraphicsDevice _graphics, ContentManager _content)
  83. {
  84. base.LoadContent(_graphics, _content);
  85. if (!string.IsNullOrEmpty(LeftTextureName))
  86. {
  87. LeftTexture = _content.Load<Texture2D>(LeftTextureName);
  88. }
  89. if (!string.IsNullOrEmpty(RightTextureName))
  90. {
  91. RightTexture = _content.Load<Texture2D>(RightTextureName);
  92. }
  93. }
  94. #endregion
  95. #region Draw
  96. /// <summary>
  97. /// Draws the control
  98. /// </summary>
  99. public override void Draw(GameTime _gameTime, SpriteBatch _spriteBatch)
  100. {
  101. base.Draw(_gameTime, _spriteBatch);
  102. int leftSideWidth = GetLeftSideWidth();
  103. Rectangle rect = GetAbsoluteRect();
  104. rect.Width = leftSideWidth;
  105. rect.X += BorderWidth;
  106. rect.Y += BorderWidth;
  107. rect.Height -= BorderWidth * 2;
  108. Rectangle srcRect = new Rectangle();
  109. srcRect.Width = rect.Width;
  110. srcRect.Height = rect.Height;
  111. if (LeftTexture != null)
  112. {
  113. _spriteBatch.Draw(LeftTexture, rect, srcRect, LeftColor);
  114. }
  115. rect = GetAbsoluteRect();
  116. rect.X += leftSideWidth + BorderWidth;
  117. rect.Width -= BorderWidth * 2 + leftSideWidth;
  118. rect.Y += BorderWidth;
  119. rect.Height -= BorderWidth * 2;
  120. srcRect = new Rectangle();
  121. srcRect.Width = rect.Width;
  122. srcRect.Height = rect.Height;
  123. srcRect.X = leftSideWidth;
  124. if (RightTexture != null)
  125. {
  126. _spriteBatch.Draw(RightTexture, rect, srcRect, RightColor);
  127. }
  128. }
  129. #endregion
  130. #region Private Methods
  131. /// <summary>
  132. /// Gets the current width of the left side based on the position and max value
  133. /// </summary>
  134. private int GetLeftSideWidth()
  135. {
  136. float balance = (float)Position / (float)(MaxValue);
  137. int progressBarWidth = Width - BorderWidth * 2;
  138. int leftSideWidth = (int)Math.Floor((float)progressBarWidth * balance);
  139. return leftSideWidth;
  140. }
  141. #endregion
  142. }
  143. }