104 lines
2.4 KiB
C++
104 lines
2.4 KiB
C++
/**
|
|
* @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::setParentMenu(Menu* menu) {
|
|
this->parentMenu = menu;
|
|
}
|
|
|
|
void Menu::addEntry(MenuEntry* 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 == 2) {
|
|
this->it = this->entrys.begin();
|
|
}
|
|
}
|
|
|
|
void Menu::printMenu() {
|
|
Serial.println("\n\n");
|
|
for (std::list<MenuEntry*>::iterator iter = this->entrys.begin(); iter != this->entrys.end(); iter++) {
|
|
MenuEntry* selectedEntry = *(iter);
|
|
if (this->it == iter)
|
|
Serial.print(" ");
|
|
Serial.println(selectedEntry->getName());
|
|
}
|
|
this->inSubmenu = false;
|
|
}
|
|
|
|
void Menu::nextEntry() {
|
|
if (this->inSubmenu) {
|
|
MenuEntry* selectedEntry = *(this->it);
|
|
selectedEntry->getMenu()->nextEntry();
|
|
return;
|
|
}
|
|
|
|
// TODO: Why I need the fucking next two lines? here and in the next function
|
|
std::list<MenuEntry*>::iterator iter = this->it;
|
|
iter++;
|
|
if (iter != this->entrys.end())
|
|
this->it++;
|
|
else
|
|
this->it = this->entrys.begin();
|
|
this->printMenu();
|
|
}
|
|
|
|
void Menu::prevEntry() {
|
|
if (this->inSubmenu) {
|
|
MenuEntry* selectedEntry = *(this->it);
|
|
selectedEntry->getMenu()->prevEntry();
|
|
return;
|
|
}
|
|
std::list<MenuEntry*>::iterator iter = this->entrys.end();
|
|
iter--;
|
|
if (this->it != this->entrys.begin()) {
|
|
Serial.println("--");
|
|
this->it--;
|
|
}
|
|
else {
|
|
Serial.println("begin");
|
|
this->it = iter;
|
|
}
|
|
this->printMenu();
|
|
}
|
|
|
|
void Menu::enterEntry() {
|
|
// TODO: find out why i cannot call runAction() directly from the iterator
|
|
MenuEntry* selectedEntry = *(this->it);
|
|
|
|
if (this->inSubmenu) {
|
|
selectedEntry->getMenu()->enterEntry();
|
|
return;
|
|
}
|
|
|
|
if (selectedEntry->getIsMenu()) {
|
|
this->inSubmenu = true;
|
|
selectedEntry->getMenu()->setParentMenu(this);
|
|
}
|
|
selectedEntry->runAction();
|
|
}
|
|
|
|
void Menu::enterParentMenu() {
|
|
if (this->inSubmenu) {
|
|
MenuEntry* selectedEntry = *(this->it);
|
|
selectedEntry->getMenu()->enterParentMenu();
|
|
return;
|
|
}
|
|
|
|
if (parentMenu)
|
|
this->parentMenu->printMenu();
|
|
} |