From 304ec6ce88180250040e654df25a70a54484c9f7 Mon Sep 17 00:00:00 2001 From: Alex Birch Date: Sun, 21 Jul 2019 15:04:19 +0100 Subject: [PATCH] pills no longer listen to valueTree; I didn't like the idea of having up to 128 listeners when the parent could do dispatch with just one listener --- Source/Pills.cpp | 56 +++++++++++++++++++++++++++++++++--------------- Source/Pills.h | 15 +++++++++---- 2 files changed, 50 insertions(+), 21 deletions(-) diff --git a/Source/Pills.cpp b/Source/Pills.cpp index 03d3056..82601ff 100644 --- a/Source/Pills.cpp +++ b/Source/Pills.cpp @@ -25,20 +25,19 @@ Pill::Pill( | (isLast ? 0 : Button::ConnectedOnRight) ); textButton.setRadioGroupId(34567); - loadToggleState(); + // loadToggleState(); textButton.setClickingTogglesState(true); addAndMakeVisible(textButton); - valueTreeState.addParameterListener("bank", this); + // valueTreeState.addParameterListener("bank", this); // valueTreeState.state.addListener(this); textButton.addListener(this); } -void Pill::paint (Graphics& g) -{ +void Pill::paint (Graphics& g) { // (Our component is opaque, so we must completely fill the background with a solid colour) - g.fillAll (getLookAndFeel().findColour (ResizableWindow::backgroundColourId)); + g.fillAll(getLookAndFeel().findColour(ResizableWindow::backgroundColourId)); } void Pill::resized() { @@ -46,19 +45,23 @@ void Pill::resized() { } Pill::~Pill() { - valueTreeState.removeParameterListener("bank", this); + // valueTreeState.removeParameterListener("bank", this); // valueTreeState.state.removeListener(this); textButton.removeListener(this); } -void Pill::loadToggleState() { - RangedAudioParameter *param {valueTreeState.getParameter("bank")}; - jassert(dynamic_cast (param) != nullptr); - AudioParameterInt* castParam {dynamic_cast (param)}; - int value{castParam->get()}; - textButton.setToggleState(value == bank, dontSendNotification); +void Pill::bankChanged(int bank) { + textButton.setToggleState(this->bank == bank, dontSendNotification); } +// void Pill::loadToggleState() { +// RangedAudioParameter *param {valueTreeState.getParameter("bank")}; +// jassert(dynamic_cast (param) != nullptr); +// AudioParameterInt* castParam {dynamic_cast (param)}; +// int value{castParam->get()}; +// textButton.setToggleState(value == bank, dontSendNotification); +// } + void Pill::buttonClicked (Button* button) { // selected = button; // onItemSelected(itemToIDMapper(button->getName().toStdString())); @@ -68,11 +71,11 @@ void Pill::buttonClicked (Button* button) { *castParam = bank; } -void Pill::parameterChanged(const String& parameterID, float newValue) { - if (parameterID == "bank") { - loadToggleState(); - } -} +// void Pill::parameterChanged(const String& parameterID, float newValue) { +// if (parameterID == "bank") { +// loadToggleState(); +// } +// } // void Pill::valueTreePropertyChanged( // ValueTree& treeWhosePropertyHasChanged, @@ -104,12 +107,30 @@ Pills::Pills( loadModelFrom(banks); valueTreeState.state.addListener(this); + valueTreeState.addParameterListener("bank", this); } Pills::~Pills() { + valueTreeState.removeParameterListener("bank", this); valueTreeState.state.removeListener(this); } +void Pills::parameterChanged(const String& parameterID, float newValue) { + if (parameterID == "bank") { + updatePillToggleStates(); + } +} + +void Pills::updatePillToggleStates() { + RangedAudioParameter *param {valueTreeState.getParameter("bank")}; + jassert(dynamic_cast (param) != nullptr); + AudioParameterInt* castParam {dynamic_cast (param)}; + int bank{castParam->get()}; + for (auto& pill: pills) { + pill->bankChanged(bank); + } +} + void Pills::valueTreePropertyChanged( ValueTree& treeWhosePropertyHasChanged, const Identifier& property) { @@ -137,6 +158,7 @@ void Pills::loadModelFrom(ValueTree& banks) { addAndMakeVisible(pill.get()); pills.push_back(move(pill)); } + updatePillToggleStates(); resized(); } diff --git a/Source/Pills.h b/Source/Pills.h index 2264231..812c2a6 100644 --- a/Source/Pills.h +++ b/Source/Pills.h @@ -11,7 +11,7 @@ using namespace std; class Pill : public Component , public Button::Listener -, public AudioProcessorValueTreeState::Listener +// , public AudioProcessorValueTreeState::Listener { public: Pill( @@ -22,14 +22,16 @@ public: ); ~Pill(); - void buttonClicked (Button* button) override; + void buttonClicked(Button* button) override; void resized() override; void paint(Graphics& g) override; - virtual void parameterChanged (const String& parameterID, float newValue) override; + void bankChanged(int bank); + + // virtual void parameterChanged (const String& parameterID, float newValue) override; private: - void loadToggleState(); + // void loadToggleState(); AudioProcessorValueTreeState& valueTreeState; int bank; @@ -41,6 +43,7 @@ private: class Pills : public Component , public ValueTree::Listener +, public AudioProcessorValueTreeState::Listener { public: Pills( @@ -61,6 +64,8 @@ public: // void buttonClicked (Button* button) override; void cycle(bool right); + virtual void parameterChanged (const String& parameterID, float newValue) override; + virtual void valueTreePropertyChanged (ValueTree& treeWhosePropertyHasChanged, const Identifier& property) override; inline virtual void valueTreeChildAdded (ValueTree& parentTree, @@ -87,6 +92,8 @@ private: // Pill* addToList (Pill* newButton); + void updatePillToggleStates(); + void populate(int initiallySelectedItem); void resized() override; void paint(Graphics& g) override;