Acme-Curses-Marquee

 view release on metacpan or  search on metacpan

lib/Acme/Curses/Marquee.pm  view on Meta::CPAN

use Curses qw( addstr refresh );

=head1 NAME

Acme::Curses::Marquee - Animated Figlet!

=head1 VERSION

Version 1.0

=cut

our $VERSION = '1.0';

=head1 SYNOPSIS

Acme::Curses::Marquee implements a scrolling messageboard widget,
using C<figlet> to render the text.

    use Curses;
    use Acme::Curses::Marquee;

    initscr;

    # curses halfdelay mode is what actually drives the display
    # and its argument is what determines the rate of the crawl
    halfdelay(1);

    # spawn subwindow to hold marquee and create marquee object
    my $mw = subwin(9,80,0,0);
    my $m = Acme::Curses::Marquee->new( window => $mw,
                                        height => 9,
                                        width  => 80,
                                        font   => larry3d,
                                        text   => 'hello, world' );

    # then, in the event loop
    while (1) {
        my $ch = getch;
        do_input_processing_and_other_crud();
        $m->scroll;
    }

=head1 METHODS

=head2 new

Creates a new A::C::M object. Three arguments are required:

    * window
    * height
    * width

C<window> should be a curses window that the marquee can write
to. C<height> and C<width> should be the height and width of that
window, in characters.

There are also two optional arguments: C<font>, which sets the figlet
font of the marquee (defaults to the figlet default, 'standard'), and
C<text> which will set an initial string to be displayed and cause the
marquee to start display as soon as it is created.

=cut

sub new {
    my ($class,%args) = @_;

    die "Can't create marquee object without a host window\n" 
        unless( defined $args{window} );
    die "Can't create marquee object without height value\n" 
        unless( defined $args{height} );
    die "Can't create marquee object without width value\n" 
        unless( defined $args{width} );

    my $self = bless { win    => $args{window},
                       height => $args{height},
                       width  => $args{width},
                       font   => $args{font} || 'standard',
                       srctxt => $args{text} || undef, 
                       figtxt => '',
                       txtlen => 0,
                       offset => 0,
                       active => 0,
                     }, $class;

    $self->text($self->{srctext}) if (defined $self->{srctxt});

    return $self;
}

=head2 scroll

Scroll the marquee one position to the right.

=cut

sub scroll {
    my $self = shift;
    my $w    = $self->{win};
    my $x    = $self->{width};
    my $y    = $self->{height};
    my $len  = $self->{txtlen};
    my $off  = $self->{offset};
    my $fig  = $self->{figtxt};

    for (0..$y) { addstr($w, $_, 0, (' ' x $x)) }

    $self->{offset} = 0 if ($self->{offset} == $len);

    foreach my $i (0..(@{$fig} - 1)) {
        if ($off + $x > $len) {
            my $end = $len - $off;
            my $rem   = $x - $end;
            my $tmp = substr($fig->[$i],$off,$end);
            $tmp   .= substr($fig->[$i],0,$rem);
            addstr($w, $i, 0, $tmp);
        } else {
            addstr($w, $i, 0, substr($fig->[$i],$off,$x));
        }
    }
    $self->{offset}++;



( run in 2.335 seconds using v1.01-cache-2.11-cpan-0bb4e1dffa6 )