Skip to content

Commit

Permalink
Start of a collection of cleanups (#49)
Browse files Browse the repository at this point in the history
* Start of a collection of cleanups

1. component-adapters exist for at least a simple discrete case
2. Make the componet take Continuous or ContinuousModulatable so
   not every client needs to drag around the modulation api.
3. Fix up the destruct sequence once and for all
4. Includes and stuff
  • Loading branch information
baconpaul authored Oct 11, 2023
1 parent 31b3f68 commit 4630c13
Show file tree
Hide file tree
Showing 19 changed files with 250 additions and 142 deletions.
13 changes: 1 addition & 12 deletions examples/component-demo/CoupledControls.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,7 @@ struct CoupledControlsDemo : public sst::jucegui::components::WindowPanel
idleTimer = std::make_unique<Idle>(this);
idleTimer->startTimer(1000.0 / 60.0);
}
~MixedControls()
{
idleTimer->stopTimer();
for (const auto &k : knobs)
{
k->setSource(nullptr);
}
for (const auto &k : sliders)
{
k->setSource(nullptr);
}
}
~MixedControls() { idleTimer->stopTimer(); }

float ival = 0.f;
void idle()
Expand Down
8 changes: 1 addition & 7 deletions examples/component-demo/CustomStyleDemo.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,7 @@ struct CustomStyleDemo : public sst::jucegui::components::WindowPanel
sources.push_back(std::move(d));
}
}
~SomeSliders()
{
for (const auto &k : knobs)
{
k->setSource(nullptr);
}
}
~SomeSliders() {}
void resized() override
{
auto b = getLocalBounds();
Expand Down
8 changes: 1 addition & 7 deletions examples/component-demo/DraggableTextDemo.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@ struct DraggableTextDemo : public sst::jucegui::components::WindowPanel
sources.push_back(std::move(d));
}
}
~SomeEditors()
{
for (const auto &k : knobs)
{
k->setSource(nullptr);
}
}
~SomeEditors() {}
void resized() override
{
auto b = getLocalBounds();
Expand Down
8 changes: 1 addition & 7 deletions examples/component-demo/HSliderDemo.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,7 @@ struct HSliderDemo : public sst::jucegui::components::WindowPanel
sources.push_back(std::move(d));
}
}
~SomeSliders()
{
for (const auto &k : knobs)
{
k->setSource(nullptr);
}
}
~SomeSliders() {}
void resized() override
{
auto b = getLocalBounds();
Expand Down
8 changes: 1 addition & 7 deletions examples/component-demo/KnobDemo.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,7 @@ struct KnobDemo : public sst::jucegui::components::WindowPanel
sources.push_back(std::move(d));
}
}
~SomeKnobs()
{
for (const auto &k : knobs)
{
k->setSource(nullptr);
}
}
~SomeKnobs() {}
void resized() override
{
auto b = getLocalBounds();
Expand Down
8 changes: 1 addition & 7 deletions examples/component-demo/MixerPrototype.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,7 @@ struct MixerProto : public sst::jucegui::components::WindowPanel
addAndMakeVisible(*lab);
}

~Channel()
{
solo->setSource(nullptr);
pan->setSource(nullptr);
mute->setSource(nullptr);
level->setSource(nullptr);
}
~Channel() {}

std::unique_ptr<cmp::VSlider> level;
std::unique_ptr<cmp::Knob> pan;
Expand Down
8 changes: 1 addition & 7 deletions examples/component-demo/VSliderDemo.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,7 @@ struct VSliderDemo : public sst::jucegui::components::WindowPanel
sources.push_back(std::move(d));
}
}
~SomeSliders()
{
for (const auto &k : knobs)
{
k->setSource(nullptr);
}
}
~SomeSliders() {}
void resized() override
{
auto b = getLocalBounds();
Expand Down
67 changes: 67 additions & 0 deletions include/sst/jucegui/component-adapters/DiscreteToReference.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* sst-juce-gui - an open source library of juce widgets
* built by Surge Synth Team.
*
* Copyright 2023, various authors, as described in the GitHub
* transaction log.
*
* sst-basic-blocks is released under the MIT license, as described
* by "LICENSE.md" in this repository. This means you may use this
* in commercial software if you are a JUCE Licensee. If you use JUCE
* in the open source / GPL3 context, your combined work must be
* released under GPL3.
*
* All source in sst-juce-gui available at
* https://github.com/surge-synthesizer/sst-juce-gui
*/

#ifndef INCLUDE_SST_JUCEGUI_COMPONENT_ADAPTERS_DISCRETETOREFERENCE_H
#define INCLUDE_SST_JUCEGUI_COMPONENT_ADAPTERS_DISCRETETOREFERENCE_H

#include <memory>
#include <type_traits>

#include "sst/jucegui/components/DiscreteParamEditor.h"

namespace sst::jucegui::component_adapters
{
template <typename T, typename V> struct DiscreteToValueReference : data::Discrete
{
static_assert(std::is_integral<V>());
std::unique_ptr<T> widget;
V &underlyer;
static_assert(std::is_base_of<components::DiscreteParamEditor, T>::value);
DiscreteToValueReference(std::unique_ptr<T> &wid, V &und)
: widget(std::move(wid)), underlyer(und)
{
setup();
}
DiscreteToValueReference(V &und) : widget(std::make_unique<T>()), underlyer(und) { setup(); }

void setup() { widget->setSource(this); }

std::string label;
void setLabel(const std::string &s)
{
label = s;
widget->repaint();
}
std::string getLabel() const override { return label; }

std::function<void(V val)> onValueChanged{nullptr};
int getValue() const override { return underlyer; }
void setValueFromGUI(const int &f) override
{
underlyer = f;
if (onValueChanged)
onValueChanged(f);
}
void setValueFromModel(const int &f) override
{
underlyer = f;
widget->repaint();
}
};
} // namespace sst::jucegui::component_adapters

#endif // CONDUIT_DISCRETETOREFERENCE_H
54 changes: 46 additions & 8 deletions include/sst/jucegui/components/ComponentBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define INCLUDE_SST_JUCEGUI_COMPONENTS_COMPONENTBASE_H

#include <functional>
#include <cassert>
#include <juce_gui_basics/juce_gui_basics.h>
#include <sst/jucegui/data/Continuous.h>

Expand Down Expand Up @@ -95,8 +96,10 @@ template <typename T> struct Modulatable : public data::Continuous::DataListener

virtual ~Modulatable()
{
if (source)
source->removeGUIDataListener(this);
if (continuous())
{
continuous()->removeGUIDataListener(this);
}
}

T *asT() { return static_cast<T *>(this); }
Expand All @@ -119,21 +122,56 @@ template <typename T> struct Modulatable : public data::Continuous::DataListener
isEditingMod = b;
asT()->repaint();
}
void setSource(data::ContinunousModulatable *s)

