Skip to content

Instantly share code, notes, and snippets.

@FLYBYME
Created November 16, 2017 19:54
Show Gist options
  • Save FLYBYME/08b880409172690833471f4f151d43ae to your computer and use it in GitHub Desktop.
Save FLYBYME/08b880409172690833471f4f151d43ae to your computer and use it in GitHub Desktop.
menu system
#include <iostream>
#include <string>
#include "sub_menu.h"
#include "menu.h"
using namespace std;
MenuHelper menu;
void searchMenuHandler() {
cout << "searchMenuHandler" << endl;
}
int main() {
SubMenu* item = menu.addItem("Search");
item->setHandler(&searchMenuHandler);
char* title = item->getTitle();
cout << title << endl;
item->callHandler();
string y;
getline(cin, y);
return 0;
}
#include "sub_menu.h"
#include "menu.h"
void MenuHelper::reset() {
this->activeItems = 0;
this->selectedItem = 0;
}
SubMenu* MenuHelper::addItem(
char* text
) {
int index = this->activeItems;
this->menuItems[index] = &SubMenu(text);
this->activeItems++;
return this->menuItems[index];
}
void MenuHelper::selectNextItem() {
if (++this->selectedItem >= this->activeItems)
this->selectedItem = 0;
}
void MenuHelper::selectPreviousItem() {
if (--this->selectedItem < 0)
this->selectedItem = this->activeItems - 1;
}
void MenuHelper::activateItem() {
//this->getCurrentItem().callHandler();
}
SubMenu* MenuHelper::getCurrentItem() {
return this->menuItems[this->selectedItem];
}
#pragma once
#ifndef UI_MENU_H
#define UI_MENU_H
#define MENU_ITEMS_MAX 4
class MenuHelper {
public:
MenuHelper() {
}
void reset();
SubMenu* addItem(
char* text
);
SubMenu* getCurrentItem();
void selectNextItem();
void selectPreviousItem();
void activateItem();
private:
SubMenu* menuItems[MENU_ITEMS_MAX];
int activeItems = 0;
int selectedItem = 0;
};
#endif
#include "sub_menu.h"
void SubMenu::setHandler(void(*_handler)()) {
handler = *_handler;
}
void SubMenu::callHandler() {
handler();
}
char* SubMenu::getTitle() {
return title;
}
#ifndef SUB_MENU_H
#define SUB_MENU_H
typedef void(*MenuHandler)();
class SubMenu {
public:
SubMenu(char* t) {
title = t;
}
char* getTitle();
void setHandler(void(*_handler)());
void callHandler();
private:
MenuHandler handler;
char* title;
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment