juce_DrawableImage.cpp 3.7 KB

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