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.

130 lines
3.6KB

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