From 38c99ad0a807a1de3ceb2b1307708a74658cc0ea Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Mon, 11 Nov 2019 18:35:08 -0500 Subject: [PATCH] Scan preset directories when preset menu is created. Add user preset directory. --- .gitignore | 2 ++ include/plugin/Model.hpp | 1 - src/app/ModuleWidget.cpp | 42 ++++++++++++++++++++++++++++++---------- src/plugin/Model.cpp | 7 ------- 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 0ca91393..c6d09d69 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,5 @@ /autosave.vcv /settings.json /screenshots +/presets +/Fundamental.zip \ No newline at end of file diff --git a/include/plugin/Model.hpp b/include/plugin/Model.hpp index fac12de5..269477f6 100644 --- a/include/plugin/Model.hpp +++ b/include/plugin/Model.hpp @@ -23,7 +23,6 @@ namespace plugin { struct Model { Plugin* plugin = NULL; - std::vector presetPaths; /** Must be unique. Used for saving patches. Never change this after releasing your module. The model slug must be unique within your plugin, but it doesn't need to be unique among different plugins. diff --git a/src/app/ModuleWidget.cpp b/src/app/ModuleWidget.cpp index d6ba215e..7b0f503a 100644 --- a/src/app/ModuleWidget.cpp +++ b/src/app/ModuleWidget.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -198,19 +199,36 @@ struct ModulePresetItem : ui::MenuItem { saveItem->moduleWidget = moduleWidget; menu->addChild(saveItem); - if (!moduleWidget->model->presetPaths.empty()) { - menu->addChild(new MenuEntry); - menu->addChild(createMenuLabel("Factory presets")); + // Create ModulePresetPathItems for each patch in a directory. + auto createPresetItems = [&](std::string presetDir) { + bool hasPresets = false; + // Note: This is not cached, so opening this menu each time might have a bit of latency. + for (const std::string& presetPath : system::getEntries(presetDir)) { + std::string presetFilename = string::filename(presetPath); + if (string::filenameExtension(presetFilename) != "vcvm") + continue; + hasPresets = true; - for (const std::string& presetPath : moduleWidget->model->presetPaths) { ModulePresetPathItem* presetItem = new ModulePresetPathItem; - std::string presetName = string::filenameBase(string::filename(presetPath)); - presetItem->text = presetName; + presetItem->text = string::filenameBase(presetFilename); presetItem->presetPath = presetPath; presetItem->moduleWidget = moduleWidget; menu->addChild(presetItem); } - } + if (!hasPresets) { + menu->addChild(createMenuLabel("(none)")); + } + }; + + // Scan `/presets//` for presets. + menu->addChild(new MenuEntry); + menu->addChild(createMenuLabel("User presets")); + createPresetItems(asset::user("presets/" + moduleWidget->model->plugin->slug + "/" + moduleWidget->model->slug)); + + // Scan `/presets/` for presets. + menu->addChild(new MenuEntry); + menu->addChild(createMenuLabel("Factory presets")); + createPresetItems(asset::plugin(moduleWidget->model->plugin, "presets/" + moduleWidget->model->slug)); return menu; } @@ -632,15 +650,19 @@ void ModuleWidget::loadDialog() { } void ModuleWidget::saveDialog() { - std::string dir = asset::user("presets"); - system::createDirectory(dir); + std::string presetDir = asset::user("presets"); + system::createDirectory(presetDir); + presetDir += "/" + model->plugin->slug; + system::createDirectory(presetDir); + presetDir += "/" + model->slug; + system::createDirectory(presetDir); osdialog_filters* filters = osdialog_filters_parse(PRESET_FILTERS); DEFER({ osdialog_filters_free(filters); }); - char* path = osdialog_file(OSDIALOG_SAVE, dir.c_str(), "Untitled.vcvm", filters); + char* path = osdialog_file(OSDIALOG_SAVE, presetDir.c_str(), "Untitled.vcvm", filters); if (!path) { // No path selected return; diff --git a/src/plugin/Model.cpp b/src/plugin/Model.cpp index 560774c5..a7684cd0 100644 --- a/src/plugin/Model.cpp +++ b/src/plugin/Model.cpp @@ -41,13 +41,6 @@ void Model::fromJson(json_t* rootJ) { json_t* manualUrlJ = json_object_get(rootJ, "manualUrl"); if (manualUrlJ) manualUrl = json_string_value(manualUrlJ); - - // Preset paths - presetPaths.clear(); - std::string presetDir = asset::plugin(plugin, "presets/" + slug); - for (const std::string& presetPath : system::getEntries(presetDir)) { - presetPaths.push_back(presetPath); - } }