The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

225 lines
6.6KB

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