#!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Zinc; my $motion_flag = 0; my $control = 'neutral'; my $mw = MainWindow->new; $mw->geometry("700x600"); $mw->resizable(0,0); my $zinc = $mw->Zinc(-width => 700, -height => 565, -backcolor => 'black', -borderwidth => 3, -relief => 'sunken')->pack; # Then we create a gray filled rectangle, in which we will display explain text. $zinc->add('rectangle', 1 , [200, 400, 490, 490], -linewidth => 2, -filled => 1, -fillcolor => 'SkyBlue', ); my $text = $zinc->add('text', 1, -position => [350, 445], -anchor => 'center', ); $zinc->add('rectangle', 1 , [250,275,450,325], #corners (xpos1,ypos1,xpos2,ypos2) -linewidth => 2, -filled => 1, -fillcolor => 'Orange', ); my $wintext = $zinc->add('text', 1, -position => [350, 300], -anchor => 'center', ); #create triangles #create first triangle, then clone and translate #maintain an array of positions my ($x1,$y1,$x2,$y2,$x3,$y3) = my @initcoords = (0,0,20,0,10,40); my @pos1 = (130,20); #initial positions to translate to my @pos2 = (340,20); my @pos3 = (550,20); my $tr1 = $zinc->add('triangles', 1, [@initcoords], #(x1,y1,x2,y2,x3,y3) -fan => 1, -colors => 'Orange', -visible => 1, ); my $tr2 = $zinc->clone($tr1); my $tr3 = $zinc->clone($tr1); $zinc->translate($tr1,@pos1); $zinc->translate($tr2,@pos2); $zinc->translate($tr3,@pos3); #redefine center positions to account for translations @pos1 = (($pos1[0]+($x2-$x1)/2), ($pos1[1]+($y3-$x3)/2)); #(140,35) @pos2 = (($pos2[0]+($x2-$x1)/2), ($pos2[1]+($y3-$x3)/2)); #(350,35) @pos3 = (($pos3[0]+($x2-$x1)/2), ($pos3[1]+($y3-$x3)/2)); #(560,35) # Display comment &comment("Hit Tab to begin"); &wincomment("READY"); # Create Tk binding $mw->Tk::bind('', \&start); my $closebutton = $mw->Button(-text => 'Exit', -command => sub{Tk::exit(0)}) ->pack; MainLoop; sub start { # set binding to stop and exit $mw->Tk::bind('', \&stopaction); $mw->Tk::bind('<1>', sub{$control = 'up'}); $mw->Tk::bind('<3>', sub{$control = 'down'}); $mw->Tk::bind('<2>', sub{$control = 'neutral'}); $mw->Tk::bind('', sub{$control = 'up'}); $mw->Tk::bind('', sub{$control = 'down'}); $mw->Tk::bind('', sub{$control = 'neutral'}); $mw->Tk::bind('', sub{$control = 'neutral'}); $motion_flag = 1; &comment("Hit Escape to stop.\nMouseButton1->up MouseButton3->down\nMouseButton2->neutral"); &startaction(); } sub stopaction{ $motion_flag = 0; &comment("Hit Tab to begin"); &wincomment("READY"); # Callback bound to '' event when pressed $mw->Tk::bind('', \&start); } sub startaction { # move triangles &wincomment("$control"); $zinc->rotate($tr1,.1416,$pos1[0],$pos1[1]); $zinc->rotate($tr2,.1416,$pos2[0],$pos2[1]); $zinc->rotate($tr3,.1416,$pos3[0],$pos3[1]); $zinc->translate($tr1,0,10); #increases radius of turn $zinc->translate($tr2,0,10); #try commenting them out $zinc->translate($tr3,0,10); #try changing values if($control eq 'down'){ $pos1[1]++; #causes downward drift $pos2[1]++; $pos3[1]++; } if($control eq 'up'){ $pos1[1]--; #causes upward drift $pos2[1]--; $pos3[1]--; } if($motion_flag == 1){ $zinc->after(1, sub {startaction()})} else {return} } # Just display comment sub comment { my $string = shift; $zinc->itemconfigure($text, -text => $string); } # display winning comment sub wincomment { my $string = shift; $zinc->itemconfigure($wintext, -text => $string); }