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.

137 lines
3.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #pragma once
  19. //==============================================================================
  20. class IconButton final : public Button
  21. {
  22. public:
  23. IconButton (String buttonName, Image imageToDisplay)
  24. : Button (buttonName),
  25. iconImage (imageToDisplay)
  26. {
  27. setTooltip (buttonName);
  28. }
  29. IconButton (String buttonName, Path pathToDisplay)
  30. : Button (buttonName),
  31. iconPath (pathToDisplay),
  32. iconImage (createImageFromPath (iconPath))
  33. {
  34. setTooltip (buttonName);
  35. }
  36. void setImage (Image newImage)
  37. {
  38. iconImage = newImage;
  39. repaint();
  40. }
  41. void setPath (Path newPath)
  42. {
  43. iconImage = createImageFromPath (newPath);
  44. repaint();
  45. }
  46. void setBackgroundColour (Colour backgroundColourToUse)
  47. {
  48. backgroundColour = backgroundColourToUse;
  49. usingNonDefaultBackgroundColour = true;
  50. }
  51. void setIconInset (int newIconInset)
  52. {
  53. iconInset = newIconInset;
  54. repaint();
  55. }
  56. void paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown) override
  57. {
  58. float alpha = 1.0f;
  59. if (! isEnabled())
  60. {
  61. isMouseOverButton = false;
  62. isButtonDown = false;
  63. alpha = 0.2f;
  64. }
  65. auto fill = isButtonDown ? backgroundColour.darker (0.5f)
  66. : isMouseOverButton ? backgroundColour.darker (0.2f)
  67. : backgroundColour;
  68. auto bounds = getLocalBounds();
  69. if (isButtonDown)
  70. bounds.reduce (2, 2);
  71. Path ellipse;
  72. ellipse.addEllipse (bounds.toFloat());
  73. g.reduceClipRegion (ellipse);
  74. g.setColour (fill.withAlpha (alpha));
  75. g.fillAll();
  76. g.setOpacity (alpha);
  77. g.drawImage (iconImage, bounds.reduced (iconInset).toFloat(), RectanglePlacement::fillDestination, false);
  78. }
  79. private:
  80. void lookAndFeelChanged() override
  81. {
  82. if (! usingNonDefaultBackgroundColour)
  83. backgroundColour = findColour (defaultButtonBackgroundColourId);
  84. if (iconPath != Path())
  85. iconImage = createImageFromPath (iconPath);
  86. repaint();
  87. }
  88. Image createImageFromPath (Path path)
  89. {
  90. Image image (Image::ARGB, 250, 250, true);
  91. Graphics g (image);
  92. g.setColour (findColour (defaultIconColourId));
  93. g.fillPath (path, RectanglePlacement (RectanglePlacement::centred)
  94. .getTransformToFit (path.getBounds(), image.getBounds().toFloat()));
  95. return image;
  96. }
  97. Path iconPath;
  98. Image iconImage;
  99. Colour backgroundColour { findColour (defaultButtonBackgroundColourId) };
  100. bool usingNonDefaultBackgroundColour = false;
  101. int iconInset = 2;
  102. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (IconButton)
  103. };