diff --git a/src/CardinalModuleWidget.cpp b/src/CardinalModuleWidget.cpp index 730c681..be173ec 100644 --- a/src/CardinalModuleWidget.cpp +++ b/src/CardinalModuleWidget.cpp @@ -27,6 +27,8 @@ #include "CardinalCommon.hpp" +#include + #include #include #include @@ -126,6 +128,43 @@ void CardinalModuleWidget__saveDialog(ModuleWidget* const w) }); } +// Create ModulePresetPathItems for each patch in a directory. +static void appendPresetItems(ui::Menu* menu, WeakPtr moduleWidget, std::string presetDir) { + bool foundPresets = false; + + // Note: This is not cached, so opening this menu each time might have a bit of latency. + if (system::isDirectory(presetDir)) + { + std::vector entries = system::getEntries(presetDir); + std::sort(entries.begin(), entries.end()); + for (std::string path : entries) { + std::string name = system::getStem(path); + // Remove "1_", "42_", "001_", etc at the beginning of preset filenames + std::regex r("^\\d+_"); + name = std::regex_replace(name, r, ""); + + if (system::getExtension(path) == ".vcvm") + { + if (!foundPresets) + menu->addChild(new ui::MenuSeparator); + + foundPresets = true; + + menu->addChild(createMenuItem(name, "", [=]() { + if (!moduleWidget) + return; + try { + moduleWidget->loadAction(path); + } + catch (Exception& e) { + async_dialog_message(e.what()); + } + })); + } + } + } +}; + static void CardinalModuleWidget__createContextMenu(ModuleWidget* const w, plugin::Model* const model, engine::Module* const module) { @@ -164,23 +203,21 @@ static void CardinalModuleWidget__createContextMenu(ModuleWidget* const w, CardinalModuleWidget__loadDialog(weakThis); })); + /* TODO requires setting up user dir menu->addChild(createMenuItem("Save as", "", [weakThis]() { if (!weakThis) return; CardinalModuleWidget__saveDialog(weakThis); })); - /* TODO // Scan `/presets//` for presets. menu->addChild(new ui::MenuSeparator); menu->addChild(createMenuLabel("User presets")); appendPresetItems(menu, weakThis, weakThis->model->getUserPresetDirectory()); + */ // Scan `/presets/` for presets. - menu->addChild(new ui::MenuSeparator); - menu->addChild(createMenuLabel("Factory presets")); appendPresetItems(menu, weakThis, weakThis->model->getFactoryPresetDirectory()); - */ })); // Initialize