Game Development Reference
In-Depth Information
4.3.8 List Box
Alistboxisacontainerthatmayholdmanyitems,allorsomemaybevisibleatatimeand
the user can choose one or many items to make a selection.
The simplest case to illustrate a list box's functionality is using text strings as the items in
the list, this will be the focus of this example. However, to give list boxes more flexibility,
we can implement it so that each item in the listbox is a user interface control, by doing
thiswecancreate list boxesofmorecomplex items, suchasgameelements represented by
an image and text, even include the item's description directly in the list box.
The first part of the list box we will look into is the list item. A list item will be an object
thatimplementstheminimumfunctionalityforaworkinglistbox.Alistboxitemmusthave
some value, typically a text string. We will need to control the visibility of each item so
that items that go beyond the bounds of the listbox control will not be visible. Another im-
portant aspect of a listbox item is to notify the user when it has been selected or clicked by
the player.
In order to allow for future specialization of list items, we will create a base class from
which we will later implement the actual working item. This base class will contain the
fundamental information to ensure the list box functions, then we will create a specialized
listbox item that draws a string of text.
class listboxitem
{
public:
listboxitem(listbox* owner)
: m_owner(owner)
, m_selected(false)
, m_visible(false)
{}
virtual void Draw(const math::vector2& position, std::shared_ptr<DirectX::SpriteFont> font, std::shared_ptr<DirectX::SpriteBatch>
spriteBatch)
{
}
virtual bool HandleInput(float deltaTime, const input::input_state& inputState)
{
return false;
}
virtual bool Selected() const { return m_selected; }
virtual bool& Visible() { return m_visible; }
virtual const math::rectangle& Rectangle() const
Search WWH ::




Custom Search