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.

132 lines
4.2KB

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