diff --git a/lib/Menu/menu.cpp b/lib/Menu/menu.cpp new file mode 100644 index 0000000..37f7e8a --- /dev/null +++ b/lib/Menu/menu.cpp @@ -0,0 +1,42 @@ +/** + * @file menu.cpp + * @author Alexander Klein (alex@kleiax.de) + * @brief + * @version 0.1 + * @date 2022-01-12 + * + * @copyright Copyright (c) 2022 + * + */ +#include "menu.h" + +Menu::Menu(MenuEntry* firstEntry) { + entrys.push_back(firstEntry); + this->it = this->entrys.begin(); +} + +void Menu::nextEntry() { + if (this->it != this->entrys.end()) + this->it++; + else + this->it = this->entrys.begin(); + this->printMenu(); +} + +void Menu::prevEntry() { + if (this->it != this->entrys.begin()) + this->it--; + else + this->it = this->entrys.end(); + this->printMenu(); +} + +void Menu::enterEntry() { + // TODO: find out why i cannot call run directly from the iterator + MenuEntry* selectedEntry = *(this->it); + selectedEntry->run(); +} + +void Menu::enterParentMenu() { + this->parentMenu->printMenu(); +} \ No newline at end of file diff --git a/lib/Menu/menu.h b/lib/Menu/menu.h new file mode 100644 index 0000000..1830803 --- /dev/null +++ b/lib/Menu/menu.h @@ -0,0 +1,39 @@ +/** + * @file menu.h + * @author Alexander Klein (alex@kleiax.de) + * @brief + * @version 0.1 + * @date 2022-01-12 + * + * @copyright Copyright (c) 2022 + * + */ +#ifndef MENU_H +#define MENU_H + +#include + +#include "menuEntry.h" + +class Menu { + public: + Menu(MenuEntry* firstEntry); + + void printMenu(); + + void nextEntry(); + void prevEntry(); + void enterEntry(); + void enterParentMenu(); + + private: + bool active = false; + + Menu* parentMenu; + + std::list entrys; + std::list::iterator it; + +}; + +#endif // MENU_H \ No newline at end of file diff --git a/lib/Menu/menuEntry.cpp b/lib/Menu/menuEntry.cpp new file mode 100644 index 0000000..708f2c7 --- /dev/null +++ b/lib/Menu/menuEntry.cpp @@ -0,0 +1,38 @@ +/** + * @file menuEntry.cpp + * @author Alexander Klein (alex@kleiax.de) + * @brief + * @version 0.1 + * @date 2022-01-12 + * + * @copyright Copyright (c) 2022 + * + */ +#include "menuEntry.h" + +MenuEntry::MenuEntry(const char* name, void (*function) (), void (*callback) ()) { + this->name = name; + this->function = function; + this->callback = callback; +} + +MenuEntry::MenuEntry(const char* name, void (*function) ()) { + this->name = name; + this->function = function; +} + +MenuEntry::MenuEntry(const char* name, Menu* menu) { + this->name = name; + this->menu = menu; + this->isMenu = true; +} + +void MenuEntry::run() { + if (isMenu) + {} + else + this->function(); + + if (this->callback) + this->callback(); +} \ No newline at end of file diff --git a/lib/Menu/menuEntry.h b/lib/Menu/menuEntry.h new file mode 100644 index 0000000..69f073c --- /dev/null +++ b/lib/Menu/menuEntry.h @@ -0,0 +1,33 @@ +/** + * @file menuEntry.h + * @author Alexander Klein (alex@kleiax.de) + * @brief + * @version 0.1 + * @date 2022-01-12 + * + * @copyright Copyright (c) 2022 + * + */ +#ifndef MENU_ENTRY_H +#define MENU_ENTRY_H + +#include "menu.h" + +class MenuEntry { + public: + MenuEntry(const char* name, void (*function) (), void (*callback) ()); + MenuEntry(const char* name, void (*function) ()); + MenuEntry(const char* name, Menu* menu); + void run(); + + private: + const char* name; + + void (*function) (); + void (*callback) (); + + bool isMenu; + Menu *menu; +}; + +#endif // MENU_ENTRY_H \ No newline at end of file