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.

119 lines
4.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. ShapeButton::ShapeButton (const String& text_,
  19. const Colour& normalColour_,
  20. const Colour& overColour_,
  21. const Colour& downColour_)
  22. : Button (text_),
  23. normalColour (normalColour_),
  24. overColour (overColour_),
  25. downColour (downColour_),
  26. maintainShapeProportions (false),
  27. outlineWidth (0.0f)
  28. {
  29. }
  30. ShapeButton::~ShapeButton()
  31. {
  32. }
  33. void ShapeButton::setColours (const Colour& newNormalColour,
  34. const Colour& newOverColour,
  35. const Colour& newDownColour)
  36. {
  37. normalColour = newNormalColour;
  38. overColour = newOverColour;
  39. downColour = newDownColour;
  40. }
  41. void ShapeButton::setOutline (const Colour& newOutlineColour,
  42. const float newOutlineWidth)
  43. {
  44. outlineColour = newOutlineColour;
  45. outlineWidth = newOutlineWidth;
  46. }
  47. void ShapeButton::setShape (const Path& newShape,
  48. const bool resizeNowToFitThisShape,
  49. const bool maintainShapeProportions_,
  50. const bool hasShadow)
  51. {
  52. shape = newShape;
  53. maintainShapeProportions = maintainShapeProportions_;
  54. shadow.setShadowProperties (DropShadow (Colours::black.withAlpha (0.5f), 3, Point<int>()));
  55. setComponentEffect (hasShadow ? &shadow : nullptr);
  56. if (resizeNowToFitThisShape)
  57. {
  58. Rectangle<float> newBounds (shape.getBounds());
  59. if (hasShadow)
  60. newBounds.expand (4.0f, 4.0f);
  61. shape.applyTransform (AffineTransform::translation (-newBounds.getX(), -newBounds.getY()));
  62. setSize (1 + (int) (newBounds.getWidth() + outlineWidth),
  63. 1 + (int) (newBounds.getHeight() + outlineWidth));
  64. }
  65. }
  66. void ShapeButton::paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown)
  67. {
  68. if (! isEnabled())
  69. {
  70. isMouseOverButton = false;
  71. isButtonDown = false;
  72. }
  73. g.setColour (isButtonDown ? downColour
  74. : isMouseOverButton ? overColour
  75. : normalColour);
  76. int w = getWidth();
  77. int h = getHeight();
  78. if (getComponentEffect() != nullptr)
  79. {
  80. w -= 4;
  81. h -= 4;
  82. }
  83. const float offset = (outlineWidth * 0.5f) + (isButtonDown ? 1.5f : 0.0f);
  84. const AffineTransform trans (shape.getTransformToScaleToFit (offset, offset,
  85. w - offset - outlineWidth,
  86. h - offset - outlineWidth,
  87. maintainShapeProportions));
  88. g.fillPath (shape, trans);
  89. if (outlineWidth > 0.0f)
  90. {
  91. g.setColour (outlineColour);
  92. g.strokePath (shape, PathStrokeType (outlineWidth), trans);
  93. }
  94. }