2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
23 ==============================================================================
29 DrawableImage::DrawableImage() : bounds ({ 0.0f
, 0.0f
, 1.0f
, 1.0f
})
33 DrawableImage::DrawableImage (const DrawableImage
& other
)
36 opacity (other
.opacity
),
37 overlayColour (other
.overlayColour
),
40 setBounds (other
.getBounds());
43 DrawableImage::DrawableImage (const Image
& imageToUse
)
45 setImageInternal (imageToUse
);
48 DrawableImage::~DrawableImage()
52 std::unique_ptr
<Drawable
> DrawableImage::createCopy() const
54 return std::make_unique
<DrawableImage
> (*this);
57 //==============================================================================
58 void DrawableImage::setImage (const Image
& imageToUse
)
60 if (setImageInternal (imageToUse
))
64 void DrawableImage::setOpacity (const float newOpacity
)
69 void DrawableImage::setOverlayColour (Colour newOverlayColour
)
71 overlayColour
= newOverlayColour
;
74 void DrawableImage::setBoundingBox (Rectangle
<float> newBounds
)
76 setBoundingBox (Parallelogram
<float> (newBounds
));
79 void DrawableImage::setBoundingBox (Parallelogram
<float> newBounds
)
81 if (bounds
!= newBounds
)
87 auto tr
= bounds
.topLeft
+ (bounds
.topRight
- bounds
.topLeft
) / (float) image
.getWidth();
88 auto bl
= bounds
.topLeft
+ (bounds
.bottomLeft
- bounds
.topLeft
) / (float) image
.getHeight();
90 auto t
= AffineTransform::fromTargetPoints (bounds
.topLeft
.x
, bounds
.topLeft
.y
,
94 if (t
.isSingularity())
102 //==============================================================================
103 void DrawableImage::paint (Graphics
& g
)
107 if (opacity
> 0.0f
&& ! overlayColour
.isOpaque())
109 g
.setOpacity (opacity
);
110 g
.drawImageAt (image
, 0, 0, false);
113 if (! overlayColour
.isTransparent())
115 g
.setColour (overlayColour
.withMultipliedAlpha (opacity
));
116 g
.drawImageAt (image
, 0, 0, true);
121 Rectangle
<float> DrawableImage::getDrawableBounds() const
123 return image
.getBounds().toFloat();
126 bool DrawableImage::hitTest (int x
, int y
)
128 return Drawable::hitTest (x
, y
) && image
.isValid() && image
.getPixelAt (x
, y
).getAlpha() >= 127;
131 Path
DrawableImage::getOutlineAsPath() const
133 return {}; // not applicable for images
136 //==============================================================================
137 bool DrawableImage::setImageInternal (const Image
& imageToUse
)
139 if (image
!= imageToUse
)
142 setBounds (image
.getBounds());
143 setBoundingBox (image
.getBounds().toFloat());
150 //==============================================================================
151 std::unique_ptr
<AccessibilityHandler
> DrawableImage::createAccessibilityHandler()
153 return std::make_unique
<AccessibilityHandler
> (*this, AccessibilityRole::image
);