Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.19 KB | None | 0 0
  1. /*
  2.   ==============================================================================
  3.  
  4.     This file was auto-generated!
  5.  
  6.     It contains the basic startup code for a Juce application.
  7.  
  8.   ==============================================================================
  9. */
  10.  
  11. #include "../JuceLibraryCode/JuceHeader.h"
  12.  
  13. inline Colour getRandomColour (float brightness)
  14. {
  15.     return Colour::fromHSV (Random::getSystemRandom().nextFloat(), 0.5f, brightness, 1.0f);
  16. }
  17.  
  18. inline Colour getRandomBrightColour()   { return getRandomColour (0.8f); }
  19. inline Colour getRandomDarkColour()     { return getRandomColour (0.3f); }
  20.  
  21.  
  22. //==============================================================================
  23. class OpenGLPerformanceApplication  : public JUCEApplication
  24. {
  25. public:
  26.     //==============================================================================
  27.     OpenGLPerformanceApplication() {}
  28.  
  29.     const String getApplicationName() override       { return ProjectInfo::projectName; }
  30.     const String getApplicationVersion() override    { return ProjectInfo::versionString; }
  31.     bool moreThanOneInstanceAllowed() override       { return true; }
  32.  
  33.     //==============================================================================
  34.     void initialise (const String& commandLine) override
  35.     {
  36.         createNewWindow();
  37.     }
  38.  
  39.     void shutdown() override   {}
  40.  
  41.     static OpenGLPerformanceApplication& getApp()
  42.     {
  43.         return *static_cast<OpenGLPerformanceApplication*> (JUCEApplication::getInstance());
  44.     }
  45.  
  46.     void createNewWindow()
  47.     {
  48.         mainWindows.add (new MainWindow (getApplicationName()));
  49.     }
  50.  
  51.     //==============================================================================
  52.     class BallGeneratorComponent    : public Component
  53.     {
  54.     public:
  55.         BallGeneratorComponent()
  56.         {
  57.         }
  58.  
  59.         void paint (Graphics& g) override
  60.         {
  61.             Rectangle<float> area (getLocalBounds().toFloat().reduced (2.0f));
  62.  
  63.             g.setColour (Colours::orange);
  64.             g.drawRoundedRectangle (area, 10.0f, 2.0f);
  65.  
  66.             AttributedString s;
  67.             s.setJustification (Justification::centred);
  68.             s.setWordWrap (AttributedString::none);
  69.             s.append ("Drag Me!");
  70.             s.setColour (findColour (TextButton::textColourOffId));
  71.             s.draw (g, area);
  72.         }
  73.  
  74.         void resized() override
  75.         {
  76.             // Just set the limits of our constrainer so that we don't drag ourselves off the screen
  77.             constrainer.setMinimumOnscreenAmounts (getHeight(), getWidth(), getHeight(), getWidth());
  78.         }
  79.  
  80.         void mouseDown (const MouseEvent& e) override
  81.         {
  82.             // Prepares our dragger to drag this Component
  83.             dragger.startDraggingComponent (this, e);
  84.         }
  85.  
  86.         void mouseDrag (const MouseEvent& e) override
  87.         {
  88.             // Moves this Component according to the mouse drag event and applies our constraints to it
  89.             dragger.dragComponent (this, e, &constrainer);
  90.         }
  91.  
  92.     private:
  93.         ComponentBoundsConstrainer constrainer;
  94.         ComponentDragger dragger;
  95.  
  96.         JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BallGeneratorComponent)
  97.     };
  98.  
  99.  
  100.     //==============================================================================
  101.     struct BallComponent  : public Component
  102.     {
  103.         BallComponent (const Point<float>& pos)
  104.             : position (pos),
  105.               speed (Random::getSystemRandom().nextFloat() *  4.0f - 2.0f,
  106.                      Random::getSystemRandom().nextFloat() * -6.0f - 2.0f),
  107.                 colour (Colours::white)
  108.         {
  109.             setSize (20, 20);
  110.             step();
  111.         }
  112.  
  113.         bool step()
  114.         {
  115.             position += speed;
  116.             speed.y += 0.1f;
  117.  
  118.             setCentrePosition ((int) position.x,
  119.                                (int) position.y);
  120.  
  121.             if (Component* parent = getParentComponent())
  122.                 return isPositiveAndBelow (position.x, (float) parent->getWidth())
  123.                          && position.y < (float) parent->getHeight();
  124.  
  125.             return position.y < 400.0f && position.x >= -10.0f;
  126.         }
  127.  
  128.         void paint (Graphics& g) override
  129.         {
  130.             g.setColour (colour);
  131.             g.fillEllipse (2.0f, 2.0f, getWidth() - 4.0f, getHeight() - 4.0f);
  132.  
  133.             g.setColour (Colours::darkgrey);
  134.             g.drawEllipse (2.0f, 2.0f, getWidth() - 4.0f, getHeight() - 4.0f, 1.0f);
  135.         }
  136.  
  137.         Point<float> position, speed;
  138.         Colour colour;
  139.  
  140.         JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BallComponent)
  141.     };
  142.  
  143.     //==============================================================================
  144.     class AnimationDemo  : public Component,
  145.                            private Button::Listener,
  146.                            private Timer
  147.     {
  148.     public:
  149.         AnimationDemo()
  150.         {
  151.             context.setComponentPaintingEnabled (true);
  152.             context.attachTo (*this);
  153.             setOpaque (true);
  154.             setSize (620, 620);
  155.  
  156.             addAndMakeVisible (newWindowButton);
  157.             newWindowButton.addListener (this);
  158.  
  159.             for (int i = 11; --i >= 0;)
  160.             {
  161.                 Button* b = createButton();
  162.                 componentsToAnimate.add (b);
  163.                 addAndMakeVisible (b);
  164.                 b->addListener (this);
  165.             }
  166.  
  167.             addAndMakeVisible (ballGenerator);
  168.             ballGenerator.centreWithSize (80, 50);
  169.  
  170.             cycleCount = 2;
  171.  
  172.             for (int i = 0; i < componentsToAnimate.size(); ++i)
  173.                 componentsToAnimate.getUnchecked (i)->setBounds (getLocalBounds().reduced (250, 250));
  174.  
  175.             for (int i = 0; i < componentsToAnimate.size(); ++i)
  176.             {
  177.                 const int newIndex = (i + 3) % componentsToAnimate.size();
  178.                 const float angle = newIndex * 2.0f * float_Pi / componentsToAnimate.size();
  179.                 const float radius = getWidth() * 0.35f;
  180.  
  181.                 Rectangle<int> r (getWidth()  / 2 + (int) (radius * std::sin (angle)) - 50,
  182.                                   getHeight() / 2 + (int) (radius * std::cos (angle)) - 50,
  183.                                   100, 100);
  184.  
  185.                 animator.animateComponent (componentsToAnimate.getUnchecked(i),
  186.                                            r.reduced (10),
  187.                                            1.0f,
  188.                                            500 + i * 100,
  189.                                            false,
  190.                                            0.0,
  191.                                            0.0);
  192.             }
  193.  
  194.             startTimerHz (60);
  195.         }
  196.  
  197.         ~AnimationDemo()
  198.         {
  199.             context.detach();
  200.         }
  201.  
  202.         void paint (Graphics& g) override
  203.         {
  204.             g.fillAll (Desktop::getInstance().getDefaultLookAndFeel().findColour (DocumentWindow::backgroundColourId));
  205.         }
  206.  
  207.         void resized() override
  208.         {
  209.             newWindowButton.setBounds (getLocalBounds().withSize (100, 20));
  210.         }
  211.  
  212.     private:
  213.         OpenGLContext context;
  214.         OwnedArray<Component> componentsToAnimate;
  215.         OwnedArray<BallComponent> balls;
  216.         BallGeneratorComponent ballGenerator;
  217.         TextButton newWindowButton {"New Window"};
  218.  
  219.         ComponentAnimator animator;
  220.         int cycleCount;
  221.  
  222.         Button* createRandomButton()
  223.         {
  224.             DrawablePath normal, over;
  225.  
  226.             Path star1;
  227.             star1.addStar (Point<float>(), 5, 20.0f, 50.0f, 0.2f);
  228.             normal.setPath (star1);
  229.             normal.setFill (Colours::red);
  230.  
  231.             Path star2;
  232.             star2.addStar (Point<float>(), 7, 30.0f, 50.0f, 0.0f);
  233.             over.setPath (star2);
  234.             over.setFill (Colours::pink);
  235.             over.setStrokeFill (Colours::black);
  236.             over.setStrokeThickness (5.0f);
  237.  
  238.             Image juceIcon = ImageCache::getFromMemory (BinaryData::juce_icon_png,
  239.                                                         BinaryData::juce_icon_pngSize);
  240.  
  241.             DrawableImage down;
  242.             down.setImage (juceIcon);
  243.             down.setOverlayColour (Colours::black.withAlpha (0.3f));
  244.  
  245.             if (Random::getSystemRandom().nextInt (10) > 2)
  246.             {
  247.                 int type = Random::getSystemRandom().nextInt (3);
  248.  
  249.                 DrawableButton* d = new DrawableButton ("Button",
  250.                                                         type == 0 ? DrawableButton::ImageOnButtonBackground
  251.                                                                   : (type == 1 ? DrawableButton::ImageFitted
  252.                                                                                : DrawableButton::ImageAboveTextLabel));
  253.                 d->setImages (&normal,
  254.                               Random::getSystemRandom().nextBool() ? &over : nullptr,
  255.                               Random::getSystemRandom().nextBool() ? &down : nullptr);
  256.  
  257.                 if (Random::getSystemRandom().nextBool())
  258.                 {
  259.                     d->setColour (DrawableButton::backgroundColourId, getRandomBrightColour());
  260.                     d->setColour (DrawableButton::backgroundOnColourId, getRandomBrightColour());
  261.                 }
  262.  
  263.                 d->setClickingTogglesState (Random::getSystemRandom().nextBool());
  264.                 return d;
  265.             }
  266.  
  267.             ImageButton* b = new ImageButton ("ImageButton");
  268.  
  269.             b->setImages (true, true, true,
  270.                           juceIcon, 0.7f, Colours::transparentBlack,
  271.                           juceIcon, 1.0f, getRandomDarkColour().withAlpha (0.2f),
  272.                           juceIcon, 1.0f, getRandomBrightColour().withAlpha (0.8f),
  273.                           0.5f);
  274.             return b;
  275.         }
  276.  
  277.         Button* createButton()
  278.         {
  279.             Image juceIcon = ImageCache::getFromMemory (BinaryData::juce_icon_png,
  280.                                                         BinaryData::juce_icon_pngSize)
  281.                                 .rescaled (128, 128);
  282.  
  283.             ImageButton* b = new ImageButton ("ImageButton");
  284.  
  285.             b->setImages (true, true, true,
  286.                           juceIcon, 1.0f, Colours::transparentBlack,
  287.                           juceIcon, 1.0f, Colours::white,
  288.                           juceIcon, 1.0f, Colours::white,
  289.                           0.5f);
  290.  
  291.             return b;
  292.         }
  293.  
  294.         void buttonClicked (Button* btn) override
  295.         {
  296.             if (btn == &newWindowButton)
  297.             {
  298.                 getApp().createNewWindow();
  299.             }
  300.             else
  301.             {
  302.                 for (int i = 0; i < componentsToAnimate.size(); ++i)
  303.                 {
  304.                     const int newIndex = (i + 3 * cycleCount) % componentsToAnimate.size();
  305.                     const float angle = newIndex * 2.0f * float_Pi / componentsToAnimate.size();
  306.                     const float radius = getWidth() * 0.35f;
  307.  
  308.                     Rectangle<int> r (getWidth()  / 2 + (int) (radius * std::sin (angle)) - 50,
  309.                                       getHeight() / 2 + (int) (radius * std::cos (angle)) - 50,
  310.                                       100, 100);
  311.  
  312.                     animator.animateComponent (componentsToAnimate.getUnchecked(i),
  313.                                                r.reduced (10),
  314.                                                1.0f,
  315.                                                900 + (int) (300 * std::sin (angle)),
  316.                                                false,
  317.                                                0.0,
  318.                                                0.0);
  319.                 }
  320.  
  321.                 ++cycleCount;
  322.             }
  323.         }
  324.  
  325.         void timerCallback() override
  326.         {
  327.             // Go through each of our balls and update their position
  328.             for (int i = balls.size(); --i >= 0;)
  329.                 if (! balls.getUnchecked(i)->step())
  330.                     balls.remove (i);
  331.  
  332.             // Randomly generate new balls
  333.             if (Random::getSystemRandom().nextInt (100) < 4)
  334.             {
  335.                 BallComponent* ball = new BallComponent (ballGenerator.getBounds().getCentre().toFloat());
  336.                 addAndMakeVisible (ball);
  337.                 balls.add (ball);
  338.             }
  339.         }
  340.  
  341.         JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AnimationDemo)
  342.     };
  343.  
  344.     //==============================================================================
  345.     class MainWindow    : public DocumentWindow
  346.     {
  347.     public:
  348.         MainWindow (String name)  : DocumentWindow (name,
  349.                                                     Desktop::getInstance().getDefaultLookAndFeel()
  350.                                                                           .findColour (ResizableWindow::backgroundColourId),
  351.                                                     DocumentWindow::allButtons)
  352.         {
  353.             setUsingNativeTitleBar (true);
  354.             setContentOwned (new AnimationDemo(), true);
  355.  
  356.             centreWithSize (getWidth(), getHeight());
  357.             setVisible (true);
  358.         }
  359.  
  360.         void closeButtonPressed() override
  361.         {
  362.             // This is called when the user tries to close this window. Here, we'll just
  363.             // ask the app to quit when this happens, but you can change this to do
  364.             // whatever you need.
  365.             JUCEApplication::getInstance()->systemRequestedQuit();
  366.         }
  367.  
  368.         /* Note: Be careful if you override any DocumentWindow methods - the base
  369.            class uses a lot of them, so by overriding you might break its functionality.
  370.            It's best to do all your work in your content component instead, but if
  371.            you really have to override any DocumentWindow methods, make sure your
  372.            subclass also calls the superclass's method.
  373.         */
  374.  
  375.     private:
  376.         JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainWindow)
  377.     };
  378.  
  379. private:
  380.     OwnedArray<MainWindow> mainWindows;
  381. };
  382.  
  383. //==============================================================================
  384. // This macro generates the main() routine that launches the app.
  385. START_JUCE_APPLICATION (OpenGLPerformanceApplication)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement