I'm making a simple UI system for a small game and i'm having some difficulty to implement a Scrollbar.
At the moment i'm having two major problems that i need some help to solve:
. Calculating the "Value" property of the Scrollbar when the current position of the Thumb on the Track is changed.
. Calculating the "Minimum" and "Maximum" properties to limit the position of the Thumb on the Track.
Below is part of the code that i currently have. Please, look at the commented blocks.
public int Minimum { get; set; }
public int Maximum { get; set; }
public int Value { get; set; }
public void Update()
{
// Calculate the Scrollbar Thumb height:
var viewable_ratio = ViewportHeight / ContentHeight;
var scrollbar_area = ViewportHeight - ( ArrowHeight * 2 );
this.ThumbHeight = scrollbar_area * viewable_ratio;
// Calculate the Scrollbar Thumb step:
var track_space = ContentHeight - ViewportHeight;
var thumb_space = ViewportHeight - ThumbHeight;
this.ThumbStep = track_space / thumb_space;
// TODO: Calculate the "Minimum" and "Maximum" values.
this.Minimum = ???
this.Maximum = ???
Point mouse = Input.MousePosition();
if(Thumb.Rectangle.Contains(mouse) && Input.MouseDown(0))
{
int x = Thumb.Position.x;
int y = ( mouse.y - Thumb.Position.y );
Thumb.Position = new Point(x, y);
if(Thumb.Position.y < Minimum) Thumb.Position.y = Minimum;
if(Thumb.Position.y > Maximum) Thumb.Position.y = Maximum;
// TODO: Set the "Value" property to the correct value when the Thumb is moved.
this.Value = ???
this.Content.Position.y = this.Value;
}
}
The "Value" property is used to scroll the Content and represent his position in the Scrollbar.
The "Minimum" and "Maximum" properties are used to not let the Thumb move outside of the Track.
Could someone please provide a direct answer with the math to calculate the scrollbar Value, Minimum and Maximum properties?
Thanks in advance!