I’ve come up with a pretty simple way of drawing inner shadows, however it requires you to specify the outline of the object you want to cast the inner shadow on as a Path
.
It works by cutting out the shape to cast the inner shadow on from a larger rectangle, essentially inverting the path, and then using the cutout to cast a DropShadow
on the target shape. To avoid the rectangle around the cutout drawing outside of the target shape, I reduce the Graphics
object’s clip region to the target shape. Easy as that.
void drawInnerShadow(Graphics &g, Path target) {
// resets the Clip Region when the function returns
Graphics::ScopedSaveState saveState(g);
// invert the path's fill shape and enlarge it,
// so it casts a shadow
Path shadowPath(target);
shadowPath.addRectangle(target.getBounds().expanded(10));
shadowPath.setUsingNonZeroWinding(false);
// reduce clip region to avoid the shadow
// being drawn outside of the shape to cast the shadow on
g.reduceClipRegion(target);
DropShadow ds(shadowColor, 1, {0, 1});
ds.drawForPath(g, shadowPath);
}
Result for a Path with a Rounded Rectangle:

Note: you’ll have to replace the shadowColor
variable with whatever color you want to use for the shadow.
Also, depending on the Path you use, you may have to remove the line shadowPath.setUsingNonZeroWinding(false);
. I haven’t found a consistency in when that is required, yet.