Implement 75% off menu

This commit is contained in:
2022-01-19 09:13:53 +01:00
parent 4ba7995bb4
commit 241123e6c9
6 changed files with 197 additions and 27 deletions
+71 -9
View File
@@ -10,13 +10,47 @@
*/
#include "menu.h"
Menu::Menu(MenuEntry* firstEntry) {
entrys.push_back(firstEntry);
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->it != this->entrys.end())
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();
@@ -24,19 +58,47 @@ void Menu::nextEntry() {
}
void Menu::prevEntry() {
if (this->it != this->entrys.begin())
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
this->it = this->entrys.end();
}
else {
Serial.println("begin");
this->it = iter;
}
this->printMenu();
}
void Menu::enterEntry() {
// TODO: find out why i cannot call run directly from the iterator
// TODO: find out why i cannot call runAction() directly from the iterator
MenuEntry* selectedEntry = *(this->it);
selectedEntry->run();
if (this->inSubmenu) {
selectedEntry->getMenu()->enterEntry();
return;
}
if (selectedEntry->getIsMenu()) {
this->inSubmenu = true;
selectedEntry->getMenu()->setParentMenu(this);
}
selectedEntry->runAction();
}
void Menu::enterParentMenu() {
this->parentMenu->printMenu();
if (this->inSubmenu) {
MenuEntry* selectedEntry = *(this->it);
selectedEntry->getMenu()->enterParentMenu();
return;
}
if (parentMenu)
this->parentMenu->printMenu();
}
+10 -2
View File
@@ -13,12 +13,20 @@
#include <list>
// Only for print in consol
// have to be deleted after display installation
#include <Arduino.h>
#include "menuEntry.h"
class MenuEntry;
class Menu {
public:
Menu(MenuEntry* firstEntry);
Menu();
void setParentMenu(Menu* menu);
void addEntry(MenuEntry* entry);
void printMenu();
void nextEntry();
@@ -27,7 +35,7 @@ class Menu {
void enterParentMenu();
private:
bool active = false;
bool inSubmenu = false;
Menu* parentMenu;
+19 -2
View File
@@ -19,20 +19,37 @@ MenuEntry::MenuEntry(const char* name, void (*function) (), void (*callback) ())
MenuEntry::MenuEntry(const char* name, void (*function) ()) {
this->name = name;
this->function = function;
this->callback = nullptr;
}
MenuEntry::MenuEntry(const char* name, Menu* menu) {
this->name = name;
this->menu = menu;
this->isMenu = true;
this->callback = nullptr;
}
void MenuEntry::run() {
void MenuEntry::runAction() {
if (isMenu)
{}
this->menu->printMenu();
else
this->function();
if (this->callback)
this->callback();
}
const char* MenuEntry::getName() {
return this->name;
}
bool MenuEntry::getIsMenu() {
return this->isMenu;
}
Menu* MenuEntry::getMenu() {
if (isMenu)
return this->menu;
else
return nullptr;
}
+8 -3
View File
@@ -13,20 +13,25 @@
#include "menu.h"
class Menu;
class MenuEntry {
public:
MenuEntry(const char* name, void (*function) (), void (*callback) ());
MenuEntry(const char* name, void (*function) ());
MenuEntry(const char* name, Menu* menu);
void run();
void runAction();
const char* getName();
bool getIsMenu();
Menu* getMenu();
private:
const char* name;
void (*function) ();
void (*callback) ();
void (*callback) () = nullptr;
bool isMenu;
bool isMenu = false;
Menu *menu;
};