1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
|
/*
==============================================================================
This file is part of the JUCE framework.
Copyright (c) Raw Material Software Limited
JUCE is an open source framework subject to commercial or open source
licensing.
By downloading, installing, or using the JUCE framework, or combining the
JUCE framework with any other source code, object code, content or any other
copyrightable work, you agree to the terms of the JUCE End User Licence
Agreement, and all incorporated terms including the JUCE Privacy Policy and
the JUCE Website Terms of Service, as applicable, which will bind you. If you
do not agree to the terms of these agreements, we will not license the JUCE
framework to you, and you must discontinue the installation or download
process and cease use of the JUCE framework.
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
Or:
You may also use this code under the terms of the AGPLv3:
https://www.gnu.org/licenses/agpl-3.0.en.html
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
==============================================================================
*/
namespace juce
{
class ParameterListener : private AudioProcessorParameter::Listener,
private AudioProcessorListener,
private Timer
{
public:
ParameterListener (AudioProcessor& proc, AudioProcessorParameter& param)
: processor (proc), parameter (param), isLegacyParam (LegacyAudioParameter::isLegacy (¶m))
{
if (isLegacyParam)
processor.addListener (this);
else
parameter.addListener (this);
startTimer (100);
}
~ParameterListener() override
{
if (isLegacyParam)
processor.removeListener (this);
else
parameter.removeListener (this);
}
AudioProcessorParameter& getParameter() const noexcept
{
return parameter;
}
virtual void handleNewParameterValue() = 0;
private:
//==============================================================================
void parameterValueChanged (int, float) override
{
parameterValueHasChanged = 1;
}
void parameterGestureChanged (int, bool) override {}
//==============================================================================
void audioProcessorParameterChanged (AudioProcessor*, int index, float) override
{
if (index == parameter.getParameterIndex())
parameterValueHasChanged = 1;
}
void audioProcessorChanged (AudioProcessor*, const ChangeDetails&) override {}
//==============================================================================
void timerCallback() override
{
if (parameterValueHasChanged.compareAndSetBool (0, 1))
{
handleNewParameterValue();
startTimerHz (50);
}
else
{
startTimer (jmin (250, getTimerInterval() + 10));
}
}
AudioProcessor& processor;
AudioProcessorParameter& parameter;
Atomic<int> parameterValueHasChanged { 0 };
const bool isLegacyParam;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ParameterListener)
};
class ParameterComponent : public Component,
public ParameterListener
{
public:
using ParameterListener::ParameterListener;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ParameterComponent)
};
//==============================================================================
class BooleanParameterComponent final : public ParameterComponent
{
public:
BooleanParameterComponent (AudioProcessor& proc, AudioProcessorParameter& param)
: ParameterComponent (proc, param)
{
// Set the initial value.
handleNewParameterValue();
button.onClick = [this] { buttonClicked(); };
addAndMakeVisible (button);
}
void paint (Graphics&) override {}
void resized() override
{
auto area = getLocalBounds();
area.removeFromLeft (8);
button.setBounds (area.reduced (0, 10));
}
void handleNewParameterValue() override
{
button.setToggleState (isParameterOn(), dontSendNotification);
}
private:
void buttonClicked()
{
if (isParameterOn() != button.getToggleState())
{
getParameter().beginChangeGesture();
getParameter().setValueNotifyingHost (button.getToggleState() ? 1.0f : 0.0f);
getParameter().endChangeGesture();
}
}
bool isParameterOn() const { return getParameter().getValue() >= 0.5f; }
ToggleButton button;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BooleanParameterComponent)
};
//==============================================================================
class SwitchParameterComponent final : public ParameterComponent
{
public:
SwitchParameterComponent (AudioProcessor& proc, AudioProcessorParameter& param)
: ParameterComponent (proc, param)
{
for (auto& button : buttons)
{
button.setRadioGroupId (293847);
button.setClickingTogglesState (true);
}
buttons[0].setButtonText (getParameter().getText (0.0f, 16));
buttons[1].setButtonText (getParameter().getText (1.0f, 16));
buttons[0].setConnectedEdges (Button::ConnectedOnRight);
buttons[1].setConnectedEdges (Button::ConnectedOnLeft);
// Set the initial value.
buttons[0].setToggleState (true, dontSendNotification);
handleNewParameterValue();
buttons[1].onStateChange = [this] { rightButtonChanged(); };
for (auto& button : buttons)
addAndMakeVisible (button);
}
void paint (Graphics&) override {}
void resized() override
{
auto area = getLocalBounds().reduced (0, 8);
area.removeFromLeft (8);
for (auto& button : buttons)
button.setBounds (area.removeFromLeft (80));
}
void handleNewParameterValue() override
{
bool newState = isParameterOn();
if (buttons[1].getToggleState() != newState)
{
buttons[1].setToggleState (newState, dontSendNotification);
buttons[0].setToggleState (! newState, dontSendNotification);
}
}
private:
void rightButtonChanged()
{
auto buttonState = buttons[1].getToggleState();
if (isParameterOn() != buttonState)
{
getParameter().beginChangeGesture();
if (getParameter().getAllValueStrings().isEmpty())
{
getParameter().setValueNotifyingHost (buttonState ? 1.0f : 0.0f);
}
else
{
// When a parameter provides a list of strings we must set its
// value using those strings, rather than a float, because VSTs can
// have uneven spacing between the different allowed values and we
// want the snapping behaviour to be consistent with what we do with
// a combo box.
auto selectedText = buttons[buttonState ? 1 : 0].getButtonText();
getParameter().setValueNotifyingHost (getParameter().getValueForText (selectedText));
}
getParameter().endChangeGesture();
}
}
bool isParameterOn() const
{
if (getParameter().getAllValueStrings().isEmpty())
return getParameter().getValue() > 0.5f;
auto index = getParameter().getAllValueStrings()
.indexOf (getParameter().getCurrentValueAsText());
if (index < 0)
{
// The parameter is producing some unexpected text, so we'll do
// some linear interpolation.
index = roundToInt (getParameter().getValue());
}
return index == 1;
}
TextButton buttons[2];
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SwitchParameterComponent)
};
//==============================================================================
class ChoiceParameterComponent final : public ParameterComponent
{
public:
ChoiceParameterComponent (AudioProcessor& proc, AudioProcessorParameter& param)
: ParameterComponent (proc, param),
parameterValues (getParameter().getAllValueStrings())
{
box.addItemList (parameterValues, 1);
// Set the initial value.
handleNewParameterValue();
box.onChange = [this] { boxChanged(); };
addAndMakeVisible (box);
}
void paint (Graphics&) override {}
void resized() override
{
auto area = getLocalBounds();
area.removeFromLeft (8);
box.setBounds (area.reduced (0, 10));
}
private:
void handleNewParameterValue() override
{
auto index = parameterValues.indexOf (getParameter().getCurrentValueAsText());
if (index < 0)
{
// The parameter is producing some unexpected text, so we'll do
// some linear interpolation.
index = roundToInt (getParameter().getValue() * (float) (parameterValues.size() - 1));
}
box.setSelectedItemIndex (index, dontSendNotification);
}
void boxChanged()
{
if (getParameter().getCurrentValueAsText() != box.getText())
{
getParameter().beginChangeGesture();
// When a parameter provides a list of strings we must set its
// value using those strings, rather than a float, because VSTs can
// have uneven spacing between the different allowed values.
getParameter().setValueNotifyingHost (getParameter().getValueForText (box.getText()));
getParameter().endChangeGesture();
}
}
ComboBox box;
const StringArray parameterValues;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChoiceParameterComponent)
};
//==============================================================================
class SliderParameterComponent final : public ParameterComponent
{
public:
SliderParameterComponent (AudioProcessor& proc, AudioProcessorParameter& param)
: ParameterComponent (proc, param)
{
if (getParameter().getNumSteps() != AudioProcessor::getDefaultNumParameterSteps())
slider.setRange (0.0, 1.0, 1.0 / (getParameter().getNumSteps() - 1.0));
else
slider.setRange (0.0, 1.0);
slider.setDoubleClickReturnValue (true, param.getDefaultValue());
slider.setScrollWheelEnabled (false);
addAndMakeVisible (slider);
valueLabel.setColour (Label::outlineColourId, slider.findColour (Slider::textBoxOutlineColourId));
valueLabel.setBorderSize ({ 1, 1, 1, 1 });
valueLabel.setJustificationType (Justification::centred);
addAndMakeVisible (valueLabel);
// Set the initial value.
handleNewParameterValue();
slider.onValueChange = [this] { sliderValueChanged(); };
slider.onDragStart = [this] { sliderStartedDragging(); };
slider.onDragEnd = [this] { sliderStoppedDragging(); };
}
void paint (Graphics&) override {}
void resized() override
{
auto area = getLocalBounds().reduced (0, 10);
valueLabel.setBounds (area.removeFromRight (80));
area.removeFromLeft (6);
slider.setBounds (area);
}
void handleNewParameterValue() override
{
if (! isDragging)
{
slider.setValue (getParameter().getValue(), dontSendNotification);
updateTextDisplay();
}
}
private:
void updateTextDisplay()
{
valueLabel.setText (getParameter().getCurrentValueAsText(), dontSendNotification);
}
void sliderValueChanged()
{
auto newVal = (float) slider.getValue();
if (! approximatelyEqual (getParameter().getValue(), newVal))
{
if (! isDragging)
getParameter().beginChangeGesture();
getParameter().setValueNotifyingHost ((float) slider.getValue());
updateTextDisplay();
if (! isDragging)
getParameter().endChangeGesture();
}
}
void sliderStartedDragging()
{
isDragging = true;
getParameter().beginChangeGesture();
}
void sliderStoppedDragging()
{
isDragging = false;
getParameter().endChangeGesture();
}
Slider slider { Slider::LinearHorizontal, Slider::TextEntryBoxPosition::NoTextBox };
Label valueLabel;
bool isDragging = false;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SliderParameterComponent)
};
//==============================================================================
class ParameterDisplayComponent final : public Component,
private AudioProcessorListener,
private AsyncUpdater
{
public:
ParameterDisplayComponent (AudioProcessorEditor& editorIn, AudioProcessorParameter& param)
: editor (editorIn), parameter (param)
{
editor.processor.addListener (this);
parameterName.setText (parameter.getName (128), dontSendNotification);
parameterName.setJustificationType (Justification::centredRight);
parameterName.setInterceptsMouseClicks (false, false);
addAndMakeVisible (parameterName);
parameterLabel.setText (parameter.getLabel(), dontSendNotification);
parameterLabel.setInterceptsMouseClicks (false, false);
addAndMakeVisible (parameterLabel);
addAndMakeVisible (*(parameterComp = createParameterComp (editor.processor)));
setSize (400, 40);
}
~ParameterDisplayComponent() override
{
cancelPendingUpdate();
editor.processor.removeListener (this);
}
void resized() override
{
auto area = getLocalBounds();
parameterName.setBounds (area.removeFromLeft (100));
parameterLabel.setBounds (area.removeFromRight (50));
parameterComp->setBounds (area);
}
void mouseDown (const MouseEvent& e) override
{
if (e.mods.isRightButtonDown())
if (auto* context = editor.getHostContext())
if (auto menu = context->getContextMenuForParameter (¶meter))
menu->getEquivalentPopupMenu().showMenuAsync (PopupMenu::Options().withTargetComponent (this)
.withMousePosition());
}
private:
AudioProcessorEditor& editor;
AudioProcessorParameter& parameter;
Label parameterName, parameterLabel;
std::unique_ptr<ParameterComponent> parameterComp;
std::unique_ptr<ParameterComponent> createParameterComp (AudioProcessor& processor) const
{
// The AU, AUv3 and VST (only via a .vstxml file) SDKs support
// marking a parameter as boolean. If you want consistency across
// all formats then it might be best to use a
// SwitchParameterComponent instead.
if (parameter.isBoolean())
return std::make_unique<BooleanParameterComponent> (processor, parameter);
// Most hosts display any parameter with just two steps as a switch.
if (parameter.getNumSteps() == 2)
return std::make_unique<SwitchParameterComponent> (processor, parameter);
// If we have a list of strings to represent the different states a
// parameter can be in then we should present a dropdown allowing a
// user to pick one of them.
if (! parameter.getAllValueStrings().isEmpty()
&& std::abs (parameter.getNumSteps() - parameter.getAllValueStrings().size()) <= 1)
return std::make_unique<ChoiceParameterComponent> (processor, parameter);
// Everything else can be represented as a slider.
return std::make_unique<SliderParameterComponent> (processor, parameter);
}
void audioProcessorParameterChanged (AudioProcessor*, int, float) override {}
void audioProcessorChanged (AudioProcessor*, const ChangeDetails& details) override
{
if (! details.parameterInfoChanged)
return;
if (MessageManager::getInstance()->isThisTheMessageThread())
handleAsyncUpdate();
else
triggerAsyncUpdate();
}
void handleAsyncUpdate() override
{
parameterName .setText (parameter.getName (128), dontSendNotification);
parameterLabel.setText (parameter.getLabel(), dontSendNotification);
if (auto* p = parameterComp.get())
p->handleNewParameterValue();
}
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ParameterDisplayComponent)
};
//==============================================================================
struct ParamControlItem final : public TreeViewItem
{
ParamControlItem (AudioProcessorEditor& editorIn, AudioProcessorParameter& paramIn)
: editor (editorIn), param (paramIn) {}
bool mightContainSubItems() override { return false; }
std::unique_ptr<Component> createItemComponent() override
{
return std::make_unique<ParameterDisplayComponent> (editor, param);
}
int getItemHeight() const override { return 40; }
AudioProcessorEditor& editor;
AudioProcessorParameter& param;
};
struct ParameterGroupItem final : public TreeViewItem
{
ParameterGroupItem (AudioProcessorEditor& editor, const AudioProcessorParameterGroup& group)
: name (group.getName())
{
for (auto* node : group)
{
if (auto* param = node->getParameter())
if (param->isAutomatable())
addSubItem (new ParamControlItem (editor, *param));
if (auto* inner = node->getGroup())
{
auto groupItem = std::make_unique<ParameterGroupItem> (editor, *inner);
if (groupItem->getNumSubItems() != 0)
addSubItem (groupItem.release());
}
}
}
bool mightContainSubItems() override { return getNumSubItems() > 0; }
std::unique_ptr<Component> createItemComponent() override
{
return std::make_unique<Label> (name, name);
}
String name;
};
//==============================================================================
struct GenericAudioProcessorEditor::Pimpl
{
Pimpl (AudioProcessorEditor& editor)
: legacyParameters (editor.processor, false),
groupItem (editor, legacyParameters.getGroup())
{
const auto numIndents = getNumIndents (groupItem);
const auto width = 400 + view.getIndentSize() * numIndents;
view.setSize (width, 400);
view.setDefaultOpenness (true);
view.setRootItemVisible (false);
view.setRootItem (&groupItem);
}
static int getNumIndents (const TreeViewItem& item)
{
int maxInner = 0;
for (auto i = 0; i < item.getNumSubItems(); ++i)
maxInner = jmax (maxInner, 1 + getNumIndents (*item.getSubItem (i)));
return maxInner;
}
LegacyAudioParametersWrapper legacyParameters;
ParameterGroupItem groupItem;
TreeView view;
};
//==============================================================================
GenericAudioProcessorEditor::GenericAudioProcessorEditor (AudioProcessor& p)
: AudioProcessorEditor (p), pimpl (std::make_unique<Pimpl> (*this))
{
auto* viewport = pimpl->view.getViewport();
setOpaque (true);
addAndMakeVisible (pimpl->view);
setResizable (true, false);
setSize (viewport->getViewedComponent()->getWidth() + viewport->getVerticalScrollBar().getWidth(),
jlimit (125, 400, viewport->getViewedComponent()->getHeight()));
}
GenericAudioProcessorEditor::~GenericAudioProcessorEditor() = default;
void GenericAudioProcessorEditor::paint (Graphics& g)
{
g.fillAll (getLookAndFeel().findColour (ResizableWindow::backgroundColourId));
}
void GenericAudioProcessorEditor::resized()
{
pimpl->view.setBounds (getLocalBounds());
}
} // namespace juce
|