From: Breno G. de Oliveira Date: Sun, 4 Oct 2009 07:07:50 +0000 (-0300) Subject: implemented fit and fit_ip X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=64c66a05c2efc805f265527cbe59c7bd45d5d43a;p=sdlgit%2FSDL_perl.git implemented fit and fit_ip --- diff --git a/lib/SDL/Game/Rect.pm b/lib/SDL/Game/Rect.pm index b3070d7..18f4067 100644 --- a/lib/SDL/Game/Rect.pm +++ b/lib/SDL/Game/Rect.pm @@ -512,6 +512,51 @@ sub unionall_ip { return; } +sub _check_fit { + my ($self, $rect) = (@_); + + my $x_ratio = $self->w / $rect->w; + my $y_ratio = $self->h / $rect->h; + my $max_ratio = ($x_ratio > $y_ratio) ? $x_ratio : $y_ratio; + + my $w = int ($self->w / $max_ratio); + my $h = int ($self->h / $max_ratio); + + my $x = $rect->x + int (($rect->w - $w) / 2); + my $y = $rect->y + int (($rect->h - $h) / 2); + + return ($x, $y, $w, $h); +} + +sub fit { + my ($self, $rect) = (@_); + + unless ($rect->isa('SDL::Rect')) { + croak "must receive an SDL::Rect-based object"; + } + + my ($x, $y, $w, $h) = _check_fit($self, $rect); + + return $self->new ($x, $y, $w, $h); +} + +sub fit_ip { + my ($self, $rect) = (@_); + + unless ($rect->isa('SDL::Rect')) { + croak "must receive an SDL::Rect-based object"; + } + + my ($x, $y, $w, $h) = _check_fit($self, $rect); + + $self->x($x); + $self->y($y); + $self->w($w); + $self->h($h); + + return; +} + 42; __END__