| 1 | # You may distribute under the terms of either the GNU General Public License | ||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||
| 3 | # | ||
| 4 | # (C) Paul Evans, 2014-2023 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | use v5.20; | ||
| 7 | use warnings; | ||
| 8 | use Object::Pad 0.807; | ||
| 9 | |||
| 10 | package Tickit::Widget::Fill 0.42; | ||
| 11 | class Tickit::Widget::Fill :strict(params); | ||
| 12 | |||
| 13 | inherit Tickit::Widget; | ||
| 14 | |||
| 15 | use Tickit::Style; | ||
| 16 | |||
| 17 | use Tickit::Utils qw( textwidth ); | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | C<Tickit::Widget::Fill> - fill an area with repeated text | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | This class provides a widget which displays a given piece of text repeatedly | ||
| 26 | over its entire area. By default the text is a single space, meaning the area | ||
| 27 | will be entirely drawn with spaces. | ||
| 28 | |||
| 29 | =head1 STYLE | ||
| 30 | |||
| 31 | The default style pen is used as the widget pen. | ||
| 32 | |||
| 33 | The following style keys are used: | ||
| 34 | |||
| 35 | =over 4 | ||
| 36 | |||
| 37 | =item text => STRING | ||
| 38 | |||
| 39 | The text to display in a repeating pattern on the window. | ||
| 40 | |||
| 41 | =item skew => INT | ||
| 42 | |||
| 43 | If defined, successive lines will be advanced by this number of columns (which | ||
| 44 | may be negative) to create a skewed repeating pattern. | ||
| 45 | |||
| 46 | =back | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | style_definition base => | ||
| 51 | text => " "; | ||
| 52 | |||
| 53 | style_redraw_keys qw( text skew ); | ||
| 54 | |||
| 55 | use constant WIDGET_PEN_FROM_STYLE => 1; | ||
| 56 | |||
| 57 | method lines { 1 } | ||
| 58 | method cols { 1 } | ||
| 59 | |||
| 60 | method render_to_rb | ||
| 61 | { | ||
| 62 | my ( $rb, $rect ) = @_; | ||
| 63 | |||
| 64 | my ( $text, $skew ) = $self->get_style_values(qw( text skew )); | ||
| 65 | $skew //= 0; | ||
| 66 | |||
| 67 | my $len = textwidth( $text ); | ||
| 68 | |||
| 69 | my $left = $rect->left; | ||
| 70 | $left -= $left % $len; | ||
| 71 | |||
| 72 | my $repeat = int( ( $rect->right - $left + $len - 1 ) / $len ); | ||
| 73 | $repeat++ if $skew; | ||
| 74 | |||
| 75 | foreach my $line ( $rect->linerange ) { | ||
| 76 | my $lineskew = ( $line * $skew ) % $len; | ||
| 77 | $lineskew -= $len if $skew; | ||
| 78 | |||
| 79 | $rb->goto( $line, $left + $lineskew ); | ||
| 80 | $rb->text( $text x $repeat ); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | =head1 AUTHOR | ||
| 85 | |||
| 86 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 87 | |||
| 88 | =cut | ||
| 89 | |||
| 90 | 0x55AA; |