JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_ImageButton.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
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.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
30 : Button (text_),
31 scaleImageToFit (true),
32 preserveProportions (true),
33 alphaThreshold (0)
34{
35}
36
40
43 const bool preserveImageProportions,
44 const Image& normalImage_,
45 const float imageOpacityWhenNormal,
47 const Image& overImage_,
48 const float imageOpacityWhenOver,
50 const Image& downImage_,
51 const float imageOpacityWhenDown,
53 const float hitTestAlphaThreshold)
54{
55 normalImage = normalImage_;
56 overImage = overImage_;
57 downImage = downImage_;
58
59 if (resizeButtonNowToFitThisImage && normalImage.isValid())
60 {
61 imageBounds.setSize (normalImage.getWidth(),
62 normalImage.getHeight());
63
64 setSize (imageBounds.getWidth(), imageBounds.getHeight());
65 }
66
67 scaleImageToFit = rescaleImagesWhenButtonSizeChanges;
68 preserveProportions = preserveImageProportions;
69
70 normalOpacity = imageOpacityWhenNormal;
71 normalOverlay = overlayColourWhenNormal;
72 overOpacity = imageOpacityWhenOver;
73 overOverlay = overlayColourWhenOver;
74 downOpacity = imageOpacityWhenDown;
75 downOverlay = overlayColourWhenDown;
76
77 alphaThreshold = (uint8) jlimit (0, 0xff, roundToInt (255.0f * hitTestAlphaThreshold));
78
79 repaint();
80}
81
82Image ImageButton::getCurrentImage() const
83{
84 if (isDown() || getToggleState())
85 return getDownImage();
86
87 if (isOver())
88 return getOverImage();
89
90 return getNormalImage();
91}
92
94{
95 return normalImage;
96}
97
99{
100 return overImage.isValid() ? overImage
101 : normalImage;
102}
103
105{
106 return downImage.isValid() ? downImage
107 : getOverImage();
108}
109
113{
114 if (! isEnabled())
115 {
118 }
119
120 Image im (getCurrentImage());
121
122 if (im.isValid())
123 {
124 const int iw = im.getWidth();
125 const int ih = im.getHeight();
126 int w = getWidth();
127 int h = getHeight();
128 int x = (w - iw) / 2;
129 int y = (h - ih) / 2;
130
131 if (scaleImageToFit)
132 {
133 if (preserveProportions)
134 {
135 int newW, newH;
136 const float imRatio = (float) ih / (float) iw;
137 const float destRatio = (float) h / (float) w;
138
139 if (imRatio > destRatio)
140 {
141 newW = roundToInt ((float) h / imRatio);
142 newH = h;
143 }
144 else
145 {
146 newW = w;
147 newH = roundToInt ((float) w * imRatio);
148 }
149
150 x = (w - newW) / 2;
151 y = (h - newH) / 2;
152 w = newW;
153 h = newH;
154 }
155 else
156 {
157 x = 0;
158 y = 0;
159 }
160 }
161
162 if (! scaleImageToFit)
163 {
164 w = iw;
165 h = ih;
166 }
167
168 imageBounds.setBounds (x, y, w, h);
169
171
172 getLookAndFeel().drawImageButton (g, &im, x, y, w, h,
173 useDownImage ? downOverlay
174 : (shouldDrawButtonAsHighlighted ? overOverlay
175 : normalOverlay),
176 useDownImage ? downOpacity
177 : (shouldDrawButtonAsHighlighted ? overOpacity
178 : normalOpacity),
179 *this);
180 }
181}
182
183bool ImageButton::hitTest (int x, int y)
184{
185 if (! Component::hitTest (x, y)) // handle setInterceptsMouseClicks
186 return false;
187
188 if (alphaThreshold == 0)
189 return true;
190
191 Image im (getCurrentImage());
192
193 return im.isNull() || ((! imageBounds.isEmpty())
194 && alphaThreshold < im.getPixelAt (((x - imageBounds.getX()) * im.getWidth()) / imageBounds.getWidth(),
195 ((y - imageBounds.getY()) * im.getHeight()) / imageBounds.getHeight()).getAlpha());
196}
197
198} // namespace juce
A base class for buttons.
Definition juce_Button.h:43
bool getToggleState() const noexcept
Returns true if the button is 'on'.
bool isDown() const noexcept
Returns true if the button is currently being held down.
bool isOver() const noexcept
Returns true if the mouse is currently over the button.
Represents a colour, also including a transparency value.
Definition juce_Colour.h:38
int getHeight() const noexcept
Returns the component's height in pixels.
float getAlpha() const noexcept
Returns the component's current transparency level.
void repaint()
Marks the whole component as needing to be redrawn.
virtual bool hitTest(int x, int y)
Tests whether a given point is inside the component.
void setSize(int newWidth, int newHeight)
Changes the size of the component.
int getWidth() const noexcept
Returns the component's width in pixels.
bool isEnabled() const noexcept
Returns true if the component (and all its parents) are enabled.
LookAndFeel & getLookAndFeel() const noexcept
Finds the appropriate look-and-feel to use for this component.
A graphics context, used for drawing a component or image.
Image getNormalImage() const
Returns the currently set 'normal' image.
Image getDownImage() const
Returns the image that's drawn when the button is held down.
~ImageButton() override
Destructor.
void paintButton(Graphics &, bool, bool) override
Subclasses should override this to actually paint the button's contents.
void setImages(bool resizeButtonNowToFitThisImage, bool rescaleImagesWhenButtonSizeChanges, bool preserveImageProportions, const Image &normalImage, float imageOpacityWhenNormal, Colour overlayColourWhenNormal, const Image &overImage, float imageOpacityWhenOver, Colour overlayColourWhenOver, const Image &downImage, float imageOpacityWhenDown, Colour overlayColourWhenDown, float hitTestAlphaThreshold=0.0f)
Sets up the images to draw in various states.
bool hitTest(int x, int y) override
Tests whether a given point is inside the component.
Image getOverImage() const
Returns the image that's drawn when the mouse is over the button.
ImageButton(const String &name=String())
Creates an ImageButton.
Holds a fixed-size bitmap.
Definition juce_Image.h:58
int getWidth() const noexcept
Returns the image's width (in pixels).
int getHeight() const noexcept
Returns the image's height (in pixels).
bool isValid() const noexcept
Returns true if this image isn't null.
Definition juce_Image.h:147
ValueType getX() const noexcept
Returns the x coordinate of the rectangle's left-hand-side.
void setSize(ValueType newWidth, ValueType newHeight) noexcept
Changes the rectangle's size, leaving the position of its top-left corner unchanged.
ValueType getWidth() const noexcept
Returns the width of the rectangle.
ValueType getY() const noexcept
Returns the y coordinate of the rectangle's top edge.
bool isEmpty() const noexcept
Returns true if the rectangle's width or height are zero or less.
ValueType getHeight() const noexcept
Returns the height of the rectangle.
void setBounds(ValueType newX, ValueType newY, ValueType newWidth, ValueType newHeight) noexcept
Changes all the rectangle's coordinates.
The JUCE String class!
Definition juce_String.h:53
typedef float
JUCE Namespace.
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Constrains a value to keep it within a given range.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
unsigned char uint8
A platform-independent 8-bit unsigned integer type.
int roundToInt(const FloatType value) noexcept
Fast floating-point-to-integer conversion.