Files
Bachelorarbeit-Rover/lib/Menu/menu.cpp
T
2023-01-08 19:57:42 +01:00

129 lines
2.7 KiB
C++

/**
* @file menu.cpp
* @author Alexander Klein (alex@kleiax.de)
* @brief Implementation of the class Menu
* @version 0.1
* @date 2022-01-12
*
* @copyright Copyright (c) 2022
*
*/
#include "menu.h"
Menu::Menu() {
this->selectedEntry = this->entrys.begin();
}
Menu::~Menu() {
for (std::list<MenuAction*>::iterator iter = this->entrys.begin(); iter != this->entrys.end(); iter++)
delete *iter;
}
void Menu::addEntry(MenuAction* entry) {
this->entrys.push_back(entry);
if (this->entrys.size() == 2)
this->selectedEntry = this->entrys.begin();
}
bool Menu::isInSubmenu() {
return this->inSubmenu;
}
void Menu::printMenu() {
std::list<MenuAction*>::iterator iter = this->selectedEntry;
String lineOne = "-> ";
String lineTwo = "";
lineOne.concat((**iter).getName());
iter++;
if (iter != this->entrys.end()) {
lineTwo.concat((**iter).getName());
} else
lineTwo.concat((*this->entrys.begin())->getName());
this->inSubmenu = false;
this->print(lineOne, lineTwo);
}
void Menu::update() {
if (this->inSubmenu) {
(**this->selectedEntry).getMenu()->update();
return;
}
}
void Menu::down() {
if (this->inSubmenu) {
(**this->selectedEntry).getMenu()->down();
return;
}
// TODO: Why I need the fucking next two lines? here and in the next function
std::list<MenuAction*>::iterator iter = this->selectedEntry;
iter++;
if (iter != this->entrys.end())
this->selectedEntry++;
else
this->selectedEntry = this->entrys.begin();
this->printMenu();
}
void Menu::up() {
if (this->inSubmenu) {
(**this->selectedEntry).getMenu()->up();
return;
}
std::list<MenuAction*>::iterator iter = this->entrys.end();
iter--;
if (this->selectedEntry != this->entrys.begin()) {
this->selectedEntry--;
}
else {
this->selectedEntry = iter;
}
this->printMenu();
}
void Menu::right() {
if (this->inSubmenu) {
(**this->selectedEntry).getMenu()->right();
return;
}
if ((**this->selectedEntry).getIsMenu()) {
this->inSubmenu = true;
(**this->selectedEntry).getMenu()->setParentMenu(this);
}
(**this->selectedEntry).runAction();
}
void Menu::left() {
if (this->inSubmenu) {
(**this->selectedEntry).getMenu()->left();
return;
}
if (parentMenu)
this->parentMenu->printMenu();
}
void Menu::yes() {
if (this->inSubmenu) {
(**this->selectedEntry).getMenu()->yes();
return;
}
this->right();
}
void Menu::no() {
if (this->inSubmenu) {
(**this->selectedEntry).getMenu()->no();
return;
}
this->left();
}