File: PluginEditor.cpp

package info (click to toggle)
iem-plugin-suite 1.15.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,080 kB
  • sloc: cpp: 58,973; python: 269; sh: 79; makefile: 41
file content (379 lines) | stat: -rw-r--r-- 14,534 bytes parent folder | download
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
/*
 ==============================================================================
 This file is part of the IEM plug-in suite.
 Authors: Daniel Rudrich, Franz Zotter
 Copyright (c) 2017 - Institute of Electronic Music and Acoustics (IEM)
 https://iem.at

 The IEM plug-in suite is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 The IEM plug-in suite is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this software.  If not, see <https://www.gnu.org/licenses/>.
 ==============================================================================
 */

#include "PluginEditor.h"
#include "PluginProcessor.h"

//==============================================================================
AllRADecoderAudioProcessorEditor::AllRADecoderAudioProcessorEditor (
    AllRADecoderAudioProcessor& p,
    juce::AudioProcessorValueTreeState& vts) :
    juce::AudioProcessorEditor (&p),
    processor (p),
    valueTreeState (vts),
    footer (p.getOSCParameterInterface()),
    lv (processor.points, processor.triangles, processor.normals, processor.imaginaryFlags),
    lspList (processor.getLoudspeakersValueTree(), lv, grid, processor.undoManager, processor),
    grid (processor.points,
          processor.imaginaryFlags,
          processor.energyDistribution,
          processor.rEVector)
{
    // ============== BEGIN: essentials ======================
    // set GUI size and lookAndFeel
    //setSize(500, 300); // use this to create a fixed-size GUI
    setResizeLimits (1000, 600, 1200, 900); // use this to create a resizable GUI
    setResizable (true, true);
    setLookAndFeel (&globalLaF);

    // make title and footer visible, and set the PluginName
    addAndMakeVisible (&title);
    title.setTitle (juce::String ("AllRA"), juce::String ("Decoder"));
    title.setFont (globalLaF.robotoBold, globalLaF.robotoLight);
    addAndMakeVisible (&footer);
    // ============= END: essentials ========================

    // create the connection between title component's comboBoxes and parameters
    cbNormalizationSettingAttachment.reset (
        new ComboBoxAttachment (valueTreeState,
                                "useSN3D",
                                *title.getInputWidgetPtr()->getNormCbPointer()));
    cbOrderSettingAttachment.reset (
        new ComboBoxAttachment (valueTreeState,
                                "inputOrderSetting",
                                *title.getInputWidgetPtr()->getOrderCbPointer()));

    addAndMakeVisible (cbDecoderOrder);
    cbDecoderOrder.setJustificationType (juce::Justification::centred);
    cbDecoderOrder.addSectionHeading ("Decoder order");
    for (int n = 1; n <= 7; ++n)
        cbDecoderOrder.addItem (getOrderString (n), n);
    cbDecoderOrderAttachment.reset (
        new ComboBoxAttachment (valueTreeState, "decoderOrder", cbDecoderOrder));

    addAndMakeVisible (lbDecoderOrder);
    lbDecoderOrder.setText ("Decoder Order", juce::Justification::left);

    addAndMakeVisible (cbDecoderWeights);
    cbDecoderWeights.setJustificationType (juce::Justification::centred);
    cbDecoderWeights.addItemList (p.weightsStrings, 1);
    cbDecoderWeightsAttachment.reset (
        new ComboBoxAttachment (valueTreeState, "weights", cbDecoderWeights));

    addAndMakeVisible (lbDecoderWeights);
    lbDecoderWeights.setText ("Weights", juce::Justification::left);

    addAndMakeVisible (gcLayout);
    gcLayout.setText ("Loudspeaker Layout");

    addAndMakeVisible (gcDecoder);
    gcDecoder.setText ("Calculate Decoder");

    addAndMakeVisible (gcExport);
    gcExport.setText ("Export Decoder/Layout");

    addAndMakeVisible (tbExportDecoder);
    tbExportDecoderAttachment.reset (
        new ButtonAttachment (valueTreeState, "exportDecoder", tbExportDecoder));
    tbExportDecoder.setButtonText ("Export Decoder");
    tbExportDecoder.setColour (juce::ToggleButton::tickColourId, juce::Colours::orange);

    addAndMakeVisible (tbExportLayout);
    tbExportLayoutAttachment.reset (
        new ButtonAttachment (valueTreeState, "exportLayout", tbExportLayout));
    tbExportLayout.setButtonText ("Export Layout");
    tbExportLayout.setColour (juce::ToggleButton::tickColourId, juce::Colours::limegreen);

    addAndMakeVisible (messageDisplay);
    messageDisplay.setMessage (processor.messageToEditor);

    addAndMakeVisible (grid);

    addAndMakeVisible (tbCalculateDecoder);
    tbCalculateDecoder.setButtonText ("CALCULATE DECODER");
    tbCalculateDecoder.setColour (juce::TextButton::buttonColourId, juce::Colours::cornflowerblue);
    tbCalculateDecoder.addListener (this);

    addAndMakeVisible (tbAddSpeakers);
    tbAddSpeakers.setButtonText ("ADD LOUDSPEAKER");
    tbAddSpeakers.setColour (juce::TextButton::buttonColourId, juce::Colours::limegreen);
    tbAddSpeakers.setTooltip (
        "Adds a new loudspeaker with random position. \n Alt+click: adds an imaginary loudspeaker to the nadir position.");
    tbAddSpeakers.addListener (this);

    addAndMakeVisible (tbClearSpeakers);
    tbClearSpeakers.setButtonText ("CLEAR");
    tbClearSpeakers.setColour (juce::TextButton::buttonColourId, juce::Colours::red);
    tbClearSpeakers.setTooltip ("Delete all loudspeakers.");
    tbClearSpeakers.addListener (this);

    addAndMakeVisible (tbJson);
    tbJson.setButtonText ("EXPORT");
    tbJson.setColour (juce::TextButton::buttonColourId, juce::Colours::orange);
    tbJson.setTooltip ("Stores the decoder and/or loudspeaker layout to a configuration file.");
    tbJson.addListener (this);

    addAndMakeVisible (tbImport);
    tbImport.setButtonText ("IMPORT");
    tbImport.setColour (juce::TextButton::buttonColourId, juce::Colours::orange);
    tbImport.setTooltip ("Imports loudspeakers from a configuration file.");
    tbImport.addListener (this);

    addAndMakeVisible (tbUndo);
    tbUndo.setButtonText ("UNDO");
    tbUndo.setColour (juce::TextButton::buttonColourId, juce::Colours::orangered);
    tbUndo.onClick = [this]() { processor.undo(); };

    addAndMakeVisible (tbRedo);
    tbRedo.setButtonText ("REDO");
    tbRedo.setColour (juce::TextButton::buttonColourId, juce::Colours::orangered);
    tbRedo.onClick = [this]() { processor.redo(); };

    addAndMakeVisible (tbRotate);
    tbRotate.setButtonText ("ROTATE");
    tbRotate.setColour (juce::TextButton::buttonColourId, juce::Colours::cornflowerblue);
    tbRotate.setTooltip ("Rotates all loudspeakers by a desired amount around the z-axis.");
    tbRotate.onClick = [this]() { openRotateWindow(); };

    addAndMakeVisible (lv);

    addAndMakeVisible (lspList);

    updateChannelCount();

    // start timer after everything is set up properly
    startTimer (50);

    tooltipWin.setLookAndFeel (&globalLaF);
    tooltipWin.setMillisecondsBeforeTipAppears (500);
    tooltipWin.setOpaque (false);
}

