| 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, 2013-2023 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | use v5.20; | ||
| 7 | use warnings; | ||
| 8 | use Object::Pad 0.807; | ||
| 9 | |||
| 10 | package Tickit::Widget::Spinner 0.42; | ||
| 11 | class Tickit::Widget::Spinner :strict(params); | ||
| 12 | |||
| 13 | inherit Tickit::Widget; | ||
| 14 | |||
| 15 | use experimental 'postderef'; | ||
| 16 | |||
| 17 | use Tickit::Style; | ||
| 18 | |||
| 19 | use List::Util qw( max ); | ||
| 20 | use Tickit::Utils qw( textwidth ); | ||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | C<Tickit::Widget::Spinner> - a widget displaying a small text animation | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | use Tickit; | ||
| 29 | use Tickit::Widget::Spinner; | ||
| 30 | |||
| 31 | my $spinner = Tickit::Widget::Spinner->new( | ||
| 32 | chars => [ "<X> ", " <X> ", " <X>", "> <X", "X> <" ], | ||
| 33 | ); | ||
| 34 | |||
| 35 | Tickit->new( root => $spinner )->run; | ||
| 36 | |||
| 37 | =head1 DESCRIPTION | ||
| 38 | |||
| 39 | This class provides a widget which displays a simple animation, cycling | ||
| 40 | through a fixed set of strings with a fixed interval. | ||
| 41 | |||
| 42 | =head1 STYLE | ||
| 43 | |||
| 44 | The default style pen is used as the widget pen. | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | use constant WIDGET_PEN_FROM_STYLE => 1; | ||
| 49 | |||
| 50 | =head1 CONSTRUCTOR | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | =head2 new | ||
| 55 | |||
| 56 | $spinner = Tickit::Widget::Spinner->new( %args ); | ||
| 57 | |||
| 58 | Constructs a new C<Tickit::Widget::Spinner> object. | ||
| 59 | |||
| 60 | Takes the following named arguments: | ||
| 61 | |||
| 62 | =over 8 | ||
| 63 | |||
| 64 | =item chars => ARRAY | ||
| 65 | |||
| 66 | Optional. An ARRAY reference containing the text strings to use. | ||
| 67 | |||
| 68 | =item interval => NUM | ||
| 69 | |||
| 70 | Optional. The time each string is displayed for. Defaults to 0.5. | ||
| 71 | |||
| 72 | =back | ||
| 73 | |||
| 74 | =cut | ||
| 75 | |||
| 76 | field @_chars; | ||
| 77 | field $_state = 0; | ||
| 78 | field $_interval :param //= 0.5; | ||
| 79 | field $_cols; | ||
| 80 | |||
| 81 | ADJUST :params ( | ||
| 82 | :$chars = [qw( - \ | / )], | ||
| 83 | ) { | ||
| 84 | @_chars = $chars->@*; | ||
| 85 | |||
| 86 | $_cols = max map { textwidth $_ } @_chars; | ||
| 87 | } | ||
| 88 | |||
| 89 | field $_running; | ||
| 90 | field $_x; | ||
| 91 | field $_y; | ||
| 92 | field $_rect; | ||
| 93 | |||
| 94 | =head1 METHODS | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | method lines | ||
| 99 | { | ||
| 100 | return 1; | ||
| 101 | } | ||
| 102 | |||
| 103 | method cols | ||
| 104 | { | ||
| 105 | return $_cols; | ||
| 106 | } | ||
| 107 | |||
| 108 | =head2 start | ||
| 109 | |||
| 110 | $spinner->start; | ||
| 111 | |||
| 112 | Starts the animation effect. | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | method start | ||
| 117 | { | ||
| 118 | return if $_running; | ||
| 119 | $_running = 1; | ||
| 120 | $self->tick; | ||
| 121 | } | ||
| 122 | |||
| 123 | =head2 stop | ||
| 124 | |||
| 125 | $spinner->stop; | ||
| 126 | |||
| 127 | Stops the animation effect. | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | method stop | ||
| 132 | { | ||
| 133 | $_running = 0; | ||
| 134 | } | ||
| 135 | |||
| 136 | method window_gained | ||
| 137 | { | ||
| 138 | $self->SUPER::window_gained( @_ ); | ||
| 139 | $self->start; | ||
| 140 | } | ||
| 141 | |||
| 142 | # precache position | ||
| 143 | method reshape | ||
| 144 | { | ||
| 145 | my $win = $self->window or return; | ||
| 146 | |||
| 147 | ( $_x, $_y ) = map int($_ / 2), $win->cols - $self->cols, $win->lines - $self->lines; | ||
| 148 | |||
| 149 | $_rect = Tickit::Rect->new( | ||
| 150 | top => $_y, | ||
| 151 | left => $_x, | ||
| 152 | lines => $self->lines, | ||
| 153 | cols => $self->cols, | ||
| 154 | ); | ||
| 155 | } | ||
| 156 | |||
| 157 | method tick | ||
| 158 | { | ||
| 159 | return unless $_running; | ||
| 160 | |||
| 161 | my $state = $_state++; | ||
| 162 | $_state %= @_chars; | ||
| 163 | |||
| 164 | if( my $win = $self->window ) { | ||
| 165 | $win->tickit->timer( after => $_interval => sub { $self->tick } ); | ||
| 166 | $win->expose( $_rect ); | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | method render_to_rb | ||
| 171 | { | ||
| 172 | my ( $rb, $rect ) = @_; | ||
| 173 | |||
| 174 | $rb->eraserect( $rect ); | ||
| 175 | |||
| 176 | $rb->text_at( $_y, $_x, $_chars[$_state] ); | ||
| 177 | } | ||
| 178 | |||
| 179 | =head1 AUTHOR | ||
| 180 | |||
| 181 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 182 | |||
| 183 | =cut | ||
| 184 | |||
| 185 | 0x55AA; |