Game Development Reference
In-Depth Information
The next part is to draw any text associated with the button, in order to draw the text, we
need to determine the text's position in a way that it will fit within the button. By default,
the text is aligned to the center of the button, and then it is drawn using the color specified
in the m_foregroundColor variable.
In addition to the input handling done by the base control class, a button has its own input
handling to do. The two situations we are interested in handling are a button press and
a release . The base control already gives us information when the mouse cursor is over
its bounds, we can take advantage of this information to avoid duplicating the boundary
checking and just check the value of m_mouseOver .
To determine if a button has been pressed we need to pass three tests, first is the mouse
over the button, second is the button not already pressed and finally is the mouse button
pressed, this will avoid notifying the button press while the mouse button is held down. In
some cases we may want to know if the button is held down on a button, if so, we should
add a separate event and leave OnPress to only mean a single press.
To determine if the button has been released we first check if the button is in the Pressed
state, if it is and the mouse button is not pressed, then we can revert the button's state back
to Default. There is an edge case that we need to consider. What should the behavior be
if the mouse button is released while the mouse cursor is no longer within the button's
boundaries?Thissituationunfortunatelyremovessomeoftheuniformityofthecodeaswe
need to add a variation to the code such that, if the button is released while the cursor is
beyond the button's bounds, then we reset the state of the button but we do not invoke the
OnRelease event.
bool button::HandleInput(float deltaTime, const input::input_state& inputState)
{
auto mouse = inputState.GetMouse();
if ( mouse != nullptr )
{
const bool leftMouseButtonPressed = mouse->ButtonPressed(input::mouse::eButton::Left);
if( m_state == Pressed )
{
if ( !leftMouseButtonPressed )
{
ChangeStateTo(Default, m_mouseOver);
}
}
else
{
if ( m_mouseOver && leftMouseButtonPressed )
{
ChangeStateTo(Pressed, true);
}
Search WWH ::




Custom Search