Game Development Reference
In-Depth Information
4.3.3 Container
A container is a special control that is used to store other controls, it provides the entry
point towards drawing a user interface and is used to call the functions responsible for up-
dating, handling input and drawing all the controls within.
The motivation for the container object is to group controls tougher but also to make the
creation of user interfaces straightforward from the user perspective, a container may also
work as a viewport enabling any controls within it to be drawn relative to its position and
size.
class container : public control
{
public:
container(std::shared_ptr<core_ui> core, const std::shared_ptr<render::platform::font>& font)
: control(core, font, nullptr)
{}
template <typename T>
std::shared_ptr<T> Add()
{
auto ctrl = factory::Create<T>(m_core, m_font, m_spriteBatch);
ctrl->SetViewport(m_viewport);
ctrl->SetPosition(math::vector2::Zero);
m_controls.push_back(ctrl);
return ctrl;
}
A container is a control as any other, with the exception that it does not allow an external
spritebatchobject tobepassedin,allcontrolsinacontainer willusethesamespritebatch.
Thisrestrictionisinplacetoreducethepossibilityofcertaintypesofbugstobeintroduced.
Adding a control to a container is done using templates, this allows us to specify the type
of the control we wish to add, we then use a factory to create the instance of the control of
that type.
class factory
{
public:
template <class T>
static std::shared_ptr<T> Create(std::shared_ptr<core_ui> core, const std::shared_ptr<render::platform::font>& font, const
std::shared_ptr<render::platform::sprite_batch> spriteBatch)
{
return std::shared_ptr<T>( new T(core, font, spriteBatch) );
}
};
Search WWH ::




Custom Search