AllRADecoderAudioProcessorEditor::~AllRADecoderAudioProcessorEditor()
{
    setLookAndFeel (nullptr);
}

//==============================================================================
void AllRADecoderAudioProcessorEditor::paint (juce::Graphics& g)
{
    g.fillAll (globalLaF.ClBackground);
}

void AllRADecoderAudioProcessorEditor::resized()
{
    // ============ BEGIN: header and footer ============
    const int leftRightMargin = 30;
    const int headerHeight = 60;
    const int footerHeight = 25;
    juce::Rectangle<int> area (getLocalBounds());

    juce::Rectangle<int> footerArea (area.removeFromBottom (footerHeight));
    footer.setBounds (footerArea);

    area.removeFromLeft (leftRightMargin);
    area.removeFromRight (leftRightMargin);
    juce::Rectangle<int> headerArea = area.removeFromTop (headerHeight);
    title.setBounds (headerArea);
    area.removeFromTop (10);
    area.removeFromBottom (5);
    // =========== END: header and footer =================

    // try to not use explicit coordinates to position your GUI components
    // the removeFrom...() methods are quite handy to create scalable areas
    // best practice would be the use of flexBoxes...
    // the following is medium level practice ;-)

    juce::Rectangle<int> rightArea = area.removeFromRight (420);
    juce::Rectangle<int> bottomRight = rightArea.removeFromBottom (100);

    rightArea.removeFromBottom (25);

    gcLayout.setBounds (rightArea);
    rightArea.removeFromTop (25);

    juce::Rectangle<int> ctrlsAndDisplay (rightArea.removeFromBottom (80));
    juce::Rectangle<int> lspCtrlArea (ctrlsAndDisplay.removeFromTop (20));
    ctrlsAndDisplay.removeFromTop (5);
    tbAddSpeakers.setBounds (lspCtrlArea.removeFromLeft (110));
    lspCtrlArea.removeFromLeft (5);
    tbRotate.setBounds (lspCtrlArea.removeFromLeft (55));
    lspCtrlArea.removeFromLeft (5);
    tbUndo.setBounds (lspCtrlArea.removeFromLeft (55));
    lspCtrlArea.removeFromLeft (5);
    tbRedo.setBounds (lspCtrlArea.removeFromLeft (55));
    lspCtrlArea.removeFromLeft (5);
    tbClearSpeakers.setBounds (lspCtrlArea.removeFromLeft (55));
    lspCtrlArea.removeFromLeft (5);
    tbImport.setBounds (lspCtrlArea.removeFromRight (80));
    messageDisplay.setBounds (ctrlsAndDisplay);

    rightArea.removeFromBottom (5);
    lspList.setBounds (rightArea);

    juce::Rectangle<int> decoderArea = bottomRight.removeFromLeft (150);
    bottomRight.removeFromLeft (20);
    juce::Rectangle<int> exportArea = bottomRight;

    gcDecoder.setBounds (decoderArea);
    decoderArea.removeFromTop (25);
    auto decoderCtrlRow = decoderArea.removeFromTop (20);
    lbDecoderOrder.setBounds (decoderCtrlRow.removeFromLeft (80));
    cbDecoderOrder.setBounds (decoderCtrlRow.removeFromLeft (55));
    ;
    decoderArea.removeFromTop (5);
    decoderCtrlRow = decoderArea.removeFromTop (20);
    lbDecoderWeights.setBounds (decoderCtrlRow.removeFromLeft (55));
    cbDecoderWeights.setBounds (decoderCtrlRow.removeFromLeft (80));
    ;
    decoderArea.removeFromTop (5);
    tbCalculateDecoder.setBounds (decoderArea.removeFromTop (20));

    gcExport.setBounds (exportArea);
    exportArea.removeFromTop (25);
    juce::Rectangle<int> toggleArea (exportArea.removeFromLeft (120));
    tbExportDecoder.setBounds (toggleArea.removeFromTop (20));
    tbExportLayout.setBounds (toggleArea.removeFromTop (20));
    exportArea.removeFromLeft (20);
    exportArea.removeFromTop (10);
    tbJson.setBounds (exportArea.removeFromTop (20).removeFromLeft (80));

    area.removeFromRight (20);
    juce::Rectangle<int> leftArea = area;

    grid.setBounds (leftArea.removeFromBottom (200));
    leftArea.removeFromBottom (10);
    lv.setBounds (leftArea);
}

