Game Development Reference
In-Depth Information
(if the item was already selected we actually unselected by this action), however, when we
broadcasttheselectionevent,weareprovidingtheitemastheevent'sparameter,sothecli-
ent can test if the item is selected or not using the Selected() function. This is one of many
small, but important design choices we must make when creating systems that will be used
by other programmers, keeping in mind that the design decision must be consistent across
the rest of the controls.
The next function that we need to implement is the ability to draw the item, this will ulti-
mately depend on the type of game or system that is being developed.
void listboxitemtext::Draw(const math::vector2& position, std::shared_ptr<DirectX::SpriteFont> font,
std::shared_ptr<DirectX::SpriteBatch> spriteBatch)
{
if ( m_visible )
{
SetPosition(position);
render::color color = m_color;
if ( m_mouseOver )
{
color = render::color::RED;
}
if ( m_selected )
{
color = render::color::GREEN;
}
m_owner->Font()->DrawString(spriteBatch.get(), m_text.c_str(), position, color );
}
}
For this simple example we'll take the liberty of hardcoding the colors of the item. It is
the listbox control that will calculate the position for each item as it iterates over the list of
visible items, this is why the position is passed in and we forward it to the item itself us-
ing SetPosition . Finally, in our overly simplistic example, if we have set the m_mouseOver
flag,wewilldrawtheiteminred,iftheitemisselected,wewilldrawitingreen,otherwise
it will draw in its default white.
Now that we the workings of the listbox item defined, we will need to focus on the listbox
itself. The listbox will be a list of items, of which we will potentially draw a subset of.
class listbox : public control
{
Search WWH ::




Custom Search