Game Development Reference
In-Depth Information
m_contents = std::shared_ptr<listbox>(new listbox(core, font, spriteBatch));
m_contents->MultiSelection() = false;
m_contents->OnItemSelected() += [&](void*, listbox::listboxitem&) { m_open = false; Refresh(); };
}
Oncewecreatethebutton,wewillinstallaneventhandleronthebuttonrelease,thishand-
ler will toggle whether the dropdown is opened or not, followed by a Refresh which will
make sure all the elements of the control are properly positioned.
Next we will create a listbox , we also will specify that the listbox will not support multiple
selection, this is important given that a dropdown control can only have one active option
at any given time.
Finally, we install an event handler when an item in the listbox has been selected, this will
allow us to close the dropdown.
Handlingtheinputforadropdownmeansdeferringinputhandlingtoeachoftheindividual
controls that make up the dropdown.
bool dropdown::HandleInput(float deltaTime, const input::input_state& inputState)
{
if ( !m_inputDelay.IsDone() )
return false;
if ( m_button->HandleInput(deltaTime, inputState) )
{
m_inputDelay.Start();
return true;
}
if ( m_open && m_contents->HandleInput(deltaTime, inputState) )
{
m_inputDelay.Start();
return true;
}
return false;
}
We use an input delay countdown to prevent multiple inputs from bombarding the input
handlersofthecontrols.Firstwewillhandleinputsonthebutton,thiswillallowustoopen
orclose the dropdown.Ifthe dropdownisopenwewill then allow the listbox tohandle the
input. In both cases, if there is an input, we will start the input delay countdown.
Updating the dropdown is straightforward, we only need to update the controls we use and
the input delay.
void dropdown::Update(float deltaTime)
Search WWH ::




Custom Search