Browse Source

Minor tweaks to make DrawableButton more customisable.

tags/2021-05-28
jules 11 years ago
parent
commit
eb8bfa1471
2 changed files with 34 additions and 31 deletions
  1. +29
    -29
      modules/juce_gui_basics/buttons/juce_DrawableButton.cpp
  2. +5
    -2
      modules/juce_gui_basics/buttons/juce_DrawableButton.h

+ 29
- 29
modules/juce_gui_basics/buttons/juce_DrawableButton.cpp View File

@@ -80,6 +80,31 @@ void DrawableButton::setEdgeIndent (const int numPixelsIndent)
resized(); 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() void DrawableButton::resized()
{ {
Button::resized(); Button::resized();
@@ -87,36 +112,11 @@ void DrawableButton::resized()
if (currentImage != nullptr) if (currentImage != nullptr)
{ {
if (style == ImageRaw) if (style == ImageRaw)
{
currentImage->setOriginWithOriginalSize (Point<float>()); currentImage->setOriginWithOriginalSize (Point<float>());
}
else if (style == ImageStretched)
{
currentImage->setTransformToFit (getLocalBounds().toFloat(), RectanglePlacement::stretchToFit);
}
else 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); currentImage->setInterceptsMouseClicks (false, false);
addAndMakeVisible (currentImage); addAndMakeVisible (currentImage);
DrawableButton::resized();
resized();
} }
} }


+ 5
- 2
modules/juce_gui_basics/buttons/juce_DrawableButton.h View File

@@ -132,6 +132,9 @@ public:
/** Returns the image that the button will use when the mouse is held down on it. */ /** Returns the image that the button will use when the mouse is held down on it. */
Drawable* getDownImage() const noexcept; 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. /** A set of colour IDs to use to change the colour of various aspects of the link.
@@ -173,8 +176,8 @@ public:
private: private:
//============================================================================== //==============================================================================
ButtonStyle style; ButtonStyle style;
ScopedPointer <Drawable> normalImage, overImage, downImage, disabledImage,
normalImageOn, overImageOn, downImageOn, disabledImageOn;
ScopedPointer<Drawable> normalImage, overImage, downImage, disabledImage,
normalImageOn, overImageOn, downImageOn, disabledImageOn;
Drawable* currentImage; Drawable* currentImage;
int edgeIndent; int edgeIndent;


Loading…
Cancel
Save