|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- /*
- ==============================================================================
-
- This file is part of the JUCE 7 technical preview.
- Copyright (c) 2022 - Raw Material Software Limited
-
- You may use this code under the terms of the GPL v3
- (see www.gnu.org/licenses).
-
- For the technical preview this file cannot be licensed commercially.
-
- JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
- EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
- DISCLAIMED.
-
- ==============================================================================
- */
-
- namespace juce
- {
-
- ParameterAttachment::ParameterAttachment (RangedAudioParameter& param,
- std::function<void (float)> parameterChangedCallback,
- UndoManager* um)
- : parameter (param),
- undoManager (um),
- setValue (std::move (parameterChangedCallback))
- {
- parameter.addListener (this);
- }
-
- ParameterAttachment::~ParameterAttachment()
- {
- parameter.removeListener (this);
- cancelPendingUpdate();
- }
-
- void ParameterAttachment::sendInitialUpdate()
- {
- parameterValueChanged ({}, parameter.getValue());
- }
-
- void ParameterAttachment::setValueAsCompleteGesture (float newDenormalisedValue)
- {
- callIfParameterValueChanged (newDenormalisedValue, [this] (float f)
- {
- beginGesture();
- parameter.setValueNotifyingHost (f);
- endGesture();
- });
- }
-
- void ParameterAttachment::beginGesture()
- {
- if (undoManager != nullptr)
- undoManager->beginNewTransaction();
-
- parameter.beginChangeGesture();
- }
-
- void ParameterAttachment::setValueAsPartOfGesture (float newDenormalisedValue)
- {
- callIfParameterValueChanged (newDenormalisedValue, [this] (float f)
- {
- parameter.setValueNotifyingHost (f);
- });
- }
-
- void ParameterAttachment::endGesture()
- {
- parameter.endChangeGesture();
- }
-
- template <typename Callback>
- void ParameterAttachment::callIfParameterValueChanged (float newDenormalisedValue,
- Callback&& callback)
- {
- const auto newValue = normalise (newDenormalisedValue);
-
- if (parameter.getValue() != newValue)
- callback (newValue);
- }
-
- void ParameterAttachment::parameterValueChanged (int, float newValue)
- {
- lastValue = newValue;
-
- if (MessageManager::getInstance()->isThisTheMessageThread())
- {
- cancelPendingUpdate();
- handleAsyncUpdate();
- }
- else
- {
- triggerAsyncUpdate();
- }
- }
-
- void ParameterAttachment::handleAsyncUpdate()
- {
- if (setValue != nullptr)
- setValue (parameter.convertFrom0to1 (lastValue));
- }
-
- //==============================================================================
- SliderParameterAttachment::SliderParameterAttachment (RangedAudioParameter& param,
- Slider& s,
- UndoManager* um)
- : slider (s),
- attachment (param, [this] (float f) { setValue (f); }, um)
- {
- slider.valueFromTextFunction = [¶m] (const String& text) { return (double) param.convertFrom0to1 (param.getValueForText (text)); };
- slider.textFromValueFunction = [¶m] (double value) { return param.getText (param.convertTo0to1 ((float) value), 0); };
- slider.setDoubleClickReturnValue (true, param.convertFrom0to1 (param.getDefaultValue()));
-
- auto range = param.getNormalisableRange();
-
- auto convertFrom0To1Function = [range] (double currentRangeStart,
- double currentRangeEnd,
- double normalisedValue) mutable
- {
- range.start = (float) currentRangeStart;
- range.end = (float) currentRangeEnd;
- return (double) range.convertFrom0to1 ((float) normalisedValue);
- };
-
- auto convertTo0To1Function = [range] (double currentRangeStart,
- double currentRangeEnd,
- double mappedValue) mutable
- {
- range.start = (float) currentRangeStart;
- range.end = (float) currentRangeEnd;
- return (double) range.convertTo0to1 ((float) mappedValue);
- };
-
- auto snapToLegalValueFunction = [range] (double currentRangeStart,
- double currentRangeEnd,
- double mappedValue) mutable
- {
- range.start = (float) currentRangeStart;
- range.end = (float) currentRangeEnd;
- return (double) range.snapToLegalValue ((float) mappedValue);
- };
-
- NormalisableRange<double> newRange { (double) range.start,
- (double) range.end,
- std::move (convertFrom0To1Function),
- std::move (convertTo0To1Function),
- std::move (snapToLegalValueFunction) };
- newRange.interval = range.interval;
- newRange.skew = range.skew;
- newRange.symmetricSkew = range.symmetricSkew;
-
- slider.setNormalisableRange (newRange);
-
- sendInitialUpdate();
- slider.valueChanged();
- slider.addListener (this);
- }
-
- SliderParameterAttachment::~SliderParameterAttachment()
- {
- slider.removeListener (this);
- }
-
- void SliderParameterAttachment::sendInitialUpdate() { attachment.sendInitialUpdate(); }
-
- void SliderParameterAttachment::setValue (float newValue)
- {
- const ScopedValueSetter<bool> svs (ignoreCallbacks, true);
- slider.setValue (newValue, sendNotificationSync);
- }
-
- void SliderParameterAttachment::sliderValueChanged (Slider*)
- {
- if (! ignoreCallbacks)
- attachment.setValueAsPartOfGesture ((float) slider.getValue());
- }
-
- //==============================================================================
- ComboBoxParameterAttachment::ComboBoxParameterAttachment (RangedAudioParameter& param,
- ComboBox& c,
- UndoManager* um)
- : comboBox (c),
- storedParameter (param),
- attachment (param, [this] (float f) { setValue (f); }, um)
- {
- sendInitialUpdate();
- comboBox.addListener (this);
- }
-
- ComboBoxParameterAttachment::~ComboBoxParameterAttachment()
- {
- comboBox.removeListener (this);
- }
-
- void ComboBoxParameterAttachment::sendInitialUpdate()
- {
- attachment.sendInitialUpdate();
- }
-
- void ComboBoxParameterAttachment::setValue (float newValue)
- {
- const auto normValue = storedParameter.convertTo0to1 (newValue);
- const auto index = roundToInt (normValue * (float) (comboBox.getNumItems() - 1));
-
- if (index == comboBox.getSelectedItemIndex())
- return;
-
- const ScopedValueSetter<bool> svs (ignoreCallbacks, true);
- comboBox.setSelectedItemIndex (index, sendNotificationSync);
- }
-
- void ComboBoxParameterAttachment::comboBoxChanged (ComboBox*)
- {
- if (ignoreCallbacks)
- return;
-
- const auto numItems = comboBox.getNumItems();
- const auto selected = (float) comboBox.getSelectedItemIndex();
- const auto newValue = numItems > 1 ? selected / (float) (numItems - 1)
- : 0.0f;
-
- attachment.setValueAsCompleteGesture (storedParameter.convertFrom0to1 (newValue));
- }
-
- //==============================================================================
- ButtonParameterAttachment::ButtonParameterAttachment (RangedAudioParameter& param,
- Button& b,
- UndoManager* um)
- : button (b),
- attachment (param, [this] (float f) { setValue (f); }, um)
- {
- sendInitialUpdate();
- button.addListener (this);
- }
-
- ButtonParameterAttachment::~ButtonParameterAttachment()
- {
- button.removeListener (this);
- }
-
- void ButtonParameterAttachment::sendInitialUpdate()
- {
- attachment.sendInitialUpdate();
- }
-
- void ButtonParameterAttachment::setValue (float newValue)
- {
- const ScopedValueSetter<bool> svs (ignoreCallbacks, true);
- button.setToggleState (newValue >= 0.5f, sendNotificationSync);
- }
-
- void ButtonParameterAttachment::buttonClicked (Button*)
- {
- if (ignoreCallbacks)
- return;
-
- attachment.setValueAsCompleteGesture (button.getToggleState() ? 1.0f : 0.0f);
- }
-
- } // namespace juce
|