Drawable Button Request ImageAboveTextLabelOnButtonBackgrou


#1

Hi Jules, would it be possible to add an option to DrawableButton to enable the ImageAboveTextLabel look but drawn on a normal image background?
Also it would be nice to specify the proportion of the height that the text takes up, at the moment this is hard coded at jmin (16, proportionOfheight (0.25f)). With large buttons having such small text looks a bit out of proportion.

I’ve made the appropriate changes but don’t know a clean way of adding in the new height proportion without breaking people’s code who rely on the maximum text size of 16.

In DrawbleButton.h

enum ButtonStyle
    {
        ImageFitted,                /**< The button will just display the images, but will resize and centre them to fit inside it. */
        ImageRaw,                   /**< The button will just display the images in their normal size and position.
                                         This leaves it up to the caller to make sure the images are the correct size and position for the button. */
        ImageAboveTextLabel,        /**< Draws the button as a text label across the bottom with the image resized and scaled to fit above it. */
        ImageOnButtonBackground,     /**< Draws the button as a standard rounded-rectangle button with the image on top. */
        ImageAboveTextLabelOnButtonBackground /**< Draws the button as a text label across the bottom with the image resized and scaled to fit above it with the standard button background. */
    };

/** Sets the proportion of the button height to use for the text.
        Only applies to ImageAboveTextLabel and ImageAboveTextLabelOnButtonBackground.
     */
    void setTextHeightProportion (float newTextHeightProportion);

private:
    float textHeightProportion;

and cpp:

[code]DrawableButton::DrawableButton (const String& name,
const DrawableButton::ButtonStyle buttonStyle)
: Button (name),
style (buttonStyle),
currentImage (nullptr),
edgeIndent (3),
textHeightProportion (0.25f)

void DrawableButton::setTextHeightProportion (float newTextHeightProportion)
{
textHeightProportion = newTextHeightProportion;
repaint();
resized();
}

void DrawableButton::resized()
{
Button::resized();

if (currentImage != nullptr)
{
    if (style == ImageRaw)
    {
        currentImage->setOriginWithOriginalSize (Point<float>());
    }
    else
    {
        Rectangle<int> imageSpace;

        if (style == ImageOnButtonBackground)
        {
            imageSpace = getLocalBounds().reduced (getWidth() / 4, getHeight() / 4);
        }
        else
        {
            const int textH = (style == ImageAboveTextLabel
                               || style == ImageAboveTextLabelOnButtonBackground) ? proportionOfHeight (textHeightProportion) : 0;

            const int indentX = jmin (edgeIndent, proportionOfWidth (0.3f));
            const int indentY = jmin (edgeIndent, proportionOfHeight (0.3f));

            imageSpace.setBounds (indentX, indentY,
                                  getWidth() - indentX * 2,
                                  getHeight() - indentY * 2 - textH);
        }

        currentImage->setTransformToFit (imageSpace.toFloat(), RectanglePlacement::centred);
    }
}

}

void DrawableButton::paintButton (Graphics& g,
bool isMouseOverButton,
bool isButtonDown)
{
if (style == ImageOnButtonBackground
|| style == ImageAboveTextLabelOnButtonBackground)
{
getLookAndFeel().drawButtonBackground (g, *this,
getBackgroundColour(),
isMouseOverButton,
isButtonDown);
}
else
{
g.fillAll (getBackgroundColour());
}

if (style != ImageOnButtonBackground)
{
    const int textH = (style == ImageAboveTextLabel || style == ImageAboveTextLabelOnButtonBackground)
                        ? proportionOfHeight (textHeightProportion)
                        : 0;

    if (textH > 0)
    {
        g.setFont ((float) textH);

        g.setColour (findColour (DrawableButton::textColourId)
                        .withMultipliedAlpha (isEnabled() ? 1.0f : 0.4f));

        g.drawFittedText (getButtonText(),
                          2, getHeight() - textH - 1,
                          getWidth() - 4, textH,
                          Justification::centred, 1);
    }
}

}[/code]

Or a flag to indicate if the button background should be drawn would be ok but that doesn’t really tie in with how the class works at the moment.