-
-
Save htfranek/5314e1e30b1e80314b59da3f0358d98b to your computer and use it in GitHub Desktop.
SA-MP progress bar converted to C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using SampSharp.GameMode; | |
using SampSharp.GameMode.Display; | |
using SampSharp.GameMode.SAMP; | |
using SampSharp.GameMode.Tools; | |
using SampSharp.GameMode.World; | |
using SampColor = SampSharp.GameMode.SAMP.Color; | |
namespace YourNameSpace | |
{ | |
class ProgressBar : Disposable | |
{ | |
private readonly PlayerTextDraw _back; | |
private readonly PlayerTextDraw _fill; | |
private readonly PlayerTextDraw _main; | |
private float _value; | |
private float _max; | |
public ProgressBar(BasePlayer player, float x, float y, float value, Color color = default, | |
float width = 55.5f, float height = 3.2f, | |
float max = 100.0f, ProgressBarDirection direction = ProgressBarDirection.Right) | |
{ | |
X = x; | |
Y = y; | |
_value = value; | |
Width = width; | |
Height = height; | |
Color = color; | |
_max = max; | |
Direction = direction; | |
if (player == null) throw new ArgumentNullException("player"); | |
switch (direction) | |
{ | |
case ProgressBarDirection.Right: | |
_back = new PlayerTextDraw(player, new Vector2(x, y), "_") | |
{ | |
UseBox = true, | |
Width = x + width - 4.0f, | |
Height = 0.0f, | |
LetterSize = new Vector2(1.0f, height / 10), | |
BoxColor = SampColor.Black | |
}; | |
_fill = new PlayerTextDraw(player, new Vector2(x + 1.2f, y + 2.15f), "_") | |
{ | |
UseBox = true, | |
Width = x + width - 5.5f, | |
Height = 0.0f, | |
LetterSize = new Vector2(1.0f, height / 10 - 0.35f), | |
BoxColor = (uint)((color & 0xFFFFFF00) | (uint)(0x66 & ((color & 0x000000FF) / 2))) | |
}; | |
_main = new PlayerTextDraw(player, new Vector2(x + 1.2f, y + 2.15f), "_") | |
{ | |
UseBox = true, | |
Width = CalculatePercentage(), | |
Height = 0.0f, | |
LetterSize = new Vector2(1.0f, height / 10 - 0.35f), | |
BoxColor = color | |
}; | |
break; | |
case ProgressBarDirection.Left: | |
_back = new PlayerTextDraw(player, new Vector2(x, y), "_") | |
{ | |
UseBox = true, | |
Width = x - width - 4.0f, | |
Height = 0.0f, | |
LetterSize = new Vector2(1.0f, height / 10), | |
BoxColor = 0x00000000 | (color & 0x000000FF) | |
}; | |
_fill = new PlayerTextDraw(player, new Vector2(x - 1.2f, y + 2.15f), "_") | |
{ | |
UseBox = true, | |
Width = x - width - 2.5f, | |
Height = 0.0f, | |
LetterSize = new Vector2(1.0f, height / 10 - 0.35f), | |
BoxColor = (int)((color & 0xFFFFFF00) | (uint)(0x66 & ((color & 0x000000FF) / 2))) | |
}; | |
_main = new PlayerTextDraw(player, new Vector2(x - 1.2f, y + 2.15f), "_") | |
{ | |
UseBox = true, | |
Width = CalculatePercentage(), | |
Height = 0.0f, | |
LetterSize = new Vector2(1.0f, height / 10 - 0.35f), | |
BoxColor = color | |
}; | |
break; | |
case ProgressBarDirection.Up: | |
_back = new PlayerTextDraw(player, new Vector2(x, y), "_") | |
{ | |
UseBox = true, | |
Width = x - width - 4.0f, | |
Height = 0.0f, | |
LetterSize = new Vector2(1.0f, -((height / 10) * 1.02f) - 0.35f), | |
BoxColor = 0x00000000 | (color & 0x000000FF) | |
}; | |
_fill = new PlayerTextDraw(player, new Vector2(x - 1.2f, y - 1.0f), "_") | |
{ | |
UseBox = true, | |
Width = x - width - 2.5f, | |
Height = 0.0f, | |
LetterSize = new Vector2(1.0f, -(height / 10.0f) * 1.02f), | |
BoxColor = (int)((color & 0xFFFFFF00) | (uint)(0x66 & ((color & 0x000000FF) / 2))) | |
}; | |
_main = new PlayerTextDraw(player, new Vector2(x - 1.2f, y - 1.0f), "_") | |
{ | |
UseBox = true, | |
Width = x - width - 2.5f, | |
Height = 0.0f, | |
LetterSize = new Vector2(0.0f, CalculatePercentage()), | |
BoxColor = color | |
}; | |
break; | |
case ProgressBarDirection.Down: | |
_back = new PlayerTextDraw(player, new Vector2(x, y), "_") | |
{ | |
UseBox = true, | |
Width = x - width - 4.0f, | |
Height = 0.0f, | |
LetterSize = new Vector2(1.0f, (height / 10) - 0.35f), | |
BoxColor = 0x00000000 | (color & 0x000000FF) | |
}; | |
_fill = new PlayerTextDraw(player, new Vector2(x - 1.2f, y + 1.0f), "_") | |
{ | |
UseBox = true, | |
Width = x - width - 2.5f, | |
Height = 0.0f, | |
LetterSize = new Vector2(1.0f, (height / 10.0f) - 0.55f), | |
BoxColor = (int)((color & 0xFFFFFF00) | (uint)(0x66 & ((color & 0x000000FF) / 2))) | |
}; | |
_main = new PlayerTextDraw(player, new Vector2(x - 1.2f, y + 1.0f), "_") | |
{ | |
UseBox = true, | |
Width = x - width - 2.5f, | |
Height = 0.0f, | |
LetterSize = new Vector2(0.0f, CalculatePercentage()), | |
BoxColor = color | |
}; | |
break; | |
} | |
} | |
public float X { get; private set; } | |
public float Y { get; private set; } | |
public float Width { get; private set; } | |
public float Height { get; private set; } | |
public Color Color { get; private set; } | |
public float Max | |
{ | |
get { return _max; } | |
private set | |
{ | |
_max = value; | |
Redraw(); | |
} | |
} | |
public float Value | |
{ | |
get { return _value; } | |
set | |
{ | |
_value = value; | |
Redraw(); | |
} | |
} | |
public ProgressBarDirection Direction { get; private set; } | |
private float CalculatePercentage() | |
{ | |
float result = 0; | |
switch (Direction) | |
{ | |
case ProgressBarDirection.Right: | |
result = ((X - 3.0f) + (((((X - 2.0f) + Width) - X) / Max) * Value)); | |
break; | |
case ProgressBarDirection.Left: | |
result = ((X - 1.0f) - (((((X + 2.0f) - Width) - X) / Max) * -Value)) - 4.0f; | |
break; | |
case ProgressBarDirection.Up: | |
result = -((((((Height / 10.0f) - 0.45f) * 1.02f) / Max) * Value) + 0.55f); | |
break; | |
case ProgressBarDirection.Down: | |
result = ((((((Height / 10.0f) - 0.45f) * 1.02f) / Max) * Value) - 0.55f); | |
break; | |
} | |
return result; | |
} | |
private void Redraw() | |
{ | |
if (_max < 0.1f) _max = 0.1f; | |
_value = _value < 0 ? 0 : (_value > Max ? Max : _value); | |
_main.UseBox = _value > 0.0f; | |
switch (Direction) | |
{ | |
case ProgressBarDirection.Right: | |
case ProgressBarDirection.Left: | |
{ | |
_main.Width = CalculatePercentage(); | |
} | |
break; | |
case ProgressBarDirection.Up: | |
case ProgressBarDirection.Down: | |
{ | |
_main.LetterSize = new Vector2(0, CalculatePercentage()); | |
} | |
break; | |
} | |
Show(); | |
} | |
protected override void Dispose(bool disposing) | |
{ | |
if (!IsDisposed) Hide(); | |
if (_back != null) _back.Dispose(); | |
if (_fill != null) _fill.Dispose(); | |
if (_main != null) _main.Dispose(); | |
} | |
public void Show() | |
{ | |
AssertNotDisposed(); | |
_back.Show(); | |
_fill.Show(); | |
_main.Show(); | |
} | |
public void Hide() | |
{ | |
AssertNotDisposed(); | |
_back.Hide(); | |
_fill.Hide(); | |
_main.Hide(); | |
} | |
} | |
public enum ProgressBarDirection | |
{ | |
Up, | |
Down, | |
Left, | |
Right | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated the code so it's compatable with the newest version of SampSharp.