From 4ec9936908a3f619234eb68ea4b6a3b20efe0b01 Mon Sep 17 00:00:00 2001 From: Roland Rabien Date: Sun, 28 Jan 2024 16:44:49 -0800 Subject: [PATCH] Allow mseg editing to be disabled --- .../components/gin_msegcomponent.cpp | 76 ++++++++++++++----- .../gin_plugin/components/gin_msegcomponent.h | 10 ++- 2 files changed, 64 insertions(+), 22 deletions(-) diff --git a/modules/gin_plugin/components/gin_msegcomponent.cpp b/modules/gin_plugin/components/gin_msegcomponent.cpp index a32b0918fe..f551cbfca4 100644 --- a/modules/gin_plugin/components/gin_msegcomponent.cpp +++ b/modules/gin_plugin/components/gin_msegcomponent.cpp @@ -33,15 +33,25 @@ void MSEGComponent::paramChanged () dirty = true; } -void MSEGComponent::createPath (juce::Rectangle area) +void MSEGComponent::createPath (juce::Rectangle area) { mseg.setSampleRate ((double) area.getWidth()); MSEG::Parameters p; - p.frequency = 1.0f * getNumSteps(); - p.phase = phase->getProcValue(); - p.offset = offset->getProcValue(); - p.depth = depth->getProcValue(); + p.frequency = 1.0f; + + if (! editable) + { + p.phase = phase->getProcValue(); + p.offset = offset->getProcValue(); + p.depth = depth->getProcValue(); + } + else + { + p.phase = 0.0f; + p.offset = 0.0f; + p.depth = 1.0f; + } mseg.setParameters (p); mseg.reset(); @@ -64,7 +74,7 @@ void MSEGComponent::createPath (juce::Rectangle area) void MSEGComponent::paint (juce::Graphics& g) { - auto rc = getLocalBounds().reduced (2); + auto rc = getLocalBounds().toFloat().reduced (2); if (dirty) { @@ -72,8 +82,24 @@ void MSEGComponent::paint (juce::Graphics& g) createPath (rc); } - g.setColour (dimIfNeeded (findColour (GinLookAndFeel::whiteColourId).withAlpha (0.3f))); - g.fillRect (rc.getX(), rc.getCentreY(), rc.getWidth(), 1); + g.setColour (dimIfNeeded (findColour (GinLookAndFeel::whiteColourId).withAlpha (0.1f))); + + juce::RectangleList rects; + + if (editable) + { + for (int i = 0; i <= 8; i++) + { + rects.add ({rc.getX(), rc.getY() + i * rc.getHeight() / 8, rc.getWidth(), 1}); + rects.add ({rc.getX() + i * rc.getWidth() / 8, rc.getY(), 1, rc.getHeight()}); + } + } + else + { + rects.add ({rc.getX(), rc.getCentreY(), rc.getWidth(), 1}); + } + + g.fillRectList (rects); auto c = findColour (GinLookAndFeel::accentColourId).withAlpha (0.7f); @@ -86,7 +112,7 @@ void MSEGComponent::paint (juce::Graphics& g) for (auto curPhase : curPhases) { - float x = std::fmod (curPhase / getNumSteps(), 1.0f) * rc.getWidth(); + float x = curPhase * rc.getWidth(); float t = x - int (x); float y = lerp (t, curve[int(x)], curve[int(x) + 1]); @@ -95,7 +121,7 @@ void MSEGComponent::paint (juce::Graphics& g) } } - if (isMouseOverOrDragging()) + if (editable && isMouseOverOrDragging()) { for (auto i = 0; i < data.numPoints; i++) { @@ -141,11 +167,6 @@ void MSEGComponent::timerCallback() } } -int MSEGComponent::getNumSteps() -{ - return 1; -} - float MSEGComponent::valueToY (float v) { auto area = getArea(); @@ -200,14 +221,13 @@ int MSEGComponent::getCurveAt (juce::Point p) void MSEGComponent::mouseDown (const juce::MouseEvent& e) { + if (! editable) + return; + if ((draggingPoint = getPointAt (e.position)) >= 0) - { repaint (); - } else if ((draggingCurve = getCurveAt (e.position)) >= 0) - { repaint (); - } if (e.getNumberOfClicks() == 2) { @@ -261,11 +281,17 @@ void MSEGComponent::mouseDown (const juce::MouseEvent& e) void MSEGComponent::mouseMove (const juce::MouseEvent& e) { + if (! editable) + return; + repaint(); } void MSEGComponent::mouseDrag (const juce::MouseEvent& e) { + if (! editable) + return; + if (draggingPoint >= 0) { auto& p = data.points.getReference (draggingPoint); @@ -314,6 +340,12 @@ void MSEGComponent::mouseDrag (const juce::MouseEvent& e) void MSEGComponent::mouseUp (const juce::MouseEvent& e) { + if (onClick && e.mouseWasClicked()) + onClick(); + + if (! editable) + return; + draggingPoint = -1; draggingCurve = -1; repaint (); @@ -321,10 +353,16 @@ void MSEGComponent::mouseUp (const juce::MouseEvent& e) void MSEGComponent::mouseEnter (const juce::MouseEvent&) { + if (! editable) + return; + repaint (); } void MSEGComponent::mouseExit (const juce::MouseEvent&) { + if (! editable) + return; + repaint (); } diff --git a/modules/gin_plugin/components/gin_msegcomponent.h b/modules/gin_plugin/components/gin_msegcomponent.h index 67c0e94f21..451e1b5c5e 100644 --- a/modules/gin_plugin/components/gin_msegcomponent.h +++ b/modules/gin_plugin/components/gin_msegcomponent.h @@ -14,12 +14,16 @@ class MSEGComponent : public MultiParamComponent, Parameter::Ptr beat, Parameter::Ptr depth, Parameter::Ptr offset, Parameter::Ptr phase, Parameter::Ptr enable); + void setEditable (bool e) { editable = e; } + std::function()> phaseCallback; + std::function onClick; -private: void paint (juce::Graphics& g) override; void resized() override; +private: + void mouseDown (const juce::MouseEvent&) override; void mouseDrag (const juce::MouseEvent&) override; void mouseUp (const juce::MouseEvent&) override; @@ -30,9 +34,8 @@ class MSEGComponent : public MultiParamComponent, void timerCallback() override; void paramChanged () override; - void createPath (juce::Rectangle area); + void createPath (juce::Rectangle area); float getSample (float phase); - int getNumSteps(); Parameter::Ptr wave, sync, rate, beat, depth, offset, phase, enable; @@ -48,6 +51,7 @@ class MSEGComponent : public MultiParamComponent, int draggingCurve = -1; float lastY = 0.0f; + bool editable = false; juce::Rectangle getArea() { return getLocalBounds().toFloat().reduced (2.0f); }