Skip to content
This repository was archived by the owner on Sep 21, 2023. It is now read-only.

Commit 763529f

Browse files
author
Alexis Huvier
committed
feat(widgets): Create ProgressBar and Slider
1 parent ec52e42 commit 763529f

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

SharpEngine/Widget/ProgressBar.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using SharpEngine.Math;
2+
using SharpEngine.Renderer;
3+
using SharpEngine.Utils;
4+
5+
namespace SharpEngine.Widget;
6+
7+
/// <summary>
8+
/// Class which represents ProgressBar
9+
/// </summary>
10+
public class ProgressBar: Widget
11+
{
12+
private float _value;
13+
/// <summary>
14+
/// Value of Bar (0 to 100)
15+
/// </summary>
16+
public float Value
17+
{
18+
get => _value;
19+
set => _value = MathHelper.Clamp(value, 0, 100);
20+
}
21+
22+
/// <summary>
23+
/// Size of Bar
24+
/// </summary>
25+
public Vec2 Size { get; set; }
26+
27+
/// <summary>
28+
/// Color of Bar
29+
/// </summary>
30+
public Color Color { get; set; }
31+
32+
/// <summary>
33+
/// Create ProgressBar
34+
/// </summary>
35+
/// <param name="position">Position</param>
36+
/// <param name="size">Size (Vec2(150, 60))</param>
37+
/// <param name="color">Color (Color.Green)</param>
38+
/// <param name="value">Value (0)</param>
39+
/// <param name="zLayer">ZLayer (0)</param>
40+
public ProgressBar(Vec2 position, Vec2? size = null, Color? color = null, float value = 0, int zLayer = 0) : base(
41+
position, zLayer)
42+
{
43+
Size = size ?? new Vec2(150, 60);
44+
Color = color ?? Color.Green;
45+
Value = value;
46+
}
47+
48+
/// <inheritdoc />
49+
public override void Draw()
50+
{
51+
base.Draw();
52+
53+
if(!Displayed || Size == Vec2.Zero) return;
54+
55+
var position = RealPosition;
56+
SERender.DrawRectangle(new Rect(position, Size), Size / 2, 0, Color.Black, InstructionSource.UI,
57+
ZLayer);
58+
SERender.DrawRectangle(new Rect(position, Size - 4), (Size - 4) / 2, 0, Color.White, InstructionSource.UI,
59+
ZLayer + 0.00001f);
60+
SERender.DrawRectangle(new Rect(position, (Size.X - 8) * Value / 100, Size.Y - 8), (Size - 8) / 2, 0, Color,
61+
InstructionSource.UI, ZLayer + 0.00002f);
62+
}
63+
}

SharpEngine/Widget/Slider.cs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using System;
2+
using SharpEngine.Manager;
3+
using SharpEngine.Math;
4+
using SharpEngine.Renderer;
5+
using SharpEngine.Utils;
6+
using SharpEngine.Utils.EventArgs;
7+
using SharpEngine.Utils.Input;
8+
9+
namespace SharpEngine.Widget;
10+
11+
/// <summary>
12+
/// Class which represents Slider
13+
/// </summary>
14+
public class Slider: Widget
15+
{
16+
private float _value;
17+
/// <summary>
18+
/// Value of Slider (0 to 100)
19+
/// </summary>
20+
public float Value
21+
{
22+
get => _value;
23+
set => _value = MathHelper.Clamp(value, 0, 100);
24+
}
25+
26+
/// <summary>
27+
/// Size of Slider
28+
/// </summary>
29+
public Vec2 Size { get; set; }
30+
31+
/// <summary>
32+
/// Color of Slider
33+
/// </summary>
34+
public Color Color { get; set; }
35+
36+
/// <summary>
37+
/// Event trigger when value is changed
38+
/// </summary>
39+
public event EventHandler<ValueEventArgs<float>>? ValueChanged;
40+
41+
/// <summary>
42+
/// Create Slider
43+
/// </summary>
44+
/// <param name="position">Position</param>
45+
/// <param name="size">Size (Vec2(150, 60))</param>
46+
/// <param name="color">Color (Color.Green)</param>
47+
/// <param name="value">Value (0)</param>
48+
/// <param name="zLayer">ZLayer (0)</param>
49+
public Slider(Vec2 position, Vec2? size = null, Color? color = null, float value = 0, int zLayer = 0) : base(
50+
position, zLayer)
51+
{
52+
Size = size ?? new Vec2(150, 60);
53+
Color = color ?? Color.Green;
54+
Value = value;
55+
}
56+
57+
/// <inheritdoc />
58+
public override void Update(float delta)
59+
{
60+
base.Update(delta);
61+
62+
if (InputManager.IsMouseButtonDown(MouseButton.Left))
63+
{
64+
var position = RealPosition - Size / 2;
65+
if(!InputManager.IsMouseInRectangle(new Rect(position, Size))) return;
66+
67+
var barSize = Size.X;
68+
var point = InputManager.GetMousePosition().X - position.X;
69+
var temp = Value;
70+
Value = (int)System.Math.Round(point * 100 / barSize, MidpointRounding.AwayFromZero);
71+
if (System.Math.Abs(temp - Value) > Internal.FloatTolerance)
72+
{
73+
ValueChanged?.Invoke(this, new ValueEventArgs<float>
74+
{
75+
OldValue = temp,
76+
NewValue = Value
77+
});
78+
}
79+
}
80+
}
81+
82+
/// <inheritdoc />
83+
public override void Draw()
84+
{
85+
base.Draw();
86+
87+
if(!Displayed || Size == Vec2.Zero) return;
88+
89+
var position = RealPosition;
90+
SERender.DrawRectangle(new Rect(position, Size), Size / 2, 0, Color.Black, InstructionSource.UI,
91+
ZLayer);
92+
SERender.DrawRectangle(new Rect(position, Size - 4), (Size - 4) / 2, 0, Color.White, InstructionSource.UI,
93+
ZLayer + 0.00001f);
94+
SERender.DrawRectangle(new Rect(position, (Size.X - 8) * Value / 100, Size.Y - 8), (Size - 8) / 2, 0, Color,
95+
InstructionSource.UI, ZLayer + 0.00002f);
96+
}
97+
}

0 commit comments

Comments
 (0)