Skip to content

Commit

Permalink
Redo LFO preset menu in JUCE (#4599)
Browse files Browse the repository at this point in the history
* Redo LFO presets menu in JUCE
* Use switch-case for what std::string

Co-authored-by: Paul Walker <[email protected]>
  • Loading branch information
mkruselj and baconpaul authored May 24, 2021
1 parent fb967fe commit 9b10d49
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 57 deletions.
127 changes: 71 additions & 56 deletions src/gui/SurgeGUIEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5123,10 +5123,7 @@ void SurgeGUIEditor::showLfoMenu(VSTGUI::CPoint &where)
menuRect.offset(where.x, where.y);
auto m = makeLfoMenu(menuRect);

frame->addView(m);
m->invalid();
m->popup();
frame->removeView(m, true);
m.showMenuAsync(juce::PopupMenu::Options());
}

void SurgeGUIEditor::toggleTuning()
Expand Down Expand Up @@ -5327,69 +5324,87 @@ void SurgeGUIEditor::showSettingsMenu(CRect &menuRect)
settingsMenu.showMenuAsync(juce::PopupMenu::Options());
}

VSTGUI::COptionMenu *SurgeGUIEditor::makeLfoMenu(VSTGUI::CRect &menuRect)
juce::PopupMenu SurgeGUIEditor::makeLfoMenu(VSTGUI::CRect &menuRect)
{
int currentLfoId = modsource_editor[current_scene] - ms_lfo1;

int shapev = synth->storage.getPatch().scene[current_scene].lfo[currentLfoId].shape.val.i;
std::string what = "LFO";
if (lt_mseg == shapev)
std::string what;

switch (shapev)
{
case lt_mseg:
what = "MSEG";
if (lt_stepseq == shapev)
break;
case lt_stepseq:
what = "Step Seq";
if (lt_envelope == shapev)
break;
case lt_envelope:
what = "Envelope";
if (lt_formula == shapev)
break;
case lt_formula:
what = "Formula";
break;
default:
what = "LFO";
break;
}

auto msurl = SurgeGUIEditor::helpURLForSpecial("lfo-presets");
auto hurl = SurgeGUIEditor::fullyResolvedHelpURL(msurl);

COptionMenu *lfoSubMenu =
new COptionMenu(menuRect, 0, 0, 0, 0, VSTGUI::COptionMenu::kNoDrawStyle);
addCallbackMenu(lfoSubMenu, "[?] LFO Presets",
[hurl]() { juce::URL(hurl).launchInDefaultBrowser(); }),

lfoSubMenu->addSeparator();

addCallbackMenu(lfoSubMenu, Surge::GUI::toOSCaseForMenu("Save " + what + " As..."),
[this, currentLfoId, what]() {
// Prompt for a name
promptForMiniEdit("preset", "Enter the name for " + what + " preset:",
what + " Preset Name", CPoint(-1, -1),
[this, currentLfoId](const std::string &s) {
Surge::ModulatorPreset::savePresetToUser(
string_to_path(s), &(this->synth->storage),
current_scene, currentLfoId);
});
// and save
});
auto lfoSubMenu = juce::PopupMenu();

lfoSubMenu.addItem("[?] LFO Presets", [hurl]() { juce::URL(hurl).launchInDefaultBrowser(); }),

lfoSubMenu.addSeparator();

lfoSubMenu.addItem(Surge::GUI::toOSCaseForMenu("Save " + what + " As..."), [this, currentLfoId,
what]() {
// Prompt for a name
promptForMiniEdit(
"preset", "Enter the name for " + what + " preset:", what + " Preset Name",
CPoint(-1, -1), [this, currentLfoId](const std::string &s) {
Surge::ModulatorPreset::savePresetToUser(string_to_path(s), &(this->synth->storage),
current_scene, currentLfoId);
});
// and save
});

auto presetCategories = Surge::ModulatorPreset::getPresets(&(synth->storage));

if (!presetCategories.empty())
lfoSubMenu->addSeparator();
{
lfoSubMenu.addSeparator();
}

std::unordered_map<std::string, std::pair<std::unique_ptr<juce::PopupMenu>, bool>> subMenuMaps;

std::unordered_map<std::string, std::pair<COptionMenu *, bool>> subMenuMaps;
subMenuMaps[""] = std::make_pair(lfoSubMenu, true);
for (auto const &cat : presetCategories)
{
COptionMenu *catSubMenu =
new COptionMenu(menuRect, 0, 0, 0, 0, VSTGUI::COptionMenu::kNoDrawStyle);
subMenuMaps[cat.path] = std::make_pair(catSubMenu, false);
subMenuMaps[cat.path] = std::make_pair(std::make_unique<juce::PopupMenu>(), false);
}

for (auto const &cat : presetCategories)
{
juce::PopupMenu *catSubMenu;
if (subMenuMaps.find(cat.path) == subMenuMaps.end())
{
// should never happen
continue;
if (cat.path.empty())
{
catSubMenu = &lfoSubMenu;
}
else
{
// should never happen
continue;
}
}
else
{
catSubMenu = subMenuMaps[cat.path].first.get();
}

auto catSubMenu = subMenuMaps[cat.path].first;

if (catSubMenu->getNbEntries() > 0 && cat.presets.size() > 0)
if (catSubMenu->getNumItems() > 0 && cat.presets.size() > 0)
{
catSubMenu->addSeparator();
}
Expand All @@ -5398,7 +5413,7 @@ VSTGUI::COptionMenu *SurgeGUIEditor::makeLfoMenu(VSTGUI::CRect &menuRect)
{
auto pname = p.name;

addCallbackMenu(catSubMenu, pname, [this, p, currentLfoId]() {
catSubMenu->addItem(pname, [this, p, currentLfoId]() {
Surge::ModulatorPreset::loadPresetFrom(p.path, &(this->synth->storage),
current_scene, currentLfoId);

Expand All @@ -5410,50 +5425,50 @@ VSTGUI::COptionMenu *SurgeGUIEditor::makeLfoMenu(VSTGUI::CRect &menuRect)
if (isAnyOverlayPresent(MSEG_EDITOR))
{
closeMSEGEditor();

if (newshape == lt_mseg)
{
showMSEGEditor();
}
}

this->synth->refresh_editor = true;
});
}
}

/*
* With the escape menu hack need to add in child firstr order; this is
* sorted by string path so go backwards
*/
for (auto it = presetCategories.rbegin(); it != presetCategories.rend(); it++)
{
const auto &cat = *it;

if (subMenuMaps.find(cat.path) == subMenuMaps.end())
{
jassert(false);
// should never happen
continue;
}
auto catSubMenu = subMenuMaps[cat.path].first;

auto parentMenu = lfoSubMenu;
auto catSubMenu = subMenuMaps[cat.path].first.get();
auto parentMenu = &lfoSubMenu;

if (subMenuMaps.find(cat.parentPath) != subMenuMaps.end())
{
parentMenu = subMenuMaps[cat.parentPath].first;
parentMenu = subMenuMaps[cat.parentPath].first.get();

if (!subMenuMaps[cat.parentPath].second)
{
subMenuMaps[cat.parentPath].second = true;
}
}

auto catname = cat.name;

parentMenu->addEntry(catSubMenu, catname.c_str());
catSubMenu->forget();
parentMenu->addSubMenu(cat.name, *catSubMenu);
}

lfoSubMenu->addSeparator();
lfoSubMenu.addSeparator();

lfoSubMenu.addItem(Surge::GUI::toOSCaseForMenu("Rescan Presets"),
[]() { Surge::ModulatorPreset::forcePresetRescan(); });

addCallbackMenu(lfoSubMenu, Surge::GUI::toOSCaseForMenu("Rescan Presets"),
[]() { Surge::ModulatorPreset::forcePresetRescan(); });
return lfoSubMenu;
}

Expand Down
2 changes: 1 addition & 1 deletion src/gui/SurgeGUIEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ class SurgeGUIEditor : public EditorType,
juce::PopupMenu makeDataMenu(VSTGUI::CRect &rect);
juce::PopupMenu makeMidiMenu(VSTGUI::CRect &rect);
juce::PopupMenu makeDevMenu(VSTGUI::CRect &rect);
VSTGUI::COptionMenu *makeLfoMenu(VSTGUI::CRect &rect);
juce::PopupMenu makeLfoMenu(VSTGUI::CRect &rect);
VSTGUI::COptionMenu *makeMonoModeOptionsMenu(VSTGUI::CRect &rect, bool updateDefaults);

bool scannedForMidiPresets = false;
Expand Down

0 comments on commit 9b10d49

Please sign in to comment.