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.

130 lines
3.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. DrawableImage::DrawableImage() : bounds ({ 0.0f, 0.0f, 1.0f, 1.0f })
  16. {
  17. }
  18. DrawableImage::DrawableImage (const DrawableImage& other)
  19. : Drawable (other),
  20. image (other.image),
  21. opacity (other.opacity),
  22. overlayColour (other.overlayColour),
  23. bounds (other.bounds)
  24. {
  25. setBounds (other.getBounds());
  26. }
  27. DrawableImage::~DrawableImage()
  28. {
  29. }
  30. std::unique_ptr<Drawable> DrawableImage::createCopy() const
  31. {
  32. return std::make_unique<DrawableImage> (*this);
  33. }
  34. //==============================================================================
  35. void DrawableImage::setImage (const Image& imageToUse)
  36. {
  37. if (image != imageToUse)
  38. {
  39. image = imageToUse;
  40. setBounds (image.getBounds());
  41. setBoundingBox (image.getBounds().toFloat());
  42. repaint();
  43. }
  44. }
  45. void DrawableImage::setOpacity (const float newOpacity)
  46. {
  47. opacity = newOpacity;
  48. }
  49. void DrawableImage::setOverlayColour (Colour newOverlayColour)
  50. {
  51. overlayColour = newOverlayColour;
  52. }
  53. void DrawableImage::setBoundingBox (Rectangle<float> newBounds)
  54. {
  55. setBoundingBox (Parallelogram<float> (newBounds));
  56. }
  57. void DrawableImage::setBoundingBox (Parallelogram<float> newBounds)
  58. {
  59. if (bounds != newBounds)
  60. {
  61. bounds = newBounds;
  62. if (image.isValid())
  63. {
  64. auto tr = bounds.topLeft + (bounds.topRight - bounds.topLeft) / (float) image.getWidth();
  65. auto bl = bounds.topLeft + (bounds.bottomLeft - bounds.topLeft) / (float) image.getHeight();
  66. auto t = AffineTransform::fromTargetPoints (bounds.topLeft.x, bounds.topLeft.y,
  67. tr.x, tr.y,
  68. bl.x, bl.y);
  69. if (t.isSingularity())
  70. t = {};
  71. setTransform (t);
  72. }
  73. }
  74. }
  75. //==============================================================================
  76. void DrawableImage::paint (Graphics& g)
  77. {
  78. if (image.isValid())
  79. {
  80. if (opacity > 0.0f && ! overlayColour.isOpaque())
  81. {
  82. g.setOpacity (opacity);
  83. g.drawImageAt (image, 0, 0, false);
  84. }
  85. if (! overlayColour.isTransparent())
  86. {
  87. g.setColour (overlayColour.withMultipliedAlpha (opacity));
  88. g.drawImageAt (image, 0, 0, true);
  89. }
  90. }
  91. }
  92. Rectangle<float> DrawableImage::getDrawableBounds() const
  93. {
  94. return image.getBounds().toFloat();
  95. }
  96. bool DrawableImage::hitTest (int x, int y)
  97. {
  98. return Drawable::hitTest (x, y) && image.isValid() && image.getPixelAt (x, y).getAlpha() >= 127;
  99. }
  100. Path DrawableImage::getOutlineAsPath() const
  101. {
  102. return {}; // not applicable for images
  103. }
  104. } // namespace juce