File: MainComponent.cpp

package info (click to toggle)
juce 8.0.10%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 78,768 kB
  • sloc: cpp: 526,464; ansic: 159,952; java: 3,038; javascript: 847; xml: 269; python: 224; sh: 167; makefile: 84
file content (471 lines) | stat: -rw-r--r-- 17,027 bytes parent folder | download | duplicates (2)
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
/*
  ==============================================================================

   This file is part of the JUCE framework examples.
   Copyright (c) Raw Material Software Limited

   The code included in this file is provided under the terms of the ISC license
   http://www.isc.org/downloads/software-support-policy/isc-license. Permission
   to use, copy, modify, and/or distribute this software for any purpose with or
   without fee is hereby granted provided that the above copyright notice and
   this permission notice appear in all copies.

   THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
   REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
   AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
   INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
   LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
   OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
   PERFORMANCE OF THIS SOFTWARE.

  ==============================================================================
*/

#include "MainComponent.h"

//==============================================================================
struct SidePanelHeader final : public Component
{
    SidePanelHeader (MainComponent& o)
        : owner (o)
    {
        setOpaque (true);

        static const unsigned char homeIconPathData[]
            = { 110,109,0,0,64,65,0,0,64,64,98,0,0,64,65,0,0,64,64,74,12,186,64,164,112,5,65,24,217,22,64,70,182,
                51,65,98,35,219,9,64,240,167,54,65,0,0,0,64,170,241,58,65,0,0,0,64,0,0,64,65,98,0,0,0,64,22,217,
                72,65,166,155,28,64,0,0,80,65,0,0,64,64,0,0,80,65,108,0,0,160,64,0,0,80,65,108,0,0,160,64,0,0,
                160,65,98,0,0,160,64,139,108,164,65,211,77,174,64,0,0,168,65,0,0,192,64,0,0,168,65,108,0,0,16,65,
                0,0,168,65,98,22,217,24,65,0,0,168,65,0,0,32,65,127,106,164,65,0,0,32,65,0,0,160,65,108,0,0,32,65,0,0,128,
                65,108,0,0,96,65,0,0,128,65,108,0,0,96,65,0,0,160,65,98,0,0,96,65,127,106,164,65,233,38,103,65,0,0,168,
                65,0,0,112,65,0,0,168,65,108,0,0,144,65,0,0,168,65,98,139,108,148,65,0,0,168,65,0,0,152,65,139,108,164,
                65,0,0,152,65,0,0,160,65,108,0,0,152,65,0,0,80,65,108,0,0,168,65,0,0,80,65,98,139,108,172,65,0,0,80,65,0,
                0,176,65,23,217,72,65,0,0,176,65,0,0,64,65,98,0,0,176,65,170,241,58,65,156,196,174,65,240,167,54,65,158,239,
                172,65,70,182,51,65,98,213,120,145,65,164,112,5,65,0,0,64,65,0,0,64,64,0,0,64,65,0,0,64,64,99,
                101,0,0 };

        static const unsigned char settingsIconPathData[]
            = { 110,109,202,111,210,64,243,226,61,64,108,0,0,224,64,0,0,0,0,108,0,0,48,65,0,0,0,0,108,27,200,54,65,243,
                226,61,64,98,91,248,63,65,174,170,76,64,95,130,72,65,231,138,96,64,46,46,80,65,180,163,120,64,108,42,
                181,124,65,20,38,49,64,108,149,90,142,65,246,108,199,64,108,68,249,118,65,2,85,1,65,98,112,166,119,65,
                201,31,6,65,0,0,120,65,111,5,11,65,0,0,120,65,0,0,16,65,98,0,0,120,65,145,250,20,65,108,166,119,65,55,
                224,25,65,72,249,118,65,254,170,30,65,108,151,90,142,65,133,73,60,65,108,46,181,124,65,123,182,115,65,
                108,50,46,80,65,18,215,97,65,98,99,130,72,65,70,221,103,65,96,248,63,65,83,213,108,65,32,200,54,65,66,
                135,112,65,108,0,0,48,65,0,0,144,65,108,0,0,224,64,0,0,144,65,108,202,111,210,64,67,135,112,65,98,74,
                15,192,64,84,213,108,65,65,251,174,64,70,221,103,65,164,163,159,64,19,215,97,65,108,92,43,13,64,123,182,
                115,65,108,187,181,82,62,133,73,60,65,108,244,26,36,64,254,170,30,65,98,64,102,33,64,55,224,25,5,0,0,32,
                64,145,250,20,65,0,0,32,64,0,0,16,65,98,0,0,32,64,111,5,11,65,64,102,33,64,201,31,6,65,244,26,36,64,2,85,
                1,65,108,187,181,82,62,246,108,199,64,108,92,43,13,64,20,38,49,64,108,164,163,159,64,180,163,120,64,98,65,
                251,174,64,231,138,96,64,74,15,192,64,175,170,76,64,202,111,210,64,243,226,61,64,99,109,0,0,16,65,0,0,64,
                65,98,121,130,42,65,0,0,64,65,0,0,64,65,121,130,42,65,0,0,64,65,0,0,16,65,98,0,0,64,65,13,251,234,64,121,
                130,42,65,0,0,192,64,0,0,16,65,0,0,192,64,98,13,251,234,64,0,0,192,64,0,0,192,64,13,251,234,64,0,0,192,64,
                0,0,16,65,98,0,0,192,64,121,130,42,65,13,251,234,64,0,0,64,65,0,0,16,65,0,0,64,65,99,101,0,0 };

        Path p;
        p.loadPathFromData (homeIconPathData, sizeof (homeIconPathData));
        homeButton.setShape (p, true, true, false);

        p.clear();
        p.loadPathFromData (settingsIconPathData, sizeof (settingsIconPathData));
        settingsButton.setShape (p, true, true, false);

        titleLabel.setText (owner.getSidePanel().getTitleText(), NotificationType::dontSendNotification);
        addAndMakeVisible (titleLabel);

        homeButton.onClick = [this] { owner.homeButtonClicked(); };
        addAndMakeVisible (homeButton);

        addAndMakeVisible (settingsButton);
        settingsButton.onClick = [this] { owner.settingsButtonClicked(); };

        updateLookAndFeel();
    }

