Skip to content

Commit

Permalink
Allow mseg editing to be disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
FigBug committed Jan 29, 2024
1 parent d1bb944 commit 4ec9936
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 22 deletions.
76 changes: 57 additions & 19 deletions modules/gin_plugin/components/gin_msegcomponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,25 @@ void MSEGComponent::paramChanged ()
dirty = true;
}

void MSEGComponent::createPath (juce::Rectangle<int> area)
void MSEGComponent::createPath (juce::Rectangle<float> 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();
Expand All @@ -64,16 +74,32 @@ void MSEGComponent::createPath (juce::Rectangle<int> area)

void MSEGComponent::paint (juce::Graphics& g)
{
auto rc = getLocalBounds().reduced (2);
auto rc = getLocalBounds().toFloat().reduced (2);

if (dirty)
{
dirty = false;
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<float> 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);

Expand All @@ -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]);

Expand All @@ -95,7 +121,7 @@ void MSEGComponent::paint (juce::Graphics& g)
}
}

if (isMouseOverOrDragging())
if (editable && isMouseOverOrDragging())
{
for (auto i = 0; i < data.numPoints; i++)
{
Expand Down Expand Up @@ -141,11 +167,6 @@ void MSEGComponent::timerCallback()
}
}

int MSEGComponent::getNumSteps()
{
return 1;
}

float MSEGComponent::valueToY (float v)
{
auto area = getArea();
Expand Down Expand Up @@ -200,14 +221,13 @@ int MSEGComponent::getCurveAt (juce::Point<float> 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)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -314,17 +340,29 @@ 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 ();
}

void MSEGComponent::mouseEnter (const juce::MouseEvent&)
{
if (! editable)
return;

repaint ();
}

void MSEGComponent::mouseExit (const juce::MouseEvent&)
{
if (! editable)
return;

repaint ();
}
10 changes: 7 additions & 3 deletions modules/gin_plugin/components/gin_msegcomponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::vector<float>()> phaseCallback;
std::function<void()> 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;
Expand All @@ -30,9 +34,8 @@ class MSEGComponent : public MultiParamComponent,
void timerCallback() override;
void paramChanged () override;

void createPath (juce::Rectangle<int> area);
void createPath (juce::Rectangle<float> area);
float getSample (float phase);
int getNumSteps();

Parameter::Ptr wave, sync, rate, beat, depth, offset, phase, enable;

Expand All @@ -48,6 +51,7 @@ class MSEGComponent : public MultiParamComponent,
int draggingCurve = -1;

float lastY = 0.0f;
bool editable = false;

juce::Rectangle<float> getArea() { return getLocalBounds().toFloat().reduced (2.0f); }

Expand Down

0 comments on commit 4ec9936

Please sign in to comment.