Skip to content

Commit

Permalink
Start of a collection of cleanups
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
baconpaul committed Oct 11, 2023
1 parent 31b3f68 commit bcecb58
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 85 deletions.
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
51 changes: 43 additions & 8 deletions include/sst/jucegui/components/ComponentBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ 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 +119,56 @@ template <typename T> struct Modulatable : public data::Continuous::DataListener
isEditingMod = b;
asT()->repaint();
}
void setSource(data::ContinunousModulatable *s)

data::Continuous *continuous()
{
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 (source)
source->removeGUIDataListener(this);
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 == source);
setSource(nullptr);
}
void setSource(data::Discrete *d)
{
if (data)
Expand Down
9 changes: 8 additions & 1 deletion include/sst/jucegui/data/Continuous.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@ namespace sst::jucegui::data
{
struct Continuous : public Labeled
{
virtual ~Continuous() = default;
virtual ~Continuous()
{
for (auto *dl : guilisteners)
{
dl->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(Continuous *) = 0;
};
void addGUIDataListener(DataListener *l) { guilisteners.insert(l); }
void removeGUIDataListener(DataListener *l) { guilisteners.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
44 changes: 23 additions & 21 deletions src/sst/jucegui/components/ContinuousParamEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ void ContinuousParamEditor::mouseDown(const juce::MouseEvent &e)

mouseMode = DRAG;
onBeginEdit();
if (isEditingMod)
mouseDownV0 = source->getModulationValuePM1();
if (isEditingMod && continuousModulatable())
mouseDownV0 = continuousModulatable()->getModulationValuePM1();
else
mouseDownV0 = source->getValue();
mouseDownV0 = continuous()->getValue();
mouseDownY0 = e.position.y;
mouseDownX0 = e.position.x;
}
Expand All @@ -62,7 +62,7 @@ void ContinuousParamEditor::mouseDoubleClick(const juce::MouseEvent &e)
return;

onBeginEdit();
source->setValueFromGUI(source->getDefaultValue());
continuous()->setValueFromGUI(continuous()->getDefaultValue());
onEndEdit();

repaint();
Expand All @@ -79,11 +79,11 @@ void ContinuousParamEditor::mouseDrag(const juce::MouseEvent &e)
float dy = -(e.position.y - mouseDownY0);
float dx = (e.position.x - mouseDownX0);
float d = 0;
float minForScaling = source->getMin();
float maxForScaling = source->getMax();
if (isEditingMod)
float minForScaling = continuous()->getMin();
float maxForScaling = continuous()->getMax();
if (isEditingMod && continuousModulatable())
{
if (source->isModulationBipolar())
if (continuousModulatable()->isModulationBipolar())
minForScaling = -1.0f;
else
minForScaling = 0.0f;
Expand All @@ -100,18 +100,18 @@ void ContinuousParamEditor::mouseDrag(const juce::MouseEvent &e)
}
if (e.mods.isShiftDown())
d = d * 0.1;
if (isEditingMod)
if (isEditingMod && continuousModulatable())
{
if (source->isModulationBipolar())
if (continuousModulatable()->isModulationBipolar())
d = d * 0.5;
auto vn = std::clamp(mouseDownV0 + d, -1.f, 1.f);
source->setModulationValuePM1(vn);
continuousModulatable()->setModulationValuePM1(vn);
mouseDownV0 = vn;
}
else
{
auto vn = std::clamp(mouseDownV0 + d, source->getMin(), source->getMax());
source->setValueFromGUI(vn);
auto vn = std::clamp(mouseDownV0 + d, continuous()->getMin(), continuous()->getMax());
continuous()->setValueFromGUI(vn);
mouseDownV0 = vn;
}
mouseDownX0 = e.position.x;
Expand All @@ -129,35 +129,37 @@ void ContinuousParamEditor::mouseWheelMove(const juce::MouseEvent &e,
return;
onBeginEdit();

if (isEditingMod)
if (isEditingMod && continuousModulatable())
{
// fixme - callibration and sharing
auto d = (wheel.isReversed ? -1 : 1) * wheel.deltaY * (2);
if (e.mods.isShiftDown())
d = d * 0.1;

auto vn = std::clamp(source->getModulationValuePM1() + d, -1.f, 1.f);
source->setModulationValuePM1(vn);
auto vn = std::clamp(continuousModulatable()->getModulationValuePM1() + d, -1.f, 1.f);
continuousModulatable()->setModulationValuePM1(vn);
}
else
{
// fixme - callibration and sharing
auto d = (wheel.isReversed ? -1 : 1) * wheel.deltaY * (source->getMax() - source->getMin());
auto d = (wheel.isReversed ? -1 : 1) * wheel.deltaY *
(continuous()->getMax() - continuous()->getMin());
if (e.mods.isShiftDown())
d = d * 0.1;

auto vn = std::clamp(source->getValue() + d, source->getMin(), source->getMax());
source->setValueFromGUI(vn);
auto vn = std::clamp(continuous()->getValue() + d, continuous()->getMin(),
continuous()->getMax());
continuous()->setValueFromGUI(vn);
}
onEndEdit();
repaint();
}

bool ContinuousParamEditor::processMouseActions()
{
if (!source)
if (!continuous())
return false;
if (source->isHidden())
if (continuous()->isHidden())
return false;
if (!isEnabled())
return false;
Expand Down
19 changes: 10 additions & 9 deletions src/sst/jucegui/components/DraggableTextEditableValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ void DraggableTextEditableValue::setFromEditor()
auto t = underlyingEditor->getText();
if (t.isEmpty())
{
source->setValueFromGUI(source->getDefaultValue());
continuous()->setValueFromGUI(continuous()->getDefaultValue());
}
else
{
source->setValueAsString(t.toStdString());
continuous()->setValueAsString(t.toStdString());
}
underlyingEditor->setVisible(false);
repaint();
Expand All @@ -73,7 +73,7 @@ void DraggableTextEditableValue::paint(juce::Graphics &g)
g.fillRoundedRectangle(getLocalBounds().toFloat(), 3.f);
g.setColour(getColour(Styles::bordercol));
g.drawRoundedRectangle(getLocalBounds().toFloat(), 3.f, 1.f);
if (source && !underlyingEditor->isVisible())
if (continuous() && !underlyingEditor->isVisible())
{
g.setFont(getFont(Styles::labelfont));
if (underlyingEditor->isVisible())
Expand All @@ -82,23 +82,24 @@ void DraggableTextEditableValue::paint(juce::Graphics &g)
g.setColour(getColour(Styles::textoffcol));
else
g.setColour(getColour(Styles::texthoveroffcol));
g.drawText(source->getValueAsString(), getLocalBounds(), juce::Justification::centred);
g.drawText(continuous()->getValueAsString(), getLocalBounds(),
juce::Justification::centred);
}
}

void DraggableTextEditableValue::mouseDown(const juce::MouseEvent &e)
{
onBeginEdit();
valueOnMouseDown = source->getValue();
valueOnMouseDown = continuous()->getValue();
}
void DraggableTextEditableValue::mouseUp(const juce::MouseEvent &e) { onEndEdit(); }
void DraggableTextEditableValue::mouseDrag(const juce::MouseEvent &e)
{
auto d = e.getDistanceFromDragStartY();
auto fac = 0.5f * (e.mods.isShiftDown() ? 0.1f : 1.f);
auto nv = valueOnMouseDown - fac * d * source->getFineQuantizedStepSize();
nv = std::clamp(nv, source->getMin(), source->getMax());
source->setValueFromGUI(nv);
auto nv = valueOnMouseDown - fac * d * continuous()->getFineQuantizedStepSize();
nv = std::clamp(nv, continuous()->getMin(), continuous()->getMax());
continuous()->setValueFromGUI(nv);
repaint();
}
void DraggableTextEditableValue::mouseWheelMove(const juce::MouseEvent &event,
Expand All @@ -109,7 +110,7 @@ void DraggableTextEditableValue::mouseWheelMove(const juce::MouseEvent &event,

void DraggableTextEditableValue::mouseDoubleClick(const juce::MouseEvent &e)
{
underlyingEditor->setText(source->getValueAsString());
underlyingEditor->setText(continuous()->getValueAsString());
underlyingEditor->setVisible(true);
underlyingEditor->selectAll();
underlyingEditor->grabKeyboardFocus();
Expand Down
Loading

0 comments on commit bcecb58

Please sign in to comment.