Audio plugin host https://kx.studio/carla
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.

juce_DrawableImage.cpp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. DrawableImage::DrawableImage() : bounds ({ 0.0f, 0.0f, 1.0f, 1.0f })
  21. {
  22. }
  23. DrawableImage::DrawableImage (const DrawableImage& other)
  24. : Drawable (other),
  25. image (other.image),
  26. opacity (other.opacity),
  27. overlayColour (other.overlayColour),
  28. bounds (other.bounds)
  29. {
  30. setBounds (other.getBounds());
  31. }
  32. DrawableImage::~DrawableImage()
  33. {
  34. }
  35. std::unique_ptr<Drawable> DrawableImage::createCopy() const
  36. {
  37. return std::make_unique<DrawableImage> (*this);
  38. }
  39. //==============================================================================
  40. void DrawableImage::setImage (const Image& imageToUse)
  41. {
  42. if (image != imageToUse)
  43. {
  44. image = imageToUse;
  45. setBounds (image.getBounds());
  46. setBoundingBox (image.getBounds().toFloat());
  47. repaint();
  48. }
  49. }
  50. void DrawableImage::setOpacity (const float newOpacity)
  51. {
  52. opacity = newOpacity;
  53. }
  54. void DrawableImage::setOverlayColour (Colour newOverlayColour)
  55. {
  56. overlayColour = newOverlayColour;
  57. }
  58. void DrawableImage::setBoundingBox (Rectangle<float> newBounds)
  59. {
  60. setBoundingBox (Parallelogram<float> (newBounds));
  61. }
  62. void DrawableImage::setBoundingBox (Parallelogram<float> newBounds)
  63. {
  64. if (bounds != newBounds)
  65. {
  66. bounds = newBounds;
  67. if (image.isValid())
  68. {
  69. auto tr = bounds.topLeft + (bounds.topRight - bounds.topLeft) / (float) image.getWidth();
  70. auto bl = bounds.topLeft + (bounds.bottomLeft - bounds.topLeft) / (float) image.getHeight();
  71. auto t = AffineTransform::fromTargetPoints (bounds.topLeft.x, bounds.topLeft.y,
  72. tr.x, tr.y,
  73. bl.x, bl.y);
  74. if (t.isSingularity())
  75. t = {};
  76. setTransform (t);
  77. }
  78. }
  79. }
  80. //==============================================================================
  81. void DrawableImage::paint (Graphics& g)
  82. {
  83. if (image.isValid())
  84. {
  85. if (opacity > 0.0f && ! overlayColour.isOpaque())
  86. {
  87. g.setOpacity (opacity);
  88. g.drawImageAt (image, 0, 0, false);
  89. }
  90. if (! overlayColour.isTransparent())
  91. {
  92. g.setColour (overlayColour.withMultipliedAlpha (opacity));
  93. g.drawImageAt (image, 0, 0, true);
  94. }
  95. }
  96. }
  97. Rectangle<float> DrawableImage::getDrawableBounds() const
  98. {
  99. return image.getBounds().toFloat();
  100. }
  101. bool DrawableImage::hitTest (int x, int y)
  102. {
  103. return Drawable::hitTest (x, y) && image.isValid() && image.getPixelAt (x, y).getAlpha() >= 127;
  104. }
  105. Path DrawableImage::getOutlineAsPath() const
  106. {
  107. return {}; // not applicable for images
  108. }
  109. } // namespace juce