From 54669f98950164bb0fc60512aac19035f4e19f51 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Thu, 3 Jun 2021 20:20:53 -0400 Subject: [PATCH] Add createMenuItem() with action callback and createCheckMenuItem(). --- include/helpers.hpp | 64 +++++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/include/helpers.hpp b/include/helpers.hpp index c4cb2cd3..38558ebe 100644 --- a/include/helpers.hpp +++ b/include/helpers.hpp @@ -161,6 +161,19 @@ TParamWidget* createLightParamCentered(math::Vec pos, engine::Module* module, in } +template +TMenu* createMenu() { + TMenu* o = new TMenu; + o->box.pos = APP->scene->mousePos; + + ui::MenuOverlay* menuOverlay = new ui::MenuOverlay; + menuOverlay->addChild(o); + + APP->scene->addChild(menuOverlay); + return o; +} + + template TMenuLabel* createMenuLabel(std::string text) { TMenuLabel* o = new TMenuLabel; @@ -178,22 +191,41 @@ TMenuItem* createMenuItem(std::string text, std::string rightText = "") { } -template -TMenu* createMenu() { - TMenu* o = new TMenu; - o->box.pos = APP->scene->mousePos; +template +TMenuItem* createMenuItem(std::string text, std::string rightText, std::function action) { + struct Item : TMenuItem { + std::function action; + void onAction(const event::Action& e) override { + action(); + } + }; - ui::MenuOverlay* menuOverlay = new ui::MenuOverlay; - menuOverlay->addChild(o); + Item* item = createMenuItem(text, rightText); + item->action = action; + return item; +} - APP->scene->addChild(menuOverlay); - return o; + +/** Creates a MenuItem with a check mark set by a lambda function. +*/ +inline ui::MenuItem* createCheckMenuItem(std::string text, std::function checked, std::function action) { + struct Item : ui::MenuItem { + std::function action; + + void onAction(const event::Action& e) override { + action(); + } + }; + + Item* item = createMenuItem(text, CHECKMARK(checked())); + item->action = action; + return item; } /** Creates a MenuItem that controls a boolean value with a check mark. */ -inline ui::MenuItem* createBoolMenuItem(std::string name, std::function getter, std::function setter) { +inline ui::MenuItem* createBoolMenuItem(std::string text, std::function getter, std::function setter) { struct Item : ui::MenuItem { std::function setter; bool val; @@ -204,7 +236,7 @@ inline ui::MenuItem* createBoolMenuItem(std::string name, std::function }; bool currVal = getter(); - Item* item = createMenuItem(name, CHECKMARK(currVal)); + Item* item = createMenuItem(text, CHECKMARK(currVal)); item->setter = setter; item->val = !currVal; return item; @@ -214,8 +246,8 @@ inline ui::MenuItem* createBoolMenuItem(std::string name, std::function /** Easy wrapper for createBoolMenuItem() to modify a bool pointer. */ template -ui::MenuItem* createBoolPtrMenuItem(std::string name, T* ptr) { - return createBoolMenuItem(name, +ui::MenuItem* createBoolPtrMenuItem(std::string text, T* ptr) { + return createBoolMenuItem(text, [=]() {return *ptr;}, [=](T val) {*ptr = val;} ); @@ -224,7 +256,7 @@ ui::MenuItem* createBoolPtrMenuItem(std::string name, T* ptr) { /** Creates a MenuItem that when hovered, opens a submenu with several MenuItems indexed by an integer. */ -inline ui::MenuItem* createIndexSubmenuItem(std::string name, std::vector labels, std::function getter, std::function setter) { +inline ui::MenuItem* createIndexSubmenuItem(std::string text, std::vector labels, std::function getter, std::function setter) { struct IndexItem : ui::MenuItem { std::function setter; size_t index; @@ -254,7 +286,7 @@ inline ui::MenuItem* createIndexSubmenuItem(std::string name, std::vector(name, label + " " + RIGHT_ARROW); + Item* item = createMenuItem(text, label + " " + RIGHT_ARROW); item->getter = getter; item->setter = setter; item->labels = labels; @@ -265,8 +297,8 @@ inline ui::MenuItem* createIndexSubmenuItem(std::string name, std::vector -ui::MenuItem* createIndexPtrSubmenuItem(std::string name, std::vector labels, T* ptr) { - return createIndexSubmenuItem(name, labels, +ui::MenuItem* createIndexPtrSubmenuItem(std::string text, std::vector labels, T* ptr) { + return createIndexSubmenuItem(text, labels, [=]() {return *ptr;}, [=](T index) {*ptr = index;} );