1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Minor tweaks to make DrawableButton more customisable.

This commit is contained in:
jules 2014-06-08 11:07:30 +01:00
parent e938b90feb
commit eb8bfa1471
2 changed files with 34 additions and 31 deletions

View file

@ -80,6 +80,31 @@ void DrawableButton::setEdgeIndent (const int numPixelsIndent)
resized();
}
Rectangle<float> DrawableButton::getImageBounds() const
{
Rectangle<int> r (getLocalBounds());
if (style != ImageStretched)
{
int indentX = jmin (edgeIndent, proportionOfWidth (0.3f));
int indentY = jmin (edgeIndent, proportionOfHeight (0.3f));
if (style == ImageOnButtonBackground)
{
indentX = jmax (getWidth() / 4, indentX);
indentY = jmax (getHeight() / 4, indentY);
}
else if (style == ImageAboveTextLabel)
{
r = r.withTrimmedBottom (jmin (16, proportionOfHeight (0.25f)));
}
r = r.reduced (indentX, indentY);
}
return r.toFloat();
}
void DrawableButton::resized()
{
Button::resized();
@ -87,36 +112,11 @@ void DrawableButton::resized()
if (currentImage != nullptr)
{
if (style == ImageRaw)
{
currentImage->setOriginWithOriginalSize (Point<float>());
}
else if (style == ImageStretched)
{
currentImage->setTransformToFit (getLocalBounds().toFloat(), RectanglePlacement::stretchToFit);
}
else
{
Rectangle<int> imageSpace;
const int indentX = jmin (edgeIndent, proportionOfWidth (0.3f));
const int indentY = jmin (edgeIndent, proportionOfHeight (0.3f));
if (style == ImageOnButtonBackground)
{
imageSpace = getLocalBounds().reduced (jmax (getWidth() / 4, indentX),
jmax (getHeight() / 4, indentY));
}
else
{
const int textH = (style == ImageAboveTextLabel) ? jmin (16, proportionOfHeight (0.25f)) : 0;
imageSpace.setBounds (indentX, indentY,
getWidth() - indentX * 2,
getHeight() - indentY * 2 - textH);
}
currentImage->setTransformToFit (imageSpace.toFloat(), RectanglePlacement::centred);
}
currentImage->setTransformToFit (getImageBounds(),
style == ImageStretched ? RectanglePlacement::stretchToFit
: RectanglePlacement::centred);
}
}
@ -152,7 +152,7 @@ void DrawableButton::buttonStateChanged()
{
currentImage->setInterceptsMouseClicks (false, false);
addAndMakeVisible (currentImage);
DrawableButton::resized();
resized();
}
}

View file

@ -132,6 +132,9 @@ public:
/** Returns the image that the button will use when the mouse is held down on it. */
Drawable* getDownImage() const noexcept;
/** Can be overridden to specify a custom position for the image within the button. */
virtual Rectangle<float> getImageBounds() const;
//==============================================================================
/** A set of colour IDs to use to change the colour of various aspects of the link.
@ -173,8 +176,8 @@ public:
private:
//==============================================================================
ButtonStyle style;
ScopedPointer <Drawable> normalImage, overImage, downImage, disabledImage,
normalImageOn, overImageOn, downImageOn, disabledImageOn;
ScopedPointer<Drawable> normalImage, overImage, downImage, disabledImage,
normalImageOn, overImageOn, downImageOn, disabledImageOn;
Drawable* currentImage;
int edgeIndent;