diff --git a/examples/Demo/Source/Demos/LookAndFeelDemo.cpp b/examples/Demo/Source/Demos/LookAndFeelDemo.cpp index 5a549a23b8..e675debd5d 100644 --- a/examples/Demo/Source/Demos/LookAndFeelDemo.cpp +++ b/examples/Demo/Source/Demos/LookAndFeelDemo.cpp @@ -292,9 +292,8 @@ struct SquareLookAndFeel : public CustomLookAndFeel if (width > 0 && height > 0) { - g.setGradientFill (ColourGradient (baseColour, 0.0f, 0.0f, - baseColour.darker (0.1f), 0.0f, height, - false)); + g.setGradientFill (ColourGradient::vertical (baseColour, 0.0f, + baseColour.darker (0.1f), height)); g.fillRect (button.getLocalBounds()); } diff --git a/modules/juce_graphics/colour/juce_ColourGradient.cpp b/modules/juce_graphics/colour/juce_ColourGradient.cpp index 6ed948c14b..49d18e06ac 100644 --- a/modules/juce_graphics/colour/juce_ColourGradient.cpp +++ b/modules/juce_graphics/colour/juce_ColourGradient.cpp @@ -27,7 +27,7 @@ namespace juce { -ColourGradient::ColourGradient() noexcept +ColourGradient::ColourGradient() noexcept : isRadial (false) { #if JUCE_DEBUG point1.setX (987654.0f); @@ -37,6 +37,33 @@ ColourGradient::ColourGradient() noexcept #endif } +ColourGradient::ColourGradient (const ColourGradient& other) + : point1 (other.point1), point2 (other.point2), isRadial (other.isRadial), colours (other.colours) +{} + +ColourGradient::ColourGradient (ColourGradient&& other) noexcept + : point1 (other.point1), point2 (other.point2), isRadial (other.isRadial), + colours (static_cast&&> (other.colours)) +{} + +ColourGradient& ColourGradient::operator= (const ColourGradient& other) +{ + point1 = other.point1; + point2 = other.point2; + isRadial = other.isRadial; + colours = other.colours; + return *this; +} + +ColourGradient& ColourGradient::operator= (ColourGradient&& other) noexcept +{ + point1 = other.point1; + point2 = other.point2; + isRadial = other.isRadial; + colours = static_cast&&> (other.colours); + return *this; +} + ColourGradient::ColourGradient (Colour colour1, float x1, float y1, Colour colour2, float x2, float y2, bool radial) : ColourGradient (colour1, Point (x1, y1), @@ -50,12 +77,20 @@ ColourGradient::ColourGradient (Colour colour1, Point p1, point2 (p2), isRadial (radial) { - colours.add (ColourPoint (0.0, colour1)); - colours.add (ColourPoint (1.0, colour2)); + colours.add ({ 0.0, colour1 }); + colours.add ({ 1.0, colour2 }); +} + +ColourGradient::~ColourGradient() {} + +ColourGradient ColourGradient::vertical (Colour c1, float y1, Colour c2, float y2) +{ + return { c1, 0, y1, c2, 0, y2, false }; } -ColourGradient::~ColourGradient() +ColourGradient ColourGradient::horizontal (Colour c1, float x1, Colour c2, float x2) { + return { c1, x1, 0, c2, x2, 0, false }; } bool ColourGradient::operator== (const ColourGradient& other) const noexcept @@ -116,7 +151,7 @@ int ColourGradient::getNumColours() const noexcept return colours.size(); } -double ColourGradient::getColourPosition (const int index) const noexcept +double ColourGradient::getColourPosition (int index) const noexcept { if (isPositiveAndBelow (index, colours.size())) return colours.getReference (index).position; @@ -124,7 +159,7 @@ double ColourGradient::getColourPosition (const int index) const noexcept return 0; } -Colour ColourGradient::getColour (const int index) const noexcept +Colour ColourGradient::getColour (int index) const noexcept { if (isPositiveAndBelow (index, colours.size())) return colours.getReference (index).colour; @@ -138,7 +173,7 @@ void ColourGradient::setColour (int index, Colour newColour) noexcept colours.getReference (index).colour = newColour; } -Colour ColourGradient::getColourAtPosition (const double position) const noexcept +Colour ColourGradient::getColourAtPosition (double position) const noexcept { jassert (colours.getReference(0).position == 0.0); // the first colour specified has to go at position 0 @@ -223,12 +258,12 @@ bool ColourGradient::isInvisible() const noexcept return true; } -bool ColourGradient::ColourPoint::operator== (const ColourPoint& other) const noexcept +bool ColourGradient::ColourPoint::operator== (ColourPoint other) const noexcept { return position == other.position && colour == other.colour; } -bool ColourGradient::ColourPoint::operator!= (const ColourPoint& other) const noexcept +bool ColourGradient::ColourPoint::operator!= (ColourPoint other) const noexcept { return position != other.position || colour != other.colour; } diff --git a/modules/juce_graphics/colour/juce_ColourGradient.h b/modules/juce_graphics/colour/juce_ColourGradient.h index 6d45ddb034..5319af21b2 100644 --- a/modules/juce_graphics/colour/juce_ColourGradient.h +++ b/modules/juce_graphics/colour/juce_ColourGradient.h @@ -36,6 +36,18 @@ namespace juce class JUCE_API ColourGradient final { public: + /** Creates an uninitialised gradient. + + If you use this constructor instead of the other one, be sure to set all the + object's public member variables before using it! + */ + ColourGradient() noexcept; + + ColourGradient (const ColourGradient&); + ColourGradient (ColourGradient&&) noexcept; + ColourGradient& operator= (const ColourGradient&); + ColourGradient& operator= (ColourGradient&&) noexcept; + //============================================================================== /** Creates a gradient object. @@ -79,12 +91,13 @@ public: Colour colour2, Point point2, bool isRadial); - /** Creates an uninitialised gradient. + /** Creates a vertical linear gradient between two Y coordinates */ + static ColourGradient vertical (Colour colour1, float y1, + Colour colour2, float y2); - If you use this constructor instead of the other one, be sure to set all the - object's public member variables before using it! - */ - ColourGradient() noexcept; + /** Creates a horizontal linear gradient between two X coordinates */ + static ColourGradient horizontal (Colour colour1, float x1, + Colour colour2, float x2); /** Destructor */ ~ColourGradient(); @@ -182,13 +195,10 @@ private: struct ColourPoint { ColourPoint() noexcept {} + ColourPoint (double pos, Colour col) noexcept : position (pos), colour (col) {} - ColourPoint (const double pos, Colour col) noexcept - : position (pos), colour (col) - {} - - bool operator== (const ColourPoint&) const noexcept; - bool operator!= (const ColourPoint&) const noexcept; + bool operator== (ColourPoint) const noexcept; + bool operator!= (ColourPoint) const noexcept; double position; Colour colour; diff --git a/modules/juce_graphics/colour/juce_FillType.cpp b/modules/juce_graphics/colour/juce_FillType.cpp index 4fc42296e9..1006b6fa84 100644 --- a/modules/juce_graphics/colour/juce_FillType.cpp +++ b/modules/juce_graphics/colour/juce_FillType.cpp @@ -42,6 +42,11 @@ FillType::FillType (const ColourGradient& g) { } +FillType::FillType (ColourGradient&& g) + : colour (0xff000000), gradient (new ColourGradient (static_cast (g))) +{ +} + FillType::FillType (const Image& im, const AffineTransform& t) noexcept : colour (0xff000000), image (im), transform (t) { diff --git a/modules/juce_graphics/colour/juce_FillType.h b/modules/juce_graphics/colour/juce_FillType.h index 05254b78a2..808ad4f392 100644 --- a/modules/juce_graphics/colour/juce_FillType.h +++ b/modules/juce_graphics/colour/juce_FillType.h @@ -53,6 +53,11 @@ public: */ FillType (const ColourGradient& gradient); + /** Creates a gradient fill type. + @see setGradient + */ + FillType (ColourGradient&& gradient); + /** Creates a tiled image fill type. The transform allows you to set the scaling, offset and rotation of the pattern. @see setTiledImage diff --git a/modules/juce_graphics/contexts/juce_GraphicsContext.cpp b/modules/juce_graphics/contexts/juce_GraphicsContext.cpp index c8df50d4b4..9bdbf057ca 100644 --- a/modules/juce_graphics/contexts/juce_GraphicsContext.cpp +++ b/modules/juce_graphics/contexts/juce_GraphicsContext.cpp @@ -190,7 +190,7 @@ void Graphics::setColour (Colour newColour) context.setFill (newColour); } -void Graphics::setOpacity (const float newOpacity) +void Graphics::setOpacity (float newOpacity) { saveStateIfPending(); context.setOpacity (newOpacity); @@ -201,6 +201,11 @@ void Graphics::setGradientFill (const ColourGradient& gradient) setFillType (gradient); } +void Graphics::setGradientFill (ColourGradient&& gradient) +{ + setFillType (static_cast (gradient)); +} + void Graphics::setTiledImageFill (const Image& imageToUse, const int anchorX, const int anchorY, const float opacity) { saveStateIfPending(); diff --git a/modules/juce_graphics/contexts/juce_GraphicsContext.h b/modules/juce_graphics/contexts/juce_GraphicsContext.h index 41bbd69b60..86260657bb 100644 --- a/modules/juce_graphics/contexts/juce_GraphicsContext.h +++ b/modules/juce_graphics/contexts/juce_GraphicsContext.h @@ -82,10 +82,12 @@ public: */ void setOpacity (float newOpacity); - /** Sets the context to use a gradient for its fill pattern. - */ + /** Sets the context to use a gradient for its fill pattern. */ void setGradientFill (const ColourGradient& gradient); + /** Sets the context to use a gradient for its fill pattern. */ + void setGradientFill (ColourGradient&& gradient); + /** Sets the context to use a tiled image pattern for filling. Make sure that you don't delete this image while it's still being used by this context! diff --git a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V2.cpp b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V2.cpp index 15d02047a7..4dde3a2232 100644 --- a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V2.cpp +++ b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V2.cpp @@ -1273,8 +1273,7 @@ void LookAndFeel_V2::drawLinearSliderBackground (Graphics& g, int x, int y, int const float iy = y + height * 0.5f - sliderRadius * 0.5f; const float ih = sliderRadius; - g.setGradientFill (ColourGradient (gradCol1, 0.0f, iy, - gradCol2, 0.0f, iy + ih, false)); + g.setGradientFill (ColourGradient::vertical (gradCol1, iy, gradCol2, iy + ih)); indent.addRoundedRectangle (x - sliderRadius * 0.5f, iy, width + sliderRadius, ih, @@ -1285,8 +1284,7 @@ void LookAndFeel_V2::drawLinearSliderBackground (Graphics& g, int x, int y, int const float ix = x + width * 0.5f - sliderRadius * 0.5f; const float iw = sliderRadius; - g.setGradientFill (ColourGradient (gradCol1, ix, 0.0f, - gradCol2, ix + iw, 0.0f, false)); + g.setGradientFill (ColourGradient::horizontal (gradCol1, ix, gradCol2, ix + iw)); indent.addRoundedRectangle (ix, y - sliderRadius * 0.5f, iw, height + sliderRadius, @@ -1765,10 +1763,8 @@ void LookAndFeel_V2::drawDocumentWindowTitleBar (DocumentWindow& window, Graphic const bool isActive = window.isActiveWindow(); - g.setGradientFill (ColourGradient (window.getBackgroundColour(), - 0.0f, 0.0f, - window.getBackgroundColour().contrasting (isActive ? 0.15f : 0.05f), - 0.0f, (float) h, false)); + g.setGradientFill (ColourGradient::vertical (window.getBackgroundColour(), 0, + window.getBackgroundColour().contrasting (isActive ? 0.15f : 0.05f), (float) h)); g.fillAll(); Font font (h * 0.65f, Font::bold); diff --git a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V3.cpp b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V3.cpp index 25dffe0b29..d48331bf3f 100644 --- a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V3.cpp +++ b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V3.cpp @@ -93,8 +93,8 @@ void LookAndFeel_V3::drawConcertinaPanelHeader (Graphics& g, const Rectangle r (width, height); @@ -485,7 +483,7 @@ void LookAndFeel_V3::drawMenuBarBackground (Graphics& g, int width, int height, g.fillRect (r.removeFromTop (1)); g.fillRect (r.removeFromBottom (1)); - g.setGradientFill (ColourGradient (colour, 0, 0, colour.darker (0.08f), 0, (float) height, false)); + g.setGradientFill (ColourGradient::vertical (colour, 0, colour.darker (0.08f), (float) height)); g.fillRect (r); } diff --git a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V4.cpp b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V4.cpp index 2c12901b81..fe8729001b 100644 --- a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V4.cpp +++ b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V4.cpp @@ -872,7 +872,7 @@ void LookAndFeel_V4::drawMenuBarBackground (Graphics& g, int width, int height, g.fillRect (r.removeFromTop (1)); g.fillRect (r.removeFromBottom (1)); - g.setGradientFill (ColourGradient (colour, 0, 0, colour.darker (0.2f), 0, (float) height, false)); + g.setGradientFill (ColourGradient::vertical (colour, 0, colour.darker (0.2f), (float) height)); g.fillRect (r); } @@ -1132,8 +1132,8 @@ void LookAndFeel_V4::drawConcertinaPanelHeader (Graphics& g, const Rectangle