data::Continuous *continuous()
{
if (source)
source->removeGUIDataListener(this);
switch (source.index())
{
case 0:
return std::get<0>(source);
case 1:
return std::get<1>(source);
}
assert(false);
return nullptr;
}
data::ContinunousModulatable *continuousModulatable()
{
if (std::holds_alternative<data::ContinunousModulatable *>(source))
{
return std::get<data::ContinunousModulatable *>(source);
}
return nullptr;
}

template <typename S> void setSource(S *s)
{
if (continuous())
continuous()->removeGUIDataListener(this);
source = s;
if (source)
source->addGUIDataListener(this);
if (continuous())
continuous()->addGUIDataListener(this);
asT()->repaint();
}

void clearSource()
{
if (continuous())
continuous()->removeGUIDataListener(this);
source = (data::ContinunousModulatable *)nullptr;
}

void dataChanged() override { asT()->repaint(); }
void sourceVanished(data::Continuous *s) override
{
assert(s == continuous());
clearSource();
}

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Modulatable<T>)

data::ContinunousModulatable *source{nullptr};
std::variant<data::Continuous *, data::ContinunousModulatable *> source{
(data::Continuous *)nullptr};
bool isEditingMod{false};
ModulationDisplay modulationDisplay{NONE};
};
Expand Down
5 changes: 5 additions & 0 deletions include/sst/jucegui/components/DiscreteParamEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ struct DiscreteParamEditor : public juce::Component,
public data::Discrete::DataListener
{
void dataChanged() override { repaint(); }
void sourceVanished(data::Discrete *d) override
{
assert(d == data);
setSource(nullptr);
}
void setSource(data::Discrete *d)
{
if (data)
Expand Down
27 changes: 24 additions & 3 deletions include/sst/jucegui/data/Continuous.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,44 @@
#include <utility>
#include <string>
#include <unordered_set>
#include <variant>
#include <cassert>

#include "Labeled.h"

namespace sst::jucegui::data
{
struct Continuous : public Labeled
{
virtual ~Continuous() = default;
virtual ~Continuous()
{
supressListenerModification = true;
for (auto *dl : guilisteners)
{
dl->sourceVanished(this);
}
supressListenerModification = false;
};

struct DataListener
{
virtual ~DataListener() = default;
// FIXME - in the future we may want this more fine grained
virtual void dataChanged() = 0;
virtual void sourceVanished(Continuous *) = 0;
};
void addGUIDataListener(DataListener *l) { guilisteners.insert(l); }
void removeGUIDataListener(DataListener *l) { guilisteners.erase(l); }
bool supressListenerModification{false};
void addGUIDataListener(DataListener *l)
{
assert(!supressListenerModification);
if (!supressListenerModification)
guilisteners.insert(l);
}
void removeGUIDataListener(DataListener *l)
{
if (!supressListenerModification)
guilisteners.erase(l);
}
void addModelDataListener(DataListener *l) { modellisteners.insert(l); }
void removeModelDataListener(DataListener *l) { modellisteners.erase(l); }

Expand Down
7 changes: 6 additions & 1 deletion include/sst/jucegui/data/Discrete.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ namespace sst::jucegui::data
{
struct Discrete : public Labeled
{
virtual ~Discrete() = default;
virtual ~Discrete()
{
for (auto *l : guilisteners)
l->sourceVanished(this);
}

struct DataListener
{
virtual ~DataListener() = default;
// FIXME - in the future we may want this more fine grained
virtual void dataChanged() = 0;
virtual void sourceVanished(Discrete *) = 0;
};
void addGUIDataListener(DataListener *l) { guilisteners.insert(l); }
void removeGUIDataListener(DataListener *l) { guilisteners.erase(l); }
Expand Down
Loading

0 comments on commit 4630c13

Please sign in to comment.