App-Tarotplane

 view release on metacpan or  search on metacpan

lib/App/Tarotplane/UI.pm  view on Meta::CPAN

	First => [ KEY_NPAGE, KEY_END, ],
	Last  => [ KEY_PPAGE, KEY_HOME, ],
	Quit  => [ 'q', ],
	Help  => [ '?', ],
);

my $CONTROLS_HELP = <<END;
Next:      right, l
Previous:  left, h
Flip:      up/down, j/k, space
First      card: page up, home
Last card: page down, end
Quit:      q
Help:      ?
END

sub init {

	my $class = shift;
	my $self = {
		MainWin => undef,
		CardWin => undef,
		InfoWin => undef,
		CardStr => '',
		InfoStr => '',
	};

	if ($Cursed) {
		croak "Curses is already running";
	}

	bless $self, $class;

	initscr();

	curs_set(0);
	cbreak();
	noecho();
	keypad(1);

	$self->{MainWin} = $stdscr;

	$self->{InfoWin} = newwin(1, $COLS, $LINES - 1, 0);
	$self->{CardWin} = newwin($LINES - 3, $COLS - 2, 1, 1);

	$Cursed = 1;

	return $self;

}

sub wipe {

	my $self = shift;

	erase($self->{MainWin});
	noutrefresh($self->{MainWin});

}

sub update {

	my $self = shift;

	doupdate();

}

sub draw_card {

	my $self = shift;
	my $str  = shift;
	my $bold = shift;

	# Make room for the box borders and a single whitespace on each side.
	my $linemax = getmaxx($self->{CardWin}) - 4;

	$self->{CardStr} = $str if defined $str;

	$columns = $linemax;
	my $text = wrap('', '', $self->{CardStr});

	if (defined $bold) {
		attrset($self->{CardWin}, $bold ? A_BOLD : A_NORMAL);
	}

	erase($self->{CardWin});
	box($self->{CardWin}, 0, 0);

	my $ypos = (getmaxy($self->{CardWin}) / 2) - (($text =~ tr/\n//) / 2);

	foreach my $l (split /\n/, $text) {
		$l =~ s/^\s+|\s+$//; # Trim leading/trailing whitespace.
		$l =~ s/\s+/ /g;     # Truncate space.
		addstr($self->{CardWin}, $ypos++, (($linemax + 4) - length($l)) / 2, $l);
	}

	noutrefresh($self->{CardWin});

}

sub draw_info {

	my $self = shift;
	my $info = shift;

	$self->{InfoStr} = $info if defined $info;

	erase($self->{InfoWin});

	addstr($self->{InfoWin}, $self->{InfoStr});

	noutrefresh($self->{InfoWin});

}

sub draw_help {

	my $self = shift;

	my $y = 0;
	foreach my $l (split /\n/, $CONTROLS_HELP) {
		addstr($self->{MainWin}, $y++, 0, $l);
	}

	noutrefresh($self->{MainWin});

}

sub update_size {

	my $self = shift;

	$self->wipe();

	resize($self->{InfoWin}, 1, $COLS);
	mvwin($self->{InfoWin}, $LINES - 1, 0);

	resize($self->{CardWin}, $LINES - 3, $COLS - 2);
	mvwin($self->{CardWin}, 1, 1);

	$self->draw_card();
	$self->draw_info();

	$self->update();

}

sub poll {

	my $self = shift;

	while (my $in = getch()) {

		if ($in eq KEY_RESIZE) {
			$self->update_size();
			next;
		}

		foreach my $k (keys %KEY_BINDINGS) {
			return $k if grep { $in eq $_ } @{$KEY_BINDINGS{$k}};
		}

		return undef;

	}

}

sub end {

	my $self = shift;

	endwin();

	foreach my $win (grep { /Win$/ } keys %{$self}) {
		$self->{$win} = undef;
	}

	$Cursed = 0;

}

DESTROY {

	my $self = shift;

	$self->end();

}



( run in 0.832 second using v1.01-cache-2.11-cpan-9581c071862 )