1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-16 00:34:19 +00:00

Improved ShapeButton's mouse-click movement behaviour.

This commit is contained in:
jules 2013-07-01 22:12:07 +01:00
parent 2091e8dfc8
commit a53a50eb35
3 changed files with 45 additions and 20 deletions

View file

@ -938,10 +938,17 @@ void Path::applyTransform (const AffineTransform& transform) noexcept
//==============================================================================
AffineTransform Path::getTransformToScaleToFit (const Rectangle<float>& 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<float> boundsRect (getBounds());

View file

@ -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<float>& area,
bool preserveProportions,
Justification justificationType = Justification::centred) const;
/** Creates a version of this path where all sharp corners have been replaced by curves.

View file

@ -68,9 +68,10 @@ void ShapeButton::setShape (const Path& newShape,
Rectangle<float> 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;
}
Rectangle<float> r (getLocalBounds().toFloat().reduced (outlineWidth * 0.5f));
if (getComponentEffect() != nullptr)
r = r.reduced (2.0f);
if (isButtonDown)
{
const float sizeReductionWhenPressed = 0.04f;
r = r.reduced (sizeReductionWhenPressed * r.getWidth(),
sizeReductionWhenPressed * r.getHeight());
}
const AffineTransform trans (shape.getTransformToScaleToFit (r, maintainShapeProportions));
g.setColour (isButtonDown ? downColour
: isMouseOverButton ? overColour
: normalColour);
int w = getWidth();
int h = getHeight();
if (getComponentEffect() != nullptr)
{
w -= 4;
h -= 4;
}
const float offset = (outlineWidth * 0.5f) + (isButtonDown ? 1.5f : 0.0f);
const AffineTransform trans (shape.getTransformToScaleToFit (offset, offset,
w - offset - outlineWidth,
h - offset - outlineWidth,
maintainShapeProportions));
g.fillPath (shape, trans);
if (outlineWidth > 0.0f)