    void paint (Graphics& g) override
    {
        g.fillAll (owner.getSidePanel().findColour (SidePanel::backgroundColour));
    }

    void resized() override
    {
        auto bounds = getLocalBounds();
        auto buttonWidth = owner.getSidePanel().getTitleBarHeight();

        bounds.removeFromLeft (10);
        homeButton.setBounds (bounds.removeFromLeft (buttonWidth).reduced (7));
        settingsButton.setBounds (bounds.removeFromLeft (buttonWidth).reduced (7));
        bounds.removeFromLeft (10);

        bounds.removeFromRight (10);
        titleLabel.setBounds (bounds);
    }

    void updateLookAndFeel()
    {
        auto& sidePanel = owner.getSidePanel();
        auto& lf = sidePanel.getLookAndFeel();

        titleLabel.setFont (lf.getSidePanelTitleFont (sidePanel));
        titleLabel.setJustificationType (lf.getSidePanelTitleJustification (sidePanel));
        titleLabel.setColour (Label::textColourId, owner.findColour (SidePanel::titleTextColour));

        auto normal = sidePanel.findColour (SidePanel::dismissButtonNormalColour);
        auto over = sidePanel.findColour (SidePanel::dismissButtonOverColour);
        auto down = sidePanel.findColour (SidePanel::dismissButtonDownColour);

        homeButton.setColours (normal, over, down);
        settingsButton.setColours (normal, over, down);

    }

    void lookAndFeelChanged() override
    {
        updateLookAndFeel();
    }

    MainComponent& owner;
    Label titleLabel;
    ShapeButton homeButton      { "Home", Colours::transparentBlack, Colours::transparentBlack, Colours::transparentBlack },
                settingsButton  { "Settings", Colours::transparentBlack, Colours::transparentBlack, Colours::transparentBlack };
};

