From dab4741eb3eebdaf67f07b8873f76ccd95367400 Mon Sep 17 00:00:00 2001 From: ed Date: Mon, 21 Oct 2019 12:22:05 +0100 Subject: [PATCH] Tidied up a few bits of graphics code with Graphics::ScopedSaveState --- .../components/juce_Component.cpp | 20 ++++-------- .../lookandfeel/juce_LookAndFeel_V2.cpp | 32 +++++++++---------- 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/modules/juce_gui_basics/components/juce_Component.cpp b/modules/juce_gui_basics/components/juce_Component.cpp index 1a2106a4c3..1362239d87 100644 --- a/modules/juce_gui_basics/components/juce_Component.cpp +++ b/modules/juce_gui_basics/components/juce_Component.cpp @@ -1899,12 +1899,10 @@ void Component::paintComponentAndChildren (Graphics& g) } else { - g.saveState(); + Graphics::ScopedSaveState ss (g); if (! (ComponentHelpers::clipObscuredRegions (*this, g, clipBounds, {}) && g.isClipEmpty())) paint (g); - - g.restoreState(); } for (int i = 0; i < childComponentList.size(); ++i) @@ -1915,17 +1913,16 @@ void Component::paintComponentAndChildren (Graphics& g) { if (child.affineTransform != nullptr) { - g.saveState(); + Graphics::ScopedSaveState ss (g); + g.addTransform (*child.affineTransform); if ((child.flags.dontClipGraphicsFlag && ! g.isClipEmpty()) || g.reduceClipRegion (child.getBounds())) child.paintWithinParentContext (g); - - g.restoreState(); } else if (clipBounds.intersects (child.getBounds())) { - g.saveState(); + Graphics::ScopedSaveState ss (g); if (child.flags.dontClipGraphicsFlag) { @@ -1949,15 +1946,12 @@ void Component::paintComponentAndChildren (Graphics& g) if (nothingClipped || ! g.isClipEmpty()) child.paintWithinParentContext (g); } - - g.restoreState(); } } } - g.saveState(); + Graphics::ScopedSaveState ss (g); paintOverChildren (g); - g.restoreState(); } void Component::paintEntireComponent (Graphics& g, bool ignoreAlphaLevel) @@ -1989,10 +1983,10 @@ void Component::paintEntireComponent (Graphics& g, bool ignoreAlphaLevel) paintComponentAndChildren (g2); } - g.saveState(); + Graphics::ScopedSaveState ss (g); + g.addTransform (AffineTransform::scale (1.0f / scale)); effect->applyEffect (effectImage, g, scale, ignoreAlphaLevel ? 1.0f : getAlpha()); - g.restoreState(); } else if (componentTransparency > 0 && ! ignoreAlphaLevel) { diff --git a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V2.cpp b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V2.cpp index f147120004..1818f73f99 100644 --- a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V2.cpp +++ b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V2.cpp @@ -774,15 +774,16 @@ void LookAndFeel_V2::drawScrollbar (Graphics& g, g.setGradientFill (ColourGradient (Colour (0x10000000), gx1, gy1, Colours::transparentBlack, gx2, gy2, false)); - g.saveState(); + { + Graphics::ScopedSaveState ss (g); - if (isScrollbarVertical) - g.reduceClipRegion (x + width / 2, y, width, height); - else - g.reduceClipRegion (x, y + height / 2, width, height); + if (isScrollbarVertical) + g.reduceClipRegion (x + width / 2, y, width, height); + else + g.reduceClipRegion (x, y + height / 2, width, height); - g.fillPath (thumbPath); - g.restoreState(); + g.fillPath (thumbPath); + } g.setColour (Colour (0x4c000000)); g.strokePath (thumbPath, PathStrokeType (0.4f)); @@ -1735,7 +1736,8 @@ void LookAndFeel_V2::drawResizableFrame (Graphics& g, int w, int h, const Border const Rectangle fullSize (0, 0, w, h); auto centreArea = border.subtractedFrom (fullSize); - g.saveState(); + Graphics::ScopedSaveState ss (g); + g.excludeClipRegion (centreArea); g.setColour (Colour (0x50000000)); @@ -1743,8 +1745,6 @@ void LookAndFeel_V2::drawResizableFrame (Graphics& g, int w, int h, const Border g.setColour (Colour (0x19000000)); g.drawRect (centreArea.expanded (1, 1)); - - g.restoreState(); } } @@ -2834,7 +2834,7 @@ void LookAndFeel_V2::drawBevel (Graphics& g, const int x, const int y, const int if (g.clipRegionIntersects (Rectangle (x, y, width, height))) { auto& context = g.getInternalContext(); - context.saveState(); + Graphics::ScopedSaveState ss (g); for (int i = bevelThickness; --i >= 0;) { @@ -2850,8 +2850,6 @@ void LookAndFeel_V2::drawBevel (Graphics& g, const int x, const int y, const int context.setFill (bottomRightColour.withMultipliedAlpha (op * 0.75f)); context.fillRect (Rectangle (x + width - i - 1, y + i + 1, 1, height - i * 2 - 2), false); } - - context.restoreState(); } } @@ -3015,11 +3013,11 @@ void LookAndFeel_V2::drawGlassLozenge (Graphics& g, if (! (flatOnLeft || flatOnTop || flatOnBottom)) { - g.saveState(); + Graphics::ScopedSaveState ss (g); + g.setGradientFill (cg); g.reduceClipRegion (intX, intY, intEdge, intH); g.fillPath (outline); - g.restoreState(); } if (! (flatOnRight || flatOnTop || flatOnBottom)) @@ -3027,11 +3025,11 @@ void LookAndFeel_V2::drawGlassLozenge (Graphics& g, cg.point1.setX (x + width - edgeBlurRadius); cg.point2.setX (x + width); - g.saveState(); + Graphics::ScopedSaveState ss (g); + g.setGradientFill (cg); g.reduceClipRegion (intX + intW - intEdge, intY, 2 + intEdge, intH); g.fillPath (outline); - g.restoreState(); } {