Add Expander from Menu

I would like to create a contextual menu option that automatically adds an expander for my module. Does anyone know of an example of something that does this. I have the whattherack code which shows me how to add a module but when I try and place it to the right of the current module, it doesn’t push existing modules to the side.

void appendContextMenu(Menu* menu) override {
	//auto module = dynamic_cast<ShiftyMod*>(this->module);

	menu->addChild(new MenuEntry); //Blank Row
	menu->addChild(createMenuLabel("Shifty"));
	
	menu->addChild(createMenuItem("Add Expander", "", 
		[=]{
			addExpander();
		}
	));
}

void addExpander(){
	Vec myPos = this->box.pos;
	Vec expanderPos = myPos.plus(Vec(this->box.size.x-RACK_GRID_WIDTH,0));

	Model* model = pluginInstance->getModel("Expander");
	Module* module = model->createModule();
	ModuleWidget *moduleWidget = model->createModuleWidget(module);
 	APP->scene->rack->setModulePosForce(moduleWidget, expanderPos);
 	APP->scene->rack->addModule(moduleWidget);

  	history::ModuleAdd *h = new history::ModuleAdd;
  	h->name = "create module";
  	h->setModule(moduleWidget);
  	APP->history->push(h);
}

Before:

After:

Substation does this! Check out ModuleInstantionMenuItem in this file dep/slime4rack/include/slime/ui/UI.hpp · main · Slime Child Audio / Substation for VCV Rack - Open Source · GitLab and a usage example near the very bottom of src/Filter.cpp · main · Slime Child Audio / Substation for VCV Rack - Open Source · GitLab

Hope this is helpful!

1 Like