| 1 | package Term::Fabulous::Render::Target::Mask; | ||
| 2 | |||
| 3 | use v5.32; | ||
| 4 | use warnings; | ||
| 5 | use feature 'signatures'; | ||
| 6 | no warnings 'experimental::signatures'; | ||
| 7 | |||
| 8 | our $VERSION = '0.03'; | ||
| 9 | |||
| 10 | use Object::Pad 0.825; | ||
| 11 | |||
| 12 | role Term::Fabulous::Render::Target::Mask { | ||
| 13 | use Scalar::Util qw(refaddr); | ||
| 14 | use Term::Fabulous::Render::Geometry qw(row_spans_outside); | ||
| 15 | |||
| 16 | # Provided by the target: reset every cell outside the given rects, show | ||
| 17 | # the frame, and write cells unconditionally. | ||
| 18 | method clear_cells; | ||
| 19 | method present_cells; | ||
| 20 | method put_cell; | ||
| 21 | method put_extension; | ||
| 22 | method put_row; | ||
| 23 | |||
| 24 | # Rects whose cells this frame must leave alone, until they are released. | ||
| 25 | field @_kept_rects; | ||
| 26 | |||
| 27 | sub _is_kept ( $x, $y, $rects ) { | ||
| 28 | foreach my $rect (@$rects) { | ||
| 29 | return 1 if $x >= $rect->[0] && $x < $rect->[2] && $y >= $rect->[1] && $y < $rect->[3]; | ||
| 30 | } | ||
| 31 | return 0; | ||
| 32 | } | ||
| 33 | |||
| 34 | method begin_frame (@kept_rects) { | ||
| 35 | @_kept_rects = @kept_rects; | ||
| 36 | $self->clear_cells(@kept_rects); | ||
| 37 | return; | ||
| 38 | } | ||
| 39 | |||
| 40 | method end_frame () { | ||
| 41 | @_kept_rects = (); | ||
| 42 | $self->present_cells; | ||
| 43 | return; | ||
| 44 | } | ||
| 45 | |||
| 46 | method release_rect ($rect) { | ||
| 47 | @_kept_rects = grep { refaddr($_) != refaddr($rect) } @_kept_rects; | ||
| 48 | return; | ||
| 49 | } | ||
| 50 | |||
| 51 | method set_cell ( $x, $y, $glyph, $fg, $bg ) { | ||
| 52 | return if @_kept_rects && _is_kept( $x, $y, \@_kept_rects ); | ||
| 53 | $self->put_cell( $x, $y, $glyph, $fg, $bg ); | ||
| 54 | return; | ||
| 55 | } | ||
| 56 | |||
| 57 | method extend_cell ( $x, $y, $character ) { | ||
| 58 | return if @_kept_rects && _is_kept( $x, $y, \@_kept_rects ); | ||
| 59 | $self->put_extension( $x, $y, $character ); | ||
| 60 | return; | ||
| 61 | } | ||
| 62 | |||
| 63 | method fill_row ( $x, $y, $columns, $bg ) { | ||
| 64 | if ( !@_kept_rects ) { | ||
| 65 | $self->put_row( $x, $y, $columns, $bg ); | ||
| 66 | return; | ||
| 67 | } | ||
| 68 | foreach my $span ( row_spans_outside( $y, $x, $x + $columns, @_kept_rects ) ) { | ||
| 69 | my ( $from, $to ) = @$span; | ||
| 70 | $self->put_row( $from, $y, $to - $from, $bg ); | ||
| 71 | } | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | 1; | ||
| 77 | |||
| 78 | __END__ |