From e197b45a35ce3816f1ebd9c9ca26917420251f24 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Sat, 3 Mar 2018 18:15:34 -0500 Subject: [PATCH] Serialize ModuleBrowser favorites --- include/app.hpp | 2 +- include/plugin.hpp | 2 ++ src/app/ModuleBrowser.cpp | 34 ++++++++++++++++++++++++++++++---- src/app/RackWidget.cpp | 24 +----------------------- src/plugin.cpp | 23 +++++++++++++++++++++++ src/ui/ScrollWidget.cpp | 2 +- 6 files changed, 58 insertions(+), 29 deletions(-) diff --git a/include/app.hpp b/include/app.hpp index 859723ee..48e3ab7c 100644 --- a/include/app.hpp +++ b/include/app.hpp @@ -524,7 +524,7 @@ void appInit(); void appDestroy(); void appModuleBrowserCreate(); json_t *appModuleBrowserToJson(); -void appModuleBrowserFromJson(json_t *root); +void appModuleBrowserFromJson(json_t *rootJ); json_t *colorToJson(NVGcolor color); diff --git a/include/plugin.hpp b/include/plugin.hpp index 54be270c..3b8a6f0e 100644 --- a/include/plugin.hpp +++ b/include/plugin.hpp @@ -105,6 +105,8 @@ bool pluginIsDownloading(); float pluginGetDownloadProgress(); std::string pluginGetDownloadName(); std::string pluginGetLoginStatus(); +Plugin *pluginGetPlugin(std::string pluginSlug); +Model *pluginGetModel(std::string pluginSlug, std::string modelSlug); extern std::list gPlugins; diff --git a/src/app/ModuleBrowser.cpp b/src/app/ModuleBrowser.cpp index ddcdfb89..b6d0b57d 100644 --- a/src/app/ModuleBrowser.cpp +++ b/src/app/ModuleBrowser.cpp @@ -377,12 +377,38 @@ void appModuleBrowserCreate() { } json_t *appModuleBrowserToJson() { - // TODO - return json_object(); + json_t *rootJ = json_object(); + + json_t *favoritesJ = json_array(); + for (Model *model : sFavoriteModels) { + json_t *modelJ = json_object(); + json_object_set_new(modelJ, "plugin", json_string(model->plugin->slug.c_str())); + json_object_set_new(modelJ, "model", json_string(model->slug.c_str())); + json_array_append_new(favoritesJ, modelJ); + } + json_object_set_new(rootJ, "favorites", favoritesJ); + + return rootJ; } -void appModuleBrowserFromJson(json_t *root) { - // TODO +void appModuleBrowserFromJson(json_t *rootJ) { + json_t *favoritesJ = json_object_get(rootJ, "favorites"); + if (favoritesJ) { + size_t i; + json_t *favoriteJ; + json_array_foreach(favoritesJ, i, favoriteJ) { + json_t *pluginJ = json_object_get(favoriteJ, "plugin"); + json_t *modelJ = json_object_get(favoriteJ, "model"); + if (!pluginJ || !modelJ) + continue; + std::string pluginSlug = json_string_value(pluginJ); + std::string modelSlug = json_string_value(modelJ); + Model *model = pluginGetModel(pluginSlug, modelSlug); + if (!model) + continue; + sFavoriteModels.insert(model); + } + } } diff --git a/src/app/RackWidget.cpp b/src/app/RackWidget.cpp index c5877142..92fc9707 100644 --- a/src/app/RackWidget.cpp +++ b/src/app/RackWidget.cpp @@ -248,29 +248,7 @@ void RackWidget::fromJson(json_t *rootJ) { std::string pluginSlug = json_string_value(pluginSlugJ); std::string modelSlug = json_string_value(modelSlugJ); - // Search for plugin - Plugin *plugin = NULL; - for (Plugin *p : gPlugins) { - if (p->slug == pluginSlug) { - plugin = p; - break; - } - } - - if (!plugin) { - message += stringf("Could not find plugin \"%s\" for module \"%s\"\n", pluginSlug.c_str(), modelSlug.c_str()); - continue; - } - - // Search for model - Model *model = NULL; - for (Model *m : plugin->models) { - if (m->slug == modelSlug) { - model = m; - break; - } - } - + Model *model = pluginGetModel(pluginSlug, modelSlug); if (!model) { message += stringf("Could not find module \"%s\" in plugin \"%s\"\n", modelSlug.c_str(), pluginSlug.c_str()); continue; diff --git a/src/plugin.cpp b/src/plugin.cpp index a0435f7d..789892bc 100644 --- a/src/plugin.cpp +++ b/src/plugin.cpp @@ -475,5 +475,28 @@ std::string pluginGetLoginStatus() { return loginStatus; } +Plugin *pluginGetPlugin(std::string pluginSlug) { + for (Plugin *plugin : gPlugins) { + if (plugin->slug == pluginSlug) { + return plugin; + } + } + return NULL; +} + +Model *pluginGetModel(std::string pluginSlug, std::string modelSlug) { + Plugin *plugin = pluginGetPlugin(pluginSlug); + if (plugin) { + for (Model *model : plugin->models) { + if (model->slug == modelSlug) { + return model; + } + } + } + return NULL; +} + + + } // namespace rack diff --git a/src/ui/ScrollWidget.cpp b/src/ui/ScrollWidget.cpp index bd409a74..f54bcced 100644 --- a/src/ui/ScrollWidget.cpp +++ b/src/ui/ScrollWidget.cpp @@ -5,7 +5,7 @@ namespace rack { -static const float SCROLL_SPEED = 1.2; +static const float SCROLL_SPEED = 1.3; /** Parent must be a ScrollWidget */