/** * @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() { this->it = this->entrys.begin(); } void Menu::addEntry(MenuAction* entry) { this->entrys.push_back(entry); // TODO: Same shit here. Why I get the first element after an increment. static uint8_t inc = 0; inc++; if (inc == 1) { this->it = this->entrys.begin(); } } void Menu::printMenu() { std::cout << std::endl; for (std::list::iterator iter = this->entrys.begin(); iter != this->entrys.end(); iter++) { MenuAction* selectedEntry = *(iter); if (this->it == iter) std::cout << " " << selectedEntry->getName() << std::endl; else std::cout << selectedEntry->getName() << std::endl; } this->inSubmenu = false; } void Menu::down() { if (this->inSubmenu) { MenuAction* selectedEntry = *(this->it); selectedEntry->getMenu()->down(); return; } // TODO: Why I need the fucking next two lines? here and in the next function std::list::iterator iter = this->it; iter++; if (iter != this->entrys.end()) this->it++; else this->it = this->entrys.begin(); this->printMenu(); } void Menu::up() { if (this->inSubmenu) { MenuAction* selectedEntry = *(this->it); selectedEntry->getMenu()->up(); return; } std::list::iterator iter = this->entrys.end(); iter--; if (this->it != this->entrys.begin()) { this->it--; } else { this->it = iter; } this->printMenu(); } void Menu::right() { // TODO: find out why i cannot call runAction() directly from the iterator MenuAction* selectedEntry = *(this->it); if (this->inSubmenu) { selectedEntry->getMenu()->right(); return; } if (selectedEntry->getIsMenu()) { this->inSubmenu = true; selectedEntry->getMenu()->setParentMenu(this); } selectedEntry->runAction(); if (selectedEntry->getIsMenu()) selectedEntry->getMenu()->down(); } void Menu::left() { if (this->inSubmenu) { MenuAction* selectedEntry = *(this->it); selectedEntry->getMenu()->left(); return; } if (parentMenu) this->parentMenu->printMenu(); } void Menu::yes() { if (this->inSubmenu) { MenuAction* selectedEntry = *(this->it); selectedEntry->getMenu()->yes(); return; } this->right(); } void Menu::no() { if (this->inSubmenu) { MenuAction* selectedEntry = *(this->it); selectedEntry->getMenu()->no(); return; } this->left(); }