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

This commit is contained in:
Alex Birch 2019-07-21 15:04:19 +01:00
parent 745adf8fde
commit 304ec6ce88
No known key found for this signature in database
GPG Key ID: 305EB1F98D44ACBA
2 changed files with 50 additions and 21 deletions

View File

@ -25,20 +25,19 @@ Pill::Pill(
| (isLast ? 0 : Button::ConnectedOnRight) | (isLast ? 0 : Button::ConnectedOnRight)
); );
textButton.setRadioGroupId(34567); textButton.setRadioGroupId(34567);
loadToggleState(); // loadToggleState();
textButton.setClickingTogglesState(true); textButton.setClickingTogglesState(true);
addAndMakeVisible(textButton); addAndMakeVisible(textButton);
valueTreeState.addParameterListener("bank", this); // valueTreeState.addParameterListener("bank", this);
// valueTreeState.state.addListener(this); // valueTreeState.state.addListener(this);
textButton.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) // (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() { void Pill::resized() {
@ -46,19 +45,23 @@ void Pill::resized() {
} }
Pill::~Pill() { Pill::~Pill() {
valueTreeState.removeParameterListener("bank", this); // valueTreeState.removeParameterListener("bank", this);
// valueTreeState.state.removeListener(this); // valueTreeState.state.removeListener(this);
textButton.removeListener(this); textButton.removeListener(this);
} }
void Pill::loadToggleState() { void Pill::bankChanged(int bank) {
RangedAudioParameter *param {valueTreeState.getParameter("bank")}; textButton.setToggleState(this->bank == bank, dontSendNotification);
jassert(dynamic_cast<AudioParameterInt*> (param) != nullptr);
AudioParameterInt* castParam {dynamic_cast<AudioParameterInt*> (param)};
int value{castParam->get()};
textButton.setToggleState(value == bank, dontSendNotification);
} }
// void Pill::loadToggleState() {
// RangedAudioParameter *param {valueTreeState.getParameter("bank")};
// jassert(dynamic_cast<AudioParameterInt*> (param) != nullptr);
// AudioParameterInt* castParam {dynamic_cast<AudioParameterInt*> (param)};
// int value{castParam->get()};
// textButton.setToggleState(value == bank, dontSendNotification);
// }
void Pill::buttonClicked (Button* button) { void Pill::buttonClicked (Button* button) {
// selected = button; // selected = button;
// onItemSelected(itemToIDMapper(button->getName().toStdString())); // onItemSelected(itemToIDMapper(button->getName().toStdString()));
@ -68,11 +71,11 @@ void Pill::buttonClicked (Button* button) {
*castParam = bank; *castParam = bank;
} }
void Pill::parameterChanged(const String& parameterID, float newValue) { // void Pill::parameterChanged(const String& parameterID, float newValue) {
if (parameterID == "bank") { // if (parameterID == "bank") {
loadToggleState(); // loadToggleState();
} // }
} // }
// void Pill::valueTreePropertyChanged( // void Pill::valueTreePropertyChanged(
// ValueTree& treeWhosePropertyHasChanged, // ValueTree& treeWhosePropertyHasChanged,
@ -104,12 +107,30 @@ Pills::Pills(
loadModelFrom(banks); loadModelFrom(banks);
valueTreeState.state.addListener(this); valueTreeState.state.addListener(this);
valueTreeState.addParameterListener("bank", this);
} }
Pills::~Pills() { Pills::~Pills() {
valueTreeState.removeParameterListener("bank", this);
valueTreeState.state.removeListener(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<AudioParameterInt*> (param) != nullptr);
AudioParameterInt* castParam {dynamic_cast<AudioParameterInt*> (param)};
int bank{castParam->get()};
for (auto& pill: pills) {
pill->bankChanged(bank);
}
}
void Pills::valueTreePropertyChanged( void Pills::valueTreePropertyChanged(
ValueTree& treeWhosePropertyHasChanged, ValueTree& treeWhosePropertyHasChanged,
const Identifier& property) { const Identifier& property) {
@ -137,6 +158,7 @@ void Pills::loadModelFrom(ValueTree& banks) {
addAndMakeVisible(pill.get()); addAndMakeVisible(pill.get());
pills.push_back(move(pill)); pills.push_back(move(pill));
} }
updatePillToggleStates();
resized(); resized();
} }

View File

@ -11,7 +11,7 @@ using namespace std;
class Pill class Pill
: public Component : public Component
, public Button::Listener , public Button::Listener
, public AudioProcessorValueTreeState::Listener // , public AudioProcessorValueTreeState::Listener
{ {
public: public:
Pill( Pill(
@ -22,14 +22,16 @@ public:
); );
~Pill(); ~Pill();
void buttonClicked (Button* button) override; void buttonClicked(Button* button) override;
void resized() override; void resized() override;
void paint(Graphics& g) 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: private:
void loadToggleState(); // void loadToggleState();
AudioProcessorValueTreeState& valueTreeState; AudioProcessorValueTreeState& valueTreeState;
int bank; int bank;
@ -41,6 +43,7 @@ private:
class Pills class Pills
: public Component : public Component
, public ValueTree::Listener , public ValueTree::Listener
, public AudioProcessorValueTreeState::Listener
{ {
public: public:
Pills( Pills(
@ -61,6 +64,8 @@ public:
// void buttonClicked (Button* button) override; // void buttonClicked (Button* button) override;
void cycle(bool right); void cycle(bool right);
virtual void parameterChanged (const String& parameterID, float newValue) override;
virtual void valueTreePropertyChanged (ValueTree& treeWhosePropertyHasChanged, virtual void valueTreePropertyChanged (ValueTree& treeWhosePropertyHasChanged,
const Identifier& property) override; const Identifier& property) override;
inline virtual void valueTreeChildAdded (ValueTree& parentTree, inline virtual void valueTreeChildAdded (ValueTree& parentTree,
@ -87,6 +92,8 @@ private:
// Pill* addToList (Pill* newButton); // Pill* addToList (Pill* newButton);
void updatePillToggleStates();
void populate(int initiallySelectedItem); void populate(int initiallySelectedItem);
void resized() override; void resized() override;
void paint(Graphics& g) override; void paint(Graphics& g) override;