Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ==============================================================================
- This file was auto-generated!
- It contains the basic startup code for a Juce application.
- ==============================================================================
- */
- #include "../JuceLibraryCode/JuceHeader.h"
- inline Colour getRandomColour (float brightness)
- {
- return Colour::fromHSV (Random::getSystemRandom().nextFloat(), 0.5f, brightness, 1.0f);
- }
- inline Colour getRandomBrightColour() { return getRandomColour (0.8f); }
- inline Colour getRandomDarkColour() { return getRandomColour (0.3f); }
- //==============================================================================
- class OpenGLPerformanceApplication : public JUCEApplication
- {
- public:
- //==============================================================================
- OpenGLPerformanceApplication() {}
- const String getApplicationName() override { return ProjectInfo::projectName; }
- const String getApplicationVersion() override { return ProjectInfo::versionString; }
- bool moreThanOneInstanceAllowed() override { return true; }
- //==============================================================================
- void initialise (const String& commandLine) override
- {
- createNewWindow();
- }
- void shutdown() override {}
- static OpenGLPerformanceApplication& getApp()
- {
- return *static_cast<OpenGLPerformanceApplication*> (JUCEApplication::getInstance());
- }
- void createNewWindow()
- {
- mainWindows.add (new MainWindow (getApplicationName()));
- }
- //==============================================================================
- class BallGeneratorComponent : public Component
- {
- public:
- BallGeneratorComponent()
- {
- }
- void paint (Graphics& g) override
- {
- Rectangle<float> area (getLocalBounds().toFloat().reduced (2.0f));
- g.setColour (Colours::orange);
- g.drawRoundedRectangle (area, 10.0f, 2.0f);
- AttributedString s;
- s.setJustification (Justification::centred);
- s.setWordWrap (AttributedString::none);
- s.append ("Drag Me!");
- s.setColour (findColour (TextButton::textColourOffId));
- s.draw (g, area);
- }
- void resized() override
- {
- // Just set the limits of our constrainer so that we don't drag ourselves off the screen
- constrainer.setMinimumOnscreenAmounts (getHeight(), getWidth(), getHeight(), getWidth());
- }
- void mouseDown (const MouseEvent& e) override
- {
- // Prepares our dragger to drag this Component
- dragger.startDraggingComponent (this, e);
- }
- void mouseDrag (const MouseEvent& e) override
- {
- // Moves this Component according to the mouse drag event and applies our constraints to it
- dragger.dragComponent (this, e, &constrainer);
- }
- private:
- ComponentBoundsConstrainer constrainer;
- ComponentDragger dragger;
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BallGeneratorComponent)
- };
- //==============================================================================
- struct BallComponent : public Component
- {
- BallComponent (const Point<float>& pos)
- : position (pos),
- speed (Random::getSystemRandom().nextFloat() * 4.0f - 2.0f,
- Random::getSystemRandom().nextFloat() * -6.0f - 2.0f),
- colour (Colours::white)
- {
- setSize (20, 20);
- step();
- }
- bool step()
- {
- position += speed;
- speed.y += 0.1f;
- setCentrePosition ((int) position.x,
- (int) position.y);
- if (Component* parent = getParentComponent())
- return isPositiveAndBelow (position.x, (float) parent->getWidth())
- && position.y < (float) parent->getHeight();
- return position.y < 400.0f && position.x >= -10.0f;
- }
- void paint (Graphics& g) override
- {
- g.setColour (colour);
- g.fillEllipse (2.0f, 2.0f, getWidth() - 4.0f, getHeight() - 4.0f);
- g.setColour (Colours::darkgrey);
- g.drawEllipse (2.0f, 2.0f, getWidth() - 4.0f, getHeight() - 4.0f, 1.0f);
- }
- Point<float> position, speed;
- Colour colour;
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BallComponent)
- };
- //==============================================================================
- class AnimationDemo : public Component,
- private Button::Listener,
- private Timer
- {
- public:
- AnimationDemo()
- {
- context.setComponentPaintingEnabled (true);
- context.attachTo (*this);
- setOpaque (true);
- setSize (620, 620);
- addAndMakeVisible (newWindowButton);
- newWindowButton.addListener (this);
- for (int i = 11; --i >= 0;)
- {
- Button* b = createButton();
- componentsToAnimate.add (b);
- addAndMakeVisible (b);
- b->addListener (this);
- }
- addAndMakeVisible (ballGenerator);
- ballGenerator.centreWithSize (80, 50);
- cycleCount = 2;
- for (int i = 0; i < componentsToAnimate.size(); ++i)
- componentsToAnimate.getUnchecked (i)->setBounds (getLocalBounds().reduced (250, 250));
- for (int i = 0; i < componentsToAnimate.size(); ++i)
- {
- const int newIndex = (i + 3) % componentsToAnimate.size();
- const float angle = newIndex * 2.0f * float_Pi / componentsToAnimate.size();
- const float radius = getWidth() * 0.35f;
- Rectangle<int> r (getWidth() / 2 + (int) (radius * std::sin (angle)) - 50,
- getHeight() / 2 + (int) (radius * std::cos (angle)) - 50,
- 100, 100);
- animator.animateComponent (componentsToAnimate.getUnchecked(i),
- r.reduced (10),
- 1.0f,
- 500 + i * 100,
- false,
- 0.0,
- 0.0);
- }
- startTimerHz (60);
- }
- ~AnimationDemo()
- {
- context.detach();
- }
- void paint (Graphics& g) override
- {
- g.fillAll (Desktop::getInstance().getDefaultLookAndFeel().findColour (DocumentWindow::backgroundColourId));
- }
- void resized() override
- {
- newWindowButton.setBounds (getLocalBounds().withSize (100, 20));
- }
- private:
- OpenGLContext context;
- OwnedArray<Component> componentsToAnimate;
- OwnedArray<BallComponent> balls;
- BallGeneratorComponent ballGenerator;
- TextButton newWindowButton {"New Window"};
- ComponentAnimator animator;
- int cycleCount;
- Button* createRandomButton()
- {
- DrawablePath normal, over;
- Path star1;
- star1.addStar (Point<float>(), 5, 20.0f, 50.0f, 0.2f);
- normal.setPath (star1);
- normal.setFill (Colours::red);
- Path star2;
- star2.addStar (Point<float>(), 7, 30.0f, 50.0f, 0.0f);
- over.setPath (star2);
- over.setFill (Colours::pink);
- over.setStrokeFill (Colours::black);
- over.setStrokeThickness (5.0f);
- Image juceIcon = ImageCache::getFromMemory (BinaryData::juce_icon_png,
- BinaryData::juce_icon_pngSize);
- DrawableImage down;
- down.setImage (juceIcon);
- down.setOverlayColour (Colours::black.withAlpha (0.3f));
- if (Random::getSystemRandom().nextInt (10) > 2)
- {
- int type = Random::getSystemRandom().nextInt (3);
- DrawableButton* d = new DrawableButton ("Button",
- type == 0 ? DrawableButton::ImageOnButtonBackground
- : (type == 1 ? DrawableButton::ImageFitted
- : DrawableButton::ImageAboveTextLabel));
- d->setImages (&normal,
- Random::getSystemRandom().nextBool() ? &over : nullptr,
- Random::getSystemRandom().nextBool() ? &down : nullptr);
- if (Random::getSystemRandom().nextBool())
- {
- d->setColour (DrawableButton::backgroundColourId, getRandomBrightColour());
- d->setColour (DrawableButton::backgroundOnColourId, getRandomBrightColour());
- }
- d->setClickingTogglesState (Random::getSystemRandom().nextBool());
- return d;
- }
- ImageButton* b = new ImageButton ("ImageButton");
- b->setImages (true, true, true,
- juceIcon, 0.7f, Colours::transparentBlack,
- juceIcon, 1.0f, getRandomDarkColour().withAlpha (0.2f),
- juceIcon, 1.0f, getRandomBrightColour().withAlpha (0.8f),
- 0.5f);
- return b;
- }
- Button* createButton()
- {
- Image juceIcon = ImageCache::getFromMemory (BinaryData::juce_icon_png,
- BinaryData::juce_icon_pngSize)
- .rescaled (128, 128);
- ImageButton* b = new ImageButton ("ImageButton");
- b->setImages (true, true, true,
- juceIcon, 1.0f, Colours::transparentBlack,
- juceIcon, 1.0f, Colours::white,
- juceIcon, 1.0f, Colours::white,
- 0.5f);
- return b;
- }
- void buttonClicked (Button* btn) override
- {
- if (btn == &newWindowButton)
- {
- getApp().createNewWindow();
- }
- else
- {
- for (int i = 0; i < componentsToAnimate.size(); ++i)
- {
- const int newIndex = (i + 3 * cycleCount) % componentsToAnimate.size();
- const float angle = newIndex * 2.0f * float_Pi / componentsToAnimate.size();
- const float radius = getWidth() * 0.35f;
- Rectangle<int> r (getWidth() / 2 + (int) (radius * std::sin (angle)) - 50,
- getHeight() / 2 + (int) (radius * std::cos (angle)) - 50,
- 100, 100);
- animator.animateComponent (componentsToAnimate.getUnchecked(i),
- r.reduced (10),
- 1.0f,
- 900 + (int) (300 * std::sin (angle)),
- false,
- 0.0,
- 0.0);
- }
- ++cycleCount;
- }
- }
- void timerCallback() override
- {
- // Go through each of our balls and update their position
- for (int i = balls.size(); --i >= 0;)
- if (! balls.getUnchecked(i)->step())
- balls.remove (i);
- // Randomly generate new balls
- if (Random::getSystemRandom().nextInt (100) < 4)
- {
- BallComponent* ball = new BallComponent (ballGenerator.getBounds().getCentre().toFloat());
- addAndMakeVisible (ball);
- balls.add (ball);
- }
- }
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AnimationDemo)
- };
- //==============================================================================
- class MainWindow : public DocumentWindow
- {
- public:
- MainWindow (String name) : DocumentWindow (name,
- Desktop::getInstance().getDefaultLookAndFeel()
- .findColour (ResizableWindow::backgroundColourId),
- DocumentWindow::allButtons)
- {
- setUsingNativeTitleBar (true);
- setContentOwned (new AnimationDemo(), true);
- centreWithSize (getWidth(), getHeight());
- setVisible (true);
- }
- void closeButtonPressed() override
- {
- // This is called when the user tries to close this window. Here, we'll just
- // ask the app to quit when this happens, but you can change this to do
- // whatever you need.
- JUCEApplication::getInstance()->systemRequestedQuit();
- }
- /* Note: Be careful if you override any DocumentWindow methods - the base
- class uses a lot of them, so by overriding you might break its functionality.
- It's best to do all your work in your content component instead, but if
- you really have to override any DocumentWindow methods, make sure your
- subclass also calls the superclass's method.
- */
- private:
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainWindow)
- };
- private:
- OwnedArray<MainWindow> mainWindows;
- };
- //==============================================================================
- // This macro generates the main() routine that launches the app.
- START_JUCE_APPLICATION (OpenGLPerformanceApplication)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement