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

10 years ago
10 years ago
10 years ago
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. Rectangle<float> DrawableButton::getImageBounds() const
  68. {
  69. Rectangle<int> r (getLocalBounds());
  70. if (style != ImageStretched)
  71. {
  72. int indentX = jmin (edgeIndent, proportionOfWidth (0.3f));
  73. int indentY = jmin (edgeIndent, proportionOfHeight (0.3f));
  74. if (style == ImageOnButtonBackground)
  75. {
  76. indentX = jmax (getWidth() / 4, indentX);
  77. indentY = jmax (getHeight() / 4, indentY);
  78. }
  79. else if (style == ImageAboveTextLabel)
  80. {
  81. r = r.withTrimmedBottom (jmin (16, proportionOfHeight (0.25f)));
  82. }
  83. r = r.reduced (indentX, indentY);
  84. }
  85. return r.toFloat();
  86. }
  87. void DrawableButton::resized()
  88. {
  89. Button::resized();
  90. if (currentImage != nullptr)
  91. {
  92. if (style == ImageRaw)
  93. currentImage->setOriginWithOriginalSize (Point<float>());
  94. else
  95. currentImage->setTransformToFit (getImageBounds(),
  96. style == ImageStretched ? RectanglePlacement::stretchToFit
  97. : RectanglePlacement::centred);
  98. }
  99. }
  100. void DrawableButton::buttonStateChanged()
  101. {
  102. repaint();
  103. Drawable* imageToDraw = nullptr;
  104. float opacity = 1.0f;
  105. if (isEnabled())
  106. {
  107. imageToDraw = getCurrentImage();
  108. }
  109. else
  110. {
  111. imageToDraw = getToggleState() ? disabledImageOn
  112. : disabledImage;
  113. if (imageToDraw == nullptr)
  114. {
  115. opacity = 0.4f;
  116. imageToDraw = getNormalImage();
  117. }
  118. }
  119. if (imageToDraw != currentImage)
  120. {
  121. removeChildComponent (currentImage);
  122. currentImage = imageToDraw;
  123. if (currentImage != nullptr)
  124. {
  125. currentImage->setInterceptsMouseClicks (false, false);
  126. addAndMakeVisible (currentImage);
  127. resized();
  128. }
  129. }
  130. if (currentImage != nullptr)
  131. currentImage->setAlpha (opacity);
  132. }
  133. void DrawableButton::enablementChanged()
  134. {
  135. Button::enablementChanged();
  136. buttonStateChanged();
  137. }
  138. void DrawableButton::colourChanged()
  139. {
  140. repaint();
  141. }
  142. void DrawableButton::paintButton (Graphics& g,
  143. const bool isMouseOverButton,
  144. const bool isButtonDown)
  145. {
  146. LookAndFeel& lf = getLookAndFeel();
  147. if (style == ImageOnButtonBackground)
  148. lf.drawButtonBackground (g, *this,
  149. findColour (getToggleState() ? TextButton::buttonOnColourId
  150. : TextButton::buttonColourId),
  151. isMouseOverButton, isButtonDown);
  152. else
  153. lf.drawDrawableButton (g, *this, isMouseOverButton, isButtonDown);
  154. }
  155. //==============================================================================
  156. Drawable* DrawableButton::getCurrentImage() const noexcept
  157. {
  158. if (isDown()) return getDownImage();
  159. if (isOver()) return getOverImage();
  160. return getNormalImage();
  161. }
  162. Drawable* DrawableButton::getNormalImage() const noexcept
  163. {
  164. return (getToggleState() && normalImageOn != nullptr) ? normalImageOn
  165. : normalImage;
  166. }
  167. Drawable* DrawableButton::getOverImage() const noexcept
  168. {
  169. if (getToggleState())
  170. {
  171. if (overImageOn != nullptr) return overImageOn;
  172. if (normalImageOn != nullptr) return normalImageOn;
  173. }
  174. return overImage != nullptr ? overImage : normalImage;
  175. }
  176. Drawable* DrawableButton::getDownImage() const noexcept
  177. {
  178. if (Drawable* const d = getToggleState() ? downImageOn : downImage)
  179. return d;
  180. return getOverImage();
  181. }