The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
8.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. //==============================================================================
  21. /**
  22. As the title suggests, this is a button containing an image.
  23. The colour and transparency of the image can be set to vary when the
  24. button state changes.
  25. @see Button, ShapeButton, TextButton
  26. */
  27. class JUCE_API ImageButton : public Button
  28. {
  29. public:
  30. //==============================================================================
  31. /** Creates an ImageButton.
  32. Use setImage() to specify the image to use. The colours and opacities that
  33. are specified here can be changed later using setImages().
  34. @param name the name to give the component
  35. */
  36. explicit ImageButton (const String& name = String());
  37. /** Destructor. */
  38. ~ImageButton();
  39. //==============================================================================
  40. /** Sets up the images to draw in various states.
  41. @param resizeButtonNowToFitThisImage if true, the button will be immediately
  42. resized to the same dimensions as the normal image
  43. @param rescaleImagesWhenButtonSizeChanges if true, the image will be rescaled to fit the
  44. button when the button's size changes
  45. @param preserveImageProportions if true then any rescaling of the image to fit
  46. the button will keep the image's x and y proportions
  47. correct - i.e. it won't distort its shape, although
  48. this might create gaps around the edges
  49. @param normalImage the image to use when the button is in its normal state.
  50. button no longer needs it.
  51. @param imageOpacityWhenNormal the opacity to use when drawing the normal image.
  52. @param overlayColourWhenNormal an overlay colour to use to fill the alpha channel of the
  53. normal image - if this colour is transparent, no overlay
  54. will be drawn. The overlay will be drawn over the top of the
  55. image, so you can basically add a solid or semi-transparent
  56. colour to the image to brighten or darken it
  57. @param overImage the image to use when the mouse is over the button. If
  58. you want to use the same image as was set in the normalImage
  59. parameter, this value can be a null image.
  60. @param imageOpacityWhenOver the opacity to use when drawing the image when the mouse
  61. is over the button
  62. @param overlayColourWhenOver an overlay colour to use to fill the alpha channel of the
  63. image when the mouse is over - if this colour is transparent,
  64. no overlay will be drawn
  65. @param downImage an image to use when the button is pressed down. If set
  66. to a null image, the 'over' image will be drawn instead (or the
  67. normal image if there isn't an 'over' image either).
  68. @param imageOpacityWhenDown the opacity to use when drawing the image when the button
  69. is pressed
  70. @param overlayColourWhenDown an overlay colour to use to fill the alpha channel of the
  71. image when the button is pressed down - if this colour is
  72. transparent, no overlay will be drawn
  73. @param hitTestAlphaThreshold if set to zero, the mouse is considered to be over the button
  74. whenever it's inside the button's bounding rectangle. If
  75. set to values higher than 0, the mouse will only be
  76. considered to be over the image when the value of the
  77. image's alpha channel at that position is greater than
  78. this level.
  79. */
  80. void setImages (bool resizeButtonNowToFitThisImage,
  81. bool rescaleImagesWhenButtonSizeChanges,
  82. bool preserveImageProportions,
  83. const Image& normalImage,
  84. float imageOpacityWhenNormal,
  85. Colour overlayColourWhenNormal,
  86. const Image& overImage,
  87. float imageOpacityWhenOver,
  88. Colour overlayColourWhenOver,
  89. const Image& downImage,
  90. float imageOpacityWhenDown,
  91. Colour overlayColourWhenDown,
  92. float hitTestAlphaThreshold = 0.0f);
  93. /** Returns the currently set 'normal' image. */
  94. Image getNormalImage() const;
  95. /** Returns the image that's drawn when the mouse is over the button.
  96. If a valid 'over' image has been set, this will return it; otherwise it'll
  97. just return the normal image.
  98. */
  99. Image getOverImage() const;
  100. /** Returns the image that's drawn when the button is held down.
  101. If a valid 'down' image has been set, this will return it; otherwise it'll
  102. return the 'over' image or normal image, depending on what's available.
  103. */
  104. Image getDownImage() const;
  105. //==============================================================================
  106. /** This abstract base class is implemented by LookAndFeel classes. */
  107. struct JUCE_API LookAndFeelMethods
  108. {
  109. virtual ~LookAndFeelMethods() {}
  110. virtual void drawImageButton (Graphics&, Image*,
  111. int imageX, int imageY, int imageW, int imageH,
  112. const Colour& overlayColour, float imageOpacity, ImageButton&) = 0;
  113. };
  114. protected:
  115. //==============================================================================
  116. /** @internal */
  117. bool hitTest (int x, int y) override;
  118. /** @internal */
  119. void paintButton (Graphics&, bool isMouseOver, bool isButtonDown) override;
  120. private:
  121. //==============================================================================
  122. bool scaleImageToFit, preserveProportions;
  123. uint8 alphaThreshold;
  124. Rectangle<int> imageBounds;
  125. Image normalImage, overImage, downImage;
  126. float normalOpacity, overOpacity, downOpacity;
  127. Colour normalOverlay, overOverlay, downOverlay;
  128. Image getCurrentImage() const;
  129. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ImageButton)
  130. };