VST3: fetch midi mappings all at once, use it for note/sound-off
[carla.git] / source / modules / juce_gui_basics / drawables / juce_DrawableImage.cpp
blob04b2e7ebcc567fd1388282a1b7b8052670ae134a
1 /*
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
8 licensing.
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
21 DISCLAIMED.
23 ==============================================================================
26 namespace juce
29 DrawableImage::DrawableImage() : bounds ({ 0.0f, 0.0f, 1.0f, 1.0f })
33 DrawableImage::DrawableImage (const DrawableImage& other)
34 : Drawable (other),
35 image (other.image),
36 opacity (other.opacity),
37 overlayColour (other.overlayColour),
38 bounds (other.bounds)
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))
61 repaint();
64 void DrawableImage::setOpacity (const float newOpacity)
66 opacity = 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)
83 bounds = newBounds;
85 if (image.isValid())
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,
91 tr.x, tr.y,
92 bl.x, bl.y);
94 if (t.isSingularity())
95 t = {};
97 setTransform (t);
102 //==============================================================================
103 void DrawableImage::paint (Graphics& g)
105 if (image.isValid())
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)
141 image = imageToUse;
142 setBounds (image.getBounds());
143 setBoundingBox (image.getBounds().toFloat());
144 return true;
147 return false;
150 //==============================================================================
151 std::unique_ptr<AccessibilityHandler> DrawableImage::createAccessibilityHandler()
153 return std::make_unique<AccessibilityHandler> (*this, AccessibilityRole::image);
156 } // namespace juce