Game Development Reference
In-Depth Information
{
public:
menu(std::shared_ptr<core> core, const std::shared_ptr<DirectX::SpriteFont>& font, const std::shared_ptr<DirectX::SpriteBatch>&
spriteBatch)
: control(core, font, spriteBatch)
, m_currentItem(nullptr)
, m_currentItemIndex(SIZE_MAX)
{
}
void AddItem(std::shared_ptr<iitem> item);
virtual void InternalDraw();
virtual bool HandleInput(float deltaTime, const input::input_state& inputState);
typedef event_handler<control&> menu_event;
menu_event& OnSelection() { return m_onSelection; }
menu_event& OnAccept() { return m_onAccept; }
menu_event& OnCancel() { return m_onCancel; }
protected:
bool m_wrapAround;
menu_event m_onSelection;
menu_event m_onAccept;
menu_event m_onCancel;
std::vector<std::shared_ptr<iitem>> m_items;
size_t m_currentItemIndex;
std::shared_ptr<iitem> m_currentItem;
};
For the menu we will use a vector as the container for our items, this will allow us to work
with it using an index as we cycle through menu options. What is important when working
withmenusistoprovideawaytoknowwhenaselectionhaschangedandwhenithasbeen
accepted. Some menus may also be cancelled through an option to close the menu or by
pressing a gamepad button.
The menu input will be handled to support the up and down arrows for navigation of the
menu, enter for accepting a menu item and escape to exit the menu.
bool menu::HandleInput(float deltaTime, const input::input_state& inputState)
{
auto keyboard = inputState.GetKeyboard();
if ( keyboard != nullptr )
{
if ( keyboard->KeyPressed(DIK_UP) )
{
if ( m_wrapAround && (m_currentItemIndex == SIZE_MAX || m_currentItemIndex == 0 ))
m_currentItemIndex = m_items.size() - 1;
else
--m_currentItemIndex;
Search WWH ::




Custom Search