Game Development Reference
In-Depth Information
Let's begin by looking at how we will handle input. An item is a part of the listbox control
and will receive a HandleInput function call from the listbox's own HandleInput . What we
are looking to detect here is whether the mouse is presently hovering over the item, and
whether the item has been clicked, or selected.
void listboxitemtext::Update(float deltaTime)
{
if ( !m_visible ) return;
listboxitem::Update(deltaTime);
m_inputDelay.Update(deltaTime);
}
bool listboxitemtext::HandleInput(float deltaTime, const input::input_state& inputState)
{
if ( !m_visible )
return false;
auto mouse = inputState.GetMouse();
if ( mouse != nullptr )
{
math::rectangle rectangle = m_owner->Rectangle();
rectangle.Top() = m_rectangle.Top();
rectangle.Height() = m_size.y();
m_mouseOver = rectangle.Contains(mouse->GetPosition());
if ( m_mouseOver )
{
if ( m_inputDelay.IsDone() && mouse->ButtonPressed(input::mouse::eButton::Left) )
{
m_inputDelay.Start();
m_selected = !m_selected;
m_onSelected.Invoke(this, this);
return true;
}
}
}
return false;
}
As you probably noticed, we have a small helper countdown object m_inputDelay . When
weclickthemouse,theactualdurationoftheclickmaylastoneorseveralgameframesno
matter how quick our mouse clicking might have been. To avoid multiple clicks from be-
ing handled each frame, whenever we detect a mouse click we will trigger this countdown
object, while the object is active no further mouse clicks will be handled.
Therestofthefunctionconsistsofbuildingtherectangularareathatiscoveredbytheitem,
and testing if the mouse coordinates are within this rectangle. If they are, we may set the
m_mouseOver flagtotrue,thiswillallowustohighlight items thatwearecurrently hover-
ingover.Furthermore, if m_mouseOver is true, andwe detect aleft mouse button click, we
will toggle the m_selected flag, and we will notify the user that a selection has occurred.
Admittedly,wecouldprovideadifferentnotificationeventiftheselectionwastrueorfalse
Search WWH ::




Custom Search