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.

juce_DrawableButton.h 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. A button that displays a Drawable.
  24. Up to three Drawable objects can be given to this button, to represent the
  25. 'normal', 'over' and 'down' states.
  26. @see Button
  27. */
  28. class JUCE_API DrawableButton : public Button
  29. {
  30. public:
  31. //==============================================================================
  32. enum ButtonStyle
  33. {
  34. ImageFitted, /**< The button will just display the images, but will resize and centre them to fit inside it. */
  35. ImageRaw, /**< The button will just display the images in their normal size and position.
  36. This leaves it up to the caller to make sure the images are the correct size and position for the button. */
  37. ImageAboveTextLabel, /**< Draws the button as a text label across the bottom with the image resized and scaled to fit above it. */
  38. ImageOnButtonBackground, /**< Draws the button as a standard rounded-rectangle button with the image on top.
  39. Note that if you use this style, the colour IDs that control the button colour are
  40. TextButton::buttonColourId and TextButton::buttonOnColourId. */
  41. ImageStretched /**< Fills the button with a stretched version of the image. */
  42. };
  43. //==============================================================================
  44. /** Creates a DrawableButton.
  45. After creating one of these, use setImages() to specify the drawables to use.
  46. @param buttonName the name to give the component
  47. @param buttonStyle the layout to use
  48. @see ButtonStyle, setButtonStyle, setImages
  49. */
  50. DrawableButton (const String& buttonName,
  51. ButtonStyle buttonStyle);
  52. /** Destructor. */
  53. ~DrawableButton();
  54. //==============================================================================
  55. /** Sets up the images to draw for the various button states.
  56. The button will keep its own internal copies of these drawables.
  57. @param normalImage the thing to draw for the button's 'normal' state. An internal copy
  58. will be made of the object passed-in if it is non-null.
  59. @param overImage the thing to draw for the button's 'over' state - if this is
  60. null, the button's normal image will be used when the mouse is
  61. over it. An internal copy will be made of the object passed-in
  62. if it is non-null.
  63. @param downImage the thing to draw for the button's 'down' state - if this is
  64. null, the 'over' image will be used instead (or the normal image
  65. as a last resort). An internal copy will be made of the object
  66. passed-in if it is non-null.
  67. @param disabledImage an image to draw when the button is disabled. If this is null,
  68. the normal image will be drawn with a reduced opacity instead.
  69. An internal copy will be made of the object passed-in if it is
  70. non-null.
  71. @param normalImageOn same as the normalImage, but this is used when the button's toggle
  72. state is 'on'. If this is nullptr, the normal image is used instead
  73. @param overImageOn same as the overImage, but this is used when the button's toggle
  74. state is 'on'. If this is nullptr, the normalImageOn is drawn instead
  75. @param downImageOn same as the downImage, but this is used when the button's toggle
  76. state is 'on'. If this is nullptr, the overImageOn is drawn instead
  77. @param disabledImageOn same as the disabledImage, but this is used when the button's toggle
  78. state is 'on'. If this is nullptr, the normal image will be drawn instead
  79. with a reduced opacity
  80. */
  81. void setImages (const Drawable* normalImage,
  82. const Drawable* overImage = nullptr,
  83. const Drawable* downImage = nullptr,
  84. const Drawable* disabledImage = nullptr,
  85. const Drawable* normalImageOn = nullptr,
  86. const Drawable* overImageOn = nullptr,
  87. const Drawable* downImageOn = nullptr,
  88. const Drawable* disabledImageOn = nullptr);
  89. //==============================================================================
  90. /** Changes the button's style.
  91. @see ButtonStyle
  92. */
  93. void setButtonStyle (ButtonStyle newStyle);
  94. /** Returns the current style. */
  95. ButtonStyle getStyle() const noexcept { return style; }
  96. //==============================================================================
  97. /** Gives the button an optional amount of space around the edge of the drawable.
  98. By default there's a gap of about 3 pixels.
  99. */
  100. void setEdgeIndent (int numPixelsIndent);
  101. /** Returns the current edge indent size. */
  102. int getEdgeIndent() const noexcept { return edgeIndent; }
  103. //==============================================================================
  104. /** Returns the image that the button is currently displaying. */
  105. Drawable* getCurrentImage() const noexcept;
  106. /** Returns the image that the button will use for its normal state. */
  107. Drawable* getNormalImage() const noexcept;
  108. /** Returns the image that the button will use when the mouse is over it. */
  109. Drawable* getOverImage() const noexcept;
  110. /** Returns the image that the button will use when the mouse is held down on it. */
  111. Drawable* getDownImage() const noexcept;
  112. /** Can be overridden to specify a custom position for the image within the button. */
  113. virtual Rectangle<float> getImageBounds() const;
  114. //==============================================================================
  115. /** A set of colour IDs to use to change the colour of various aspects of the link.
  116. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  117. methods.
  118. Note that when the ImageOnButtonBackground style is used, the colour IDs that control
  119. the button colour are TextButton::buttonColourId and TextButton::buttonOnColourId.
  120. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  121. */
  122. enum ColourIds
  123. {
  124. textColourId = 0x1004010, /**< The colour to use for the button's text label. */
  125. textColourOnId = 0x1004013, /**< The colour to use for the button's text when the button's toggle state is "on". */
  126. backgroundColourId = 0x1004011, /**< The colour used to fill the button's background (when
  127. the button is toggled 'off'). Note that if you use the
  128. ImageOnButtonBackground style, you should use TextButton::buttonColourId
  129. to change the button's colour. */
  130. backgroundOnColourId = 0x1004012, /**< The colour used to fill the button's background (when
  131. the button is toggled 'on'). Note that if you use the
  132. ImageOnButtonBackground style, you should use TextButton::buttonOnColourId
  133. to change the button's colour. */
  134. };
  135. //==============================================================================
  136. /** @internal */
  137. void paintButton (Graphics&, bool isMouseOverButton, bool isButtonDown) override;
  138. /** @internal */
  139. void buttonStateChanged() override;
  140. /** @internal */
  141. void resized() override;
  142. /** @internal */
  143. void enablementChanged() override;
  144. /** @internal */
  145. void colourChanged() override;
  146. private:
  147. //==============================================================================
  148. ButtonStyle style;
  149. ScopedPointer<Drawable> normalImage, overImage, downImage, disabledImage,
  150. normalImageOn, overImageOn, downImageOn, disabledImageOn;
  151. Drawable* currentImage = nullptr;
  152. int edgeIndent = 3;
  153. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DrawableButton)
  154. };
  155. } // namespace juce