Skip to content

Commit

Permalink
- added drag to change values in sampler workspace
Browse files Browse the repository at this point in the history
- changed loop button appearance
- prevent negative loop ranges when clicking on sample area
  • Loading branch information
christoph-hart committed Aug 4, 2023
1 parent 00aeadf commit a512f68
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 58 deletions.
7 changes: 4 additions & 3 deletions hi_components/audio_components/SampleComponents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -814,9 +814,10 @@ void SamplerSoundWaveform::mouseDown(const MouseEvent& e)
return;

auto r = currentSound->getPropertyRange(propId);

value = jlimit(r.getStart(), r.getEnd(), value);


if (!r.contains(value))
return;

currentSound->setSampleProperty(propId, value, true);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion hi_sampler/sampler/components/SampleEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ String SampleEditor::getNameForCommand(SampleMapCommands c, bool on)
case SampleMapCommands::EnableSampleStartArea: return on ? "samplestart-area" : "";
case SampleMapCommands::EnableLoopArea: return on ? "loop-area" : "";
case SampleMapCommands::NormalizeVolume: return on ? "normalise-on" : "normalise-off";
case SampleMapCommands::LoopEnabled: return on ? "loop-on" : "loop-off";
case SampleMapCommands::LoopEnabled: return on ? "loop-on" : "loop-on";
case SampleMapCommands::ExternalEditor: return on ? "external" : "";
case SampleMapCommands::ZeroCrossing: return on ? "zero" : "";
case SampleMapCommands::ImproveLoopPoints: return on ? "improve-loop" : "";
Expand Down
146 changes: 92 additions & 54 deletions hi_sampler/sampler/components/ValueSettingComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct ValueSettingComponent::ValueSlider : public Component
parentC->addAndMakeVisible(this);

auto b = parentC->getLocalArea(c, c->getLocalBounds());
b = b.withSizeKeepingCentre(600, 32).translated(0, -40);
b = b.withSizeKeepingCentre(600, 40).translated(0, -48);
setWantsKeyboardFocus(false);
setBounds(b);
}
Expand All @@ -79,14 +79,16 @@ struct ValueSettingComponent::ValueSlider : public Component

void paint(Graphics& g) override
{
g.setColour(Colour(0x88222222));
g.setColour(Colour(0xDD222222));
g.fillAll();
g.setColour(Colours::white.withAlpha(0.2f));
g.drawRect(getLocalBounds().toFloat().reduced(1.0f), 1.0f);

auto b = getLocalBounds().toFloat().reduced(3.0f);
auto b = getLocalBounds().toFloat().reduced(8.0f, 3.0f);
g.setFont(GLOBAL_FONT());



g.drawText(String(roundToInt(slider.getValue())), b, Justification::centredTop);
g.drawText(String(fullRange.getStart()), b, Justification::topLeft);
g.drawText(String(fullRange.getEnd()), b, Justification::topRight);
Expand Down Expand Up @@ -258,6 +260,8 @@ void ValueSettingComponent::paint (Graphics& g)

void ValueSettingComponent::resized()
{


auto b = getLocalBounds().reduced(2, 0);

descriptionLabel->setBounds(b.removeFromTop(JUCE_LIVE_CONSTANT_OFF(15)));
Expand Down Expand Up @@ -333,6 +337,63 @@ void ValueSettingComponent::buttonClicked (Button* buttonThatWasClicked)
//[/UserbuttonClicked_Post]
}

void ValueSettingComponent::mouseDrag(const MouseEvent& e)
{
if (e.mods.isRightButtonDown())
return;

auto distX = e.getDistanceFromDragStartX();
auto distY = e.getDistanceFromDragStartY();

auto distToUse = 0;

if (hmath::abs(distX) > hmath::abs(distY))
distToUse = distX / 2;
else
distToUse = distY * -1;

auto normed = (double)distToUse / (400.0);

for (int i = 0; i < currentSelection.size(); i++)
{
auto r = downRanges[i];

NormalisableRange<double> nr(r.getStart(), r.getEnd());

auto startValue = downValues[i];
auto newValue = jlimit(0.0, 1.0, nr.convertTo0to1(startValue) + normed);
newValue = nr.convertFrom0to1(newValue);
currentSelection[i]->setSampleProperty(soundProperty, newValue);
}

updateValue();
}

void ValueSettingComponent::mouseDoubleClick(const MouseEvent& event)
{
if (event.mods.isRightButtonDown())
return;

for(auto s: currentSelection)
{
s->setSampleProperty(soundProperty, s->getDefaultValue(soundProperty));
}

updateValue();
}

void ValueSettingComponent::Dismisser::mouseDown(const MouseEvent& e)
{
auto isParentOrSame = [&](Component* parent)
{
return e.eventComponent == parent || parent->isParentOf(e.eventComponent);
};

if (isParentOrSame(&parent) || isParentOrSame(parent.newSlider))
return;

parent.resetValueSlider();
}


//[MiscUserCode] You can add your own definitions of your custom methods or any other code here...
Expand All @@ -356,67 +417,44 @@ void ValueSettingComponent::mouseDown(const MouseEvent &e)
}
#endif

