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.cpp 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. DrawableButton::DrawableButton (const String& name, const DrawableButton::ButtonStyle buttonStyle)
  18. : Button (name),
  19. style (buttonStyle),
  20. currentImage (nullptr),
  21. edgeIndent (3)
  22. {
  23. }
  24. DrawableButton::~DrawableButton()
  25. {
  26. }
  27. //==============================================================================
  28. static Drawable* copyDrawableIfNotNull (const Drawable* const d)
  29. {
  30. return d != nullptr ? d->createCopy() : nullptr;
  31. }
  32. void DrawableButton::setImages (const Drawable* normal,
  33. const Drawable* over,
  34. const Drawable* down,
  35. const Drawable* disabled,
  36. const Drawable* normalOn,
  37. const Drawable* overOn,
  38. const Drawable* downOn,
  39. const Drawable* disabledOn)
  40. {
  41. jassert (normal != nullptr); // you really need to give it at least a normal image..
  42. normalImage = copyDrawableIfNotNull (normal);
  43. overImage = copyDrawableIfNotNull (over);
  44. downImage = copyDrawableIfNotNull (down);
  45. disabledImage = copyDrawableIfNotNull (disabled);
  46. normalImageOn = copyDrawableIfNotNull (normalOn);
  47. overImageOn = copyDrawableIfNotNull (overOn);
  48. downImageOn = copyDrawableIfNotNull (downOn);
  49. disabledImageOn = copyDrawableIfNotNull (disabledOn);
  50. buttonStateChanged();
  51. }
  52. //==============================================================================
  53. void DrawableButton::setButtonStyle (const DrawableButton::ButtonStyle newStyle)
  54. {
  55. if (style != newStyle)
  56. {
  57. style = newStyle;
  58. buttonStateChanged();
  59. }
  60. }
  61. void DrawableButton::setEdgeIndent (const int numPixelsIndent)
  62. {
  63. edgeIndent = numPixelsIndent;
  64. repaint();
  65. resized();
  66. }
  67. void DrawableButton::resized()
  68. {
  69. Button::resized();
  70. if (currentImage != nullptr)
  71. {
  72. if (style == ImageRaw)
  73. {
  74. currentImage->setOriginWithOriginalSize (Point<float>());
  75. }
  76. else if (style == ImageStretched)
  77. {
  78. currentImage->setTransformToFit (getLocalBounds().toFloat(), RectanglePlacement::stretchToFit);
  79. }
  80. else
  81. {
  82. Rectangle<int> imageSpace;
  83. const int indentX = jmin (edgeIndent, proportionOfWidth (0.3f));
  84. const int indentY = jmin (edgeIndent, proportionOfHeight (0.3f));
  85. if (style == ImageOnButtonBackground)
  86. {
  87. imageSpace = getLocalBounds().reduced (jmax (getWidth() / 4, indentX),
  88. jmax (getHeight() / 4, indentY));
  89. }
  90. else
  91. {
  92. const int textH = (style == ImageAboveTextLabel) ? jmin (16, proportionOfHeight (0.25f)) : 0;
  93. imageSpace.setBounds (indentX, indentY,
  94. getWidth() - indentX * 2,
  95. getHeight() - indentY * 2 - textH);
  96. }
  97. currentImage->setTransformToFit (imageSpace.toFloat(), RectanglePlacement::centred);
  98. }
  99. }
  100. }
  101. void DrawableButton::buttonStateChanged()
  102. {
  103. repaint();
  104. Drawable* imageToDraw = nullptr;
  105. float opacity = 1.0f;
  106. if (isEnabled())
  107. {
  108. imageToDraw = getCurrentImage();
  109. }
  110. else
  111. {
  112. imageToDraw = getToggleState() ? disabledImageOn
  113. : disabledImage;
  114. if (imageToDraw == nullptr)
  115. {
  116. opacity = 0.4f;
  117. imageToDraw = getNormalImage();
  118. }
  119. }
  120. if (imageToDraw != currentImage)
  121. {
  122. removeChildComponent (currentImage);
  123. currentImage = imageToDraw;
  124. if (currentImage != nullptr)
  125. {
  126. currentImage->setInterceptsMouseClicks (false, false);
  127. addAndMakeVisible (currentImage);
  128. DrawableButton::resized();
  129. }
  130. }
  131. if (currentImage != nullptr)
  132. currentImage->setAlpha (opacity);
  133. }
  134. void DrawableButton::enablementChanged()
  135. {
  136. Button::enablementChanged();
  137. buttonStateChanged();
  138. }
  139. void DrawableButton::colourChanged()
  140. {
  141. repaint();
  142. }
  143. void DrawableButton::paintButton (Graphics& g,
  144. const bool isMouseOverButton,
  145. const bool isButtonDown)
  146. {
  147. LookAndFeel& lf = getLookAndFeel();
  148. if (style == ImageOnButtonBackground)
  149. lf.drawButtonBackground (g, *this,
  150. findColour (getToggleState() ? TextButton::buttonOnColourId
  151. : TextButton::buttonColourId),
  152. isMouseOverButton, isButtonDown);
  153. else
  154. lf.drawDrawableButton (g, *this, isMouseOverButton, isButtonDown);
  155. }
  156. //==============================================================================
  157. Drawable* DrawableButton::getCurrentImage() const noexcept
  158. {
  159. if (isDown()) return getDownImage();
  160. if (isOver()) return getOverImage();
  161. return getNormalImage();
  162. }
  163. Drawable* DrawableButton::getNormalImage() const noexcept
  164. {
  165. return (getToggleState() && normalImageOn != nullptr) ? normalImageOn
  166. : normalImage;
  167. }
  168. Drawable* DrawableButton::getOverImage() const noexcept
  169. {
  170. if (getToggleState())
  171. {
  172. if (overImageOn != nullptr) return overImageOn;
  173. if (normalImageOn != nullptr) return normalImageOn;
  174. }
  175. return overImage != nullptr ? overImage : normalImage;
  176. }
  177. Drawable* DrawableButton::getDownImage() const noexcept
  178. {
  179. if (Drawable* const d = getToggleState() ? downImageOn : downImage)
  180. return d;
  181. return getOverImage();
  182. }