//==============================================================================
class DemoList final : public Component,
                       public ListBoxModel
{
public:
    DemoList (DemoContentComponent& holder)
        : demoHolder (holder)
    {
        addAndMakeVisible (demos);
        demos.setModel (this);
        demos.setRowHeight (40);
    }

    void resized() override
    {
        demos.setBounds (getLocalBounds());
    }

    //==============================================================================
    void paintListBoxItem (int rowNumber, Graphics& g, int width, int height, bool rowIsSelected) override
    {
        Rectangle<int> bounds (0, 0, width, height);

        auto textColour = findColour (Label::textColourId);

        g.setColour (textColour.withAlpha (0.4f));

        if (rowNumber == 0)
            g.fillRect (bounds.removeFromTop (2).reduced (7, 0));

        g.fillRect (bounds.removeFromBottom (2).reduced (7, 0));

        if (rowIsSelected)
        {
            g.setColour (findColour (TextEditor::highlightColourId).withAlpha (0.4f));
            g.fillRect (bounds);
            textColour = findColour (TextEditor::highlightedTextColourId);
        }

        g.setColour (textColour);
        g.drawFittedText (getNameForRow (rowNumber), bounds, Justification::centred, 1);
    }

    int getNumRows() override
    {
        return (int) (selectedCategory.isEmpty() ? JUCEDemos::getCategories().size()
                                                 : JUCEDemos::getCategory (selectedCategory).demos.size());
    }

    String getNameForRow (int rowNumber) override
    {
        if (selectedCategory.isEmpty())
        {
            if (isPositiveAndBelow (rowNumber, JUCEDemos::getCategories().size()))
                return JUCEDemos::getCategories()[(size_t) rowNumber].name;
        }
        else
        {
            auto& category = JUCEDemos::getCategory (selectedCategory);

            if (isPositiveAndBelow (rowNumber, category.demos.size()))
                return category.demos[(size_t) rowNumber].demoFile.getFileName();
        }

        return {};
    }

    void returnKeyPressed (int row) override                       { selectRow (row); }
    void listBoxItemClicked (int row, const MouseEvent&) override  { selectRow (row); }

    //==============================================================================
    void showCategory (const String& categoryName) noexcept
    {
        selectedCategory = categoryName;

        demos.deselectAllRows();
        demos.setHeaderComponent (categoryName.isEmpty() ? nullptr
                                                         : std::make_unique<CategoryListHeaderComponent> (*this));
        demos.updateContent();
    }

private:
    //==============================================================================
    class CategoryListHeaderComponent final : public Button
    {
    public:
        explicit CategoryListHeaderComponent (DemoList& o)
            : Button ({}),
              owner (o)
        {
            setTitle ("Previous");
            setSize (0, 30);
        }

        void paintButton (Graphics& g, bool, bool) override
        {
            g.setColour (findColour (Label::textColourId));
            g.drawFittedText ("<", getLocalBounds().reduced (20, 0), Justification::centredLeft, 1);
        }

        void clicked() override
        {
            owner.showCategory ({});
        }

        using Button::clicked;

    private:
        DemoList& owner;
    };

    //==============================================================================
    void selectRow (int row)
    {
        if (row < 0)
            return;

        if (selectedCategory.isEmpty())
            showCategory (JUCEDemos::getCategories()[(size_t) row].name);
        else
            demoHolder.setDemo (selectedCategory, row);

        if (demos.isShowing())
            selectFirstRow();
    }

    void selectFirstRow()
    {
        if (auto* handler = demos.getAccessibilityHandler())
        {
            for (auto* child : handler->getChildren())
            {
                if (child->getRole() == AccessibilityRole::listItem)
                {
                    child->grabFocus();
                    break;
                }
            }
        }
    }

    //==============================================================================
    String selectedCategory;

    DemoContentComponent& demoHolder;
    ListBox demos;
};

