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.

150 lines
4.0KB

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