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