//==============================================================================
MainComponent::MainComponent()
{
    contentComponent.reset (new DemoContentComponent (*this, [this] (bool isHeavyweight)
    {
        demosPanel.showOrHide (false);

        if (isHeavyweight)
        {
           #if (JUCE_MAC && USE_COREGRAPHICS_RENDERING) || JUCE_WINDOWS
            constexpr auto fallbackEngine = 1;
           #else
            constexpr auto fallbackEngine = 0;
           #endif

            setRenderingEngine (fallbackEngine);
        }

        isShowingHeavyweightDemo = isHeavyweight;
        resized();
    }));

    demosPanel.setContent (new DemoList (*contentComponent));
    demosPanel.setTitleBarComponent (new SidePanelHeader (*this), true);

    addAndMakeVisible (contentComponent.get());
    addAndMakeVisible (showDemosButton);
    addAndMakeVisible (demosPanel);

    demosPanel.setTitle ("Demos");
    demosPanel.setFocusContainerType (FocusContainerType::focusContainer);
    demosPanel.setContentRestrictedToSafeArea (true);

    showDemosButton.onClick = [this] { demosPanel.showOrHide (true); };

    demosPanel.onPanelMove = [this]
    {
        sidePanelWidth = jmax (0, demosPanel.getRight());

        if (isShowingHeavyweightDemo)
            resized();
    };

    demosPanel.onPanelShowHide = [this] (bool isShowing)
    {
        if (isShowing)
        {
            sidePanelWidth = jmax (0, demosPanel.getWidth());

            if (isShowingHeavyweightDemo)
                resized();

            if (auto* handler = demosPanel.getAccessibilityHandler())
                handler->grabFocus();
        }
        else
        {
            sidePanelWidth = 0;

            if (isShowingHeavyweightDemo)
                Timer::callAfterDelay (250, [this] { resized(); });
        }
    };

    contentComponent->showHomeScreen();

    setOpaque (true);
    setSize (800, 800);
}

MainComponent::~MainComponent()
{
    contentComponent->clearCurrentDemo();
}

//==============================================================================
void MainComponent::paint (Graphics& g)
{
    g.fillAll (findColour (ResizableWindow::backgroundColourId));
}

void MainComponent::resized()
{
    auto safeBounds = [this]
    {
        auto bounds = getLocalBounds();

       #if JUCE_IOS || JUCE_ANDROID
        if (auto* display = Desktop::getInstance().getDisplays().getDisplayForRect (getScreenBounds()))
            return display->safeAreaInsets.subtractedFrom (display->keyboardInsets.subtractedFrom (bounds));
       #endif

        return bounds;
    }();

    showDemosButton.setBounds (safeBounds.getX(), safeBounds.getY(), 150, contentComponent->getTabBarDepth());

    if (isShowingHeavyweightDemo)
    {
        safeBounds.removeFromLeft (sidePanelWidth);
        contentComponent->setTabBarIndent (jmax (0, 150 - sidePanelWidth));
    }
    else
    {
        contentComponent->setTabBarIndent (150);
    }

    contentComponent->setBounds (safeBounds);
}

void MainComponent::homeButtonClicked()
{
    if (auto* list = dynamic_cast<DemoList*> (demosPanel.getContent()))
        list->showCategory ({});

    if (contentComponent != nullptr)
    {
        if (contentComponent->isShowingHomeScreen())
            return;

        contentComponent->showHomeScreen();

        if (isShowingHeavyweightDemo)
        {
            isShowingHeavyweightDemo = false;
            resized();
        }
    }
}

void MainComponent::settingsButtonClicked()
{
    if (contentComponent != nullptr)
        contentComponent->setCurrentTabIndex (2);
}

void MainComponent::setRenderingEngine (int renderingEngineIndex)
{
    if (renderingEngineIndex != currentRenderingEngineIdx)
        updateRenderingEngine (renderingEngineIndex);
}

void MainComponent::parentHierarchyChanged()
{
    auto* newPeer = getPeer();

    if (peer != newPeer)
    {
        peer = newPeer;

        auto previousRenderingEngine = renderingEngines[currentRenderingEngineIdx];

        renderingEngines.clear();
        if (peer != nullptr)
            renderingEngines = peer->getAvailableRenderingEngines();

        renderingEngines.add ("OpenGL Renderer");

        currentRenderingEngineIdx = renderingEngines.indexOf (previousRenderingEngine);

        if (currentRenderingEngineIdx < 0)
        {
           #if JUCE_ANDROID
            currentRenderingEngineIdx = (renderingEngines.size() - 1);
           #else
            currentRenderingEngineIdx = peer != nullptr ? peer->getCurrentRenderingEngine() : -1;
           #endif
        }

        updateRenderingEngine (currentRenderingEngineIdx);
    }
}

void MainComponent::updateRenderingEngine (int renderingEngineIndex)
{
    if (renderingEngineIndex == (renderingEngines.size() - 1))
    {
        if (isShowingHeavyweightDemo)
            return;

        openGLContext.attachTo (*getTopLevelComponent());
    }
    else
    {
        openGLContext.detach();

        if (peer != nullptr)
            peer->setCurrentRenderingEngine (renderingEngineIndex);
    }

    currentRenderingEngineIdx = renderingEngineIndex;
}