Skip to content

Commit

Permalink
Add snap to mseg
Browse files Browse the repository at this point in the history
  • Loading branch information
FigBug committed Feb 3, 2024
1 parent d9d93b5 commit 752f338
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
53 changes: 45 additions & 8 deletions modules/gin_plugin/components/gin_msegcomponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ void MSEGComponent::resized()

void MSEGComponent::setParams (Parameter::Ptr wave_, Parameter::Ptr sync_, Parameter::Ptr rate_,
Parameter::Ptr beat_, Parameter::Ptr depth_, Parameter::Ptr offset_,
Parameter::Ptr phase_, Parameter::Ptr enable_)
Parameter::Ptr phase_, Parameter::Ptr enable_, Parameter::Ptr xgrid_,
Parameter::Ptr ygrid_)
{
unwatchParams();

Expand All @@ -23,6 +24,8 @@ void MSEGComponent::setParams (Parameter::Ptr wave_, Parameter::Ptr sync_, Param
watchParam (offset = offset_);
watchParam (phase = phase_);
watchParam (enable = enable_);
watchParam (xgrid = xgrid_);
watchParam (ygrid = ygrid_);

startTimerHz (30);
}
Expand Down Expand Up @@ -88,11 +91,11 @@ void MSEGComponent::paint (juce::Graphics& g)

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()});
}
for (int i = 0; i <= ygrid->getUserValueInt(); i++)
rects.add ({rc.getX(), rc.getY() + i * rc.getHeight() / ygrid->getUserValueInt(), rc.getWidth(), 1});

for (int i = 0; i <= xgrid->getUserValueInt(); i++)
rects.add ({rc.getX() + i * rc.getWidth() / xgrid->getUserValueInt(), rc.getY(), 1, rc.getHeight()});
}
else
{
Expand Down Expand Up @@ -305,8 +308,8 @@ void MSEGComponent::mouseDrag (const juce::MouseEvent& e)
if (draggingPoint < data.numPoints - 1)
maxT = data.points[draggingPoint + 1].time;

p.time = std::clamp (xToTime (e.position.x), minT, maxT);
p.value = std::clamp (yToValue (e.position.y), -1.0f, 1.0f);
p.time = snapT (std::clamp (xToTime (e.position.x), minT, maxT));
p.value = snapV (std::clamp (yToValue (e.position.y), -1.0f, 1.0f));

if (draggingPoint == 0)
{
Expand Down Expand Up @@ -422,3 +425,37 @@ void MSEGComponent::hideBubbleMessage()
{
bubbleMessage = nullptr;
}

float MSEGComponent::snapT (float t)
{
if (juce::ModifierKeys::currentModifiers.isShiftDown())
return t;

auto d = 1.0f / getWidth() * 10.0f;

for (int i = 0; i <= xgrid->getUserValueInt(); i++)
{
auto step = i * 1.0f / xgrid->getUserValueInt();
if (std::abs (step - t) < d)
return step;
}

return t;
}

float MSEGComponent::snapV (float v)
{
if (juce::ModifierKeys::currentModifiers.isShiftDown())
return v;

auto d = 1.0f / getHeight() * 10.0f;

for (int i = 0; i <= xgrid->getUserValueInt(); i++)
{
auto step = -1.0f + i * 2.0f / xgrid->getUserValueInt();
if (std::abs (step - v) < d)
return step;
}

return v;
}
8 changes: 6 additions & 2 deletions modules/gin_plugin/components/gin_msegcomponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class MSEGComponent : public MultiParamComponent,

void setParams (Parameter::Ptr wave, Parameter::Ptr sync, Parameter::Ptr rate,
Parameter::Ptr beat, Parameter::Ptr depth, Parameter::Ptr offset,
Parameter::Ptr phase, Parameter::Ptr enable);
Parameter::Ptr phase, Parameter::Ptr enable, Parameter::Ptr xgrid,
Parameter::Ptr ygrid);

void setEditable (bool e) { editable = e; }

Expand All @@ -23,6 +24,9 @@ class MSEGComponent : public MultiParamComponent,
void resized() override;

private:
float snapT (float t);
float snapV (float v);

void mouseDown (const juce::MouseEvent&) override;
void mouseDrag (const juce::MouseEvent&) override;
void mouseUp (const juce::MouseEvent&) override;
Expand All @@ -39,7 +43,7 @@ class MSEGComponent : public MultiParamComponent,
void createPath (juce::Rectangle<float> area);
float getSample (float phase);

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

MSEG::Data& data;
MSEG mseg {data};
Expand Down

0 comments on commit 752f338

Please sign in to comment.