Game Development Reference
In-Depth Information
of data to listbox items. A straightforward way to add this support is to create an empty
base class, if users are interested in providing custom data to listbox items they can derive
from this class and assign the data to items.
We can add the following class within the lisboxitem class.
class data
{
};
void SetData(std::shared_ptr<data> data)
{
m_data = data;
}
std::shared_ptr<data> Data() { return m_data; }
protected:
std::shared_ptr<data> m_data;
Wethencanmodifythe Add functiontoreceivea shared_ptr ofanobjectthatderivesfrom
data .
void listbox::Add(const std::wstring text, std::shared_ptr<listboxitem::data> data = nullptr)
{
listboxitemtext* item = new listboxitemtext(this, math::vector2(m_rectangle.Width(), m_font->GetLineSpacing()), text,
render::color::WHITE);
item->SetData(data);
item->OnSelected() += [=] (void*, listboxitem* item) { UpdateList(item); };
if ( m_sorting == Descending )
{
m_items.push_back(item);
}
else
{
m_items.push_front(item);
}
Refresh();
}
This allows to create custom data objects that we can optionally use at the time of adding
items into the listbox.
class sampledata : public listbox::listboxitem::data
{
public:
sampledata(int value)
: m_value(value)
{}
int Value() const { return m_value; }
Search WWH ::




Custom Search