OnOffManager.cpp (3257B)
1 #include "OnOffManager.h" 2 #include <unordered_map> 3 4 namespace 5 { 6 std::unordered_map<String, StringArray> createTriggerMap() 7 { 8 return { 9 { String ("ifilt_onoff"), StringArray ({ "Low Cut", "High Cut", "Makeup" }) }, 10 { String ("hyst_onoff"), StringArray ({ "Bias", "Saturation", "Drive" }) }, 11 { String ("tone_onoff"), StringArray ({ "Bass", "Treble", "Transition Frequency" }) }, 12 { String ("loss_onoff"), StringArray ({ "Gap", "Thickness", "Spacing", "Azimuth", "Speed", "3.75 ips", "7.5 ips", "15 ips", "30 ips" }) }, 13 { String ("chew_onoff"), StringArray ({ "Chew Depth", "Chew Frequency", "Chew Variance" }) }, 14 { String ("deg_onoff"), StringArray ({ "Depth", "Amount", "Variance", "Envelope", "0.1x" }) }, 15 { String ("flutter_onoff"), StringArray ({ "Flutter Depth", "Flutter Rate", "Wow Depth", "Wow Rate", "Wow Variance", "Wow Drift" }) }, 16 { String ("comp_onoff"), StringArray ({ "Compression Amount", "Compression Attack", "Compression Release" }) }, 17 }; 18 } 19 20 void toggleEnableDisable (Component* root, StringArray& compNames, bool shouldBeEnabled) 21 { 22 if (root == nullptr) 23 return; 24 25 if (compNames.isEmpty()) 26 return; 27 28 for (auto child : root->getChildren()) 29 { 30 auto compName = child->getName(); 31 if (compNames.contains (compName)) 32 { 33 MessageManagerLock mml; 34 compNames.removeString (compName); 35 child->setEnabled (shouldBeEnabled); 36 continue; 37 } 38 39 toggleEnableDisable (child, compNames, shouldBeEnabled); 40 } 41 } 42 } // namespace 43 44 OnOffManager::OnOffManager (AudioProcessorValueTreeState& vts, const AudioProcessor* proc) : vts (vts), 45 proc (proc), 46 triggerMap (createTriggerMap()) 47 { 48 for (const auto& trigger : triggerMap) 49 vts.addParameterListener (trigger.first, this); 50 } 51 52 OnOffManager::~OnOffManager() 53 { 54 for (auto& trigger : triggerMap) 55 vts.removeParameterListener (trigger.first, this); 56 } 57 58 void OnOffManager::setOnOffForNewEditor (AudioProcessorEditor* editor) 59 { 60 for (const auto& trigger : triggerMap) 61 { 62 auto paramValue = vts.getRawParameterValue (trigger.first)->load(); 63 StringArray compNames { trigger.second }; 64 toggleEnableDisable (editor, compNames, (bool) paramValue); 65 } 66 } 67 68 void OnOffManager::parameterChanged (const String& paramID, float newValue) 69 { 70 if (const auto triggerMapIter = triggerMap.find (paramID); triggerMapIter != triggerMap.end()) 71 { 72 componentsToToggle = &triggerMapIter->second; 73 turningOn = (bool) newValue; 74 75 if (MessageManager::existsAndIsCurrentThread()) 76 onOffButtonToggled(); 77 else 78 triggerAsyncUpdate(); 79 } 80 } 81 82 void OnOffManager::handleAsyncUpdate() 83 { 84 onOffButtonToggled(); 85 } 86 87 void OnOffManager::onOffButtonToggled() 88 { 89 if (componentsToToggle == nullptr) 90 return; 91 92 auto compNames = StringArray { *componentsToToggle }; 93 toggleEnableDisable (proc->getActiveEditor(), compNames, turningOn); 94 componentsToToggle = nullptr; 95 }