Skip to content

Commit

Permalink
Improve patch browser
Browse files Browse the repository at this point in the history
  • Loading branch information
FigBug committed Mar 4, 2024
1 parent 90274e0 commit 62353bf
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 47 deletions.
45 changes: 35 additions & 10 deletions modules/gin_plugin/components/gin_patchbrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,57 @@ PatchBrowser::PatchBrowser (Processor& p) : proc (p)
auto inst = proc.state.getChildWithName ("instance");
selectedAuthors = juce::StringArray::fromTokens (inst.getProperty ("selectedAuthors", {}).toString(), "|", "");
selectedTags = juce::StringArray::fromTokens (inst.getProperty ("selectedTags", {}).toString(), "|", "");
selectedPresets = juce::StringArray::fromTokens (inst.getProperty ("selectedPresets", {}).toString(), "|", "");

refresh();
}

void PatchBrowser::next()
{
if (presetsModel.getNumRows() == 0)
return;

auto idx = presets.getSelectedRow() + 1;

if (idx >= presetsModel.getNumRows())
idx = 0;

presets.selectRow (idx);
proc.setCurrentProgram (currentPresets[idx]);
}

void PatchBrowser::prev()
{
if (presetsModel.getNumRows() == 0)
return;

auto idx = presets.getSelectedRow() - 1;

if (idx < 0)
idx = presetsModel.getNumRows() - 1;

presets.selectRow (idx);
proc.setCurrentProgram (currentPresets[idx]);
}

void PatchBrowser::selectionUpdated()
{
if (updatingSelection)
return;

selectedAuthors.clear();
selectedTags.clear();
selectedPresets.clear();

for (int i = 0; i < authors.getNumSelectedRows(); i++)
if (auto row = authors.getSelectedRow (i); row > 0)
selectedAuthors.add (currentAuthors[row]);

for (int i = 0; i < tags.getNumSelectedRows(); i++)
if (auto row = tags.getSelectedRow (i); row > 0)
selectedTags.add (currentTags[row]);
selectedTags.add (currentTags[row]);

for (int i = 0; i < presets.getNumSelectedRows(); i++)
selectedPresets.add (currentPresets[presets.getSelectedRow (i)]);

auto inst = proc.state.getChildWithName ("instance");
inst.setProperty ("selectedAuthors", selectedAuthors.joinIntoString ("|"), nullptr);
inst.setProperty ("selectedTags", selectedTags.joinIntoString ("|"), nullptr);
inst.setProperty ("selectedPresets", selectedPresets.joinIntoString ("|"), nullptr);
}

void PatchBrowser::refresh()
Expand Down Expand Up @@ -126,8 +148,10 @@ void PatchBrowser::refresh()
}

presets.deselectAllRows();
for (auto p : selectedPresets)
presets.selectRow (currentPresets.indexOf (p), true, false);

if (auto idx = proc.getCurrentProgram(); idx >= 0)
if (auto idx2 = currentPresets.indexOf (proc.getProgramName (idx)); idx2 >= 0)
presets.selectRow (idx2, true, false);

