Browse Source

Add "moduleUsages", "moduleBrowserSort", and "moduleBrowserZoom" to settings. Make module browser's SortButton and ZoomButton control these settings.

tags/v2.0.0
Andrew Belt 3 years ago
parent
commit
e2e33c09c6
3 changed files with 181 additions and 26 deletions
  1. +18
    -2
      include/settings.hpp
  2. +90
    -6
      src/app/ModuleBrowser.cpp
  3. +73
    -18
      src/settings.cpp

+ 18
- 2
include/settings.hpp View File

@@ -51,11 +51,27 @@ extern bool skipLoadOnLaunch;
extern std::string patchPath; extern std::string patchPath;
extern std::list<std::string> recentPatchPaths; extern std::list<std::string> recentPatchPaths;
extern std::vector<NVGcolor> cableColors; extern std::vector<NVGcolor> cableColors;
// pluginSlug -> moduleSlugs
extern std::map<std::string, std::set<std::string>> moduleWhitelist;
extern bool autoCheckUpdates; extern bool autoCheckUpdates;
extern bool showTipsOnLaunch; extern bool showTipsOnLaunch;
extern int tipIndex; extern int tipIndex;
enum ModuleBrowserSort {
MODULE_BROWSER_SORT_UPDATED,
MODULE_BROWSER_SORT_MOST_USED,
MODULE_BROWSER_SORT_LAST_USED,
MODULE_BROWSER_SORT_BRAND,
MODULE_BROWSER_SORT_NAME,
MODULE_BROWSER_SORT_RANDOM,
};
extern ModuleBrowserSort moduleBrowserSort;
extern float moduleBrowserZoom;
// pluginSlug -> moduleSlugs
extern std::map<std::string, std::set<std::string>> moduleWhitelist;

struct ModuleUsage {
int count = 0;
double lastTime = NAN;
};
extern std::map<std::string, std::map<std::string, ModuleUsage>> moduleUsages;


json_t* toJson(); json_t* toJson();
void fromJson(json_t* rootJ); void fromJson(json_t* rootJ);


+ 90
- 6
src/app/ModuleBrowser.cpp View File

