#! /usr/bin/perl -w
use strict;
use Gtk2 -init;
use Glib qw/TRUE FALSE/;

#array pointers to indicate the displayed file
my $current_file = 0;
my $current_bounce = 0;
my $current_side_bounce = 0;
#direction indicators for the stepping sub
my $up_flag = FALSE;
my $left_flag = FALSE;
#indicate if it is currently bouncing up/down
my $bounce_flag =FALSE;
#indicate if it is currently bouncing sideways
my $side_bounce_flag =FALSE;

#Initial position horizontal and vertical
my $x =5;
my $y=5;
#flag to indicate if the image changing and movement stepping
#should be stopped
my $freeze_flag = FALSE;
#step size
my $step_x = 10;
my $step_y = 10;

#Pre Widget prep START --------

#list of files to show if not bouncing
my @file_list = qw/t1.xpm t2.xpm t3.xpm t4.xpm t5.xpm t6.xpm t7.xpm t8.xpm t9.xpm/;
 my $list_count = scalar @file_list;

#list of files to show for top and bottom bouncing
my @bounce_top_bottom = qw/t1.xpm b1.xpm b2.xpm b3.xpm b2.xpm b1.xpm t1.xpm/;
 my $bounce_top_bottom_count = scalar @bounce_top_bottom;
 
#list of files to show for side bouncing
my @bounce_sides = qw/t3.xpm s1.xpm s2.xpm s3.xpm s2.xpm s1.xpm t3.xpm/;
 my $bounce_sides_count = scalar @bounce_sides;

#Pre Widget prep END --------
#timeout that will repeat.
Glib::Timeout->add (200,sub{ &change() });

#popup windows will create shaped windows.
 my $window = Gtk2::Window->new ('popup');
 $window->resize(4,4);
 
 	#create an event box that will house the pixmaps
  	my $eventbox = Gtk2::EventBox->new ();
  	#set the freeze flag when we enter or leave the eventbox
  	$eventbox->signal_connect('enter-notify-event' => sub { $freeze_flag = TRUE});
 	$eventbox->signal_connect('leave-notify-event' => sub { $freeze_flag = FALSE});

		my $img = Gtk2::Image->new();

	$eventbox->add($img);
$window->add ($eventbox);

#discover which size of screen we have
my $screen = $window->get_screen();
my $screen_width = $screen->get_width();
my $screen_height = $screen->get_height();

$window->show_all;

	#first run
	&change();
Gtk2->main;

sub change {

	#IF the freese flag is set, put a message for the user, and return	
	if ($freeze_flag) {
		my($pxm_current, $msk_current) = Gtk2::Gdk::Pixmap->create_from_xpm ($window->window,undef, "./pix/stop.xpm");
		$img->set_from_pixmap ($pxm_current, $msk_current);
		$window->window->shape_combine_mask($msk_current, 0, 0);	
	return TRUE;
	
	} 
	#----------------------------------
	
	my ($w_w,$w_h) = $window->get_size;
	
	#we test for the following"
	#	1.) if x values are to far, if YES, we bounce 
	# 	sides and reverse the direction
	#	2.) if y values are to far, if YES, we bounce
	# 	top_bottom and reverse the direction 
	
	#test x:
	my $max_x = $screen_width - $w_w;
	if($x > $max_x){
		$left_flag = TRUE;
		$side_bounce_flag = TRUE;
	}
	
	if ($x < 0){
		$left_flag = FALSE;
		$side_bounce_flag = TRUE;
	}		
	
	#test y:
	my $max_y = $screen_height - $w_h;
	if ($y > $max_y){
		$up_flag = TRUE;
		$bounce_flag = TRUE;
	}
	
	if ($y < 0){
		$up_flag = FALSE;
		$bounce_flag = TRUE;
	}
	
	#this will shrink the window prior to changing the image
	$window->resize(4,4);
	#move the window to the new position
	$window->move($x,$y);
	
	
	if ((!($bounce_flag))&&(!($side_bounce_flag))){
	
		&do_steps();
		&do_motions();
	}
	
	($side_bounce_flag)	&&(&bounce_side);
	($bounce_flag)		&&(&bounce_top_bottom);
	return TRUE;
 
}

sub bounce_top_bottom {
	#working through the top/bottom bounce list of images
	my $file_string = "./pix/".$bounce_top_bottom[$current_bounce];
	$current_bounce ++;
	
	my($pxm_current, $msk_current) = Gtk2::Gdk::Pixmap->create_from_xpm ($window->window,undef, "$file_string");
	$img->set_from_pixmap ($pxm_current, $msk_current);
	$window->window->shape_combine_mask($msk_current, 0, 0);
	
	if($current_bounce == $bounce_top_bottom_count -1){
	#if we got to the end of the list, we have to reset a few variables.
	#and do the step thing to continue moving
		$bounce_flag = FALSE;
		$current_bounce = FALSE;
		&do_steps();
		
	}	
		
}

sub bounce_side {
	#working through the side bounce list of images
	my $file_string = "./pix/".$bounce_sides[$current_side_bounce];
	$current_side_bounce ++;
	
	my($pxm_current, $msk_current) = Gtk2::Gdk::Pixmap->create_from_xpm ($window->window,undef, "$file_string");
	$img->set_from_pixmap ($pxm_current, $msk_current);
	$window->window->shape_combine_mask($msk_current, 0, 0);
	
	if($current_side_bounce == $bounce_sides_count -1){
	#if we got to the end of the list, we have to reset a few variables.
	#and do the step thing to continue moving
		$side_bounce_flag = FALSE;
		$current_side_bounce = FALSE;
		&do_steps();
		
	}	
	
}

sub do_steps {

	#depending on the $up_flag we will step up or down
	if ($up_flag){
		$y = $y - $step_y;
	}else{
		$y = $y + $step_y;
	}

	#depending on the $left_flag we will step to the left or right
	if ($left_flag){
		$x = $x - $step_x;
		
	}else{
		$x = $x + $step_x;
		
	}

}

sub do_motions {
	#when we are not bouncing, we are in motion, this gives the illusion
	my $file_string = "./pix/".$file_list[$current_file];
		$current_file ++;
		
	($current_file == $list_count)&&($current_file = 0);
		
	my($pxm_current, $msk_current) = Gtk2::Gdk::Pixmap->create_from_xpm ($window->window,undef, "$file_string");
	$img->set_from_pixmap ($pxm_current, $msk_current);
	$window->window->shape_combine_mask($msk_current, 0, 0);

}