controllerlink.cpp (1845B)
1 #include "controllerlink.h" 2 3 #include "editor.h" 4 5 namespace genericUI 6 { 7 ControllerLink::ControllerLink(std::string _source, std::string _dest, std::string _conditionParam) 8 : m_sourceName(std::move(_source)) 9 , m_destName(std::move(_dest)) 10 , m_conditionParam(std::move(_conditionParam)) 11 { 12 } 13 14 ControllerLink::~ControllerLink() 15 { 16 if(m_source) 17 { 18 m_source->removeListener(this); 19 m_source->removeComponentListener(this); 20 } 21 } 22 23 void ControllerLink::create(const Editor& _editor) 24 { 25 create( 26 _editor.findComponentT<juce::Slider>(m_sourceName), 27 _editor.findComponentT<juce::Slider>(m_destName), 28 _editor.findComponentT<juce::Button>(m_conditionParam) 29 ); 30 } 31 32 void ControllerLink::create(juce::Slider* _source, juce::Slider* _dest, juce::Button* _condition) 33 { 34 m_source = _source; 35 m_dest = _dest; 36 m_condition = _condition; 37 38 m_lastSourceValue = m_source->getValue(); 39 m_source->addListener(this); 40 m_source->addComponentListener(this); 41 } 42 43 void ControllerLink::sliderValueChanged(juce::Slider* _slider) 44 { 45 const auto current = m_source->getValue(); 46 const auto delta = current - m_lastSourceValue; 47 m_lastSourceValue = current; 48 49 if(!m_sourceIsBeingDragged) 50 return; 51 52 if(std::abs(delta) < 0.0001) 53 return; 54 55 if(m_condition && !m_condition->getToggleState()) 56 return; 57 58 const auto destValue = m_dest->getValue(); 59 const auto newDestValue = destValue + delta; 60 61 if(std::abs(newDestValue - destValue) < 0.0001) 62 return; 63 64 m_dest->setValue(newDestValue); 65 } 66 67 void ControllerLink::sliderDragStarted(juce::Slider*) 68 { 69 m_lastSourceValue = m_source->getValue(); 70 m_sourceIsBeingDragged = true; 71 } 72 73 void ControllerLink::sliderDragEnded(juce::Slider*) 74 { 75 m_sourceIsBeingDragged = false; 76 } 77 78 void ControllerLink::componentBeingDeleted(juce::Component& component) 79 { 80 m_source = nullptr; 81 } 82 }