|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- /*
- ==============================================================================
-
- This file is part of the JUCE library.
- Copyright (c) 2022 - Raw Material Software Limited
-
- JUCE is an open source library subject to commercial or open-source
- licensing.
-
- By using JUCE, you agree to the terms of both the JUCE 7 End-User License
- Agreement and JUCE Privacy Policy.
-
- End User License Agreement: www.juce.com/juce-7-licence
- Privacy Policy: www.juce.com/juce-privacy-policy
-
- Or: You may also use this code under the terms of the GPL v3 (see
- www.gnu.org/licenses).
-
- JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
- EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
- DISCLAIMED.
-
- ==============================================================================
- */
-
- namespace juce
- {
-
- ImageButton::ImageButton (const String& text_)
- : Button (text_),
- scaleImageToFit (true),
- preserveProportions (true),
- alphaThreshold (0)
- {
- }
-
- ImageButton::~ImageButton()
- {
- }
-
- void ImageButton::setImages (const bool resizeButtonNowToFitThisImage,
- const bool rescaleImagesWhenButtonSizeChanges,
- const bool preserveImageProportions,
- const Image& normalImage_,
- const float imageOpacityWhenNormal,
- Colour overlayColourWhenNormal,
- const Image& overImage_,
- const float imageOpacityWhenOver,
- Colour overlayColourWhenOver,
- const Image& downImage_,
- const float imageOpacityWhenDown,
- Colour overlayColourWhenDown,
- const float hitTestAlphaThreshold)
- {
- normalImage = normalImage_;
- overImage = overImage_;
- downImage = downImage_;
-
- if (resizeButtonNowToFitThisImage && normalImage.isValid())
- {
- imageBounds.setSize (normalImage.getWidth(),
- normalImage.getHeight());
-
- setSize (imageBounds.getWidth(), imageBounds.getHeight());
- }
-
- scaleImageToFit = rescaleImagesWhenButtonSizeChanges;
- preserveProportions = preserveImageProportions;
-
- normalOpacity = imageOpacityWhenNormal;
- normalOverlay = overlayColourWhenNormal;
- overOpacity = imageOpacityWhenOver;
- overOverlay = overlayColourWhenOver;
- downOpacity = imageOpacityWhenDown;
- downOverlay = overlayColourWhenDown;
-
- alphaThreshold = (uint8) jlimit (0, 0xff, roundToInt (255.0f * hitTestAlphaThreshold));
-
- repaint();
- }
-
- Image ImageButton::getCurrentImage() const
- {
- if (isDown() || getToggleState())
- return getDownImage();
-
- if (isOver())
- return getOverImage();
-
- return getNormalImage();
- }
-
- Image ImageButton::getNormalImage() const
- {
- return normalImage;
- }
-
- Image ImageButton::getOverImage() const
- {
- return overImage.isValid() ? overImage
- : normalImage;
- }
-
- Image ImageButton::getDownImage() const
- {
- return downImage.isValid() ? downImage
- : getOverImage();
- }
-
- void ImageButton::paintButton (Graphics& g,
- bool shouldDrawButtonAsHighlighted,
- bool shouldDrawButtonAsDown)
- {
- if (! isEnabled())
- {
- shouldDrawButtonAsHighlighted = false;
- shouldDrawButtonAsDown = false;
- }
-
- Image im (getCurrentImage());
-
- if (im.isValid())
- {
- const int iw = im.getWidth();
- const int ih = im.getHeight();
- int w = getWidth();
- int h = getHeight();
- int x = (w - iw) / 2;
- int y = (h - ih) / 2;
-
- if (scaleImageToFit)
- {
- if (preserveProportions)
- {
- int newW, newH;
- const float imRatio = (float) ih / (float) iw;
- const float destRatio = (float) h / (float) w;
-
- if (imRatio > destRatio)
- {
- newW = roundToInt ((float) h / imRatio);
- newH = h;
- }
- else
- {
- newW = w;
- newH = roundToInt ((float) w * imRatio);
- }
-
- x = (w - newW) / 2;
- y = (h - newH) / 2;
- w = newW;
- h = newH;
- }
- else
- {
- x = 0;
- y = 0;
- }
- }
-
- if (! scaleImageToFit)
- {
- w = iw;
- h = ih;
- }
-
- imageBounds.setBounds (x, y, w, h);
-
- const bool useDownImage = shouldDrawButtonAsDown || getToggleState();
-
- getLookAndFeel().drawImageButton (g, &im, x, y, w, h,
- useDownImage ? downOverlay
- : (shouldDrawButtonAsHighlighted ? overOverlay
- : normalOverlay),
- useDownImage ? downOpacity
- : (shouldDrawButtonAsHighlighted ? overOpacity
- : normalOpacity),
- *this);
- }
- }
-
- bool ImageButton::hitTest (int x, int y)
- {
- if (! Component::hitTest (x, y)) // handle setInterceptsMouseClicks
- return false;
-
- if (alphaThreshold == 0)
- return true;
-
- Image im (getCurrentImage());
-
- return im.isNull() || ((! imageBounds.isEmpty())
- && alphaThreshold < im.getPixelAt (((x - imageBounds.getX()) * im.getWidth()) / imageBounds.getWidth(),
- ((y - imageBounds.getY()) * im.getHeight()) / imageBounds.getHeight()).getAlpha());
- }
-
- } // namespace juce
|