@@ -79,6 +79,11 @@ static void modelDbInit() {




static ModuleWidget* chooseModel(plugin::Model* model) { static ModuleWidget* chooseModel(plugin::Model* model) {
// Record usage
settings::ModuleUsage& mu = settings::moduleUsages[model->plugin->slug][model->slug];
mu.count++;
mu.lastTime = system::getUnixTime();

// Create Module and ModuleWidget // Create Module and ModuleWidget
engine::Module* module = model->createModule(); engine::Module* module = model->createModule();
APP->engine->addModule(module); APP->engine->addModule(module);
@@ -363,15 +368,85 @@ struct TagButton : ui::ChoiceButton {
}; };




static const std::string sortNames[] = {
"Last updated",
"Most used",
"Last used",
"Brand",
"Module name",
"Random",
};


struct SortItem : ui::MenuItem {
ModuleBrowser* browser;
settings::ModuleBrowserSort sort;
void onAction(const event::Action& e) override;
void step() override {
rightText = CHECKMARK(settings::moduleBrowserSort == sort);
MenuItem::step();
}
};


struct SortButton : ui::ChoiceButton { struct SortButton : ui::ChoiceButton {
void onAction(const event::Action& e) override {}
void step() override {}
ModuleBrowser* browser;

void onAction(const event::Action& e) override {
ui::Menu* menu = createMenu();
menu->box.pos = getAbsoluteOffset(math::Vec(0, box.size.y));
menu->box.size.x = box.size.x;

for (int sortId = 0; sortId <= settings::MODULE_BROWSER_SORT_RANDOM; sortId++) {
SortItem* sortItem = new SortItem;
sortItem->text = sortNames[sortId];
sortItem->sort = (settings::ModuleBrowserSort) sortId;
sortItem->browser = browser;
menu->addChild(sortItem);
}
}

void step() override {
text = "Sort: ";
text += sortNames[settings::moduleBrowserSort];
ChoiceButton::step();
}
};


struct ZoomItem : ui::MenuItem {
ModuleBrowser* browser;
float zoom;
void onAction(const event::Action& e) override;
void step() override {
rightText = CHECKMARK(settings::moduleBrowserZoom == zoom);
MenuItem::step();
}
}; };




struct ZoomButton : ui::ChoiceButton { struct ZoomButton : ui::ChoiceButton {
void onAction(const event::Action& e) override {}
void step() override {}
ModuleBrowser* browser;

void onAction(const event::Action& e) override {
ui::Menu* menu = createMenu();
menu->box.pos = getAbsoluteOffset(math::Vec(0, box.size.y));
menu->box.size.x = box.size.x;

for (int zoom = 0; zoom >= -3; zoom--) {
ZoomItem* sortItem = new ZoomItem;
sortItem->text = string::f("%g%%", std::pow(2.f, zoom) * 100.f);
sortItem->zoom = zoom;
sortItem->browser = browser;
menu->addChild(sortItem);
}
}

void step() override {
text = "Zoom: ";
text += string::f("%g%%", std::pow(2.f, settings::moduleBrowserZoom) * 100.f);
ChoiceButton::step();
}
}; };




@@ -440,12 +515,12 @@ struct ModuleBrowser : widget::OpaqueWidget {


SortButton* sortButton = new SortButton; SortButton* sortButton = new SortButton;
sortButton->box.size.x = 150; sortButton->box.size.x = 150;
sortButton->text = "Sort: Most used";
sortButton->browser = this;
headerLayout->addChild(sortButton); headerLayout->addChild(sortButton);


ZoomButton* zoomButton = new ZoomButton; ZoomButton* zoomButton = new ZoomButton;
zoomButton->box.size.x = 100; zoomButton->box.size.x = 100;
zoomButton->text = "Zoom: 25%";
zoomButton->browser = this;
headerLayout->addChild(zoomButton); headerLayout->addChild(zoomButton);


UrlButton* libraryButton = new UrlButton; UrlButton* libraryButton = new UrlButton;
@@ -769,6 +844,15 @@ inline void TagButton::step() {
ChoiceButton::step(); ChoiceButton::step();
} }


inline void SortItem::onAction(const event::Action& e) {
settings::moduleBrowserSort = sort;
browser->refresh();
}

inline void ZoomItem::onAction(const event::Action& e) {
settings::moduleBrowserZoom = zoom;
}



} // namespace moduleBrowser } // namespace moduleBrowser




+ 73
- 18
src/settings.cpp View File

@@ -49,10 +49,13 @@ std::vector<NVGcolor> cableColors = {
color::fromHexString("#0f8df4ff"), // blue color::fromHexString("#0f8df4ff"), // blue
color::fromHexString("#8c1889ff"), // purple color::fromHexString("#8c1889ff"), // purple
}; };
std::map<std::string, std::set<std::string>> moduleWhitelist = {};
bool autoCheckUpdates = true; bool autoCheckUpdates = true;
bool showTipsOnLaunch = true; bool showTipsOnLaunch = true;
int tipIndex = -1; int tipIndex = -1;
ModuleBrowserSort moduleBrowserSort = MODULE_BROWSER_SORT_UPDATED;
float moduleBrowserZoom = -2.f;
std::map<std::string, std::set<std::string>> moduleWhitelist = {};
std::map<std::string, std::map<std::string, ModuleUsage>> moduleUsages = {};




json_t* toJson() { json_t* toJson() {
@@ -112,6 +115,16 @@ json_t* toJson() {
} }
json_object_set_new(rootJ, "cableColors", cableColorsJ); json_object_set_new(rootJ, "cableColors", cableColorsJ);


json_object_set_new(rootJ, "autoCheckUpdates", json_boolean(autoCheckUpdates));

json_object_set_new(rootJ, "showTipsOnLaunch", json_boolean(showTipsOnLaunch));

json_object_set_new(rootJ, "tipIndex", json_integer(tipIndex));

json_object_set_new(rootJ, "moduleBrowserSort", json_integer((int) moduleBrowserSort));

json_object_set_new(rootJ, "moduleBrowserZoom", json_real(moduleBrowserZoom));

json_t* moduleWhitelistJ = json_object(); json_t* moduleWhitelistJ = json_object();
for (const auto& pair : moduleWhitelist) { for (const auto& pair : moduleWhitelist) {
json_t* moduleSlugsJ = json_array(); json_t* moduleSlugsJ = json_array();
@@ -122,11 +135,23 @@ json_t* toJson() {
} }
json_object_set_new(rootJ, "moduleWhitelist", moduleWhitelistJ); json_object_set_new(rootJ, "moduleWhitelist", moduleWhitelistJ);


json_object_set_new(rootJ, "autoCheckUpdates", json_boolean(autoCheckUpdates));

json_object_set_new(rootJ, "showTipsOnLaunch", json_boolean(showTipsOnLaunch));

json_object_set_new(rootJ, "tipIndex", json_integer(tipIndex));
json_t* moduleUsagesJ = json_object();
for (const auto& pair : moduleUsages) {
json_t* modulesJ = json_object();
for (const auto& modulePair : pair.second) {
const ModuleUsage& mu = modulePair.second;
if (mu.count <= 0 || !std::isfinite(mu.lastTime))
continue;
json_t* moduleUsagesJ = json_object();
{
json_object_set_new(moduleUsagesJ, "count", json_integer(mu.count));
json_object_set_new(moduleUsagesJ, "lastTime", json_real(mu.lastTime));
}
json_object_set_new(modulesJ, modulePair.first.c_str(), moduleUsagesJ);
}
json_object_set_new(moduleUsagesJ, pair.first.c_str(), modulesJ);
}
json_object_set_new(rootJ, "moduleUsages", moduleUsagesJ);


return rootJ; return rootJ;
} }
@@ -236,6 +261,26 @@ void fromJson(json_t* rootJ) {
} }
} }


