From: Breno G. de Oliveira Date: Tue, 29 Sep 2009 07:30:24 +0000 (-0300) Subject: implemented clamp and clamp_ip X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=57f262e845da701522b91e477ba7c819c0b90da6;p=sdlgit%2FSDL_perl.git implemented clamp and clamp_ip --- diff --git a/lib/SDL/Game/Rect.pm b/lib/SDL/Game/Rect.pm index 19fb2ec..95332df 100644 --- a/lib/SDL/Game/Rect.pm +++ b/lib/SDL/Game/Rect.pm @@ -278,6 +278,51 @@ sub inflate_ip { $self->h( $self->h + $y ); } +sub _get_clamp_coordinates { + my ($self_pos, $self_len, $rect_pos, $rect_len) = (@_); + + if ($self_len >= $rect_len) { + return $rect_pos + ($rect_len / 2) - ($self_len / 2); + } + elsif ($self_pos < $rect_pos) { + return $rect_pos; + } + elsif ( ($self_pos + $self_len) > ($rect_pos + $rect_len) ) { + return $rect_pos + $rect_len - $self_len; + } + else { + return $self_pos; + } +} + +sub clamp { + my ($self, $rect) = (@_); + + unless ($rect->isa('SDL::Rect')) { + croak "must receive an SDL::Rect-based object"; + } + + my $x = _get_clamp_coordinates($self->x, $self->w, $rect->x, $rect->w); + my $y = _get_clamp_coordinates($self->y, $self->h, $rect->y, $rect->h); + + return $self->new($x, $y, $self->w, $self->h); +} + +sub clamp_ip { + my ($self, $rect) = (@_); + + unless ($rect->isa('SDL::Rect')) { + croak "must receive an SDL::Rect-based object"; + } + + my $x = _get_clamp_coordinates($self->x, $self->w, $rect->x, $rect->w); + my $y = _get_clamp_coordinates($self->y, $self->h, $rect->y, $rect->h); + + $self->x($x); + $self->y($y); + + return; +} 42; __END__