repaint();
}
Expand Down Expand Up @@ -325,11 +349,12 @@ void PatchBrowser::PresetsModel::paintListBoxItem (int row, juce::Graphics& g, i

void PatchBrowser::PresetsModel::listBoxItemDoubleClicked (int row, const juce::MouseEvent&)
{
owner.proc.setCurrentProgram (owner.currentPresets[row]);
}

void PatchBrowser::PresetsModel::listBoxItemClicked (int row, const juce::MouseEvent& e)
{
owner.proc.setCurrentProgram (owner.currentPresets[row]);

if ( ! e.mouseWasClicked() || ! e.mods.isPopupMenu())
return;

Expand Down
7 changes: 5 additions & 2 deletions modules/gin_plugin/components/gin_patchbrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ class PatchBrowser : public juce::Component
{
public:
PatchBrowser (Processor& p);

void next();
void prev();

void selectionUpdated();
void refresh();
Expand All @@ -12,7 +15,7 @@ class PatchBrowser : public juce::Component

void editPreset (int row);
void deletePreset (int row);

Processor& proc;

struct AuthorsModel : public juce::ListBoxModel
Expand Down Expand Up @@ -57,5 +60,5 @@ class PatchBrowser : public juce::Component

bool updatingSelection = false;
juce::StringArray currentAuthors, currentTags, currentPresets;
juce::StringArray selectedAuthors, selectedTags, selectedPresets;
juce::StringArray selectedAuthors, selectedTags;
};
122 changes: 97 additions & 25 deletions modules/gin_plugin/components/gin_plugineditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ void NewsChecker::handleAsyncUpdate()
}

//==============================================================================
TitleBar::TitleBar (ProcessorEditor& e, Processor& p)
: editor (e), slProc (p)
TitleBar::TitleBar (ProcessorEditor& e, Processor& p, PatchBrowser& pb)
: editor (e), slProc (p), patchBrowser (pb)
{
setName ("titlebar");

Expand All @@ -159,6 +159,7 @@ TitleBar::TitleBar (ProcessorEditor& e, Processor& p)
addAndMakeVisible (menuButton);
addAndMakeVisible (browseButton);
addAndMakeVisible (programs);
addAndMakeVisible (programName);
addAndMakeVisible (nextButton);
addAndMakeVisible (prevButton);
addAndMakeVisible (addButton);
Expand All @@ -184,6 +185,8 @@ TitleBar::TitleBar (ProcessorEditor& e, Processor& p)
prevButton.setTooltip ("Prev Preset");
infoButton.setTooltip ("Info");
menuButton.setTooltip ("Menu");

programName.setJustificationType (juce::Justification::centred);

slProc.addChangeListener (this);

Expand All @@ -198,22 +201,66 @@ TitleBar::TitleBar (ProcessorEditor& e, Processor& p)
newsChecker = std::make_unique<NewsChecker> (slProc);
newsChecker->onNewsUpdate = [](juce::String) {};
}

programName.addMouseListener (&labelListener, false);
labelListener.onMouseDown = [this] (const juce::MouseEvent&)
{
if (menuShowing)
{
menuShowing = false;
juce::PopupMenu::dismissAllActiveMenus();
return;
}

juce::PopupMenu m;
m.setLookAndFeel (&getLookAndFeel());

juce::String cur;
if (auto idx = slProc.getCurrentProgram(); idx >= 0)
cur = slProc.getProgramName (idx);

m.addItem ("Reset to default", [this] { slProc.setCurrentProgram ("Default"); });
m.addSeparator();

for (auto s : patchBrowser.currentPresets)
{
auto selected = s == cur;
m.addItem (s, true, selected, [this, s] { slProc.setCurrentProgram (s); });
}

auto opts = juce::PopupMenu::Options().withTargetComponent (programName).withDeletionCheck (programName);
m.showMenuAsync (opts, [&] (int) { menuShowing = false; } );
};

nextButton.onClick = [this]
{
int prog = slProc.getCurrentProgram() + 1;
if (prog >= slProc.getPrograms().size())
prog = 0;

slProc.setCurrentProgram (prog);
if (hasBrowser)
{
patchBrowser.next();
}
else
{
int prog = slProc.getCurrentProgram() + 1;
if (prog >= slProc.getPrograms().size())
prog = 0;

slProc.setCurrentProgram (prog);
}
};
prevButton.onClick = [this]
{
int prog = slProc.getCurrentProgram() - 1;
if (prog < 0)
prog = slProc.getPrograms().size() - 1;

slProc.setCurrentProgram (prog);
if (hasBrowser)
{
patchBrowser.prev();
}
else
{
int prog = slProc.getCurrentProgram() - 1;
if (prog < 0)
prog = slProc.getPrograms().size() - 1;

slProc.setCurrentProgram (prog);
}
};
browseButton.onClick = [this]
{
Expand Down Expand Up @@ -287,7 +334,7 @@ TitleBar::TitleBar (ProcessorEditor& e, Processor& p)
w->setVisible (false);
if (r == 1)
{
slProc.deleteProgram (programs.getSelectedItemIndex());
slProc.deleteProgram (slProc.getCurrentProgram());
refreshPrograms();
}
});
Expand Down Expand Up @@ -318,6 +365,9 @@ void TitleBar::parentHierarchyChanged()
prevButton.setWantsKeyboardFocus (a);
infoButton.setWantsKeyboardFocus (a);
menuButton.setWantsKeyboardFocus (a);

auto c = findColour (PluginLookAndFeel::accentColourId);
programName.setColour (juce::Label::textColourId, c);
}

void TitleBar::paint (juce::Graphics& g)
Expand All @@ -333,6 +383,7 @@ void TitleBar::setShowBrowser (bool s)
{
hasBrowser = s;
resized ();
refreshPrograms();
}

void TitleBar::setShowPresets (bool s)
Expand All @@ -357,7 +408,16 @@ void TitleBar::resized()

