Game Development Reference
In-Depth Information
In order to give menus some versatility, we will implement menus so that they support any
control as a menu item. This means the code will be more complex to setup a menu than it
would be for a simple menu, but we'll take some steps to try and reduce the complexity.
class iitem
{
public:
virtual std::shared_ptr<control> Control() = 0;
};
We will start by creating an abstract interface that will allow us to retreive the control from
the menu item. This will be the interface our menu items will use.
template <typename T = control>
class item : public iitem
{
public:
item(std::shared_ptr<T> control)
{
m_control = std::move(control);
}
virtual std::shared_ptr<control> Control() { return m_control; }
protected:
std::shared_ptr<T> m_control;
};
The menu item base class will take a template which by default will be a control. We will
construct a menu item by providing a control to it and the template argument will define
which type of control. This means that a menu item could be a label, a button, a container
of other controls, even a text box if we wanted to.
The fact that we are using template arguments, together with shared_ptr can lead to some
cumbersomesyntaxwhencreatingamenuitem.Wecansimplifythissomewhatbyprovid-
ing a typedef of some common used controls.
typedef item<label> itemlabel;
typedef item<button> itembutton;
typedef item<container> itemcontainer;
Having established how menu items work, we will continue to develop the menu itself.
class menu : public control
Search WWH ::




Custom Search