All pastes #757793 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-11-01 16:48 UTC
rendered paste body
#the basic window class, from which every window will be derived.

package Window;
use Moose;
extends (qw(Object));

#position for x in pixel
has 'x' => (
	is => 'rw',
 	required =>1,
	isa => 'Num',
	default => 0
);

#position for y in pixel
has 'y' => (
	is => 'rw',
 	required =>1,
	isa => 'Num',
	default=> 0
);


#widgets list
has 'content' => (
	is =>'rw',
	required =>0,
	isa => 'ArrayRef'
);

#return value for the window, once set it will close it.
has 'answer' => (
	is => 'rw',
	required => 0,
	isa => 'Any'
);

#constructor.
sub BUILD {
	my ( $self )  = @_ ;
	#every window get a close button.
        my $widget = new Widget name =>"close",
                        type => "icon",
                        x    => 0,
                        y    => 0,
                        img  => 'close',
                        xsize => 16,
                        ysize => 16 ; #useless again.
			window => $self;


        my $wt;
        push @$wt,$widget;
	$self->content( $wt );
	
}

#Mouse actions on that window.
# if a widget is clicked, then its action will be called.
# if the click is done outside  the window code() function will be called with current cursor position.
sub action {
	my ( $self,$x,$y ) = @_;
	
	#calculate action in the window.
	my $rx = $x - $self->x ;
	my $ry = $y - $self->y ;
	my $max_x = $self->x  + $self->xsize;
	my $max_y = $self->y  + $self->ysize;

	#click inside window
	if( $x > $self->x  and $x < $max_x ) {
		if( $y > $self->y  and $y < $max_y ) {
			my $objects = $self->content;
			print "click inside the window $rx $ry\n";
			foreach my $object ( @$objects ) {
				
				if( $rx > $object->x  and $ry > $object->y  ) {
					if( $rx < ( $object->x + $object->xsize ) and  $ry < ( $object->y + $object->ysize ) ) {
						#click on a widget
						print "match ! " . $object->name ."\n";
						$object->click;
					}
				}
			}
		}
	}
	else {
	#click outside window
		if( defined( $self->answer ) ) {
			$self->code( $x,$y);
		}
	}
}

#show the full window
sub show($) {
	my ( $self ) = @_;

	#show the cursor.
	SDL::ShowCursor (1);

	#first the background color.
	my $color = SDL::Color->new(
		-r => 0x00,
		-g => 0x00,
		-b => 0x00,
	);
	
	#the background rect.
	my $window = SDL::Rect->new(
		-height => $self->xsize,
		-width  => $self->ysize,
		-x      => $self->x,
		-y      => $self->y,
	);
	
	#draw window's background
	$ProneClient::screen->fill( $window, $color );

	#the borders
	$ProneClient::screen = ProneClient::prone_image_copy( $ProneClient::screen,
							      $ProneClient::imgs->{"corner1"},
							      $self->x-5,
							      $self->y-5 );
	$ProneClient::screen = ProneClient::prone_image_copy( $ProneClient::screen,
							      $ProneClient::imgs->{"corner2"},
							      $self->x+$self->xsize + 5 - 32,
							      $self->y -5);
	$ProneClient::screen = ProneClient::prone_image_copy( $ProneClient::screen,
							      $ProneClient::imgs->{"corner3"},
							      $self->x+  $self->xsize +5 -32  ,
							      $self->y + $self->ysize +5 -32 );

	$ProneClient::screen = ProneClient::prone_image_copy( $ProneClient::screen,
							      $ProneClient::imgs->{"corner4"},
							      $self->x-5,
							      $self->y + $self->ysize +5 -32 ) ;
	#show the widget.
	my $wid = $self->content;
	foreach my $dget ( @$wid ) {
		$dget->show($ProneClient::screen,$self->x,$self->y);
	}

	return 0;
}


return 1;