if (hasPresets)
{
programs.setBounds (programsRC);
if (hasBrowser)
{
programName.setBounds (programsRC);
programs.setBounds ({});
}
else
{
programs.setBounds (programsRC);
programName.setBounds ({});
}

int x = programsRC.getRight() + 10;
addButton.setBounds (x, 10, 19, 19);
Expand All @@ -367,6 +427,7 @@ void TitleBar::resized()
else
{
programs.setBounds ({});
programName.setBounds ({});
addButton.setBounds ({});
deleteButton.setBounds ({});
}
Expand Down Expand Up @@ -394,19 +455,30 @@ void TitleBar::resized()

void TitleBar::refreshPrograms()
{
programs.clear();

for (int i = 0; i < slProc.getPrograms().size(); i++)
if (hasBrowser)
{
programs.addItem (slProc.getProgramName (i), i + 1);
if (i == 0)
programs.addSeparator();
editor.refreshPatchBrowser();

if (auto idx = slProc.getCurrentProgram(); idx >= 0)
programName.setText (slProc.getProgramName (idx), juce::dontSendNotification);
else
programName.setText ({}, juce::dontSendNotification);
deleteButton.setEnabled (slProc.getCurrentProgram() != 0);
}
else
{
programs.clear();

for (int i = 0; i < slProc.getPrograms().size(); i++)
{
programs.addItem (slProc.getProgramName (i), i + 1);
if (i == 0)
programs.addSeparator();
}

programs.setSelectedItemIndex (slProc.getCurrentProgram(), juce::dontSendNotification);
deleteButton.setEnabled (slProc.getCurrentProgram() != 0);
}

programs.setSelectedItemIndex (slProc.getCurrentProgram(), juce::dontSendNotification);
deleteButton.setEnabled (slProc.getCurrentProgram() != 0);

editor.refreshPatchBrowser();
}

void TitleBar::showMenu()
Expand Down
9 changes: 7 additions & 2 deletions modules/gin_plugin/components/gin_plugineditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class TitleBar : public juce::Component,
protected juce::ChangeListener
{
public:
TitleBar (ProcessorEditor&, Processor&);
TitleBar (ProcessorEditor&, Processor&, PatchBrowser&);
~TitleBar () override;

void refreshPrograms();
Expand All @@ -188,11 +188,13 @@ class TitleBar : public juce::Component,

ProcessorEditor& editor;
Processor& slProc;
PatchBrowser& patchBrowser;

bool hasPresets = true;
bool hasBrowser = false;

juce::ComboBox programs;
juce::Label programName;
SVGButton addButton { "add", gin::Assets::add };
SVGButton deleteButton { "delete", gin::Assets::del };
SVGButton browseButton { "browse", gin::Assets::browse };
Expand All @@ -201,6 +203,9 @@ class TitleBar : public juce::Component,

SVGButton menuButton { "menu", gin::Assets::menu };
SVGButton infoButton { "info", gin::Assets::info };

bool menuShowing = false;
LambdaMouseListener labelListener;

std::unique_ptr<NewsChecker> newsChecker;
std::unique_ptr<UpdateChecker> updateChecker;
Expand Down Expand Up @@ -235,6 +240,6 @@ class ProcessorEditor : public ProcessorEditorBase,
std::unique_ptr<UpdateChecker> updateChecker;
std::unique_ptr<NewsChecker> newsChecker;

TitleBar titleBar { *this, slProc };
PatchBrowser patchBrowser { slProc };
TitleBar titleBar { *this, slProc, patchBrowser };
};
17 changes: 9 additions & 8 deletions modules/gin_plugin/plugin/gin_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,26 +383,27 @@ void Processor::loadAllPrograms()

programs.clear();

// create the default program
auto defaultProgram = new Program();
defaultProgram->name = "Default";
defaultProgram->saveProcessor (*this);

programs.add (defaultProgram);

// load programs from disk
juce::File dir = getProgramDirectory();

juce::Array<juce::File> programFiles;
dir.findChildFiles (programFiles, juce::File::findFiles, false, "*.xml");
programFiles.sort();

for (auto f : programFiles)
{
auto program = new Program();
program->loadFromFile (f, false);
programs.add (program);
}

std::sort (programs.begin(), programs.end(), [](const auto& a, const auto& b) { return a->name.compareIgnoreCase (b->name) < 0; });

// create the default program
auto defaultProgram = new Program();
defaultProgram->name = "Default";
defaultProgram->saveProcessor (*this);

programs.insert (0, defaultProgram);
}

void Processor::extractProgram (const juce::String& name, const juce::MemoryBlock& data)
Expand Down

0 comments on commit 62353bf

Please sign in to comment.