PartButton.cpp (2299B)
1 #include "PartButton.h" 2 3 #include "VirusEditor.h" 4 #include "VirusController.h" 5 6 #include "jucePluginEditorLib/patchmanager/savepatchdesc.h" 7 8 namespace genericVirusUI 9 { 10 PartButton::PartButton(VirusEditor& _editor) : jucePluginEditorLib::PartButton<TextButton>(_editor), m_editor(_editor) // NOLINT(clang-diagnostic-undefined-func-template) 11 { 12 } 13 14 bool PartButton::isInterestedInDragSource(const SourceDetails& _dragSourceDetails) 15 { 16 if(getPart() > 0 && !m_editor.getController().isMultiMode()) 17 return false; 18 19 return jucePluginEditorLib::PartButton<TextButton>::isInterestedInDragSource(_dragSourceDetails); // NOLINT(clang-diagnostic-undefined-func-template) 20 } 21 22 void PartButton::paint(juce::Graphics& g) 23 { 24 jucePluginEditorLib::PartButton<TextButton>::paint(g); 25 } 26 27 void PartButton::onClick() 28 { 29 selectPreset(getPart()); 30 } 31 32 void PartButton::selectPreset(uint8_t _part) const 33 { 34 pluginLib::patchDB::SearchRequest req; 35 req.sourceType = pluginLib::patchDB::SourceType::Rom; 36 37 m_editor.getPatchManager()->search(std::move(req), [this](const pluginLib::patchDB::Search& _search) 38 { 39 std::map<std::string, std::vector<pluginLib::patchDB::PatchPtr>> patches; 40 41 { 42 std::shared_lock lock(_search.resultsMutex); 43 for (const auto& patch : _search.results) 44 { 45 const auto s = patch->source.lock(); 46 if(!s) 47 continue; 48 patches[s->name].push_back(patch); 49 } 50 } 51 52 for (auto& it : patches) 53 { 54 std::sort(it.second.begin(), it.second.end(), [](const pluginLib::patchDB::PatchPtr& _a, const pluginLib::patchDB::PatchPtr& _b) 55 { 56 return _a->program < _b->program; 57 }); 58 } 59 60 juce::MessageManager::callAsync([this, patches = std::move(patches)] 61 { 62 juce::PopupMenu selector; 63 64 for (const auto& it : patches) 65 { 66 juce::PopupMenu p; 67 for (const auto& patch : it.second) 68 { 69 const auto& presetName = patch->getName(); 70 p.addItem(presetName, [this, patch] 71 { 72 if(m_editor.getPatchManager()->activatePatch(patch, getPart())) 73 m_editor.getPatchManager()->setSelectedPatch(getPart(), patch); 74 }); 75 } 76 selector.addSubMenu(it.first, p); 77 } 78 selector.showMenuAsync(juce::PopupMenu::Options()); 79 }); 80 }); 81 } 82 }