if(currentSelection.size() != 0 && e.mods.isRightButtonDown())
if(!currentSelection.isEmpty())
{
if (newSlider != nullptr)
downValues.clearQuick();
downRanges.clearQuick();

for(auto s: currentSelection)
{
resetValueSlider();
return;
downValues.add((int)s->getSampleProperty(soundProperty));
downRanges.add(s->getPropertyRange(soundProperty));
}

auto se = getSubEditorComponent(this);

callRecursive<ValueSettingComponent>(se, [](ValueSettingComponent* v)
if(e.mods.isRightButtonDown())
{
v->resetValueSlider();
return false;
});


newSlider = new ValueSlider(this);
descriptionLabel->setFont(GLOBAL_BOLD_FONT());
descriptionLabel->setColour(Label::textColourId, Colours::white);
if (newSlider != nullptr)
{
resetValueSlider();
return;
}

#if 0
Slider *s = new Slider();


auto se = getSubEditorComponent(this);

s->setSliderStyle(Slider::LinearBar);
s->setTextBoxStyle(Slider::NoTextBox, true, 0, 0);
s->setRange(currentSelection[0]->getPropertyRange(soundProperty).getStart(),
currentSelection[0]->getPropertyRange(soundProperty).getEnd(),
1);
s->setTextBoxStyle (Slider::NoTextBox, false, 80, 20);
s->setColour (Slider::thumbColourId, Colours::white);
s->setColour (Slider::rotarySliderOutlineColourId, Colour (0x00000000));
s->setColour (Slider::textBoxOutlineColourId, Colour (0x00808080));
s->addListener (this);

s->setLookAndFeel(&laf);

currentSlider = s;

s->setWantsKeyboardFocus(false);

if(currentSelection.size() != 0)
{
s->setValue(currentSelection[0]->getSampleProperty(soundProperty), dontSendNotification);
}
else
{
s->setEnabled(false);
}

s->setSize (600, 32);

if (auto root = TopLevelWindowWithOptionalOpenGL::findRoot(this))
{
auto rootPos = root->getLocalArea(getParentComponent(), getBoundsInParent());
if(dismisser == nullptr)
{
dismisser = new Dismisser(*this, se);
}

callRecursive<ValueSettingComponent>(se, [](ValueSettingComponent* v)
{
v->resetValueSlider();
return false;
});

auto& cb = CallOutBox::launchAsynchronously(std::unique_ptr<Component>(s), rootPos, root);
cb.setWantsKeyboardFocus(false);
newSlider = new ValueSlider(this);
descriptionLabel->setFont(GLOBAL_BOLD_FONT());
descriptionLabel->setColour(Label::textColourId, Colours::white);
}
#endif
}
}

Expand Down
29 changes: 29 additions & 0 deletions hi_sampler/sampler/components/ValueSettingComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,33 @@ class ValueSettingComponent : public Component,
void labelTextChanged (Label* labelThatHasChanged);
void buttonClicked (Button* buttonThatWasClicked);

void mouseDrag(const MouseEvent& event) override;

void mouseDoubleClick(const MouseEvent& event) override;

struct Dismisser: public juce::MouseListener
{
Dismisser(ValueSettingComponent& parent_, Component* mainParent_):
mainParent(mainParent_),
parent(parent_)
{
mainParent->addMouseListener(this, true);
}

~Dismisser()
{
if (mainParent.getComponent() != nullptr)
mainParent->removeMouseListener(this);
}

void mouseDown(const MouseEvent& e) override;

Component::SafePointer<Component> mainParent;
ValueSettingComponent& parent;
};

ScopedPointer<Dismisser> dismisser;

private:

ChainBarButtonLookAndFeel cb;
Expand All @@ -218,6 +245,8 @@ class ValueSettingComponent : public Component,

SampleSelection currentSelection;

Array<int> downValues;
Array<Range<int>> downRanges;
Array<int> dragStartValues;

int sliderStartValue;
Expand Down

0 comments on commit a512f68

Please sign in to comment.