using Microsoft.Xna.Framework;
namespace MonoGame.Extended.NuclexGui.Controls.Desktop
{
/// Horizontal slider that can be moved using the mouse
public class GuiHorizontalSliderControl : GuiSliderControl
{
/// Obtains the region covered by the slider's thumb
/// The region covered by the slider's thumb
protected override RectangleF GetThumbRegion()
{
var bounds = GetAbsoluteBounds();
if (ThumbLocator != null)
return ThumbLocator.GetThumbPosition(bounds, ThumbPosition, ThumbSize);
var thumbWidth = bounds.Width*ThumbSize;
var thumbX = (bounds.Width - thumbWidth)*ThumbPosition;
return new RectangleF(thumbX, 0, thumbWidth, bounds.Height);
}
/// Moves the thumb to the specified location
/// Location the thumb will be moved to
protected override void MoveThumb(float x, float y)
{
var bounds = GetAbsoluteBounds();
var thumbWidth = bounds.Width*ThumbSize;
var maxX = bounds.Width - thumbWidth;
// Prevent divide-by-zero if the thumb fills out the whole rail
if (maxX > 0.0f)
ThumbPosition = MathHelper.Clamp(x/maxX, 0.0f, 1.0f);
else
ThumbPosition = 0.0f;
OnMoved();
}
}
}