/******************************************************************************
Online C++ Debugger.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Debug" button to debug it.
*******************************************************************************/
#include <iostream>
#include <string>
using namespace std;
typedef void (*function) (int kk) ;
class MenuItem
{
public:
static const int MAX_ITEMS = 4;
//constructor needles
string name = "";
MenuItem () {}
MenuItem (string title)
{
name = title;
}
//parent
MenuItem *parentItem = NULL;
//item handler section
void setHandler (function handlerFunction)
{
handler = handlerFunction;
};
function handler;
//sub items
bool active = false;
MenuItem *subItems[MenuItem::MAX_ITEMS] = {NULL, NULL, NULL, NULL};
bool addSubItem(MenuItem *item)
{
for (int k = 0; k < MenuItem::MAX_ITEMS; k++) {
if (!subItems[k]) {
item->parentItem = NULL;
item->parentItem = this;
subItems[k] = item;
return true;
}
}
return false;
}
};
class Display
{
public:
static const int ACTION_ENTER = 0;
static const int ACTION_BACK = 1;
static const int ACTION_INCREASE = 2;
static const int ACTION_DECREASE = 3;
MenuItem *currentItem;
void show(MenuItem item)
{
currentItem = NULL;
currentItem = &item;
cout<<currentItem->name;
cout<<"\n\n";
bool hasActiveItems = false;
for (int k = 0; k < MenuItem::MAX_ITEMS; k++) {
if (currentItem->subItems[k]) {
!hasActiveItems && (currentItem->subItems[k]->active = true);
hasActiveItems = true;
cout<< (currentItem->subItems[k]->active ? "> " : " ") + currentItem->subItems[k]->name;
cout<<"\n\n";
}
}
}
void processAction(int action)
{
int res = 1;
switch (action) {
case (Display::ACTION_BACK):
if (¤tItem && ¤tItem->parentItem != NULL) {
this->show(
*currentItem->parentItem
);
return;
}
break;
case (Display::ACTION_ENTER):
for (int k = 0; k < MenuItem::MAX_ITEMS; k++) {
if (currentItem->subItems[k] && currentItem->subItems[k]->active) {
this->show(
*currentItem->subItems[k]
);
return;
}
}
break;
case (Display::ACTION_INCREASE):
res = 3;
break;
case (Display::ACTION_DECREASE):
res = 3;
break;
}
}
};
MenuItem menuItem("menu item 1");
MenuItem firtsItem("a 11");
int main()
{
firtsItem.addSubItem(new MenuItem("level 2 - 1"));
firtsItem.addSubItem(new MenuItem("level 2 - 2"));
menuItem.addSubItem(&firtsItem);
menuItem.addSubItem(new MenuItem("b 222"));
menuItem.addSubItem(new MenuItem("c 333"));
menuItem.addSubItem(new MenuItem("d 444"));
Display display;
display.show(menuItem);
display.processAction(Display::ACTION_ENTER);
display.processAction(Display::ACTION_BACK);
display.processAction(Display::ACTION_ENTER);
return 0;
}