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.

157 lines
4.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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 (const Image& imageToUse)
  33. {
  34. setImageInternal (imageToUse);
  35. }
  36. DrawableImage::~DrawableImage()
  37. {
  38. }
  39. std::unique_ptr<Drawable> DrawableImage::createCopy() const
  40. {
  41. return std::make_unique<DrawableImage> (*this);
  42. }
  43. //==============================================================================
  44. void DrawableImage::setImage (const Image& imageToUse)
  45. {
  46. if (setImageInternal (imageToUse))
  47. repaint();
  48. }
  49. void DrawableImage::setOpacity (const float newOpacity)
  50. {
  51. opacity = newOpacity;
  52. }
  53. void DrawableImage::setOverlayColour (Colour newOverlayColour)
  54. {
  55. overlayColour = newOverlayColour;
  56. }
  57. void DrawableImage::setBoundingBox (Rectangle<float> newBounds)
  58. {
  59. setBoundingBox (Parallelogram<float> (newBounds));
  60. }
  61. void DrawableImage::setBoundingBox (Parallelogram<float> newBounds)
  62. {
  63. if (bounds != newBounds)
  64. {
  65. bounds = newBounds;
  66. if (image.isValid())
  67. {
  68. auto tr = bounds.topLeft + (bounds.topRight - bounds.topLeft) / (float) image.getWidth();
  69. auto bl = bounds.topLeft + (bounds.bottomLeft - bounds.topLeft) / (float) image.getHeight();
  70. auto t = AffineTransform::fromTargetPoints (bounds.topLeft.x, bounds.topLeft.y,
  71. tr.x, tr.y,
  72. bl.x, bl.y);
  73. if (t.isSingularity())
  74. t = {};
  75. setTransform (t);
  76. }
  77. }
  78. }
  79. //==============================================================================
  80. void DrawableImage::paint (Graphics& g)
  81. {
  82. if (image.isValid())
  83. {
  84. if (opacity > 0.0f && ! overlayColour.isOpaque())
  85. {
  86. g.setOpacity (opacity);
  87. g.drawImageAt (image, 0, 0, false);
  88. }
  89. if (! overlayColour.isTransparent())
  90. {
  91. g.setColour (overlayColour.withMultipliedAlpha (opacity));
  92. g.drawImageAt (image, 0, 0, true);
  93. }
  94. }
  95. }
  96. Rectangle<float> DrawableImage::getDrawableBounds() const
  97. {
  98. return image.getBounds().toFloat();
  99. }
  100. bool DrawableImage::hitTest (int x, int y)
  101. {
  102. return Drawable::hitTest (x, y) && image.isValid() && image.getPixelAt (x, y).getAlpha() >= 127;
  103. }
  104. Path DrawableImage::getOutlineAsPath() const
  105. {
  106. return {}; // not applicable for images
  107. }
  108. //==============================================================================
  109. bool DrawableImage::setImageInternal (const Image& imageToUse)
  110. {
  111. if (image != imageToUse)
  112. {
  113. image = imageToUse;
  114. setBounds (image.getBounds());
  115. setBoundingBox (image.getBounds().toFloat());
  116. return true;
  117. }
  118. return false;
  119. }
  120. //==============================================================================
  121. std::unique_ptr<AccessibilityHandler> DrawableImage::createAccessibilityHandler()
  122. {
  123. return std::make_unique<AccessibilityHandler> (*this, AccessibilityRole::image);
  124. }
  125. } // namespace juce