Game Development Reference
In-Depth Information
Figure 58 - Taking into account the cursor's size, the scrollbar's effective area is slightly smaller.
Since we are using the cursor's middle coordinate as the point at which we calculate the
ratio, we need to subtract the cursor's half height from the scroll bar's height to make sure
our ratio falls within the range of [0,1], that is, so the cursor falls within the effective area.
float Ratio() const
{
math::rectangle effectiveArea = m_rectangle;
float halfSize = 0.f;
float edge = 0.f;
float size = 0.f;
float coordinate = 0.f;
if ( m_type == Vertical )
{
halfSize = m_cursor.Height() / 2;
edge = effectiveArea.Top() + halfSize;
size = effectiveArea.Height() - m_cursor.Height();
coordinate = m_cursor.Top();
}
else
{
halfSize = m_cursor.Width() / 2;
edge = effectiveArea.Left() + halfSize;
size = effectiveArea.Width() - m_cursor.Width();
coordinate = m_cursor.Left();
}
return ((coordinate + halfSize) - edge) / size;
}
The function is designed to work for either a vertical or horizontal scrollbar, the calcula-
tions are the same, what changes is the coordinates they are applied on. We calculate half
thesizeofthecursor,halftheheightifit'saverticalscrollbar,halfthewidthforahorizont-
Search WWH ::




Custom Search