| 1 | package Acme::Monkey::Frame; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | |||
| 5 | use Acme::Monkey::ClearScreen; | ||
| 6 | use Term::Screen; | ||
| 7 | use Term::ANSIColor qw(:constants); | ||
| 8 | |||
| 9 | extends 'Acme::Monkey::ClearScreen'; | ||
| 10 | |||
| 11 | has 'width' => (is=>'rw', isa=>'Int', required=>1); | ||
| 12 | has 'height' => (is=>'rw', isa=>'Int', required=>1); | ||
| 13 | |||
| 14 | has 'layers' => (is=>'rw', isa=>'HashRef', default=>sub{ {} }); | ||
| 15 | |||
| 16 | sub draw { | ||
| 17 | my ($self) = @_; | ||
| 18 | |||
| 19 | my @layers = map { $self->layers->{$_} } sort keys( %{ $self->layers() } ); | ||
| 20 | my $content = ''; | ||
| 21 | |||
| 22 | foreach my $y (1..$self->height()) { | ||
| 23 | foreach my $x (1..$self->width()) { | ||
| 24 | my $char; | ||
| 25 | my $color; | ||
| 26 | foreach my $layer (@layers) { | ||
| 27 | $char = $layer->get( | ||
| 28 | $x - $layer->x() + 1, | ||
| 29 | $y - $layer->y() + 1, | ||
| 30 | ); | ||
| 31 | if ($char) { | ||
| 32 | $color = $layer->color(); | ||
| 33 | last; | ||
| 34 | } | ||
| 35 | } | ||
| 36 | if ($char) { | ||
| 37 | $content .= $color.$char.RESET; | ||
| 38 | } | ||
| 39 | else { | ||
| 40 | $content .= ' '; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | $content .= "\n"; | ||
| 44 | } | ||
| 45 | |||
| 46 | $self->clear_screen(); | ||
| 47 | print $content; | ||
| 48 | } | ||
| 49 | |||
| 50 | 1; |