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.

135 lines
4.2KB

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