Browse Source

Improved ShapeButton's mouse-click movement behaviour.

tags/2021-05-28
jules 12 years ago
parent
commit
a53a50eb35
3 changed files with 42 additions and 17 deletions
  1. +8
    -1
      modules/juce_graphics/geometry/juce_Path.cpp
  2. +19
    -1
      modules/juce_graphics/geometry/juce_Path.h
  3. +15
    -15
      modules/juce_gui_basics/buttons/juce_ShapeButton.cpp

+ 8
- 1
modules/juce_graphics/geometry/juce_Path.cpp 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());


+ 19
- 1
modules/juce_graphics/geometry/juce_Path.h 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.


+ 15
- 15
modules/juce_gui_basics/buttons/juce_ShapeButton.cpp 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;
}
g.setColour (isButtonDown ? downColour
: isMouseOverButton ? overColour
: normalColour);
int w = getWidth();
int h = getHeight();
Rectangle<float> 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)


Loading…
Cancel
Save