diff --git a/modules/juce_graphics/geometry/juce_Path.cpp b/modules/juce_graphics/geometry/juce_Path.cpp index ddf02cdae8..cd66df0ab6 100644 --- a/modules/juce_graphics/geometry/juce_Path.cpp +++ b/modules/juce_graphics/geometry/juce_Path.cpp @@ -938,10 +938,17 @@ void Path::applyTransform (const AffineTransform& transform) noexcept //============================================================================== +AffineTransform Path::getTransformToScaleToFit (const Rectangle& area, + bool preserveProportions, Justification justification) const +{ + return getTransformToScaleToFit (area.getX(), area.getY(), area.getWidth(), area.getHeight(), + preserveProportions, justification); +} + AffineTransform Path::getTransformToScaleToFit (const float x, const float y, const float w, const float h, const bool preserveProportions, - const Justification& justification) const + Justification justification) const { Rectangle boundsRect (getBounds()); diff --git a/modules/juce_graphics/geometry/juce_Path.h b/modules/juce_graphics/geometry/juce_Path.h index 39861afcf8..ba082327d2 100644 --- a/modules/juce_graphics/geometry/juce_Path.h +++ b/modules/juce_graphics/geometry/juce_Path.h @@ -612,7 +612,25 @@ public: */ AffineTransform getTransformToScaleToFit (float x, float y, float width, float height, bool preserveProportions, - const Justification& justificationType = Justification::centred) const; + Justification justificationType = Justification::centred) const; + + /** Returns a transform that can be used to rescale the path to fit into a given space. + + @param area the rectangle to fit the path inside + @param preserveProportions if true, it will fit the path into the space without altering its + horizontal/vertical scale ratio; if false, it will distort the + path to fill the specified ratio both horizontally and vertically + @param justificationType if the proportions are preseved, the resultant path may be smaller + than the available rectangle, so this describes how it should be + positioned within the space. + @returns an appropriate transformation + + @see applyTransform, scaleToFit + + */ + AffineTransform getTransformToScaleToFit (const Rectangle& area, + bool preserveProportions, + Justification justificationType = Justification::centred) const; /** Creates a version of this path where all sharp corners have been replaced by curves. diff --git a/modules/juce_gui_basics/buttons/juce_ShapeButton.cpp b/modules/juce_gui_basics/buttons/juce_ShapeButton.cpp index 5494ca2819..049edda693 100644 --- a/modules/juce_gui_basics/buttons/juce_ShapeButton.cpp +++ b/modules/juce_gui_basics/buttons/juce_ShapeButton.cpp @@ -68,9 +68,10 @@ void ShapeButton::setShape (const Path& newShape, Rectangle newBounds (shape.getBounds()); if (hasShadow) - newBounds.expand (4.0f, 4.0f); + newBounds = newBounds.expanded (4.0f); - shape.applyTransform (AffineTransform::translation (-newBounds.getX(), -newBounds.getY())); + shape.applyTransform (AffineTransform::translation (-newBounds.getX(), + -newBounds.getY())); setSize (1 + (int) (newBounds.getWidth() + outlineWidth), 1 + (int) (newBounds.getHeight() + outlineWidth)); @@ -85,25 +86,24 @@ void ShapeButton::paintButton (Graphics& g, bool isMouseOverButton, bool isButto isButtonDown = false; } - g.setColour (isButtonDown ? downColour - : isMouseOverButton ? overColour - : normalColour); - - int w = getWidth(); - int h = getHeight(); + Rectangle r (getLocalBounds().toFloat().reduced (outlineWidth * 0.5f)); if (getComponentEffect() != nullptr) + r = r.reduced (2.0f); + + if (isButtonDown) { - w -= 4; - h -= 4; + const float sizeReductionWhenPressed = 0.04f; + + r = r.reduced (sizeReductionWhenPressed * r.getWidth(), + sizeReductionWhenPressed * r.getHeight()); } - const float offset = (outlineWidth * 0.5f) + (isButtonDown ? 1.5f : 0.0f); + const AffineTransform trans (shape.getTransformToScaleToFit (r, maintainShapeProportions)); - const AffineTransform trans (shape.getTransformToScaleToFit (offset, offset, - w - offset - outlineWidth, - h - offset - outlineWidth, - maintainShapeProportions)); + g.setColour (isButtonDown ? downColour + : isMouseOverButton ? overColour + : normalColour); g.fillPath (shape, trans); if (outlineWidth > 0.0f)