From 3df7f8ff8bd005037556de84ab06dfbe8fd8f322 Mon Sep 17 00:00:00 2001 From: jules Date: Wed, 9 Aug 2017 10:41:08 +0100 Subject: [PATCH] Did a bit of minor modernising in Graphics + Path --- .../contexts/juce_GraphicsContext.cpp | 48 ++++++++----------- .../contexts/juce_GraphicsContext.h | 18 +++---- modules/juce_graphics/geometry/juce_Path.cpp | 39 ++++++++------- modules/juce_graphics/geometry/juce_Path.h | 36 +++++++------- .../native/juce_RenderingHelpers.h | 19 ++++---- 5 files changed, 71 insertions(+), 89 deletions(-) diff --git a/modules/juce_graphics/contexts/juce_GraphicsContext.cpp b/modules/juce_graphics/contexts/juce_GraphicsContext.cpp index cf527a6ef4..c72c834f05 100644 --- a/modules/juce_graphics/contexts/juce_GraphicsContext.cpp +++ b/modules/juce_graphics/contexts/juce_GraphicsContext.cpp @@ -38,7 +38,7 @@ namespace && (int) h >= 0 && (int) h <= maxVal); #endif - return Rectangle (x, y, w, h); + return { x, y, w, h }; } } @@ -49,15 +49,13 @@ LowLevelGraphicsContext::~LowLevelGraphicsContext() {} //============================================================================== Graphics::Graphics (const Image& imageToDrawOnto) : context (*imageToDrawOnto.createLowLevelContext()), - contextToDelete (&context), - saveStatePending (false) + contextToDelete (&context) { jassert (imageToDrawOnto.isValid()); // Can't draw into a null image! } Graphics::Graphics (LowLevelGraphicsContext& internalContext) noexcept - : context (internalContext), - saveStatePending (false) + : context (internalContext) { } @@ -85,7 +83,7 @@ bool Graphics::reduceClipRegion (Rectangle area) return context.clipToRectangle (area); } -bool Graphics::reduceClipRegion (const int x, const int y, const int w, const int h) +bool Graphics::reduceClipRegion (int x, int y, int w, int h) { return reduceClipRegion (coordsToRectangle (x, y, w, h)); } @@ -157,7 +155,7 @@ void Graphics::setOrigin (Point newOrigin) void Graphics::setOrigin (int x, int y) { - setOrigin (Point (x, y)); + setOrigin ({ x, y }); } void Graphics::addTransform (const AffineTransform& transform) @@ -239,23 +237,20 @@ void Graphics::drawSingleLineText (const String& text, const int startX, const i // Don't pass any vertical placement flags to this method - they'll be ignored. jassert (justification.getOnlyVerticalFlags() == 0); - const int flags = justification.getOnlyHorizontalFlags(); + auto flags = justification.getOnlyHorizontalFlags(); - if (flags == Justification::right) - { - if (startX < context.getClipBounds().getX()) - return; - } - else if (flags == Justification::left) - if (startX > context.getClipBounds().getRight()) - return; + if (flags == Justification::right && startX < context.getClipBounds().getX()) + return; + + if (flags == Justification::left && startX > context.getClipBounds().getRight()) + return; GlyphArrangement arr; arr.addLineOfText (context.getFont(), text, (float) startX, (float) baselineY); if (flags != Justification::left) { - float w = arr.getBoundingBox (0, -1, true).getWidth(); + auto w = arr.getBoundingBox (0, -1, true).getWidth(); if ((flags & (Justification::horizontallyCentred | Justification::horizontallyJustified)) != 0) w /= 2.0f; @@ -371,11 +366,6 @@ void Graphics::fillRectList (const RectangleList& rects) const context.fillRect (r, false); } -void Graphics::setPixel (int x, int y) const -{ - context.fillRect (coordsToRectangle (x, y, 1, 1), false); -} - void Graphics::fillAll() const { fillRect (context.getClipBounds()); @@ -385,7 +375,7 @@ void Graphics::fillAll (Colour colourToUse) const { if (! colourToUse.isTransparent()) { - const Rectangle clip (context.getClipBounds()); + auto clip = context.getClipBounds(); context.saveState(); context.setFill (colourToUse); @@ -507,7 +497,7 @@ void Graphics::drawRoundedRectangle (Rectangle r, float cornerSize, float strokePath (p, PathStrokeType (lineThickness)); } -void Graphics::drawArrow (const Line& line, float lineThickness, float arrowheadWidth, float arrowheadLength) const +void Graphics::drawArrow (Line line, float lineThickness, float arrowheadWidth, float arrowheadLength) const { Path p; p.addArrow (line, lineThickness, arrowheadWidth, arrowheadLength); @@ -531,7 +521,7 @@ void Graphics::fillCheckerBoard (Rectangle area, } else { - const Rectangle clipped (context.getClipBounds().getIntersection (area)); + auto clipped = context.getClipBounds().getIntersection (area); if (! clipped.isEmpty()) { @@ -573,7 +563,7 @@ void Graphics::drawHorizontalLine (const int y, float left, float right) const context.fillRect (Rectangle (left, (float) y, right - left, 1.0f)); } -void Graphics::drawLine (const Line& line) const +void Graphics::drawLine (Line line) const { context.drawLine (line); } @@ -588,15 +578,15 @@ void Graphics::drawLine (float x1, float y1, float x2, float y2, float lineThick drawLine (Line (x1, y1, x2, y2), lineThickness); } -void Graphics::drawLine (const Line& line, const float lineThickness) const +void Graphics::drawLine (Line line, const float lineThickness) const { Path p; p.addLineSegment (line, lineThickness); fillPath (p); } -void Graphics::drawDashedLine (const Line& line, const float* const dashLengths, - const int numDashLengths, const float lineThickness, int n) const +void Graphics::drawDashedLine (Line line, const float* dashLengths, + int numDashLengths, float lineThickness, int n) const { jassert (n >= 0 && n < numDashLengths); // your start index must be valid! diff --git a/modules/juce_graphics/contexts/juce_GraphicsContext.h b/modules/juce_graphics/contexts/juce_GraphicsContext.h index ac2c9ce1bd..1e2e40053b 100644 --- a/modules/juce_graphics/contexts/juce_GraphicsContext.h +++ b/modules/juce_graphics/contexts/juce_GraphicsContext.h @@ -338,12 +338,6 @@ public: void drawRoundedRectangle (Rectangle rectangle, float cornerSize, float lineThickness) const; - /** Fills a 1x1 pixel using the current colour or brush. - Note that because the context may be transformed, this is effectively the same as - calling fillRect (x, y, 1, 1), and the actual result may involve multiple pixels. - */ - void setPixel (int x, int y) const; - //============================================================================== /** Fills an ellipse with the current colour or brush. The ellipse is drawn to fit inside the given rectangle. @@ -388,14 +382,14 @@ public: TIP: If you're trying to draw horizontal or vertical lines, don't use this - it's better to use fillRect() instead unless you really need an angled line. */ - void drawLine (const Line& line) const; + void drawLine (Line line) const; /** Draws a line between two points with a given thickness. @see Path::addLineSegment TIP: If you're trying to draw horizontal or vertical lines, don't use this - it's better to use fillRect() instead unless you really need an angled line. */ - void drawLine (const Line& line, float lineThickness) const; + void drawLine (Line line, float lineThickness) const; /** Draws a dashed line using a custom set of dash-lengths. @@ -408,7 +402,7 @@ public: @param dashIndexToStartFrom the index in the dash-length array to use for the first segment @see PathStrokeType::createDashedStroke */ - void drawDashedLine (const Line& line, + void drawDashedLine (Line line, const float* dashLengths, int numDashLengths, float lineThickness = 1.0f, int dashIndexToStartFrom = 0) const; @@ -441,7 +435,7 @@ public: /** Draws a path's outline using the currently selected colour or brush. */ void strokePath (const Path& path, const PathStrokeType& strokeType, - const AffineTransform& transform = AffineTransform()) const; + const AffineTransform& transform = {}) const; /** Draws a line with an arrowhead at its end. @@ -450,7 +444,7 @@ public: @param arrowheadWidth the width of the arrow head (perpendicular to the line) @param arrowheadLength the length of the arrow head (along the length of the line) */ - void drawArrow (const Line& line, + void drawArrow (Line line, float lineThickness, float arrowheadWidth, float arrowheadLength) const; @@ -743,7 +737,7 @@ private: LowLevelGraphicsContext& context; ScopedPointer contextToDelete; - bool saveStatePending; + bool saveStatePending = false; void saveStateIfPending(); JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Graphics) diff --git a/modules/juce_graphics/geometry/juce_Path.cpp b/modules/juce_graphics/geometry/juce_Path.cpp index 9466776ce1..38b0d53cc0 100644 --- a/modules/juce_graphics/geometry/juce_Path.cpp +++ b/modules/juce_graphics/geometry/juce_Path.cpp @@ -280,7 +280,7 @@ void Path::startNewSubPath (const float x, const float y) data.elements[numElements++] = y; } -void Path::startNewSubPath (const Point start) +void Path::startNewSubPath (Point start) { startNewSubPath (start.x, start.y); } @@ -301,7 +301,7 @@ void Path::lineTo (const float x, const float y) bounds.extend (x, y); } -void Path::lineTo (const Point end) +void Path::lineTo (Point end) { lineTo (end.x, end.y); } @@ -326,8 +326,7 @@ void Path::quadraticTo (const float x1, const float y1, bounds.extend (x1, y1, x2, y2); } -void Path::quadraticTo (const Point controlPoint, - const Point endPoint) +void Path::quadraticTo (Point controlPoint, Point endPoint) { quadraticTo (controlPoint.x, controlPoint.y, endPoint.x, endPoint.y); @@ -358,9 +357,9 @@ void Path::cubicTo (const float x1, const float y1, bounds.extend (x3, y3); } -void Path::cubicTo (const Point controlPoint1, - const Point controlPoint2, - const Point endPoint) +void Path::cubicTo (Point controlPoint1, + Point controlPoint2, + Point endPoint) { cubicTo (controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, @@ -675,7 +674,7 @@ void Path::addPieSegment (Rectangle segmentBounds, } //============================================================================== -void Path::addLineSegment (const Line& line, float lineThickness) +void Path::addLineSegment (Line line, float lineThickness) { auto reversed = line.reversed(); lineThickness *= 0.5f; @@ -687,7 +686,7 @@ void Path::addLineSegment (const Line& line, float lineThickness) closeSubPath(); } -void Path::addArrow (const Line& line, float lineThickness, +void Path::addArrow (Line line, float lineThickness, float arrowheadWidth, float arrowheadLength) { auto reversed = line.reversed(); @@ -705,8 +704,8 @@ void Path::addArrow (const Line& line, float lineThickness, closeSubPath(); } -void Path::addPolygon (const Point centre, const int numberOfSides, - const float radius, const float startAngle) +void Path::addPolygon (Point centre, int numberOfSides, + float radius, float startAngle) { jassert (numberOfSides > 1); // this would be silly. @@ -729,8 +728,8 @@ void Path::addPolygon (const Point centre, const int numberOfSides, } } -void Path::addStar (const Point centre, const int numberOfPoints, - const float innerRadius, const float outerRadius, const float startAngle) +void Path::addStar (Point centre, int numberOfPoints, float innerRadius, + float outerRadius, float startAngle) { jassert (numberOfPoints > 1); // this would be silly. @@ -755,9 +754,9 @@ void Path::addStar (const Point centre, const int numberOfPoints, } } -void Path::addBubble (const Rectangle& bodyArea, - const Rectangle& maximumArea, - const Point arrowTip, +void Path::addBubble (Rectangle bodyArea, + Rectangle maximumArea, + Point arrowTip, const float cornerSize, const float arrowBaseWidth) { @@ -968,8 +967,8 @@ void Path::applyTransform (const AffineTransform& transform) noexcept //============================================================================== -AffineTransform Path::getTransformToScaleToFit (const Rectangle& area, - bool preserveProportions, Justification justification) const +AffineTransform Path::getTransformToScaleToFit (Rectangle area, bool preserveProportions, + Justification justification) const { return getTransformToScaleToFit (area.getX(), area.getY(), area.getWidth(), area.getHeight(), preserveProportions, justification); @@ -1059,7 +1058,7 @@ bool Path::contains (const float x, const float y, const float tolerance) const : ((negativeCrossings + positiveCrossings) & 1) != 0; } -bool Path::contains (const Point point, const float tolerance) const +bool Path::contains (Point point, const float tolerance) const { return contains (point.x, point.y, tolerance); } @@ -1138,7 +1137,7 @@ Point Path::getPointAlongPath (float distanceFromStart, return { i.x2, i.y2 }; } -float Path::getNearestPoint (const Point targetPoint, Point& pointOnPath, +float Path::getNearestPoint (Point targetPoint, Point& pointOnPath, const AffineTransform& transform, float tolerance) const { diff --git a/modules/juce_graphics/geometry/juce_Path.h b/modules/juce_graphics/geometry/juce_Path.h index ac1880ca69..03dc768949 100644 --- a/modules/juce_graphics/geometry/juce_Path.h +++ b/modules/juce_graphics/geometry/juce_Path.h @@ -129,7 +129,7 @@ public: @see closeSubPath, setUsingNonZeroWinding */ - bool contains (const Point point, + bool contains (Point point, float tolerance = defaultToleranceForTesting) const; /** Checks whether a line crosses the path. @@ -211,7 +211,7 @@ public: @see lineTo, quadraticTo, cubicTo, closeSubPath */ - void startNewSubPath (const Point start); + void startNewSubPath (Point start); /** Closes a the current sub-path with a line back to its start-point. @@ -247,7 +247,7 @@ public: @see startNewSubPath, quadraticTo, cubicTo, closeSubPath */ - void lineTo (const Point end); + void lineTo (Point end); /** Adds a quadratic bezier curve from the shape's last position to a new position. @@ -272,8 +272,8 @@ public: @see startNewSubPath, lineTo, cubicTo, closeSubPath */ - void quadraticTo (const Point controlPoint, - const Point endPoint); + void quadraticTo (Point controlPoint, + Point endPoint); /** Adds a cubic bezier curve from the shape's last position to a new position. @@ -300,9 +300,9 @@ public: @see startNewSubPath, lineTo, quadraticTo, closeSubPath */ - void cubicTo (const Point controlPoint1, - const Point controlPoint2, - const Point endPoint); + void cubicTo (Point controlPoint1, + Point controlPoint2, + Point endPoint); /** Returns the last point that was added to the path by one of the drawing methods. */ @@ -320,7 +320,7 @@ public: @see addRoundedRectangle, addTriangle */ template - void addRectangle (const Rectangle& rectangle) + void addRectangle (Rectangle rectangle) { addRectangle (static_cast (rectangle.getX()), static_cast (rectangle.getY()), static_cast (rectangle.getWidth()), static_cast (rectangle.getHeight())); @@ -355,7 +355,7 @@ public: @see addRectangle, addTriangle */ template - void addRoundedRectangle (const Rectangle& rectangle, float cornerSizeX, float cornerSizeY) + void addRoundedRectangle (Rectangle rectangle, float cornerSizeX, float cornerSizeY) { addRoundedRectangle (static_cast (rectangle.getX()), static_cast (rectangle.getY()), static_cast (rectangle.getWidth()), static_cast (rectangle.getHeight()), @@ -367,7 +367,7 @@ public: @see addRectangle, addTriangle */ template - void addRoundedRectangle (const Rectangle& rectangle, float cornerSize) + void addRoundedRectangle (Rectangle rectangle, float cornerSize) { addRoundedRectangle (rectangle, cornerSize, cornerSize); } @@ -534,13 +534,13 @@ public: @see addArrow */ - void addLineSegment (const Line& line, float lineThickness); + void addLineSegment (Line line, float lineThickness); /** Adds a line with an arrowhead on the end. The arrow is added as a new closed sub-path. (Any currently open paths will be left open). @see PathStrokeType::createStrokeWithArrowheads */ - void addArrow (const Line& line, + void addArrow (Line line, float lineThickness, float arrowheadWidth, float arrowheadLength); @@ -548,7 +548,7 @@ public: /** Adds a polygon shape to the path. @see addStar */ - void addPolygon (const Point centre, + void addPolygon (Point centre, int numberOfSides, float radius, float startAngle = 0.0f); @@ -556,7 +556,7 @@ public: /** Adds a star shape to the path. @see addPolygon */ - void addStar (const Point centre, + void addStar (Point centre, int numberOfPoints, float innerRadius, float outerRadius, @@ -572,8 +572,8 @@ public: @param cornerSize the size of the rounded corners @param arrowBaseWidth the width of the base of the arrow where it joins the main rectangle */ - void addBubble (const Rectangle& bodyArea, - const Rectangle& maximumArea, + void addBubble (Rectangle bodyArea, + Rectangle maximumArea, const Point arrowTipPosition, const float cornerSize, const float arrowBaseWidth); @@ -677,7 +677,7 @@ public: @see applyTransform, scaleToFit */ - AffineTransform getTransformToScaleToFit (const Rectangle& area, + AffineTransform getTransformToScaleToFit (Rectangle area, bool preserveProportions, Justification justificationType = Justification::centred) const; diff --git a/modules/juce_graphics/native/juce_RenderingHelpers.h b/modules/juce_graphics/native/juce_RenderingHelpers.h index 1c4cd31efa..5cbb679e38 100644 --- a/modules/juce_graphics/native/juce_RenderingHelpers.h +++ b/modules/juce_graphics/native/juce_RenderingHelpers.h @@ -2295,11 +2295,11 @@ public: } } - void drawLine (const Line& line) + void drawLine (Line line) { Path p; p.addLineSegment (line, 1.0f); - fillPath (p, AffineTransform()); + fillPath (p, {}); } void drawImage (const Image& sourceImage, const AffineTransform& trans) @@ -2319,7 +2319,7 @@ public: void renderImage (const Image& sourceImage, const AffineTransform& trans, const BaseRegionType* const tiledFillClipRegion) { - const AffineTransform t (transform.getTransformWith (trans)); + auto t = transform.getTransformWith (trans); const int alpha = fillType.colour.getAlpha(); @@ -2356,18 +2356,17 @@ public: { if (tiledFillClipRegion != nullptr) { - tiledFillClipRegion->renderImageTransformed (getThis(), sourceImage, alpha, t, interpolationQuality, true); + tiledFillClipRegion->renderImageTransformed (getThis(), sourceImage, alpha, + t, interpolationQuality, true); } else { Path p; p.addRectangle (sourceImage.getBounds()); - typename BaseRegionType::Ptr c (clip->clone()); - c = c->clipToPath (p, t); - - if (c != nullptr) - c->renderImageTransformed (getThis(), sourceImage, alpha, t, interpolationQuality, false); + if (auto c = clip->clone()->clipToPath (p, t)) + c->renderImageTransformed (getThis(), sourceImage, alpha, + t, interpolationQuality, false); } } } @@ -2386,7 +2385,7 @@ public: ColourGradient g2 (*(fillType.gradient)); g2.multiplyOpacity (fillType.getOpacity()); - AffineTransform t (transform.getTransformWith (fillType.transform).translated (-0.5f, -0.5f)); + auto t = transform.getTransformWith (fillType.transform).translated (-0.5f, -0.5f); const bool isIdentity = t.isOnlyTranslation();