Skip to content

Commit

Permalink
go to category
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Dempsey committed Sep 18, 2023
1 parent 1394915 commit 21fa411
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 8 deletions.
26 changes: 24 additions & 2 deletions src/HC-1/HC-1-menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ void Hc1ModuleWidget::addSortBy(Menu *menu, std::string name, PresetOrder order)
menu->addChild(createCheckMenuItem(name, "",
[=](){ return my_module->preset_order == order; },
[=](){
my_module->preset_order = order;
std::sort(my_module->system_presets.begin(), my_module->system_presets.end(), getPresetSort(my_module->preset_order));
my_module->setPresetOrder(order);
if (PresetTab::System == tab) {
populatePresetWidgets();
showCurrentPreset(false);
Expand All @@ -26,6 +25,13 @@ void Hc1ModuleWidget::addSortBy(Menu *menu, std::string name, PresetOrder order)
));
}

void Hc1ModuleWidget::addJumpCategory(Menu *menu, uint16_t category)
{
auto title = format_string("%s (%s)", hcCategoryCode.categoryName(category).c_str(), CategoryCode(category).to_string().c_str());
menu->addChild(createMenuItem(title, "",
[=](){ toCategory(category); }));
}

void Hc1ModuleWidget::addRecirculator(Menu *menu, EM_Recirculator kind)
{
menu->addChild(createCheckMenuItem(RecirculatorName(kind), "",
Expand Down Expand Up @@ -122,6 +128,22 @@ void Hc1ModuleWidget::appendContextMenu(Menu *menu)
menu->addChild(createMenuItem("Refresh User presets", "", [=](){ my_module->transmitRequestUserPresets(); }));
}));

menu->addChild(createSubmenuItem("Go to category", "", [=](Menu* menu) {
addJumpCategory(menu, ST);
addJumpCategory(menu, WI);
addJumpCategory(menu, VO);
addJumpCategory(menu, KY);
addJumpCategory(menu, CL);
addJumpCategory(menu, OT);
addJumpCategory(menu, PE);
addJumpCategory(menu, PT);
addJumpCategory(menu, PR);
addJumpCategory(menu, DO);
addJumpCategory(menu, MD);
addJumpCategory(menu, CV);
addJumpCategory(menu, UT);
}));

menu->addChild(createSubmenuItem("Sort System presets", "", [=](Menu* menu) {
addSortBy(menu, "Alphabetically", PresetOrder::Alpha);
addSortBy(menu, "by Category", PresetOrder::Category);
Expand Down
10 changes: 10 additions & 0 deletions src/HC-1/HC-1-presets.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
#include "HC-1.hpp"
namespace pachde {


void Hc1Module::setPresetOrder(PresetOrder order)
{
if (order != preset_order)
{
preset_order = order;
std::sort(system_presets.begin(), system_presets.end(), getPresetSort(preset_order));
}
}

std::string Hc1Module::userPresetsPath()
{
if (deviceName().empty()) return "";
Expand Down
24 changes: 21 additions & 3 deletions src/HC-1/HC-1-ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,18 +387,36 @@ void Hc1ModuleWidget::onPresetChanged(const PresetChangedEvent& e)
showCurrentPreset(true);
}

void Hc1ModuleWidget::toCategory(uint16_t code)
{
if (!my_module) return;
my_module->setPresetOrder(PresetOrder::Category);
setTab(PresetTab::System);
if (my_module->current_preset && code == my_module->current_preset->primaryCategory()) {
return;
}
auto mp = my_module->getPresets(tab);
auto index = std::distance(mp.cbegin(), std::find_if(mp.cbegin(), mp.cend(), [=](std::shared_ptr<Preset> p){ return code == p->primaryCategory(); }));
auto size = static_cast<ptrdiff_t>(mp.size());
if (index == size) {
return; // not found
}
my_module->setPreset(mp[index]);
}

void Hc1ModuleWidget::toRelativePreset(int delta)
{
if (!my_module) return;
auto mp = my_module->getPresets(tab);
auto index = std::distance(mp.cbegin(), std::find_if(mp.cbegin(), mp.cend(), [=](std::shared_ptr<Preset> p){ return isCurrentPreset(p); }));
if (index == mp.size()) {
auto size = static_cast<ptrdiff_t>(mp.size());
if (index == size) {
return; // not found
}
index = (index + delta);
if (index < 0) {
index = mp.size() - 1;
} else if (index >= mp.size()) {
index = size - 1;
} else if (index >= size) {
index = 0; // wrap to start (clipped)
}
my_module->setPreset(mp[index]);
Expand Down
3 changes: 3 additions & 0 deletions src/HC-1/HC-1.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ struct Hc1Module : IPresetHolder, ISendMidi, midi::Input, Module
PresetFilter preset_filter;
#endif
PresetOrder preset_order = PresetOrder::Alpha;
void setPresetOrder(PresetOrder order);

std::string favoritesPath();
void clearFavorites();
Expand Down Expand Up @@ -444,6 +445,7 @@ struct Hc1ModuleWidget : ModuleWidget, IPresetHolder, IHandleHcEvents
void pageUp();
void pageDown();
void toRelativePreset(int delta);
void toCategory(uint16_t code);
void clearPresetWidgets();
void populatePresetWidgets();
void updatePresetWidgets();
Expand Down Expand Up @@ -486,6 +488,7 @@ struct Hc1ModuleWidget : ModuleWidget, IPresetHolder, IHandleHcEvents

// HC-1-menu.cpp
void addSortBy(Menu *menu, std::string name, PresetOrder order);
void addJumpCategory(Menu *menu, uint16_t category);
void addRecirculator(Menu *menu, EM_Recirculator kind);
void appendContextMenu(Menu *menu) override;
};
Expand Down
7 changes: 6 additions & 1 deletion src/preset_meta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,14 @@ HCCategoryCode::HCCategoryCode()
std::sort(data.begin(), data.end(), [](const std::shared_ptr<PresetMeta>& a, const std::shared_ptr<PresetMeta>& b) { return a->code < b->code; });
}

std::string HCCategoryCode::categoryName(uint16_t key) const
{
auto cat = find(key);
return cat ? cat->name : "Unknown";
}

std::shared_ptr<PresetMeta> HCCategoryCode::find(uint16_t key) const
{
//auto key = make_cat_code(code_text);
auto item = std::lower_bound(data.cbegin(), data.cend(), key, [](const std::shared_ptr<PresetMeta> &p, uint16_t key){ return p->code < key; });
return data.cend() != item ? *item : nullptr;
}
Expand Down
1 change: 1 addition & 0 deletions src/preset_meta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class HCCategoryCode
std::vector<std::shared_ptr<PresetMeta>> make_category_list(const std::string& text) const;
std::string make_category_json(const std::string& text) const;
std::string make_category_mulitline_text(const std::string& text) const;
std::string categoryName(uint16_t key) const;
};

void FillCategoryCodeList(const std::string& text, std::vector<uint16_t>& vec);
Expand Down
3 changes: 1 addition & 2 deletions src/preset_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ struct PresetWidget : TipWidget
symbol->setSymbol((0 == preset->bank_hi) ? 1 : 0);
symbol->box.pos.x = preset->favorite ? 12.f : 4.f;
text_label->text(preset->name);
auto code = *preset->get_category_list().cbegin();
text_code->text(CategoryCode(code).to_string());
text_code->text(preset->categoryName());
} else {
tip_text = "(no preset)";
symbol->setSymbol(0);
Expand Down
11 changes: 11 additions & 0 deletions src/presets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ std::string Preset::describe(bool multi_line /*= true*/)
line_break, m.empty() ? "-" : m.c_str() );
}

uint16_t Preset::primaryCategory()
{
ensure_category_list();
if (categories.empty()) return OT;
return *categories.cbegin();
}

std::string Preset::categoryName()
{
return CategoryCode(primaryCategory()).to_string();
}

void Preset::nada()
{
Expand Down
2 changes: 2 additions & 0 deletions src/presets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ struct Preset

std::string meta(); // cached
std::string make_friendly_text(); // not cached
std::string categoryName();
uint16_t primaryCategory();
void clear();
std::string describe_short();
std::string describe(bool multi_line = true);
Expand Down

0 comments on commit 21fa411

Please sign in to comment.