Browse Source

Added child menus

tags/v0.4.0
Andrew Belt 7 years ago
parent
commit
401176d50d
4 changed files with 55 additions and 9 deletions
  1. +7
    -1
      include/widgets.hpp
  2. +21
    -8
      src/app/RackWidget.cpp
  3. +18
    -0
      src/widgets/Menu.cpp
  4. +9
    -0
      src/widgets/MenuItem.cpp

+ 7
- 1
include/widgets.hpp View File

@@ -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);
};



+ 21
- 8
src/app/RackWidget.cpp View File

@@ -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);
}
}
}


+ 18
- 0
src/widgets/Menu.cpp View File

@@ -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)


+ 9
- 0
src/widgets/MenuItem.cpp View File

@@ -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<Menu*>(parent);
assert(parentMenu);
parentMenu->setChildMenu(childMenu);
}
}

void MenuItem::onMouseLeave() {


Loading…
Cancel
Save