Skip to content

Commit

Permalink
plugin: double click loads preset in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
JoepVanlier committed Nov 28, 2024
1 parent 692eed2 commit 6e1575b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
42 changes: 35 additions & 7 deletions plugin/components/rpl_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ class BankItemsListBoxModel final : public juce::ListBox, public juce::ListBoxMo
m_deleteCallback = deleteCallback;
}

void setDoubleClickCallback(std::function<void(int)> dblClickCallback) {
m_dblClickCallback = dblClickCallback;
}

private:
std::vector<juce::String> m_items;
std::optional<std::function<void()>> m_bankUpdatedCallback{};
std::function<void(int)> m_dblClickCallback;
std::function<void(std::vector<int>, juce::WeakReference<juce::Component>)> m_dropCallback;
std::function<void(std::vector<int>)> m_deleteCallback;

Expand Down Expand Up @@ -120,6 +124,11 @@ class BankItemsListBoxModel final : public juce::ListBox, public juce::ListBoxMo
m_deleteCallback(elements);
}
}

void listBoxItemDoubleClicked(int row, const juce::MouseEvent &) override
{
m_dblClickCallback(row);
}
};

class LoadedBank : public juce::Component, public juce::DragAndDropContainer {
Expand All @@ -133,7 +142,8 @@ class LoadedBank : public juce::Component, public juce::DragAndDropContainer {
std::unique_ptr<juce::TextButton> m_btnLoadFile;
std::unique_ptr<juce::FileChooser> m_fileChooser;

std::function<void()> m_bankUpdatedCallback;
std::function<void(void)> m_bankUpdatedCallback;
std::function<void(std::string)> m_loadPresetCallback;

public:
void resized() override
Expand All @@ -156,6 +166,11 @@ class LoadedBank : public juce::Component, public juce::DragAndDropContainer {
m_bankUpdatedCallback = bankUpdatedCallback;
}

void setLoadPresetCallback(std::function<void(std::string)> loadPresetCallback)
{
m_loadPresetCallback = loadPresetCallback;
}

void setLabelTooltip(juce::String tooltip) {
m_label->setTooltip(tooltip);
}
Expand Down Expand Up @@ -210,11 +225,17 @@ class LoadedBank : public juce::Component, public juce::DragAndDropContainer {
}
}

if (names.empty()) return;

juce::AlertWindow::showAsync
(
juce::MessageBoxOptions()
.withTitle("Are you certain?")
.withMessage("Are you certain you want to delete several presets?\nThis operation cannot be undone!")
.withMessage(
TRANS("Are you certain you want to delete ") +
((names.size() > 1) ? TRANS("several presets") : names[0])
+ "\n" + TRANS("This operation cannot be undone!")
)
.withButton("Yes")
.withButton("No")
.withParentComponent(this)
Expand Down Expand Up @@ -247,6 +268,7 @@ class LoadedBank : public juce::Component, public juce::DragAndDropContainer {
m_listBox->setOutlineThickness(1);
m_listBox->setDropCallback([this](std::vector<int> indices, juce::WeakReference<juce::Component> ref) { this->transferPresets(indices, ref); });
m_listBox->setDeleteCallback([this](std::vector<int> indices) { this->deletePresets(indices); });
m_listBox->setDoubleClickCallback([this](int idx) { if (m_loadPresetCallback) m_loadPresetCallback(std::string{m_bank->presets[idx].name}); });
addAndMakeVisible(*m_listBox);
addAndMakeVisible(*m_label);
}
Expand Down Expand Up @@ -345,7 +367,8 @@ struct YsfxRPLView::Impl {

std::unique_ptr<juce::Timer> m_relayoutTimer;
std::unique_ptr<juce::Timer> m_fileCheckTimer;
std::function<void(void)> m_callback;
std::function<void(void)> m_bankUpdateCallback;
std::function<void(std::string)> m_loadPresetCallback;

//==========================================================================
void createUI();
Expand Down Expand Up @@ -383,16 +406,21 @@ void YsfxRPLView::setEffect(ysfx_t *fx)
m_impl->setupNewFx();
}

void YsfxRPLView::setBankUpdateCallback(std::function<void(void)> callback) {
m_impl->m_callback = callback;
void YsfxRPLView::setBankUpdateCallback(std::function<void(void)> bankUpdateCallback) {
m_impl->m_bankUpdateCallback = bankUpdateCallback;
}

void YsfxRPLView::setLoadPresetCallback(std::function<void(std::string)> loadPresetCallback) {
m_impl->m_loadPresetCallback = loadPresetCallback;
}

void YsfxRPLView::Impl::createUI()
{
m_left.createUI(false);
m_left.setLabelTooltip("Location of the currently loaded presets");
m_self->addAndMakeVisible(m_left);
m_left.setBankUpdatedCallback([this](void) {this->m_callback();});
m_left.setBankUpdatedCallback([this](void) { if (m_bankUpdateCallback) m_bankUpdateCallback(); });
m_left.setLoadPresetCallback([this](std::string name) { if (m_loadPresetCallback) this->m_loadPresetCallback(name); });

m_right.createUI(true);
m_right.setLabelTooltip("Click to select preset file to import from");
Expand Down
3 changes: 2 additions & 1 deletion plugin/components/rpl_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class YsfxRPLView : public juce::Component {
YsfxRPLView();
~YsfxRPLView() override;
void setEffect(ysfx_t *fx);
void setBankUpdateCallback(std::function<void(void)> callback);
void setBankUpdateCallback(std::function<void(void)> bankUpdateCallback);
void setLoadPresetCallback(std::function<void(std::string)> loadPresetCallback);

void focusOnPresetViewer();

Expand Down
12 changes: 12 additions & 0 deletions plugin/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,18 @@ void YsfxEditor::Impl::updateInfo()
m_proc->reloadBank();
}
);
m_rplView->setLoadPresetCallback(
[this](std::string preset) {
YsfxInfo::Ptr info = m_info;
ysfx_bank_shared bank = m_bank;
if (!bank) return;

auto index = ysfx_preset_exists(bank.get(), preset.c_str());
if (index > 0) {
m_proc->loadJsfxPreset(info, bank, (uint32_t)(index - 1), true, true);
}
}
);

// We always just want the sliders the user meant to expose
switchEditor(true);
Expand Down

0 comments on commit 6e1575b

Please sign in to comment.