84 lines
1.8 KiB
C++
84 lines
1.8 KiB
C++
#include "samplemenu.h"
|
|
|
|
// Static
|
|
Menu* Menu::m_currentMenu = 0;
|
|
|
|
#if CIN_MENU == 1
|
|
static void cinCallBack(void*)
|
|
{
|
|
char choice[32] = "";
|
|
cin >> choice;
|
|
|
|
Gear::U32 iChoice = 0;
|
|
Gear::Str::FromString(choice, iChoice);
|
|
Menu::SetCurrentMenu(Menu::GetCurrentMenu()->GetChildByIndex(iChoice-1));
|
|
}
|
|
#endif
|
|
|
|
// MENU IMPLEMENTATION
|
|
|
|
// Construction/Destruction
|
|
Menu::Menu() : m_parent(0), m_no(-1), m_name(""), m_cb(0), m_ReturnToParent(Gear::False), m_Shown(Gear::False)
|
|
{
|
|
|
|
}
|
|
|
|
Menu::Menu(Menu* parent, Gear::S32 no, Gear::GearString name, doSomething cb, Gear::Bool ReturnToParent) :
|
|
m_parent(parent), m_no(no), m_name(name), m_cb(cb), m_ReturnToParent(ReturnToParent), m_Shown(Gear::False)
|
|
{
|
|
|
|
}
|
|
|
|
Menu::~Menu()
|
|
{
|
|
for (size_t i = 0 ; i < m_menu.Size() ; i++)
|
|
{
|
|
GEAR_FAST_DELETE(m_menu[i], Gear::DefaultAllocator());
|
|
}
|
|
|
|
m_menu.Clear();
|
|
}
|
|
|
|
// Method
|
|
void Menu::AddMenu(Menu* menu)
|
|
{
|
|
m_menu.PushBack(menu);
|
|
#if CIN_MENU == 1
|
|
m_cb = &cinCallBack;
|
|
#endif
|
|
|
|
}
|
|
|
|
void Menu::Execute(void* param)
|
|
{
|
|
if (m_menu.Size() > 0) // menu
|
|
{
|
|
if (!m_Shown)
|
|
{
|
|
Gear::ConsoleOut::Print("%s\n",m_name.CStr());
|
|
for (size_t i = 0 ; i < m_menu.Size() ; i++)
|
|
{
|
|
Gear::ConsoleOut::Print("\t%d: %s\n", m_menu[i]->m_no, m_menu[i]->m_name.CStr());
|
|
}
|
|
|
|
Gear::ConsoleOut::Print("\nMake a choice\n");
|
|
|
|
// Don't show the same menu twice
|
|
m_Shown = Gear::True;
|
|
}
|
|
}
|
|
// Call the cb
|
|
if (m_cb)
|
|
m_cb(param);
|
|
|
|
if (m_ReturnToParent)
|
|
Return();
|
|
}
|
|
|
|
void Menu::Return()
|
|
{
|
|
if (m_currentMenu && m_currentMenu->GetParent())
|
|
SetCurrentMenu(m_currentMenu->GetParent());
|
|
else
|
|
SetCurrentMenu(m_currentMenu);
|
|
}
|