From c723200a27571f9790d91a428d2314da0da8db5e Mon Sep 17 00:00:00 2001 From: Alexander Klein Date: Wed, 4 Jan 2023 00:14:03 +0100 Subject: [PATCH] added MenuActionWrapper to call callbacks /classes --- lib/Menu/menuAction.cpp | 19 ++++++++++++++++--- lib/Menu/menuAction.h | 15 +++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/Menu/menuAction.cpp b/lib/Menu/menuAction.cpp index 981d398..f4bda6a 100644 --- a/lib/Menu/menuAction.cpp +++ b/lib/Menu/menuAction.cpp @@ -20,19 +20,32 @@ MenuAction::MenuAction(const char* name, MenuControl* menu) { this->name = name; this->menu = menu; this->isMenu = true; - this->callback = nullptr; +} + +MenuAction::MenuAction(const char* name, MenuActionWrapper* action) { + this->name = name; + this->action = action; } MenuAction::~MenuAction() { if (this->menu) delete this->menu; + + if (this->action) + delete this->action; } void MenuAction::runAction() { if (isMenu) this->menu->printMenu(); - else - this->function(); + else { + if (this->function) + this->function(); + else if (this->action) + this->action->action(); + else + std::cout << "Error in MenuAction::runAction" << std::endl; + } if (this->callback) this->callback(); diff --git a/lib/Menu/menuAction.h b/lib/Menu/menuAction.h index be80aca..7698a2e 100644 --- a/lib/Menu/menuAction.h +++ b/lib/Menu/menuAction.h @@ -15,6 +15,12 @@ class MenuControl; +class MenuActionWrapper { + public: + virtual ~MenuActionWrapper() {} + virtual void action() = 0; +}; + /** * @brief This class forms the transition between menus. * @@ -33,6 +39,14 @@ class MenuAction { */ MenuAction(const char* name, void (*function) (), void (*callback) () = nullptr); + /** + * @brief Construct a new Menu Action object + * + * @param name shown in the Menu + * @param action to call when activate the entry + */ + MenuAction(const char* name, MenuActionWrapper* action); + /** * @brief Construct a new Menu Action object * @@ -83,6 +97,7 @@ class MenuAction { bool isMenu = false; MenuControl* menu = nullptr; + MenuActionWrapper* action = nullptr; }; #endif // MENU_ENTRY_H