/** * @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); if (this->entrys.size() == 2) this->it = this->entrys.begin(); } void Menu::printMenu() { if (this->lcd) { lcd->clear(); lcd->setCursor(0, 0); lcd->print("-> "); lcd->print((**this->it).getName()); lcd->setCursor(0, 1); std::list::iterator iter = this->it; iter++; if (iter != this->entrys.end()) { lcd->print((**iter).getName()); } else lcd->print((*this->entrys.begin())->getName()); } else { std::cout << std::endl; for (std::list::iterator iter = this->entrys.begin(); iter != this->entrys.end(); iter++) { if (this->it == iter) std::cout << " " << (**iter).getName() << std::endl; else std::cout << (**iter).getName() << std::endl; } } this->inSubmenu = false; } void Menu::down() { if (this->inSubmenu) { (**this->it).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) { (**this->it).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() { if (this->inSubmenu) { (**this->it).getMenu()->right(); return; } if ((**this->it).getIsMenu()) { this->inSubmenu = true; (**this->it).getMenu()->setParentMenu(this); } (**this->it).runAction(); } void Menu::left() { if (this->inSubmenu) { (**this->it).getMenu()->left(); return; } if (parentMenu) this->parentMenu->printMenu(); } void Menu::yes() { if (this->inSubmenu) { (**this->it).getMenu()->yes(); return; } this->right(); } void Menu::no() { if (this->inSubmenu) { (**this->it).getMenu()->no(); return; } this->left(); }