Game Development Reference
In-Depth Information
eState m_state;
eType m_type;
math::rectangle m_cursor;
float m_delta;
int m_lastMouseWheelValue;
};
As the scroll bar is a control, it may implement the same functions as any other controls
do. The bulk of the work for a scroll bar takes place within the HandleInput function.
There are three distinct cases that we need to account for, the first is what happens when
the user clicks within the scroll bar's cursor, the second is what happens when the user
clicks within the scroll bar's boundaries, but not within the cursor. And finally, in the case
that the input is coming through the mouse wheel, we will add a way to apply movement
to the scroll bar from the outside, this enables the control that owns the scroll bar to
change the cursor's position within its own input handler.
The first case we will handle is when the player is clicking within the scroll bar's cursor.
In the event of a left mouse press, we will check the state of the scroll bar, if it's in its de-
fault state, meaning not pressed, then we will change the state into Pressed and we will re-
cord the distance from the point of the mouse click to the edge of the scroll bar ( ) the
edge will be the left of the cursor for a horizontal scroll bar or the top for a vertical scroll
bar.
Otherwise, if the control is already in the Pressed state, then we will adjust the position of
the cursor's top, or left coordinate to the position of the mouse input minus the recorded
, this will place the cursor at the precise position as the mouse is moved as long as the
mouse button is held.
bool HandleInput(float, const input::input_state& inputState)
{
auto& edge = (m_type == Vertical) ? m_cursor.Top() : m_cursor.Left();
auto& cursorSize = (m_type == Vertical) ? m_cursor.Height() : m_cursor.Width();
const auto minimal = (m_type == Vertical) ? m_rectangle.Top() : m_rectangle.Left();
const auto maximal = (m_type == Vertical) ? m_rectangle.Bottom() - m_cursor.Height() : m_rectangle.Right() - m_cursor.Width();
auto mouse = inputState.GetMouse();
if ( mouse != nullptr )
{
auto& input = (m_type == Vertical) ? mouse->GetPosition().y() : mouse->GetPosition().x();
if ( m_cursor.Contains(mouse->GetPosition()) )
{
if ( mouse->ButtonPressed(input::mouse::Left) )
{
if ( m_state == Default )
{
m_state = Pressed;
Search WWH ::




Custom Search