From 401176d50d4b6718d05a7e50d2c87f8c2ae29555 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Tue, 26 Sep 2017 19:40:23 -0400 Subject: [PATCH] Added child menus --- include/widgets.hpp | 8 +++++++- src/app/RackWidget.cpp | 29 +++++++++++++++++++++-------- src/widgets/Menu.cpp | 18 ++++++++++++++++++ src/widgets/MenuItem.cpp | 9 +++++++++ 4 files changed, 55 insertions(+), 9 deletions(-) diff --git a/include/widgets.hpp b/include/widgets.hpp index d6ac8b96..353de939 100644 --- a/include/widgets.hpp +++ b/include/widgets.hpp @@ -261,11 +261,16 @@ struct MenuOverlay : OpaqueWidget { }; struct Menu : OpaqueWidget { + Menu *parentMenu = NULL; + Menu *childMenu = NULL; + Menu() { box.size = Vec(0, 0); } + ~Menu(); // Resizes menu and calls addChild() void pushChild(Widget *child); + void setChildMenu(Menu *menu); void fit(); void step(); void draw(NVGcontext *vg); @@ -291,8 +296,9 @@ struct MenuItem : MenuEntry { float computeMinWidth(NVGcontext *vg); void draw(NVGcontext *vg); + virtual Menu *createChildMenu() {return NULL;} void onMouseEnter(); - void onMouseLeave() ; + void onMouseLeave(); void onDragDrop(Widget *origin); }; diff --git a/src/app/RackWidget.cpp b/src/app/RackWidget.cpp index 1bc0f384..58dc055b 100644 --- a/src/app/RackWidget.cpp +++ b/src/app/RackWidget.cpp @@ -366,6 +366,22 @@ struct AddModuleMenuItem : MenuItem { } }; +struct AddPluginMenuItem : MenuItem { + Plugin *plugin; + Vec modulePos; + Menu *createChildMenu() { + Menu *menu = new Menu(); + for (Model *model : plugin->models) { + AddModuleMenuItem *item = new AddModuleMenuItem(); + item->text = model->name; + item->model = model; + item->modulePos = modulePos; + menu->pushChild(item); + } + return menu; + } +}; + void RackWidget::onMouseDownOpaque(int button) { if (button == 1) { Vec modulePos = gMousePos.minus(getAbsolutePos()); @@ -375,14 +391,11 @@ void RackWidget::onMouseDownOpaque(int button) { menuLabel->text = "Add Module"; menu->pushChild(menuLabel); for (Plugin *plugin : gPlugins) { - for (Model *model : plugin->models) { - AddModuleMenuItem *item = new AddModuleMenuItem(); - item->text = model->name; - item->rightText = model->plugin->name; - item->model = model; - item->modulePos = modulePos; - menu->pushChild(item); - } + AddPluginMenuItem *item = new AddPluginMenuItem(); + item->text = plugin->name; + item->plugin = plugin; + item->modulePos = modulePos; + menu->pushChild(item); } } } diff --git a/src/widgets/Menu.cpp b/src/widgets/Menu.cpp index f4a96955..d895f7ab 100644 --- a/src/widgets/Menu.cpp +++ b/src/widgets/Menu.cpp @@ -3,12 +3,30 @@ namespace rack { +Menu::~Menu() { + setChildMenu(NULL); +} + void Menu::pushChild(Widget *child) { child->box.pos = Vec(0, box.size.y); addChild(child); box.size.y += child->box.size.y; } +void Menu::setChildMenu(Menu *menu) { + if (childMenu) { + if (childMenu->parent) + childMenu->parent->removeChild(childMenu); + delete childMenu; + childMenu = NULL; + } + if (menu) { + childMenu = menu; + assert(parent); + parent->addChild(childMenu); + } +} + void Menu::fit() { // Try to fit into the parent's box if (parent) diff --git a/src/widgets/MenuItem.cpp b/src/widgets/MenuItem.cpp index 92213e68..31fe8d36 100644 --- a/src/widgets/MenuItem.cpp +++ b/src/widgets/MenuItem.cpp @@ -22,6 +22,15 @@ void MenuItem::draw(NVGcontext *vg) { void MenuItem::onMouseEnter() { state = BND_HOVER; + + // Try to create child menu + Menu *childMenu = createChildMenu(); + if (childMenu) { + childMenu->box.pos = parent->box.pos.plus(box.getTopRight()); + Menu *parentMenu = dynamic_cast(parent); + assert(parentMenu); + parentMenu->setChildMenu(childMenu); + } } void MenuItem::onMouseLeave() {