-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIHelpers.h
More file actions
702 lines (589 loc) · 29.3 KB
/
UIHelpers.h
File metadata and controls
702 lines (589 loc) · 29.3 KB
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
#pragma once
#include <juce_core/juce_core.h>
#include <juce_audio_processors/juce_audio_processors.h>
#include <juce_dsp/juce_dsp.h>
#include <juce_gui_basics/juce_gui_basics.h>
namespace moiraesoftware {
inline juce::Rectangle<int>
extractTileByNumber (const juce::Image& mosaic, int tileWidth, int tileHeight, int n) {
int numColumns = mosaic.getWidth() / tileWidth;
// Calculate row and column for the image n (0-based index n)
int row = n / numColumns;
int column = n % numColumns;
// Calculate the coordinates of the desired tile
int x = column * tileWidth;
int y = row * tileHeight;
// Return the desired tile from the mosaic
return { x, y, tileWidth, tileHeight };
}
enum class RadioButtonParameterType { IndexBased, ComponentIdBased };
/*
To implement a new attachment type, create a new class which includes an instance of this class as a data member.
* Your class should pass a function to the constructor of the ParameterAttachment, which will then be called on the
* message thread when the parameter changes. You can use this function to update the state of the UI control.
* Your class should also register as a listener of the UI control and respond to changes in the UI element by
* calling either setValueAsCompleteGesture or beginGesture, setValueAsPartOfGesture and endGesture.
Make sure to call sendInitialUpdate at the end of your new attachment's constructor, so that the UI immediately
reflects the state of the parameter.*/
class RadioButtonParameterAttachment : juce::Button::Listener {
public:
// Creates a connection between a plug-in parameter and some radio buttons.
RadioButtonParameterAttachment (juce::RangedAudioParameter& param,
const juce::Array<juce::Button*>& _buttons,
const int groupID,
juce::UndoManager* undoManager,
const RadioButtonParameterType type = RadioButtonParameterType::IndexBased) :
storedParameter (param),
attachment (
param,
[this] (const float newValue) { setValue (newValue); },
undoManager),
radioButtonType (type) {
for (int i = 0; i < _buttons.size(); ++i) {
auto button = _buttons.getUnchecked (i);
if (!buttons.contains (button)) {
if (groupID > 0) {
button->setRadioGroupId (groupID);
}
button->setClickingTogglesState (true);
buttons.add (button);
button->addListener (this);
}
}
attachment.sendInitialUpdate();
}
~RadioButtonParameterAttachment() override {
for (int i = 0; i < buttons.size(); ++i) {
juce::Button* button = buttons.getUnchecked (i);
button->removeListener (this);
}
}
juce::Button* getButton (const int index) const { return buttons.getUnchecked (index); }
juce::Array<juce::Component::SafePointer<juce::Button>> getButtons() { return buttons; }
int numButtons() const { return buttons.size(); }
// place all buttons in a row with specified margin
void setBounds (const int x, const int y, const int width, const int height, const int margin) const {
for (int i = 0; i < buttons.size(); i++) {
buttons.getUnchecked (i)->setBounds (x + margin * i, y, width, height);
}
}
[[nodiscard]] juce::RangedAudioParameter& getParam() const { return storedParameter; }
private:
void setValueUsingIndex() {
const juce::ScopedValueSetter<bool> svs (ignoreCallbacks, true);
auto button = buttons[static_cast<int> (value)];
button->setToggleState (true, juce::sendNotification);
}
void buttonClickUseIndex (juce::Button* b) {
for (int i = 0; i < buttons.size(); i++) {
if (b == buttons.getUnchecked (i) && b->getToggleState()) {
//the value to set comes from the buttons index in the array 0-<no of buttons>
const auto newValue = static_cast<float> (i);
attachment.setValueAsCompleteGesture (newValue);
}
}
}
void setValueUsingComponentId() {
const juce::ScopedValueSetter<bool> svs (ignoreCallbacks, true);
{
auto is_valueMatch = [this] (juce::Component::SafePointer<juce::Button> b) {
const auto componentName = b->getName();
const auto buttonComponentNameValue = componentName.getFloatValue();
const auto match = value == buttonComponentNameValue;
return match;
};
auto matchedButton = std::find_if (buttons.begin(), buttons.end(), is_valueMatch);
if (matchedButton != std::end (buttons)) {
auto component = matchedButton->getComponent();
if (component) {
component->setToggleState (true, juce::sendNotification);
}
} else {
//There is no match so toggle all buttons to off
std::ranges::for_each (buttons, [this] (const juce::Component::SafePointer<juce::Button>& b) {
b->setToggleState (false, juce::NotificationType::dontSendNotification);
});
}
}
}
void buttonClickUseComponentId (juce::Button* b) {
for (int i = 0; i < buttons.size(); i++) {
if (b == buttons.getUnchecked (i) && b->getToggleState()) {
//the value to set comes from the componentId for the button, yuck! Alternatively we could use a tuple passed in with the
// button, the second value in the tuple could be an enum with a value which is then cast to float, or just the float value.
const auto newValue = b->getName().getFloatValue();
auto existingValue = storedParameter.convertFrom0to1 (storedParameter.getValue());
if (newValue != existingValue) {
attachment.setValueAsCompleteGesture (newValue);
} else {
//if this is setting the value to what it was then we need to reset it to a known default, so we use the default value
// for this. We could assign a reset value if we ever need a default and reset. This would onyl really be needed if
// the default was say 3, and when you re-clicked this radio button you wanted it to goto 0 or another value. We can
// revisit this if needed...
const auto defaultValue = storedParameter.getDefaultValue();
attachment.setValueAsCompleteGesture (defaultValue);
}
}
}
}
void setValue (float newValue) {
value = newValue;
switch (radioButtonType) {
case RadioButtonParameterType::IndexBased:
setValueUsingIndex();
break;
case RadioButtonParameterType::ComponentIdBased:
setValueUsingComponentId();
break;
}
}
void buttonClicked (juce::Button* b) override {
if (ignoreCallbacks) {
return;
}
switch (radioButtonType) {
case RadioButtonParameterType::IndexBased:
buttonClickUseIndex (b);
break;
case RadioButtonParameterType::ComponentIdBased:
buttonClickUseComponentId (b);
break;
}
}
void buttonStateChanged (juce::Button* b) override {}
// if (ignoreCallbacks) {
// return;
// }
// state change occurs on mouse over and mouse down etc. So we don't want to toggle in this callback
// for (int i = 0; i < buttons.size(); i++) {
// if (b == buttons.getUnchecked(i) && b->getToggleState()) {
// attachment.setValueAsCompleteGesture(static_cast<float>(i));
// }
// }
// }
float value {};
juce::RangedAudioParameter& storedParameter;
juce::ParameterAttachment attachment;
juce::Array<juce::Component::SafePointer<juce::Button>> buttons;
bool ignoreCallbacks = false;
RadioButtonParameterType radioButtonType;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RadioButtonParameterAttachment)
};
class ComponentWithParamMenu : public juce::Component {
public:
ComponentWithParamMenu (juce::AudioProcessorEditor& editorIn, juce::RangedAudioParameter& paramIn) :
editor (editorIn), param (paramIn) {}
void mouseUp (const juce::MouseEvent& e) override {
if (!e.mods.isRightButtonDown()) return;
const auto* hostContext = editor.getHostContext();
if (!hostContext) return;
const auto menuInfo = hostContext->getContextMenuForParameter (¶m);
if (!menuInfo) return;
try {
auto popupMenu = menuInfo->getEquivalentPopupMenu();
popupMenu.showMenuAsync (
juce::PopupMenu::Options {}.withTargetComponent (this).withMousePosition());
} catch (...) {
// Silently handle any exceptions from the host
}
}
[[nodiscard]] juce::RangedAudioParameter& getParam() const { return param; }
private:
juce::AudioProcessorEditor& editor;
juce::RangedAudioParameter& param;
};
// Improved suffix system using function-based strategy pattern
using SuffixStrategy = std::function<std::string(float value, const juce::String& defaultSuffix)>;
struct SuffixStrategies {
static SuffixStrategy always() {
return [](float, const juce::String& suffix) { return suffix.toStdString(); };
}
static SuffixStrategy never() {
return [](float, const juce::String&) { return ""; };
}
static SuffixStrategy hideAt(float hideValue) {
return [hideValue](float value, const juce::String& suffix) {
return juce::approximatelyEqual(value, hideValue) ? "" : suffix.toStdString();
};
}
static SuffixStrategy hideAtMin() {
return [](float value, const juce::String& suffix) {
// This will be set up properly in AttachedSlider constructor
return suffix.toStdString();
};
}
static SuffixStrategy hideAtMax() {
return [](float value, const juce::String& suffix) {
// This will be set up properly in AttachedSlider constructor
return suffix.toStdString();
};
}
static SuffixStrategy hideAtZero() {
return hideAt(0.0f);
}
static SuffixStrategy custom(std::function<std::string(float)> func) {
return [func](float value, const juce::String&) { return func(value); };
}
// Parameter-aware suffix factories - eliminate hardcoded values!
// USAGE NOTE: These factories extract min/max values directly from the parameter's NormalisableRange,
// eliminating duplication between parameter definitions and suffix logic. Always prefer these over
// hardcoded values when you need "OFF" behavior at range boundaries.
//
// Example: For a lowcut filter (19-220Hz, OFF at 19Hz):
// AttachedSlider(editor, lowCutParam, undoManager, SuffixStrategies::frequencyWithOff(lowCutParam, true))
// - Automatically shows "OFF" at 19Hz (range.start)
// - Shows "Hz" for all other values
// - No hardcoded 19.0f anywhere!
static SuffixStrategy offAtMin(const juce::RangedAudioParameter& param, const std::string& unit = "") {
return [¶m, unit](float value, const juce::String& defaultSuffix) {
auto range = param.getNormalisableRange();
if (juce::approximatelyEqual(value, range.start)) {
return std::string("OFF");
}
return unit.empty() ? defaultSuffix.toStdString() : unit;
};
}
static SuffixStrategy offAtMax(const juce::RangedAudioParameter& param, const std::string& unit = "") {
return [¶m, unit](float value, const juce::String& defaultSuffix) {
auto range = param.getNormalisableRange();
if (juce::approximatelyEqual(value, range.end)) {
return std::string("OFF");
}
return unit.empty() ? defaultSuffix.toStdString() : unit;
};
}
// Convenience factory for frequency parameters with OFF states
static SuffixStrategy frequencyWithOff(const juce::RangedAudioParameter& param, bool offAtMinimum = true) {
return offAtMinimum ? offAtMin(param, "Hz") : offAtMax(param, "Hz");
}
};
// Legacy enum for backwards compatibility
enum SuffixDisplay { OffOnMinimum, OffOnMaximum, Always, Never, Zero };
class AttachedSlider : public ComponentWithParamMenu {
public:
// Legacy constructor for backwards compatibility
AttachedSlider (juce::AudioProcessorEditor& editorIn,
juce::RangedAudioParameter& paramIn,
juce::UndoManager* undoManager,
const SuffixDisplay suffix = Always,
juce::Slider::SliderStyle style = juce::Slider::RotaryVerticalDrag,
bool showLabel = false) :
ComponentWithParamMenu (editorIn, paramIn),
slider { style, juce::Slider::TextBoxBelow },
label ("", paramIn.name),
attachment (paramIn, slider, undoManager),
suffixDisplay (suffix),
useLegacySuffix(true) {
initializeSlider(showLabel);
}
// Modern constructor with improved suffix strategy
AttachedSlider (juce::AudioProcessorEditor& editorIn,
juce::RangedAudioParameter& paramIn,
juce::UndoManager* undoManager,
SuffixStrategy suffixStrategy,
juce::Slider::SliderStyle style = juce::Slider::RotaryVerticalDrag,
bool showLabel = false) :
ComponentWithParamMenu (editorIn, paramIn),
slider { style, juce::Slider::TextBoxBelow },
label ("", paramIn.name),
attachment (paramIn, slider, undoManager),
suffixStrategy (std::move(suffixStrategy)),
useLegacySuffix(false) {
initializeSlider(showLabel);
}
private:
void initializeSlider(bool showLabel) {
slider.addMouseListener (this, true);
addAndMakeVisible (slider);
if (showLabel) {
addAndMakeVisible (label);
label.attachToComponent (&slider, false);
label.setJustificationType (juce::Justification::centred);
}
UpdateSuffix();
}
public:
void UpdateSuffix() {
if (useLegacySuffix) {
updateLegacySuffix();
} else {
updateModernSuffix();
}
}
private:
void updateLegacySuffix() {
const auto value = slider.getValueObject();
const bool isMin = value == slider.getMinimum();
const bool isMax = value == slider.getMaximum();
const bool isZero = juce::approximatelyEqual (static_cast<float> (value.getValue()), 0.0f);
if ((suffixDisplay == OffOnMinimum && isMin) || (suffixDisplay == OffOnMaximum && isMax)
|| (suffixDisplay == Never) || (suffixDisplay == Zero && isZero)) {
ClearSuffix();
} else if (suffixDisplay != Never) {
SetDefaultSuffix();
}
}
void updateModernSuffix() {
if (suffixStrategy) {
auto currentValue = static_cast<float>(slider.getValue());
auto suffixText = suffixStrategy(currentValue, getParam().label);
slider.setTextValueSuffix(suffixText.empty() ? "" : " " + juce::String(suffixText));
} else {
SetDefaultSuffix();
}
}
public:
void enableLabel (juce::Component& labelParent) {
label.setJustificationType (juce::Justification::centred);
labelParent.addAndMakeVisible (label);
label.attachToComponent (this, false); // positions above AttachedSlider in labelParent's coords
}
void setLabelText (const juce::String& text) { label.setText (text, juce::dontSendNotification); }
void setLabelLookAndFeel (juce::LookAndFeel& lf) { label.setLookAndFeel (&lf); }
void SetDefaultSuffix() { slider.setTextValueSuffix (" " + getParam().label); }
void ClearSuffix() { slider.setTextValueSuffix (""); }
void resized() override { slider.setBounds (getLocalBounds()); }
juce::Slider& getSlider() { return slider; }
juce::Label& getLabel() { return label; }
juce::SliderParameterAttachment& getAttachment() { return attachment; }
private:
juce::Slider slider;
juce::Label label;
juce::SliderParameterAttachment attachment;
// Legacy suffix system
SuffixDisplay suffixDisplay = Always;
// Modern suffix system
SuffixStrategy suffixStrategy;
bool useLegacySuffix = true;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(AttachedSlider)
};
class AttachedToggle : public ComponentWithParamMenu {
public:
AttachedToggle (juce::AudioProcessorEditor& editorIn, juce::RangedAudioParameter& paramIn) :
ComponentWithParamMenu (editorIn, paramIn),
toggleButton (paramIn.name),
attachment (paramIn, toggleButton) {
toggleButton.addMouseListener (this, true);
addAndMakeVisible (toggleButton);
}
void resized() override { toggleButton.setBounds (getLocalBounds()); }
juce::ToggleButton& getToggle() { return toggleButton; }
juce::ButtonParameterAttachment& getAttachment() { return attachment; }
private:
juce::ToggleButton toggleButton;
juce::ButtonParameterAttachment attachment;
};
class AttachedRadioButtons : public ComponentWithParamMenu {
public:
AttachedRadioButtons (juce::AudioProcessorEditor& editorIn,
juce::RangedAudioParameter& paramIn,
juce::Array<juce::Button*>& buttons,
const int groupId,
juce::UndoManager* um,
const RadioButtonParameterType radioType) :
ComponentWithParamMenu (editorIn, paramIn), attachment (paramIn, buttons, groupId, um, radioType) {
std::ranges::for_each (buttons, [this] (juce::Button* b) {
b->addMouseListener (this, true);
addAndMakeVisible (b);
});
}
~AttachedRadioButtons() override {
auto buttons = attachment.getButtons();
std::ranges::for_each (buttons, [this] (juce::Button* button) { button->removeMouseListener (this); });
}
#if DEBUG
void paint (juce::Graphics& g) override {
auto buttons = attachment.getButtons();
std::for_each (buttons.begin(), buttons.end(), [this, &g] (juce::Button* button) {
g.setColour (juce::Colours::yellowgreen);
g.drawRect (button->getBounds(), 1);
});
}
#endif
// void resized() override {
// auto buttons = attachment.getButtons();
// auto lb = getLocalBounds();
// std::for_each (
// buttons.begin(), buttons.end(), [this, &lb] (Button* button) {
// button->setBounds (lb);
// });
// }
juce::Button* getButtonAtIndex (const int i) const { return attachment.getButton (i); }
RadioButtonParameterAttachment& getAttachment() { return attachment; }
private:
RadioButtonParameterAttachment attachment;
};
template <typename T = juce::ImageButton, typename = std::enable_if_t<std::is_base_of_v<juce::ImageButton, T>>>
class AttachedImageButton : public ComponentWithParamMenu {
public:
AttachedImageButton (juce::AudioProcessorEditor& editorIn,
juce::RangedAudioParameter& paramIn,
juce::UndoManager* undoManager) :
ComponentWithParamMenu (editorIn, paramIn),
param (paramIn),
button (paramIn.name),
attachment (paramIn, button, undoManager) {
button.addMouseListener (this, true);
addAndMakeVisible (button);
}
void resized() override { button.setBounds (getLocalBounds()); }
T& getButton() { return button; }
juce::ButtonParameterAttachment& getAttachment() { return attachment; }
// RangedAudioParameter &getParameter() {
// return param;
// }
private:
juce::RangedAudioParameter& param;
T button;
juce::ButtonParameterAttachment attachment;
};
class AttachedCombo : public ComponentWithParamMenu {
public:
AttachedCombo (juce::AudioProcessorEditor& editorIn,
juce::RangedAudioParameter& paramIn,
juce::UndoManager* undoManager) :
ComponentWithParamMenu (editorIn, paramIn),
combo (paramIn),
label ("", paramIn.name),
attachment (paramIn, combo, undoManager) {
combo.addMouseListener (this, true);
combo.setJustificationType (juce::Justification::centred);
addAndMakeVisible(combo);
addAndMakeVisible (label);
label.attachToComponent (&combo, false);
label.setJustificationType (juce::Justification::centred);
}
void resized() override {
combo.setBounds (getLocalBounds());
//.withSizeKeepingCentre(jmin(getWidth(), 150), 24));
}
private:
struct ComboWithItems final : public juce::ComboBox {
explicit ComboWithItems (juce::RangedAudioParameter& param) {
// Adding the list here in the constructor means that the combo
// is already populated when we construct the attachment below
addItemList (dynamic_cast<juce::AudioParameterChoice&> (param).choices, 1);
}
};
ComboWithItems combo;
juce::Label label;
juce::ComboBoxParameterAttachment attachment;
public:
ComboWithItems& getCombo() { return combo; }
juce::ComboBoxParameterAttachment& getAttachment() { return attachment; }
};
// AttachedCycler: Reusable template for discrete parameter cycling with custom UI components
template<typename CustomComponent>
class AttachedCycler : public ComponentWithParamMenu {
public:
AttachedCycler(juce::AudioProcessorEditor& editorIn,
juce::RangedAudioParameter& paramIn,
juce::UndoManager* undoManager = nullptr,
std::function<void(float)> valueChangedCallback = nullptr,
std::function<uint32_t(uint32_t)> customCycleNext = nullptr,
std::function<uint32_t(uint32_t)> customCyclePrevious = nullptr)
: ComponentWithParamMenu(editorIn, paramIn)
, component()
, attachment(paramIn, [this](float v) { updateDisplay(v); }, undoManager)
, customValueCallback(valueChangedCallback)
, customCycleNextFunc(customCycleNext)
, customCyclePreviousFunc(customCyclePrevious)
{
component.addMouseListener(this, true);
addAndMakeVisible(component);
// Set up cycling callbacks from component to parameter
component.onCyclePrevious = [this]() { cycleToPrevious(); };
component.onCycleNext = [this]() { cycleToNext(); };
// Set up direct value selection callback (for popup menus, etc.)
if constexpr (requires { component.onValueSelected; }) {
component.onValueSelected = [this](float value) { setValueDirect(value); };
}
attachment.sendInitialUpdate();
}
void resized() override {
component.setBounds(getLocalBounds());
}
CustomComponent& getComponent() { return component; }
juce::ParameterAttachment& getAttachment() { return attachment; }
private:
CustomComponent component;
juce::ParameterAttachment attachment;
std::function<void(float)> customValueCallback;
std::function<uint32_t(uint32_t)> customCycleNextFunc;
std::function<uint32_t(uint32_t)> customCyclePreviousFunc;
void updateDisplay(float newValue) {
component.setValue(newValue);
if (customValueCallback) {
customValueCallback(newValue);
}
}
void cycleToPrevious() {
auto& param = getParam();
auto currentValue = param.getValue();
auto denormalized = param.convertFrom0to1(currentValue);
float newValue;
if (customCyclePreviousFunc) {
// Use custom cycling function
auto currentPacked = static_cast<uint32_t>(denormalized);
auto newPacked = customCyclePreviousFunc(currentPacked);
newValue = static_cast<float>(newPacked);
} else {
// Default linear cycling
auto range = param.getNormalisableRange();
newValue = denormalized - range.interval;
if (newValue < range.start) {
newValue = range.end;
}
}
attachment.setValueAsCompleteGesture(newValue);
}
void cycleToNext() {
auto& param = getParam();
auto currentValue = param.getValue();
auto denormalized = param.convertFrom0to1(currentValue);
float newValue;
if (customCycleNextFunc) {
// Use custom cycling function
auto currentPacked = static_cast<uint32_t>(denormalized);
auto newPacked = customCycleNextFunc(currentPacked);
newValue = static_cast<float>(newPacked);
} else {
// Default linear cycling
auto range = param.getNormalisableRange();
newValue = denormalized + range.interval;
if (newValue > range.end) {
newValue = range.start;
}
}
attachment.setValueAsCompleteGesture(newValue);
}
void setValueDirect(float value) {
attachment.setValueAsCompleteGesture(value);
}
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(AttachedCycler)
};
using Px = juce::Grid::Px;
using Fr = juce::Grid::Fr;
struct GetTrackInfo {
// Combo boxes need a lot of room
juce::Grid::TrackInfo operator() (AttachedCombo&) const { return Px (120); }
// Toggles are a bit smaller
juce::Grid::TrackInfo operator() (AttachedToggle&) const { return Px (80); }
// Sliders take up as much room as they can
juce::Grid::TrackInfo operator() (AttachedSlider&) const { return Fr (1); }
};
template <typename... Components>
static void performLayout (const juce::Rectangle<int>& bounds, Components&... components) {
juce::Grid grid;
grid.alignContent = juce::Grid::AlignContent::spaceAround;
grid.autoColumns = juce::Grid::TrackInfo (Fr (1));
grid.autoRows = juce::Grid::TrackInfo (Fr (1));
grid.columnGap = juce::Grid::Px (80);
grid.rowGap = juce::Grid::Px (80);
grid.autoFlow = juce::Grid::AutoFlow::column;
grid.templateColumns = { Fr (1), Fr (1) };
grid.templateRows = { Fr (1), Fr (1) };
grid.items = { GridItem (components)... };
grid.performLayout (bounds);
}
}