| 1 | package Acme::Monkey::Frame::Layer; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | |||
| 5 | use Term::ANSIColor qw(:constants); | ||
| 6 | |||
| 7 | has 'x' => (is=>'rw', isa=>'Int', default=>1); | ||
| 8 | has 'y' => (is=>'rw', isa=>'Int', default=>1); | ||
| 9 | |||
| 10 | has 'width' => (is=>'rw', isa=>'Int', required=>1); | ||
| 11 | has 'height' => (is=>'rw', isa=>'Int', required=>1); | ||
| 12 | |||
| 13 | has 'hidden' => (is=>'rw', isa=>'Int', default=>0); | ||
| 14 | has 'constrain' => (is=>'rw', isa=>'Int', default=>0); | ||
| 15 | has 'color' => (is=>'rw', isa=>'Str', default=>WHITE); | ||
| 16 | |||
| 17 | has '_canvas' => (is=>'rw', isa=>'ArrayRef', default=>\&clear, lazy=>1); | ||
| 18 | |||
| 19 | sub clear { | ||
| 20 | my ($self) = @_; | ||
| 21 | |||
| 22 | my $canvas = []; | ||
| 23 | |||
| 24 | foreach my $x (1..$self->width()) { | ||
| 25 | foreach my $y (1..$self->height()) { | ||
| 26 | $canvas->[$x]->[$y] = ''; | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | $self->_canvas( $canvas ); | ||
| 31 | |||
| 32 | return $canvas; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub set { | ||
| 36 | my ($self, $x, $y, $string) = @_; | ||
| 37 | |||
| 38 | my $current_x = $x; | ||
| 39 | my $current_y = $y; | ||
| 40 | foreach my $char (split(//, $string)) { | ||
| 41 | if ($char eq "\n") { | ||
| 42 | $current_y ++; | ||
| 43 | $current_x = $x; | ||
| 44 | } | ||
| 45 | else { | ||
| 46 | $char = '' if ($char eq "\t"); | ||
| 47 | $self->_canvas->[$current_x]->[$current_y] = $char; | ||
| 48 | $current_x ++; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | sub get { | ||
| 54 | my ($self, $x, $y) = @_; | ||
| 55 | |||
| 56 | return '' if ($self->hidden()); | ||
| 57 | return '' if ($x<1 or $y<1 or $x>$self->width() or $y>$self->height()); | ||
| 58 | return $self->_canvas->[$x]->[$y] || ''; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub move_up { | ||
| 62 | my ($self, $shift) = @_; | ||
| 63 | |||
| 64 | $self->y( $self->y() - ($shift || 1) ); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub move_down { | ||
| 68 | my ($self, $shift) = @_; | ||
| 69 | |||
| 70 | $self->y( $self->y() + ($shift || 1) ); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub move_left { | ||
| 74 | my ($self, $shift) = @_; | ||
| 75 | |||
| 76 | $self->x( $self->x() - ($shift || 1) ); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub move_right { | ||
| 80 | my ($self, $shift) = @_; | ||
| 81 | |||
| 82 | $self->x( $self->x() + ($shift || 1) ); | ||
| 83 | } | ||
| 84 | |||
| 85 | 1; |