From 14d9a8ea829a09509a86af9fc80fe9452bae5703 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Wed, 6 Nov 2019 14:31:25 -0500 Subject: [PATCH] Add module whitelist to settings and Module Browser. --- include/settings.hpp | 2 ++ src/app/ModuleBrowser.cpp | 27 +++++++++++++++++++++------ src/settings.cpp | 29 ++++++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/include/settings.hpp b/include/settings.hpp index 7f990e63..2c7017e9 100644 --- a/include/settings.hpp +++ b/include/settings.hpp @@ -38,6 +38,8 @@ extern float autosavePeriod; extern bool skipLoadOnLaunch; extern std::string patchPath; extern std::vector cableColors; +// pluginSlug -> moduleSlugs +extern std::map> moduleWhitelist; json_t* toJson(); void fromJson(json_t* rootJ); diff --git a/src/app/ModuleBrowser.cpp b/src/app/ModuleBrowser.cpp index 9ff34e77..a0b6f0ca 100644 --- a/src/app/ModuleBrowser.cpp +++ b/src/app/ModuleBrowser.cpp @@ -428,16 +428,31 @@ struct ModuleBrowser : widget::OpaqueWidget { modelContainer->spacing = math::Vec(10, 10); modelMargin->addChild(modelContainer); - // Add ModelBoxes for each Model + resetModelContainer(); + + clear(); + } + + void resetModelContainer() { + modelContainer->clearChildren(); + // Iterate plugins for (plugin::Plugin* plugin : plugin::plugins) { + // Get module slugs from module whitelist + const auto& pluginIt = settings::moduleWhitelist.find(plugin->slug); + // Iterate models in plugin for (plugin::Model* model : plugin->models) { - ModelBox* moduleBox = new ModelBox; - moduleBox->setModel(model); - modelContainer->addChild(moduleBox); + // Don't show module if plugin whitelist exists but the module is not in it. + if (pluginIt != settings::moduleWhitelist.end()) { + auto moduleIt = std::find(pluginIt->second.begin(), pluginIt->second.end(), model->slug); + if (moduleIt == pluginIt->second.end()) + continue; + } + // Create ModelBox + ModelBox* modelBox = new ModelBox; + modelBox->setModel(model); + modelContainer->addChild(modelBox); } } - - clear(); } void step() override { diff --git a/src/settings.cpp b/src/settings.cpp index 3739077d..0fad8abe 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -43,6 +43,7 @@ std::vector cableColors = { nvgRGB(0xc9, 0x18, 0x47), // red nvgRGB(0x09, 0x86, 0xad), // blue }; +std::map> moduleWhitelist = {}; json_t* toJson() { @@ -93,6 +94,16 @@ json_t* toJson() { } json_object_set_new(rootJ, "cableColors", cableColorsJ); + json_t* moduleWhitelistJ = json_object(); + for (const auto& pair : moduleWhitelist) { + json_t* moduleSlugsJ = json_array(); + for (const std::string& moduleSlug : pair.second) { + json_array_append_new(moduleSlugsJ, json_string(moduleSlug.c_str())); + } + json_object_set_new(moduleWhitelistJ, pair.first.c_str(), moduleSlugsJ); + } + json_object_set_new(rootJ, "moduleWhitelist", moduleWhitelistJ); + return rootJ; } @@ -171,9 +182,9 @@ void fromJson(json_t* rootJ) { if (patchPathJ) patchPath = json_string_value(patchPathJ); + cableColors.clear(); json_t* cableColorsJ = json_object_get(rootJ, "cableColors"); if (cableColorsJ) { - cableColors.clear(); size_t i; json_t* cableColorJ; json_array_foreach(cableColorsJ, i, cableColorJ) { @@ -181,6 +192,22 @@ void fromJson(json_t* rootJ) { cableColors.push_back(color::fromHexString(colorStr)); } } + + moduleWhitelist.clear(); + json_t* moduleWhitelistJ = json_object_get(rootJ, "moduleWhitelist"); + if (moduleWhitelistJ) { + const char* pluginSlug; + json_t* moduleSlugsJ; + json_object_foreach(moduleWhitelistJ, pluginSlug, moduleSlugsJ) { + auto& moduleSlugs = moduleWhitelist[pluginSlug]; + size_t i; + json_t* moduleSlugJ; + json_array_foreach(moduleSlugsJ, i, moduleSlugJ) { + std::string moduleSlug = json_string_value(moduleSlugJ); + moduleSlugs.push_back(moduleSlug); + } + } + } } void save(const std::string& path) {