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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. DrawableButton::DrawableButton (const String& name, const DrawableButton::ButtonStyle buttonStyle)
  22. : Button (name), style (buttonStyle)
  23. {
  24. }
  25. DrawableButton::~DrawableButton()
  26. {
  27. }
  28. //==============================================================================
  29. static Drawable* copyDrawableIfNotNull (const Drawable* const d)
  30. {
  31. return d != nullptr ? d->createCopy() : nullptr;
  32. }
  33. void DrawableButton::setImages (const Drawable* normal,
  34. const Drawable* over,
  35. const Drawable* down,
  36. const Drawable* disabled,
  37. const Drawable* normalOn,
  38. const Drawable* overOn,
  39. const Drawable* downOn,
  40. const Drawable* disabledOn)
  41. {
  42. jassert (normal != nullptr); // you really need to give it at least a normal image..
  43. normalImage = copyDrawableIfNotNull (normal);
  44. overImage = copyDrawableIfNotNull (over);
  45. downImage = copyDrawableIfNotNull (down);
  46. disabledImage = copyDrawableIfNotNull (disabled);
  47. normalImageOn = copyDrawableIfNotNull (normalOn);
  48. overImageOn = copyDrawableIfNotNull (overOn);
  49. downImageOn = copyDrawableIfNotNull (downOn);
  50. disabledImageOn = copyDrawableIfNotNull (disabledOn);
  51. currentImage = nullptr;
  52. buttonStateChanged();
  53. }
  54. //==============================================================================
  55. void DrawableButton::setButtonStyle (const DrawableButton::ButtonStyle newStyle)
  56. {
  57. if (style != newStyle)
  58. {
  59. style = newStyle;
  60. buttonStateChanged();
  61. }
  62. }
  63. void DrawableButton::setEdgeIndent (const int numPixelsIndent)
  64. {
  65. edgeIndent = numPixelsIndent;
  66. repaint();
  67. resized();
  68. }
  69. Rectangle<float> DrawableButton::getImageBounds() const
  70. {
  71. auto r = getLocalBounds();
  72. if (style != ImageStretched)
  73. {
  74. auto indentX = jmin (edgeIndent, proportionOfWidth (0.3f));
  75. auto indentY = jmin (edgeIndent, proportionOfHeight (0.3f));
  76. if (style == ImageOnButtonBackground)
  77. {
  78. indentX = jmax (getWidth() / 4, indentX);
  79. indentY = jmax (getHeight() / 4, indentY);
  80. }
  81. else if (style == ImageAboveTextLabel)
  82. {
  83. r = r.withTrimmedBottom (jmin (16, proportionOfHeight (0.25f)));
  84. }
  85. r = r.reduced (indentX, indentY);
  86. }
  87. return r.toFloat();
  88. }
  89. void DrawableButton::resized()
  90. {
  91. Button::resized();
  92. if (currentImage != nullptr)
  93. {
  94. if (style == ImageRaw)
  95. currentImage->setOriginWithOriginalSize (Point<float>());
  96. else
  97. currentImage->setTransformToFit (getImageBounds(),
  98. style == ImageStretched ? RectanglePlacement::stretchToFit
  99. : RectanglePlacement::centred);
  100. }
  101. }
  102. void DrawableButton::buttonStateChanged()
  103. {
  104. repaint();
  105. Drawable* imageToDraw = nullptr;
  106. float opacity = 1.0f;
  107. if (isEnabled())
  108. {
  109. imageToDraw = getCurrentImage();
  110. }
  111. else
  112. {
  113. imageToDraw = getToggleState() ? disabledImageOn
  114. : disabledImage;
  115. if (imageToDraw == nullptr)
  116. {
  117. opacity = 0.4f;
  118. imageToDraw = getNormalImage();
  119. }
  120. }
  121. if (imageToDraw != currentImage)
  122. {
  123. removeChildComponent (currentImage);
  124. currentImage = imageToDraw;
  125. if (currentImage != nullptr)
  126. {
  127. currentImage->setInterceptsMouseClicks (false, false);
  128. addAndMakeVisible (currentImage);
  129. resized();
  130. }
  131. }
  132. if (currentImage != nullptr)
  133. currentImage->setAlpha (opacity);
  134. }
  135. void DrawableButton::enablementChanged()
  136. {
  137. Button::enablementChanged();
  138. buttonStateChanged();
  139. }
  140. void DrawableButton::colourChanged()
  141. {
  142. repaint();
  143. }
  144. void DrawableButton::paintButton (Graphics& g,
  145. const bool isMouseOverButton,
  146. const bool isButtonDown)
  147. {
  148. auto& lf = getLookAndFeel();
  149. if (style == ImageOnButtonBackground)
  150. lf.drawButtonBackground (g, *this,
  151. findColour (getToggleState() ? TextButton::buttonOnColourId
  152. : TextButton::buttonColourId),
  153. isMouseOverButton, isButtonDown);
  154. else
  155. lf.drawDrawableButton (g, *this, isMouseOverButton, isButtonDown);
  156. }
  157. //==============================================================================
  158. Drawable* DrawableButton::getCurrentImage() const noexcept
  159. {
  160. if (isDown()) return getDownImage();
  161. if (isOver()) return getOverImage();
  162. return getNormalImage();
  163. }
  164. Drawable* DrawableButton::getNormalImage() const noexcept
  165. {
  166. return (getToggleState() && normalImageOn != nullptr) ? normalImageOn
  167. : normalImage;
  168. }
  169. Drawable* DrawableButton::getOverImage() const noexcept
  170. {
  171. if (getToggleState())
  172. {
  173. if (overImageOn != nullptr) return overImageOn;
  174. if (normalImageOn != nullptr) return normalImageOn;
  175. }
  176. return overImage != nullptr ? overImage : normalImage;
  177. }
  178. Drawable* DrawableButton::getDownImage() const noexcept
  179. {
  180. if (auto* d = getToggleState() ? downImageOn.get() : downImage.get())
  181. return d;
  182. return getOverImage();
  183. }
  184. } // namespace juce