Audio plugin host https://kx.studio/carla
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.

155 lines
7.9KB

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