Game Development Reference
In-Depth Information
In this case, we calculate the cursor's top coordinate to be at the input coordinate, but we
offset vertically by half the cursor's height, this has the effect of manipulating the cursor
from its middle.
Finally, we need to handle the case in which the player used the scroll wheel. For this we
will introduce another variable to keep track of the value of the mouse wheel when it was
last used, m_lastMouseWheelValue .
if ( mouse->GetMouseWheelValue() != m_lastMouseWheelValue )
{
int mouseWheelDelta = m_lastMouseWheelValue - mouse->GetMouseWheelValue();
const float spacing = m_rectangle.Height() * 0.1f;
m_cursor.Top() += (mouseWheelDelta > 0) ? spacing : -spacing;
math::Clamp(m_cursor.Top(), m_rectangle.Top(), m_rectangle.Bottom() - m_cursor.Height());
m_lastMouseWheelValue = mouse->GetMouseWheelValue();
return true;
}
Using the mouse wheel consists of determining which direction the mouse wheel moved,
we do this by getting the difference between the current mouse wheel value and the last
value we recorded. If the mouse wheel delta is greater than zero, meaning the scroll has
moved down, we will increase the cursor's top coordinate by the vertical spacing, if it's
less than zero, it means the wheel was moved up and we need to decrease the cursor's top
coordinate by the vertical spacing. It's important to clamp the cursor within the scroll bar's
boundaries and to record the value of the mouse wheel.
Oncewehavehandled anyinputbytheuser,wecanfocusondrawingthescroll bar,thisis
fairly straightforward, we will draw a rectangle for the scroll bar's area and a rectangle for
the cursor.
void scrollbar::Draw()
{
auto& texture = *m_core->GetWhiteTexture()->GetView();
m_spriteBatch->Draw(texture, m_rectangle , nullptr, m_backgroundColor);
m_spriteBatch->Draw(texture, m_cursor , nullptr, m_foregroundColor);
}
Finally, the most important function to the owner of a scroll bar is the function that will
return the ratio that represents the position of the cursor within the scroll bar. It's the ratio
that will be used to determine which part of the owner control should be visible.
Search WWH ::




Custom Search