Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions MaterialSkin/Controls/MaterialSlider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ namespace MaterialSkin.Controls
{
public class MaterialSlider : Control, IMaterialControl
{
#region "Private members"
private bool _mousePressed;
public enum Directions { Normal, Reverse }

#region "Private members"
private bool _mousePressed;
private int _mouseX;
//private int _indicatorSize;
private bool _hovered = false;
Expand Down Expand Up @@ -182,12 +184,26 @@ public MaterialSkinManager.fontType FontType
}
}

protected int _stepChange;
[DefaultValue(2)]
[Category("Material Skin")]
[Description("Define control step change value")]
public int StepChange
{
get => _stepChange;
set => _stepChange = value.Clamp(1, RangeMax);
}

#endregion
[DefaultValue(Directions.Normal)]
[Category("Material Skin")]
[Description("Define control direction change value with mouse wheel")]
public Directions ScrollDirection { get; set; }

#endregion

#region "Events"
#region "Events"

[Category("Behavior")]
[Category("Behavior")]
[Description("Occurs when value change.")]
public delegate void ValueChanged(object sender, int newValue);
public event ValueChanged onValueChanged;
Expand Down Expand Up @@ -255,14 +271,13 @@ protected override void OnMouseDown(MouseEventArgs e)
protected override void OnMouseWheel(MouseEventArgs e)
{
base.OnMouseWheel(e);
if (_valueMax != 0 && (Value + e.Delta / -40) > _valueMax)
Value = _valueMax;
else
Value += e.Delta/-40;
onValueChanged?.Invoke(this, _value);
int scrollLines = SystemInformation.MouseWheelScrollLines;
Value += e.Delta / 40 / scrollLines * StepChange * (ScrollDirection == Directions.Normal ? 1 : -1);
Value = Value.Clamp(RangeMin, RangeMax);
onValueChanged?.Invoke(this, _value);
}

protected override void OnMouseEnter(EventArgs e)
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
_hovered = true;
Expand Down
21 changes: 21 additions & 0 deletions MaterialSkin/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,26 @@ public static int PercentageToColorComponent(this int percentage)
{
return (int)((percentage / 100d) * 255d);
}

// Simulate Clamp function from .NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8
// https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Math.cs,538
internal static int Clamp(this int value, int min, int max)
{
if (min > max)
{
throw new ArgumentException(string.Format("'{0}' cannot be greater than {1}.", min, max));
}

if (value < min)
{
return min;
}
else if (value > max)
{
return max;
}

return value;
}
}
}