Sometimes you don’t know your shadow color at compile-time. That’s fine, there’s a default constructor as well.
Add melatonin::DropShadow shadow
as a class member to your JUCE component and then you can call setters inside the paint
method before calling shadow.render
.
You can also specify melatonin::DropShadow { 5 }
if the radius is fixed at compile-time.
Available Setters
You can change everything about the shadow via a setter:
setColor
setOpacity
setRadius
setOffset
setSpread
setColor
, setOpacity
and setOffset
are “inexpensive.” The blur remains cached and is simply re-composited. setRadius
and setSpread
are “expensive” and both require blur recalculation.
All setters can take floating point values.
setOpacity
setOpacity
is just a shortcut that will change the alpha on shadow’s color. This is useful for fading a shadow in and out.
If you specify setColor
and then setOpacity
immediately afterwards, it will change the alpha value of the color!
setOffset
setOffset
can take an x
, y
, a juce::Point
or an initializer list which will construct a juce::Point
.
// x,y
shadow.setOffset (1, 2);
// via juce::Point
juce::Point point { 2, 2 };
shadow.setOffset (point);
// via initializer list
shadow.setOffset ( { 1, 1 });
Setters for multiple shadows
You can also use setters with multiple shadows. For example, here’s 3 shadows:
// component member
melatonin::DropShadow shadow {
{ juce::Colours::black, 16, { 0, 0 } },
{ juce::Colours::gray, 8, { 0, 0 } },
{ juce::Colours::blue, 3, { 0, 0 } }};
In this case, you can specify the integer index of the shadow you wish to change. When no index is provided, it works with the first shadow.
// in component "paint"
shadow.setRadius (8); // reduces the first shadow's radius
shadow.setOffset ({1,1}, 2); // adds offset to the last shadow
Chaining Setters
Setters can be chained together (they all return a CachedShadow&
):
shadow.setRadius (5).setOffset (point);
When working with multiple shadows, you would need to specify the shadow index for each setter:
shadow.setRadius (5, 2).setOffset (point, 2);
I’m open to improving this API. Feel free to comment here, or open an issue or PR on the repo.
Leave a Reply