From 73828a8cecfb05cb2ac6498989e5bac8d6c7201a Mon Sep 17 00:00:00 2001 From: ed Date: Mon, 8 May 2017 12:49:11 +0100 Subject: [PATCH] Projucer: added drawProgressBar() method to ProjucerLookAndFeel to draw spinning progress indicator --- .../Utility/jucer_ProjucerLookAndFeel.cpp | 66 +++++++++++++++++++ .../Utility/jucer_ProjucerLookAndFeel.h | 2 + 2 files changed, 68 insertions(+) diff --git a/extras/Projucer/Source/Utility/jucer_ProjucerLookAndFeel.cpp b/extras/Projucer/Source/Utility/jucer_ProjucerLookAndFeel.cpp index 41b9c1fa4e..9094e69bd7 100644 --- a/extras/Projucer/Source/Utility/jucer_ProjucerLookAndFeel.cpp +++ b/extras/Projucer/Source/Utility/jucer_ProjucerLookAndFeel.cpp @@ -364,6 +364,72 @@ void ProjucerLookAndFeel::drawTreeviewPlusMinusBox (Graphics& g, const Rectangle g.strokePath (getArrowPath (area, isOpen ? 2 : 1, false, Justification::centredRight), PathStrokeType (2.0f)); } +void ProjucerLookAndFeel::drawProgressBar (Graphics& g, ProgressBar& progressBar, + int width, int height, double progress, const String& textToShow) +{ + ignoreUnused (width, height, progress); + + const auto background = progressBar.findColour (ProgressBar::backgroundColourId); + const auto foreground = progressBar.findColour (defaultButtonBackgroundColourId); + + const auto sideLength = jmin (width, height); + + auto barBounds = progressBar.getLocalBounds().withSizeKeepingCentre (sideLength, sideLength).reduced (1).toFloat(); + + auto rotationInDegrees = static_cast ((Time::getMillisecondCounter() / 10) % 360); + auto normalisedRotation = rotationInDegrees / 360.0f; + + const auto rotationOffset = 22.5f; + const auto maxRotation = 315.0f; + + auto startInDegrees = rotationInDegrees; + auto endInDegrees = startInDegrees + rotationOffset; + + if (normalisedRotation >= 0.25f && normalisedRotation < 0.5f) + { + const auto rescaledRotation = (normalisedRotation * 4.0f) - 1.0f; + endInDegrees = startInDegrees + rotationOffset + (maxRotation * rescaledRotation); + } + else if (normalisedRotation >= 0.5f && normalisedRotation <= 1.0f) + { + endInDegrees = startInDegrees + rotationOffset + maxRotation; + const auto rescaledRotation = 1.0f - ((normalisedRotation * 2.0f) - 1.0f); + startInDegrees = endInDegrees - rotationOffset - (maxRotation * rescaledRotation); + } + + g.setColour (background); + Path arcPath2; + arcPath2.addCentredArc (barBounds.getCentreX(), + barBounds.getCentreY(), + barBounds.getWidth() * 0.5f, + barBounds.getHeight() * 0.5f, 0.0f, + 0.0f, + 2.0f * float_Pi, + true); + g.strokePath (arcPath2, PathStrokeType (2.0f)); + + g.setColour (foreground); + Path arcPath; + arcPath.addCentredArc (barBounds.getCentreX(), + barBounds.getCentreY(), + barBounds.getWidth() * 0.5f, + barBounds.getHeight() * 0.5f, + 0.0f, + degreesToRadians (startInDegrees), + degreesToRadians (endInDegrees), + true); + + arcPath.applyTransform (AffineTransform::rotation (normalisedRotation * float_Pi * 2.25f, barBounds.getCentreX(), barBounds.getCentreY())); + g.strokePath (arcPath, PathStrokeType (2.0f)); + + if (textToShow.isNotEmpty()) + { + g.setColour (progressBar.findColour (TextButton::textColourOffId)); + g.setFont (Font (12.0f, 2)); + g.drawText (textToShow, barBounds, Justification::centred, false); + } +} + //============================================================================== Path ProjucerLookAndFeel::getArrowPath (Rectangle arrowZone, const int direction, bool filled, const Justification justification) diff --git a/extras/Projucer/Source/Utility/jucer_ProjucerLookAndFeel.h b/extras/Projucer/Source/Utility/jucer_ProjucerLookAndFeel.h index 5c81d6c680..093284cc48 100644 --- a/extras/Projucer/Source/Utility/jucer_ProjucerLookAndFeel.h +++ b/extras/Projucer/Source/Utility/jucer_ProjucerLookAndFeel.h @@ -75,6 +75,8 @@ public: void drawTreeviewPlusMinusBox (Graphics&, const Rectangle& area, Colour backgroundColour, bool isItemOpen, bool isMouseOver) override; + void drawProgressBar (Graphics&, ProgressBar&, int width, int height, double progress, const String& textToShow) override; + //============================================================================== static Path getArrowPath (Rectangle arrowZone, const int direction, const bool filled, const Justification justification);