json_t* autoCheckUpdatesJ = json_object_get(rootJ, "autoCheckUpdates");
if (autoCheckUpdatesJ)
autoCheckUpdates = json_boolean_value(autoCheckUpdatesJ);

json_t* showTipsOnLaunchJ = json_object_get(rootJ, "showTipsOnLaunch");
if (showTipsOnLaunchJ)
showTipsOnLaunch = json_boolean_value(showTipsOnLaunchJ);

json_t* tipIndexJ = json_object_get(rootJ, "tipIndex");
if (tipIndexJ)
tipIndex = json_integer_value(tipIndexJ);

json_t* moduleBrowserSortJ = json_object_get(rootJ, "moduleBrowserSort");
if (moduleBrowserSortJ)
moduleBrowserSort = (ModuleBrowserSort) json_integer_value(moduleBrowserSortJ);

json_t* moduleBrowserZoomJ = json_object_get(rootJ, "moduleBrowserZoom");
if (moduleBrowserZoomJ)
moduleBrowserZoom = json_number_value(moduleBrowserZoomJ);

moduleWhitelist.clear(); moduleWhitelist.clear();
json_t* moduleWhitelistJ = json_object_get(rootJ, "moduleWhitelist"); json_t* moduleWhitelistJ = json_object_get(rootJ, "moduleWhitelist");
if (moduleWhitelistJ) { if (moduleWhitelistJ) {
@@ -252,17 +297,26 @@ void fromJson(json_t* rootJ) {
} }
} }


json_t* autoCheckUpdatesJ = json_object_get(rootJ, "autoCheckUpdates");
if (autoCheckUpdatesJ)
autoCheckUpdates = json_boolean_value(autoCheckUpdatesJ);

json_t* showTipsOnLaunchJ = json_object_get(rootJ, "showTipsOnLaunch");
if (showTipsOnLaunchJ)
showTipsOnLaunch = json_boolean_value(showTipsOnLaunchJ);

json_t* tipIndexJ = json_object_get(rootJ, "tipIndex");
if (tipIndexJ)
tipIndex = json_integer_value(tipIndexJ);
moduleUsages.clear();
json_t* moduleUsagesJ = json_object_get(rootJ, "moduleUsages");
if (moduleUsagesJ) {
const char* pluginSlug;
json_t* modulesJ;
json_object_foreach(moduleUsagesJ, pluginSlug, modulesJ) {
const char* moduleSlug;
json_t* moduleJ;
json_object_foreach(modulesJ, moduleSlug, moduleJ) {
ModuleUsage mu;
json_t* countJ = json_object_get(moduleJ, "count");
if (countJ)
mu.count = json_integer_value(countJ);
json_t* lastTimeJ = json_object_get(moduleJ, "lastTime");
if (lastTimeJ)
mu.lastTime = json_number_value(lastTimeJ);
moduleUsages[pluginSlug][moduleSlug] = mu;
}
}
}
} }


void save(const std::string& path) { void save(const std::string& path) {
@@ -276,7 +330,8 @@ void save(const std::string& path) {
return; return;
DEFER({std::fclose(file);}); DEFER({std::fclose(file);});


json_dumpf(rootJ, file, JSON_INDENT(2) | JSON_REAL_PRECISION(9));
// Because settings include doubles, it should use 17 decimal digits of precision instead of just 9 for float32.
json_dumpf(rootJ, file, JSON_INDENT(2) | JSON_REAL_PRECISION(17));
json_decref(rootJ); json_decref(rootJ);
} }




Loading…
Cancel
Save