Advertisement
Some_Devil

Progressbar

Oct 7th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.04 KB | None | 0 0
  1. public class ProgressBar : Disposable
  2.     {
  3.         private readonly PlayerTextDraw _back;
  4.         private readonly PlayerTextDraw _fill;
  5.         private readonly PlayerTextDraw _main;
  6.         private float _value;
  7.         private float _max;
  8.  
  9.         public ProgressBar(BasePlayer player, float x, float y, float value, Color color = default(Color),
  10.             float width = 55.5f, float height = 3.2f,
  11.             float max = 100.0f, ProgressBarDirection direction = ProgressBarDirection.Right)
  12.         {
  13.             X = x;
  14.             Y = y;
  15.             _value = value;
  16.             Width = width;
  17.             Height = height;
  18.             Color = color;
  19.             _max = max;
  20.             Direction = direction;
  21.  
  22.             if (player == null) throw new ArgumentNullException(nameof(player));
  23.  
  24.             switch (direction)
  25.             {
  26.                 case ProgressBarDirection.Right:
  27.                     _back = new PlayerTextDraw(player, new Vector2(x, y), "_")
  28.                     {
  29.                         UseBox = true,
  30.                         Width = x + width - 4.0f,
  31.                         Height = 0.0f,
  32.                         LetterSize = new Vector2(1.0f, height / 10),
  33.                         BoxColor = 0x00000000 | (color & 0x000000FF)
  34.                     };
  35.  
  36.                     _fill = new PlayerTextDraw(player, new Vector2(x + 1.2f, y + 2.15f), "_")
  37.                     {
  38.                         UseBox = true,
  39.                         Width = x + width - 5.5f,
  40.                         Height = 0.0f,
  41.                         LetterSize = new Vector2(1.0f, height / 10 - 0.35f),
  42.                         BoxColor = (int)((color & 0xFFFFFF00) | (0x66 & ((color & 0x000000FF) / 2)))
  43.                     };
  44.  
  45.                     _main = new PlayerTextDraw(player, new Vector2(x + 1.2f, y + 2.15f), "_")
  46.                     {
  47.                         UseBox = true,
  48.                         Width = CalculatePercentage(),
  49.                         Height = 0.0f,
  50.                         LetterSize = new Vector2(1.0f, height / 10 - 0.35f),
  51.                         BoxColor = color
  52.                     };
  53.                     break;
  54.                 case ProgressBarDirection.Left:
  55.                     _back = new PlayerTextDraw(player, new Vector2(x, y), "_")
  56.                     {
  57.                         UseBox = true,
  58.                         Width = x - width - 4.0f,
  59.                         Height = 0.0f,
  60.                         LetterSize = new Vector2(1.0f, height / 10),
  61.                         BoxColor = 0x00000000 | (color & 0x000000FF)
  62.                     };
  63.  
  64.                     _fill = new PlayerTextDraw(player, new Vector2(x - 1.2f, y + 2.15f), "_")
  65.                     {
  66.                         UseBox = true,
  67.                         Width = x - width - 2.5f,
  68.                         Height = 0.0f,
  69.                         LetterSize = new Vector2(1.0f, height / 10 - 0.35f),
  70.                         BoxColor = (int)((color & 0xFFFFFF00) | (0x66 & ((color & 0x000000FF) / 2)))
  71.                     };
  72.  
  73.                     _main = new PlayerTextDraw(player, new Vector2(x - 1.2f, y + 2.15f), "_")
  74.                     {
  75.                         UseBox = true,
  76.                         Width = CalculatePercentage(),
  77.                         Height = 0.0f,
  78.                         LetterSize = new Vector2(1.0f, height / 10 - 0.35f),
  79.                         BoxColor = color
  80.                     };
  81.                     break;
  82.                 case ProgressBarDirection.Up:
  83.                     _back = new PlayerTextDraw(player, new Vector2(x, y), "_")
  84.                     {
  85.                         UseBox = true,
  86.                         Width = x - width - 4.0f,
  87.                         Height = 0.0f,
  88.                         LetterSize = new Vector2(1.0f, -(height / 10 * 1.02f) - 0.35f),
  89.                         BoxColor = 0x00000000 | (color & 0x000000FF)
  90.                     };
  91.  
  92.                     _fill = new PlayerTextDraw(player, new Vector2(x - 1.2f, y - 1.0f), "_")
  93.                     {
  94.                         UseBox = true,
  95.                         Width = x - width - 2.5f,
  96.                         Height = 0.0f,
  97.                         LetterSize = new Vector2(1.0f, -(height / 10.0f) * 1.02f),
  98.                         BoxColor = (int)((color & 0xFFFFFF00) | (0x66 & ((color & 0x000000FF) / 2)))
  99.                     };
  100.  
  101.                     _main = new PlayerTextDraw(player, new Vector2(x - 1.2f, y - 1.0f), "_")
  102.                     {
  103.                         UseBox = true,
  104.                         Width = x - width - 2.5f,
  105.                         Height = 0.0f,
  106.                         LetterSize = new Vector2(0.0f, CalculatePercentage()),
  107.                         BoxColor = color
  108.                     };
  109.                     break;
  110.  
  111.                 case ProgressBarDirection.Down:
  112.                     _back = new PlayerTextDraw(player, new Vector2(x, y), "_")
  113.                     {
  114.                         UseBox = true,
  115.                         Width = x - width - 4.0f,
  116.                         Height = 0.0f,
  117.                         LetterSize = new Vector2(1.0f, height / 10 - 0.35f),
  118.                         BoxColor = 0x00000000 | (color & 0x000000FF)
  119.                     };
  120.  
  121.                     _fill = new PlayerTextDraw(player, new Vector2(x - 1.2f, y + 1.0f), "_")
  122.                     {
  123.                         UseBox = true,
  124.                         Width = x - width - 2.5f,
  125.                         Height = 0.0f,
  126.                         LetterSize = new Vector2(1.0f, height / 10.0f - 0.55f),
  127.                         BoxColor = (int)((color & 0xFFFFFF00) | (0x66 & ((color & 0x000000FF) / 2)))
  128.                     };
  129.  
  130.                     _main = new PlayerTextDraw(player, new Vector2(x - 1.2f, y + 1.0f), "_")
  131.                     {
  132.                         UseBox = true,
  133.                         Width = x - width - 2.5f,
  134.                         Height = 0.0f,
  135.                         LetterSize = new Vector2(0.0f, CalculatePercentage()),
  136.                         BoxColor = color
  137.                     };
  138.                     break;
  139.             }
  140.         }
  141.  
  142.         public float X { get; private set; }
  143.         public float Y { get; private set; }
  144.         public float Width { get; private set; }
  145.         public float Height { get; private set; }
  146.         public Color Color { get; private set; }
  147.  
  148.         public float Max
  149.         {
  150.             get { return _max; }
  151.             private set
  152.             {
  153.                 _max = value;
  154.                 Redraw();
  155.             }
  156.         }
  157.  
  158.         public float Value
  159.         {
  160.             get { return _value; }
  161.             set
  162.             {
  163.                 _value = value;
  164.                 Redraw();
  165.             }
  166.         }
  167.  
  168.         public ProgressBarDirection Direction { get; private set; }
  169.  
  170.         private float CalculatePercentage()
  171.         {
  172.             float result = 0;
  173.  
  174.             switch (Direction)
  175.             {
  176.                 case ProgressBarDirection.Right:
  177.                     result = X - 3.0f + (X - 2.0f + Width - X) / Max * Value;
  178.                     break;
  179.                 case ProgressBarDirection.Left:
  180.                     result = X - 1.0f - (X + 2.0f - Width - X) / Max * -Value - 4.0f;
  181.                     break;
  182.                 case ProgressBarDirection.Up:
  183.                     result = -((Height / 10.0f - 0.45f) * 1.02f / Max * Value + 0.55f);
  184.                     break;
  185.                 case ProgressBarDirection.Down:
  186.                     result = (Height / 10.0f - 0.45f) * 1.02f / Max * Value - 0.55f;
  187.                     break;
  188.             }
  189.  
  190.             return result;
  191.         }
  192.  
  193.         private void Redraw()
  194.         {
  195.             if (_max < 0.1f) _max = 0.1f;
  196.             _value = _value < 0 ? 0 : (_value > Max ? Max : _value);
  197.  
  198.             _main.UseBox = _value > 0.0f;
  199.  
  200.             switch (Direction)
  201.             {
  202.                 case ProgressBarDirection.Right:
  203.                 case ProgressBarDirection.Left:
  204.                     {
  205.                         _main.Width = CalculatePercentage();
  206.                     }
  207.                     break;
  208.                 case ProgressBarDirection.Up:
  209.                 case ProgressBarDirection.Down:
  210.                     {
  211.                         _main.LetterSize = new Vector2(0, CalculatePercentage());
  212.                     }
  213.                     break;
  214.             }
  215.  
  216.             Show();
  217.         }
  218.  
  219.  
  220.         protected override void Dispose(bool disposing)
  221.         {
  222.             if (!IsDisposed) Hide();
  223.  
  224.             _back?.Dispose();
  225.             _fill?.Dispose();
  226.             _main?.Dispose();
  227.         }
  228.  
  229.         public void Show()
  230.         {
  231.             AssertNotDisposed();
  232.  
  233.             _back.Show();
  234.             _fill.Show();
  235.             _main.Show();
  236.         }
  237.  
  238.         public void Hide()
  239.         {
  240.             AssertNotDisposed();
  241.  
  242.             _back.Hide();
  243.             _fill.Hide();
  244.             _main.Hide();
  245.         }
  246.  
  247.     }
  248.  
  249.     public enum ProgressBarDirection
  250.     {
  251.         Up,
  252.         Down,
  253.         Left,
  254.         Right
  255.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement