Permalink
Browse files

Refactor item rects

  • Loading branch information...
1 parent 6eb3362 commit 33cff0cf28a2d2a65fe737aefca1c577bba37610 @jtpalmer jtpalmer committed Sep 11, 2011
Showing with 13 additions and 28 deletions.
  1. +13 −28 lib/SDLx/Widget/Menu.pm
View
@@ -41,8 +41,6 @@ has '_items' => (is => 'rw', isa => 'ArrayRef', default => sub {[]} );
has '_font' => (is => 'rw', isa => 'SDLx::Text' );
has '_change_sound' => (is => 'rw', isa => 'SDL::Mixer::MixChunk' );
has '_select_sound' => (is => 'rw', isa => 'SDL::Mixer::MixChunk' );
-has '_item_rects' => ( is => 'rw', isa => 'ArrayRef[SDLx::Rect]',
- default => sub {[]} );
sub BUILD {
my $self = shift;
@@ -92,41 +90,28 @@ sub _build_sound {
}
}
-sub _build_item_rects {
- my ($self) = @_;
+# this is the method used to indicate
+# all menu items and their callbacks
+sub items {
+ my ($self, @items) = @_;
my ( $top, $left ) = @{$self->topleft};
- my @rects;
-
- foreach my $item ( @{$self->_items} ) {
+ while( my ($name, $val) = splice @items, 0, 2 ) {
my ( $width, $height )
- = @{ SDL::TTF::size_text( $self->_font->font, $item->{name} ) };
+ = @{ SDL::TTF::size_text( $self->_font->font, $name ) };
# XXX left? not really
my $rect
= SDLx::Rect->new( $left - $width / 2, $top, $width, $height );
- push @rects, $rect;
+ push @{$self->_items},
+ { name => $name, trigger => $val, rect => $rect };
# XXX this seems like a bad idea, see render()
$top += 50;
}
- $self->_item_rects( \@rects );
-}
-
-# this is the method used to indicate
-# all menu items and their callbacks
-sub items {
- my ($self, @items) = @_;
-
- while( my ($name, $val) = splice @items, 0, 2 ) {
- push @{$self->_items}, { name => $name, trigger => $val };
- }
-
- $self->_build_item_rects;
-
return $self;
}
@@ -154,20 +139,20 @@ sub event_hook {
}
elsif ( $self->mouse && ( $mask & SDL_MOUSEEVENTMASK ) ) {
my ( $x, $y ) = ( $event->button_x, $event->button_y );
- my @rects = @{$self->_item_rects};
+ my @items = @{$self->_items};
if( $type == SDL_MOUSEBUTTONDOWN ) {
- for ( 0 .. $#rects ) {
- if ( $rects[$_]->collidepoint( $x, $y ) ) {
+ for ( 0 .. $#items ) {
+ if ( $items[$_]->{rect}->collidepoint( $x, $y ) ) {
$self->current( $_ );
last;
}
}
}
elsif ( $type == SDL_MOUSEBUTTONUP ) {
- if ( $rects[$self->current]->collidepoint( $x, $y ) ) {
+ if ( $items[$self->current]->{rect}->collidepoint( $x, $y ) ) {
$self->_play($self->_select_sound);
- return $self->_items->[$self->current]->{trigger}->();
+ return $items[$self->current]->{trigger}->();
}
}
}

0 comments on commit 33cff0c

Please sign in to comment.