| 1 | package Term::Fabulous::Render::Frame; | ||
| 2 | |||
| 3 | use v5.32; | ||
| 4 | use warnings; | ||
| 5 | use feature 'signatures'; | ||
| 6 | no warnings 'experimental::signatures'; | ||
| 7 | |||
| 8 | our $VERSION = '0.05'; | ||
| 9 | |||
| 10 | use Object::Pad 0.825; | ||
| 11 | |||
| 12 | class Term::Fabulous::Render::Frame :strict(params) { | ||
| 13 | use Clay::XS qw( | ||
| 14 | CLAY_RENDER_COMMAND_TYPE_NONE | ||
| 15 | CLAY_RENDER_COMMAND_TYPE_RECTANGLE | ||
| 16 | CLAY_RENDER_COMMAND_TYPE_BORDER | ||
| 17 | CLAY_RENDER_COMMAND_TYPE_TEXT | ||
| 18 | CLAY_RENDER_COMMAND_TYPE_IMAGE | ||
| 19 | CLAY_RENDER_COMMAND_TYPE_SCISSOR_START | ||
| 20 | CLAY_RENDER_COMMAND_TYPE_SCISSOR_END | ||
| 21 | CLAY_RENDER_COMMAND_TYPE_OVERLAY_COLOR_START | ||
| 22 | CLAY_RENDER_COMMAND_TYPE_OVERLAY_COLOR_END | ||
| 23 | CLAY_RENDER_COMMAND_TYPE_CUSTOM | ||
| 24 | ); | ||
| 25 | use List::Util qw(any); | ||
| 26 | use Scalar::Util qw(looks_like_number refaddr); | ||
| 27 | use Term::Fabulous::Render::Geometry qw(cell_rect intersect_cell_rects rects_overlap); | ||
| 28 | |||
| 29 | my %command_type_name = ( | ||
| 30 | CLAY_RENDER_COMMAND_TYPE_NONE() => 'NONE', | ||
| 31 | CLAY_RENDER_COMMAND_TYPE_RECTANGLE() => 'RECTANGLE', | ||
| 32 | CLAY_RENDER_COMMAND_TYPE_BORDER() => 'BORDER', | ||
| 33 | CLAY_RENDER_COMMAND_TYPE_TEXT() => 'TEXT', | ||
| 34 | CLAY_RENDER_COMMAND_TYPE_IMAGE() => 'IMAGE', | ||
| 35 | CLAY_RENDER_COMMAND_TYPE_SCISSOR_START() => 'SCISSOR_START', | ||
| 36 | CLAY_RENDER_COMMAND_TYPE_SCISSOR_END() => 'SCISSOR_END', | ||
| 37 | CLAY_RENDER_COMMAND_TYPE_OVERLAY_COLOR_START() => 'OVERLAY_COLOR_START', | ||
| 38 | CLAY_RENDER_COMMAND_TYPE_OVERLAY_COLOR_END() => 'OVERLAY_COLOR_END', | ||
| 39 | CLAY_RENDER_COMMAND_TYPE_CUSTOM() => 'CUSTOM', | ||
| 40 | ); | ||
| 41 | |||
| 42 | field $width :param :reader; | ||
| 43 | field $height :param :reader; | ||
| 44 | |||
| 45 | # In paint order; for each command, the cells it may paint into and the | ||
| 46 | # non-empty rects it does paint. The painted rects are worked out on the | ||
| 47 | # first question about them: a frame without canvases that no pointer | ||
| 48 | # report hit-tests never needs them. | ||
| 49 | field @_commands; | ||
| 50 | field @_clip_rects; | ||
| 51 | field $_painted_rects; | ||
| 52 | |||
| 53 | ADJUST :params ( :$commands ) { | ||
| 54 | die "Term::Fabulous::Render::Frame: commands must be an array reference of render commands, got " . ( defined $commands ? "'$commands'" : 'undef' ) | ||
| 55 | unless ref $commands eq 'ARRAY'; | ||
| 56 | _check_size( width => $width ); | ||
| 57 | _check_size( height => $height ); | ||
| 58 | |||
| 59 | @_commands = _with_custom_backgrounds(@$commands); | ||
| 60 | @_clip_rects = $self->_replay_scissors; | ||
| 61 | } | ||
| 62 | |||
| 63 | method _all_painted_rects () { | ||
| 64 | return $_painted_rects //= [ map { [ _painted_rects( $_commands[$_], $_clip_rects[$_] ) ] } 0 .. $#_commands ]; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub _check_size ( $name, $value ) { | ||
| 68 | die "Term::Fabulous::Render::Frame: $name must be a number of at least 0, got " . ( defined $value ? "'$value'" : 'undef' ) | ||
| 69 | unless looks_like_number($value) && $value >= 0; | ||
| 70 | return; | ||
| 71 | } | ||
| 72 | |||
| 73 | # Clay carries a custom element's background in the custom command | ||
| 74 | # itself, without a rectangle of its own; a rectangle painted before | ||
| 75 | # the custom command puts the background below the custom content. | ||
| 76 | sub _with_custom_backgrounds (@commands) { | ||
| 77 | return map { ( _custom_background($_), $_ ) } @commands; | ||
| 78 | } | ||
| 79 | |||
| 80 | # The background rectangle of a custom command: the command's box in | ||
| 81 | # its background color. Nothing for other commands and for a custom | ||
| 82 | # command without a visible background, as Clay emits no rectangle for | ||
| 83 | # a background with alpha 0 either. | ||
| 84 | sub _custom_background ($command) { | ||
| 85 | return () unless $command->{commandType} == CLAY_RENDER_COMMAND_TYPE_CUSTOM; | ||
| 86 | my $data = $command->{renderData}; | ||
| 87 | return () unless ( $data->{backgroundColor}{a} // 0 ) > 0; | ||
| 88 | return { %$command, commandType => CLAY_RENDER_COMMAND_TYPE_RECTANGLE, renderData => { backgroundColor => $data->{backgroundColor}, cornerRadius => $data->{cornerRadius} } }; | ||
| 89 | } | ||
| 90 | |||
| 91 | # Everything between a SCISSOR_START and its SCISSOR_END is clipped to | ||
| 92 | # the start's box, narrowed to the scissor around it and the viewport. | ||
| 93 | method _replay_scissors () { | ||
| 94 | my @open = ( [ 0, 0, $width, $height ] ); | ||
| 95 | my @clip_rects; | ||
| 96 | foreach my $command (@_commands) { | ||
| 97 | my $type = $command->{commandType}; | ||
| 98 | die sprintf( "Term::Fabulous::Render::Frame: unhandled render command type %s", $command_type_name{$type} // $type ) | ||
| 99 | unless _is_handled($type); | ||
| 100 | push @clip_rects, $open[-1]; | ||
| 101 | push @open, intersect_cell_rects( $open[-1], [ cell_rect( $command->{boundingBox} ) ] ) if $type == CLAY_RENDER_COMMAND_TYPE_SCISSOR_START; | ||
| 102 | next unless $type == CLAY_RENDER_COMMAND_TYPE_SCISSOR_END; | ||
| 103 | die "Term::Fabulous::Render::Frame: scissor end without an open scissor" if @open == 1; | ||
| 104 | pop @open; | ||
| 105 | } | ||
| 106 | return @clip_rects; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub _is_handled ($type) { | ||
| 110 | return | ||
| 111 | $type == CLAY_RENDER_COMMAND_TYPE_RECTANGLE | ||
| 112 | || $type == CLAY_RENDER_COMMAND_TYPE_BORDER | ||
| 113 | || $type == CLAY_RENDER_COMMAND_TYPE_TEXT | ||
| 114 | || $type == CLAY_RENDER_COMMAND_TYPE_CUSTOM | ||
| 115 | || $type == CLAY_RENDER_COMMAND_TYPE_SCISSOR_START | ||
| 116 | || $type == CLAY_RENDER_COMMAND_TYPE_SCISSOR_END; | ||
| 117 | } | ||
| 118 | |||
| 119 | # Rectangles, text and canvases paint their box, a border the one-cell | ||
| 120 | # edges of its sides with a positive width, a scissor nothing; all of it | ||
| 121 | # only inside the clip rect. | ||
| 122 | sub _painted_rects ( $command, $clip ) { | ||
| 123 | my $type = $command->{commandType}; | ||
| 124 | return () if $type == CLAY_RENDER_COMMAND_TYPE_SCISSOR_START || $type == CLAY_RENDER_COMMAND_TYPE_SCISSOR_END; | ||
| 125 | |||
| 126 | my ( $x0, $y0, $x1, $y1 ) = cell_rect( $command->{boundingBox} ); | ||
| 127 | return () unless $x0 < $x1 && $y0 < $y1; | ||
| 128 | return grep { rects_overlap( $_, $_ ) } intersect_cell_rects( [ $x0, $y0, $x1, $y1 ], $clip ) unless $type == CLAY_RENDER_COMMAND_TYPE_BORDER; | ||
| 129 | |||
| 130 | my $widths = $command->{renderData}{width} // {}; | ||
| 131 | my %edge = ( | ||
| 132 | top => [ $x0, $y0, $x1, $y0 + 1 ], | ||
| 133 | bottom => [ $x0, $y1 - 1, $x1, $y1 ], | ||
| 134 | left => [ $x0, $y0, $x0 + 1, $y1 ], | ||
| 135 | right => [ $x1 - 1, $y0, $x1, $y1 ], | ||
| 136 | ); | ||
| 137 | return grep { rects_overlap( $_, $_ ) } map { intersect_cell_rects( $edge{$_}, $clip ) } grep { ( $widths->{$_} // 0 ) > 0 } sort keys %edge; | ||
| 138 | } | ||
| 139 | |||
| 140 | method _check_index ($index) { | ||
| 141 | die "Term::Fabulous::Render::Frame: no command at index " . ( defined $index ? "'$index'" : 'undef' ) . "; the frame has " . scalar(@_commands) . " commands" | ||
| 142 | unless defined $index && $index =~ /\A[0-9]+\z/ && $index <= $#_commands; | ||
| 143 | return; | ||
| 144 | } | ||
| 145 | |||
| 146 | method commands () { | ||
| 147 | return @_commands; | ||
| 148 | } | ||
| 149 | |||
| 150 | method command_count () { | ||
| 151 | return scalar @_commands; | ||
| 152 | } | ||
| 153 | |||
| 154 | method command ($index) { | ||
| 155 | $self->_check_index($index); | ||
| 156 | return $_commands[$index]; | ||
| 157 | } | ||
| 158 | |||
| 159 | method clip_rect ($index) { | ||
| 160 | $self->_check_index($index); | ||
| 161 | return [ @{ $_clip_rects[$index] } ]; | ||
| 162 | } | ||
| 163 | |||
| 164 | method painted_rects ($index) { | ||
| 165 | $self->_check_index($index); | ||
| 166 | return map { [@$_] } @{ $self->_all_painted_rects->[$index] }; | ||
| 167 | } | ||
| 168 | |||
| 169 | method painted_after ( $index, $rect ) { | ||
| 170 | $self->_check_index($index); | ||
| 171 | my $painted = $self->_all_painted_rects; | ||
| 172 | my ( $x0, $y0, $x1, $y1 ) = @$rect; | ||
| 173 | |||
| 174 | # Called for every canvas, against every command after it: compare | ||
| 175 | # in place rather than build intersection rects. | ||
| 176 | foreach my $later ( @{$painted}[ $index + 1 .. $#_commands ] ) { | ||
| 177 | foreach my $painted_rect (@$later) { | ||
| 178 | return 1 if $painted_rect->[0] < $x1 && $x0 < $painted_rect->[2] && $painted_rect->[1] < $y1 && $y0 < $painted_rect->[3]; | ||
| 179 | } | ||
| 180 | } | ||
| 181 | return 0; | ||
| 182 | } | ||
| 183 | |||
| 184 | method painted_over ( $index, $rect ) { | ||
| 185 | $self->_check_index($index); | ||
| 186 | my $painted = $self->_all_painted_rects; | ||
| 187 | return grep { rects_overlap( $_, $_ ) } map { intersect_cell_rects( $_, $rect ) } map { @$_ } @{$painted}[ $index + 1 .. $#_commands ]; | ||
| 188 | } | ||
| 189 | |||
| 190 | # The painted lines of a text widget: for each TEXT command the widget | ||
| 191 | # emitted, the screen row and the offset in its text of the line's | ||
| 192 | # first character, in the order of the rows. A text that was not laid | ||
| 193 | # out, or whose lines Clay left out below the window, has fewer lines | ||
| 194 | # here than it has; a line without characters has none. | ||
| 195 | method text_rows ($widget) { | ||
| 196 | my $user_data = refaddr $widget; | ||
| 197 | return map { [ ( cell_rect( $_->{boundingBox} ) )[1], $_->{renderData}{stringOffset} // 0 ] } | ||
| 198 | sort { $a->{boundingBox}{y} <=> $b->{boundingBox}{y} } | ||
| 199 | grep { $_->{commandType} == CLAY_RENDER_COMMAND_TYPE_TEXT && ( $_->{userData} // -1 ) == $user_data } @_commands; | ||
| 200 | } | ||
| 201 | |||
| 202 | method topmost_at ( $x, $y ) { | ||
| 203 | my $painted = $self->_all_painted_rects; | ||
| 204 | return grep { | ||
| 205 | any { $x >= $_->[0] && $x < $_->[2] && $y >= $_->[1] && $y < $_->[3] } | ||
| 206 | @{ $painted->[$_] } | ||
| 207 | } reverse 0 .. $#_commands; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | 1; | ||
| 212 | |||
| 213 | __END__ |