added update for menu

This commit is contained in:
2022-02-01 18:25:23 +01:00
parent d356b28ffb
commit 15fd17e058
6 changed files with 139 additions and 45 deletions
+27 -20
View File
@@ -11,14 +11,14 @@
#include "menu.h"
Menu::Menu() {
this->it = this->entrys.begin();
this->selectedEntry = this->entrys.begin();
}
void Menu::addEntry(MenuAction* entry) {
this->entrys.push_back(entry);
if (this->entrys.size() == 2)
this->it = this->entrys.begin();
this->selectedEntry = this->entrys.begin();
}
void Menu::printMenu() {
@@ -26,9 +26,9 @@ void Menu::printMenu() {
lcd->clear();
lcd->setCursor(0, 0);
lcd->print("-> ");
lcd->print((**this->it).getName());
lcd->print((**this->selectedEntry).getName());
lcd->setCursor(0, 1);
std::list<MenuAction*>::iterator iter = this->it;
std::list<MenuAction*>::iterator iter = this->selectedEntry;
iter++;
if (iter != this->entrys.end()) {
lcd->print((**iter).getName());
@@ -37,7 +37,7 @@ void Menu::printMenu() {
} else {
std::cout << std::endl;
for (std::list<MenuAction*>::iterator iter = this->entrys.begin(); iter != this->entrys.end(); iter++) {
if (this->it == iter)
if (this->selectedEntry == iter)
std::cout << " " << (**iter).getName() << std::endl;
else
std::cout << (**iter).getName() << std::endl;
@@ -46,54 +46,61 @@ void Menu::printMenu() {
this->inSubmenu = false;
}
void Menu::update() {
if (this->inSubmenu) {
(**this->selectedEntry).getMenu()->update();
return;
}
}
void Menu::down() {
if (this->inSubmenu) {
(**this->it).getMenu()->down();
(**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->it;
std::list<MenuAction*>::iterator iter = this->selectedEntry;
iter++;
if (iter != this->entrys.end())
this->it++;
this->selectedEntry++;
else
this->it = this->entrys.begin();
this->selectedEntry = this->entrys.begin();
this->printMenu();
}
void Menu::up() {
if (this->inSubmenu) {
(**this->it).getMenu()->up();
(**this->selectedEntry).getMenu()->up();
return;
}
std::list<MenuAction*>::iterator iter = this->entrys.end();
iter--;
if (this->it != this->entrys.begin()) {
this->it--;
if (this->selectedEntry != this->entrys.begin()) {
this->selectedEntry--;
}
else {
this->it = iter;
this->selectedEntry = iter;
}
this->printMenu();
}
void Menu::right() {
if (this->inSubmenu) {
(**this->it).getMenu()->right();
(**this->selectedEntry).getMenu()->right();
return;
}
if ((**this->it).getIsMenu()) {
if ((**this->selectedEntry).getIsMenu()) {
this->inSubmenu = true;
(**this->it).getMenu()->setParentMenu(this);
(**this->selectedEntry).getMenu()->setParentMenu(this);
}
(**this->it).runAction();
(**this->selectedEntry).runAction();
}
void Menu::left() {
if (this->inSubmenu) {
(**this->it).getMenu()->left();
(**this->selectedEntry).getMenu()->left();
return;
}
@@ -103,7 +110,7 @@ void Menu::left() {
void Menu::yes() {
if (this->inSubmenu) {
(**this->it).getMenu()->yes();
(**this->selectedEntry).getMenu()->yes();
return;
}
@@ -112,7 +119,7 @@ void Menu::yes() {
void Menu::no() {
if (this->inSubmenu) {
(**this->it).getMenu()->no();
(**this->selectedEntry).getMenu()->no();
return;
}
+2 -1
View File
@@ -23,6 +23,7 @@ class Menu : public MenuControl {
void addEntry(MenuAction* entry);
void printMenu();
void update();
void down();
void up();
@@ -35,7 +36,7 @@ class Menu : public MenuControl {
bool inSubmenu = false;
std::list<MenuAction*> entrys;
std::list<MenuAction*>::iterator it;
std::list<MenuAction*>::iterator selectedEntry;
};
+4 -2
View File
@@ -26,9 +26,11 @@ class MenuControl {
virtual void yes();
virtual void no();
virtual void update();
virtual void printMenu();
void setParentMenu(MenuControl* menu) {this->parentMenu = menu;}
void setLcd(LiquidCrystal_I2C* lcd) {this->lcd = lcd;}
void setParentMenu(MenuControl* menu) { this->parentMenu = menu; }
void setLcd(LiquidCrystal_I2C* lcd) { this->lcd = lcd; }
protected:
MenuControl* parentMenu = nullptr;