LMMS
Loading...
Searching...
No Matches
juce_ParameterAttachments.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
30 std::function<void (float)> parameterChangedCallback,
31 UndoManager* um)
32 : parameter (param),
33 undoManager (um),
34 setValue (std::move (parameterChangedCallback))
35{
36 parameter.addListener (this);
37}
38
44
49
50void ParameterAttachment::setValueAsCompleteGesture (float newDenormalisedValue)
51{
52 callIfParameterValueChanged (newDenormalisedValue, [this] (float f)
53 {
55 parameter.setValueNotifyingHost (f);
56 endGesture();
57 });
58}
59
61{
62 if (undoManager != nullptr)
63 undoManager->beginNewTransaction();
64
65 parameter.beginChangeGesture();
66}
67
68void ParameterAttachment::setValueAsPartOfGesture (float newDenormalisedValue)
69{
70 callIfParameterValueChanged (newDenormalisedValue, [this] (float f)
71 {
72 parameter.setValueNotifyingHost (f);
73 });
74}
75
77{
78 parameter.endChangeGesture();
79}
80
81template <typename Callback>
82void ParameterAttachment::callIfParameterValueChanged (float newDenormalisedValue,
83 Callback&& callback)
84{
85 const auto newValue = normalise (newDenormalisedValue);
86
87 if (parameter.getValue() != newValue)
88 callback (newValue);
89}
90
92{
93 lastValue = newValue;
94
95 if (MessageManager::getInstance()->isThisTheMessageThread())
96 {
99 }
100 else
101 {
103 }
104}
105
107{
108 if (setValue != nullptr)
109 setValue (parameter.convertFrom0to1 (lastValue));
110}
111
112//==============================================================================
114 Slider& s,
115 UndoManager* um)
116 : slider (s),
117 attachment (param, [this] (float f) { setValue (f); }, um)
118{
119 slider.valueFromTextFunction = [&param] (const String& text) { return (double) param.convertFrom0to1 (param.getValueForText (text)); };
120 slider.textFromValueFunction = [&param] (double value) { return param.getText (param.convertTo0to1 ((float) value), 0); };
121 slider.setDoubleClickReturnValue (true, param.convertFrom0to1 (param.getDefaultValue()));
122
123 auto range = param.getNormalisableRange();
124
125 auto convertFrom0To1Function = [range] (double currentRangeStart,
126 double currentRangeEnd,
127 double normalisedValue) mutable
128 {
129 range.start = (float) currentRangeStart;
130 range.end = (float) currentRangeEnd;
131 return (double) range.convertFrom0to1 ((float) normalisedValue);
132 };
133
134 auto convertTo0To1Function = [range] (double currentRangeStart,
135 double currentRangeEnd,
136 double mappedValue) mutable
137 {
138 range.start = (float) currentRangeStart;
139 range.end = (float) currentRangeEnd;
140 return (double) range.convertTo0to1 ((float) mappedValue);
141 };
142
143 auto snapToLegalValueFunction = [range] (double currentRangeStart,
144 double currentRangeEnd,
145 double mappedValue) mutable
146 {
147 range.start = (float) currentRangeStart;
148 range.end = (float) currentRangeEnd;
149 return (double) range.snapToLegalValue ((float) mappedValue);
150 };
151
152 NormalisableRange<double> newRange { (double) range.start,
153 (double) range.end,
154 std::move (convertFrom0To1Function),
155 std::move (convertTo0To1Function),
156 std::move (snapToLegalValueFunction) };
157 newRange.interval = range.interval;
158 newRange.skew = range.skew;
159 newRange.symmetricSkew = range.symmetricSkew;
160
161 slider.setNormalisableRange (newRange);
162
163 sendInitialUpdate();
164 slider.valueChanged();
165 slider.addListener (this);
166}
167
172
174
176{
178 slider.setValue (newValue, sendNotificationSync);
179}
180
182{
183 if (! ignoreCallbacks)
184 attachment.setValueAsPartOfGesture ((float) slider.getValue());
185}
186
187//==============================================================================
189 ComboBox& c,
190 UndoManager* um)
191 : comboBox (c),
192 storedParameter (param),
193 attachment (param, [this] (float f) { setValue (f); }, um)
194{
195 sendInitialUpdate();
196 comboBox.addListener (this);
197}
198
203
205{
206 attachment.sendInitialUpdate();
207}
208
210{
211 const auto normValue = storedParameter.convertTo0to1 (newValue);
212 const auto index = roundToInt (normValue * (float) (comboBox.getNumItems() - 1));
213
214 if (index == comboBox.getSelectedItemIndex())
215 return;
216
218 comboBox.setSelectedItemIndex (index, sendNotificationSync);
219}
220
222{
223 if (ignoreCallbacks)
224 return;
225
226 const auto numItems = comboBox.getNumItems();
227 const auto selected = (float) comboBox.getSelectedItemIndex();
228 const auto newValue = numItems > 1 ? selected / (float) (numItems - 1)
229 : 0.0f;
230
231 attachment.setValueAsCompleteGesture (storedParameter.convertFrom0to1 (newValue));
232}
233
234//==============================================================================
236 Button& b,
237 UndoManager* um)
238 : button (b),
239 attachment (param, [this] (float f) { setValue (f); }, um)
240{
241 sendInitialUpdate();
242 button.addListener (this);
243}
244
249
251{
252 attachment.sendInitialUpdate();
253}
254
256{
258 button.setToggleState (newValue >= 0.5f, sendNotificationSync);
259}
260
262{
263 if (ignoreCallbacks)
264 return;
265
266 attachment.setValueAsCompleteGesture (button.getToggleState() ? 1.0f : 0.0f);
267}
268
269} // namespace juce
void triggerAsyncUpdate()
Definition juce_AsyncUpdater.cpp:62
void cancelPendingUpdate() noexcept
Definition juce_AsyncUpdater.cpp:74
virtual float getDefaultValue() const =0
virtual String getText(float normalisedValue, int) const
Definition juce_AudioProcessor.cpp:1607
virtual float getValueForText(const String &text) const =0
Definition juce_Button.h:43
ButtonParameterAttachment(RangedAudioParameter &parameter, Button &button, UndoManager *undoManager=nullptr)
Definition juce_ParameterAttachments.cpp:235
ParameterAttachment attachment
Definition juce_ParameterAttachments.h:247
bool ignoreCallbacks
Definition juce_ParameterAttachments.h:248
Button & button
Definition juce_ParameterAttachments.h:246
void setValue(float newValue)
Definition juce_ParameterAttachments.cpp:255
void sendInitialUpdate()
Definition juce_ParameterAttachments.cpp:250
~ButtonParameterAttachment() override
Definition juce_ParameterAttachments.cpp:245
void buttonClicked(Button *) override
Definition juce_ParameterAttachments.cpp:261
Definition juce_ComboBox.h:49
~ComboBoxParameterAttachment() override
Definition juce_ParameterAttachments.cpp:199
RangedAudioParameter & storedParameter
Definition juce_ParameterAttachments.h:206
ParameterAttachment attachment
Definition juce_ParameterAttachments.h:207
void sendInitialUpdate()
Definition juce_ParameterAttachments.cpp:204
ComboBox & comboBox
Definition juce_ParameterAttachments.h:205
bool ignoreCallbacks
Definition juce_ParameterAttachments.h:208
void comboBoxChanged(ComboBox *) override
Definition juce_ParameterAttachments.cpp:221
ComboBoxParameterAttachment(RangedAudioParameter &parameter, ComboBox &combo, UndoManager *undoManager=nullptr)
Definition juce_ParameterAttachments.cpp:188
void setValue(float newValue)
Definition juce_ParameterAttachments.cpp:209
static MessageManager * getInstance()
Definition juce_MessageManager.cpp:47
ValueType interval
Definition juce_NormalisableRange.h:220
void endGesture()
Definition juce_ParameterAttachments.cpp:76
void setValueAsCompleteGesture(float newDenormalisedValue)
Definition juce_ParameterAttachments.cpp:50
void sendInitialUpdate()
Definition juce_ParameterAttachments.cpp:45
void handleAsyncUpdate() override
Definition juce_ParameterAttachments.cpp:106
RangedAudioParameter & parameter
Definition juce_ParameterAttachments.h:114
float normalise(float f) const
Definition juce_ParameterAttachments.h:105
std::atomic< float > lastValue
Definition juce_ParameterAttachments.h:115
void beginGesture()
Definition juce_ParameterAttachments.cpp:60
void setValueAsPartOfGesture(float newDenormalisedValue)
Definition juce_ParameterAttachments.cpp:68
void parameterValueChanged(int, float) override
Definition juce_ParameterAttachments.cpp:91
~ParameterAttachment() override
Definition juce_ParameterAttachments.cpp:39
UndoManager * undoManager
Definition juce_ParameterAttachments.h:116
void callIfParameterValueChanged(float newDenormalisedValue, Callback &&callback)
Definition juce_ParameterAttachments.cpp:82
ParameterAttachment(RangedAudioParameter &parameter, std::function< void(float)> parameterChangedCallback, UndoManager *undoManager=nullptr)
Definition juce_ParameterAttachments.cpp:29
std::function< void(float)> setValue
Definition juce_ParameterAttachments.h:117
Definition juce_RangedAudioParameter.h:98
float convertTo0to1(float v) const noexcept
Definition juce_RangedAudioParameter.cpp:39
virtual const NormalisableRange< float > & getNormalisableRange() const =0
float convertFrom0to1(float v) const noexcept
Definition juce_RangedAudioParameter.cpp:45
Definition juce_ScopedValueSetter.h:55
Definition juce_Slider.h:54
void sendInitialUpdate()
Definition juce_ParameterAttachments.cpp:173
void sliderValueChanged(Slider *) override
Definition juce_ParameterAttachments.cpp:181
~SliderParameterAttachment() override
Definition juce_ParameterAttachments.cpp:168
Slider & slider
Definition juce_ParameterAttachments.h:160
bool ignoreCallbacks
Definition juce_ParameterAttachments.h:162
ParameterAttachment attachment
Definition juce_ParameterAttachments.h:161
void setValue(float newValue)
Definition juce_ParameterAttachments.cpp:175
SliderParameterAttachment(RangedAudioParameter &parameter, Slider &slider, UndoManager *undoManager=nullptr)
Definition juce_ParameterAttachments.cpp:113
Definition juce_String.h:53
Definition juce_UndoManager.h:52
unsigned s
Definition inflate.c:1555
unsigned f
Definition inflate.c:1572
static PuglViewHint int value
Definition pugl.h:1708
Definition carla_juce.cpp:31
@ sendNotificationSync
Definition juce_NotificationType.h:35
int roundToInt(const FloatType value) noexcept
Definition juce_MathsFunctions.h:465
@ slider
Definition juce_AccessibilityRole.h:43
@ comboBox
Definition juce_AccessibilityRole.h:41
@ button
Definition juce_AccessibilityRole.h:38
Definition juce_Uuid.h:141
const char * text
Definition swell-functions.h:167
RECT const char void(* callback)(const char *droppath))) SWELL_API_DEFINE(BOOL
Definition swell-functions.h:1004
return c
Definition crypt.c:175
b
Definition crypt.c:628