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
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. ShapeButton::ShapeButton (const String& t, Colour n, Colour o, Colour d)
  18. : Button (t),
  19. normalColour (n), overColour (o), downColour (d),
  20. normalColourOn (n), overColourOn (o), downColourOn (d),
  21. useOnColours(false),
  22. maintainShapeProportions (false),
  23. outlineWidth (0.0f)
  24. {
  25. }
  26. ShapeButton::~ShapeButton() {}
  27. void ShapeButton::setColours (Colour newNormalColour, Colour newOverColour, Colour newDownColour)
  28. {
  29. normalColour = newNormalColour;
  30. overColour = newOverColour;
  31. downColour = newDownColour;
  32. }
  33. void ShapeButton::setOnColours (Colour newNormalColourOn, Colour newOverColourOn, Colour newDownColourOn)
  34. {
  35. normalColourOn = newNormalColourOn;
  36. overColourOn = newOverColourOn;
  37. downColourOn = newDownColourOn;
  38. }
  39. void ShapeButton::shouldUseOnColours (bool shouldUse)
  40. {
  41. useOnColours = shouldUse;
  42. }
  43. void ShapeButton::setOutline (Colour newOutlineColour, const float newOutlineWidth)
  44. {
  45. outlineColour = newOutlineColour;
  46. outlineWidth = newOutlineWidth;
  47. }
  48. void ShapeButton::setBorderSize (BorderSize<int> newBorder)
  49. {
  50. border = newBorder;
  51. }
  52. void ShapeButton::setShape (const Path& newShape,
  53. const bool resizeNowToFitThisShape,
  54. const bool maintainShapeProportions_,
  55. const bool hasShadow)
  56. {
  57. shape = newShape;
  58. maintainShapeProportions = maintainShapeProportions_;
  59. shadow.setShadowProperties (DropShadow (Colours::black.withAlpha (0.5f), 3, Point<int>()));
  60. setComponentEffect (hasShadow ? &shadow : nullptr);
  61. if (resizeNowToFitThisShape)
  62. {
  63. Rectangle<float> newBounds (shape.getBounds());
  64. if (hasShadow)
  65. newBounds = newBounds.expanded (4.0f);
  66. shape.applyTransform (AffineTransform::translation (-newBounds.getX(),
  67. -newBounds.getY()));
  68. setSize (1 + (int) (newBounds.getWidth() + outlineWidth) + border.getLeftAndRight(),
  69. 1 + (int) (newBounds.getHeight() + outlineWidth) + border.getTopAndBottom());
  70. }
  71. repaint();
  72. }
  73. void ShapeButton::paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown)
  74. {
  75. if (! isEnabled())
  76. {
  77. isMouseOverButton = false;
  78. isButtonDown = false;
  79. }
  80. Rectangle<float> r (border.subtractedFrom (getLocalBounds()).toFloat().reduced (outlineWidth * 0.5f));
  81. if (getComponentEffect() != nullptr)
  82. r = r.reduced (2.0f);
  83. if (isButtonDown)
  84. {
  85. const float sizeReductionWhenPressed = 0.04f;
  86. r = r.reduced (sizeReductionWhenPressed * r.getWidth(),
  87. sizeReductionWhenPressed * r.getHeight());
  88. }
  89. const AffineTransform trans (shape.getTransformToScaleToFit (r, maintainShapeProportions));
  90. if (isButtonDown) g.setColour (getToggleState() && useOnColours ? downColourOn : downColour);
  91. else if (isMouseOverButton) g.setColour (getToggleState() && useOnColours ? overColourOn : overColour);
  92. else g.setColour (getToggleState() && useOnColours ? normalColourOn : normalColour);
  93. g.fillPath (shape, trans);
  94. if (outlineWidth > 0.0f)
  95. {
  96. g.setColour (outlineColour);
  97. g.strokePath (shape, PathStrokeType (outlineWidth), trans);
  98. }
  99. }