Canvas: allow to group rendering of child items

> This group functionality can be convenient for performing
> intermediate compositing. One common use of a group is to
> render objects as opaque within the group, (so that they
> occlude each other), and then blend the result with
> translucence onto the destination.
https://www.cairographics.org/manual/cairo-cairo-t.html#cairo-push-group

The main use case where will be to render opaque layered
[MIDI] regions transparently onto a grid.
This commit is contained in:
Robin Gareus 2022-12-09 01:34:01 +01:00
parent dcf981fe07
commit 70b00f5201
No known key found for this signature in database
GPG key ID: A090BCE02CF57F04
2 changed files with 45 additions and 0 deletions

View file

@ -23,17 +23,20 @@ using namespace ArdourCanvas;
Container::Container (Canvas* canvas)
: Item (canvas)
, _render_with_alpha (-1)
{
}
Container::Container (Item* parent)
: Item (parent)
, _render_with_alpha (-1)
{
}
Container::Container (Item* parent, Duple const & p)
: Item (parent, p)
, _render_with_alpha (-1)
{
}
@ -46,7 +49,20 @@ Container::prepare_for_render (Rect const & area) const
void
Container::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
{
if (_render_with_alpha == 0) {
return;
} else if (_render_with_alpha > 0) {
context->push_group ();
}
Item::render_children (area, context);
if (_render_with_alpha >= 1.0) {
context->pop_group ();
} else if (_render_with_alpha > 0) {
context->pop_group_to_source ();
context->paint_with_alpha (_render_with_alpha);
}
}
void
@ -56,3 +72,13 @@ Container::compute_bounding_box () const
/* nothing to do here; Item::bounding_box() will add all children for us */
set_bbox_clean ();
}
void
Container::set_render_with_alpha (double alpha)
{
if (_render_with_alpha == alpha) {
return;
}
_render_with_alpha = alpha;
redraw ();
}