void AllRADecoderAudioProcessorEditor::timerCallback()
{
    // === update titleBar widgets according to available input/output channel counts
    title.setMaxSize (processor.getMaxSize());
    // ==========================================

    if (processor.updateLoudspeakerVisualization.get())
    {
        processor.updateLoudspeakerVisualization = false;
        lv.updateVerticesAndIndices();
        grid.repaint();
    }

    if (processor.updateTable.get())
    {
        processor.updateTable = false;
        lspList.updateContent();
    }

    if (processor.updateMessage.get())
    {
        processor.updateMessage = false;
        messageDisplay.setMessage (processor.messageToEditor);
    }

    if (processor.updateChannelCount.get())
    {
        processor.updateChannelCount = false;
        updateChannelCount();
    }
}

void AllRADecoderAudioProcessorEditor::buttonClicked (juce::Button* button)
{
    if (button == &tbAddSpeakers)
    {
        const auto& modifiers = juce::ModifierKeys::getCurrentModifiers();
        if (modifiers.isAltDown())
            processor.addImaginaryLoudspeakerBelow();
        else
            processor.addRandomPoint();
    }
    else if (button == &tbClearSpeakers)
    {
        processor.clearLoudspeakers();
    }
    else if (button == &tbCalculateDecoder)
    {
        processor.calculateDecoder();
    }
    else if (button == &tbJson)
    {
        juce::FileChooser myChooser (
            "Save configuration...",
            processor.getLastDir().exists()
                ? processor.getLastDir()
                : juce::File::getSpecialLocation (juce::File::userHomeDirectory),
            "*.json");
        if (myChooser.browseForFileToSave (true))
        {
            juce::File configFile (myChooser.getResult());
            processor.setLastDir (configFile.getParentDirectory());
            processor.saveConfigurationToFile (configFile);
        }
    }
    else if (button == &tbImport)
    {
        juce::FileChooser myChooser (
            "Load configuration...",
            processor.getLastDir().exists()
                ? processor.getLastDir()
                : juce::File::getSpecialLocation (juce::File::userHomeDirectory),
            "*.json");
        if (myChooser.browseForFileToOpen())
        {
            juce::File configFile (myChooser.getResult());
            processor.setLastDir (configFile.getParentDirectory());
            processor.loadConfiguration (configFile);
        }
    }
}

void AllRADecoderAudioProcessorEditor::updateChannelCount()
{
    ReferenceCountedDecoder::Ptr currentDecoder = processor.getCurrentDecoder();
    if (currentDecoder != nullptr)
    {
        const int order = currentDecoder->getOrder();
        title.getInputWidgetPtr()->setMaxOrder (order);

        const int nCh = currentDecoder->getNumOutputChannels();
        title.getOutputWidgetPtr()->setSizeIfUnselectable (nCh);
    }
    else
    {
        title.getInputWidgetPtr()->setMaxOrder (0);
        title.getOutputWidgetPtr()->setSizeIfUnselectable (0);
    }
};

void AllRADecoderAudioProcessorEditor::buttonStateChanged (juce::Button* button) {};

void AllRADecoderAudioProcessorEditor::openRotateWindow()
{
    auto rotateWindow = std::make_unique<RotateWindow> (processor);
    rotateWindow->setSize (120, 35);

    auto& myBox = juce::CallOutBox::launchAsynchronously (std::move (rotateWindow),
                                                          tbRotate.getScreenBounds(),
                                                          nullptr);
    myBox.setLookAndFeel (&globalLaF);
}