| 1 | #!/usr/bin/perl | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Tk; | ||
| 5 | use Data::Dumper; | ||
| 6 | use Scalar::Util qw/ weaken /; | ||
| 7 | #require Tk::GraphItems::TextBox; | ||
| 8 | #require Tk::GraphItems::Connector; | ||
| 9 | require Tk::GraphItems; | ||
| 10 | my $mw = tkinit(); | ||
| 11 | my$text; | ||
| 12 | $mw->Label(-textvariable=>\$text)->pack; | ||
| 13 | my $s_can = $mw -> Scrolled('Canvas', | ||
| 14 | -scrollregion=>[0,0,600,400], | ||
| 15 | )->pack(-side =>'right', | ||
| 16 | -fill =>'both', | ||
| 17 | -expand=>1); | ||
| 18 | my $can = $s_can->Subwidget('scrolled'); | ||
| 19 | |||
| 20 | my @node; | ||
| 21 | my @conn; | ||
| 22 | my @coords= map [(int rand 500)+50,(int rand 300) +50],(0..4); | ||
| 23 | my %command; | ||
| 24 | $command{build_items}=sub{ | ||
| 25 | for my $n(0..4){ | ||
| 26 | $node[$n] = Tk::GraphItems->TextBox(canvas => $can, | ||
| 27 | text => "object $n", | ||
| 28 | 'x' => \$coords[$n][0], | ||
| 29 | 'y' => \$coords[$n][1] | ||
| 30 | ); | ||
| 31 | } | ||
| 32 | for my $n(0..4){ | ||
| 33 | $conn[$n] = Tk::GraphItems->Connector( | ||
| 34 | source => $node[$n], | ||
| 35 | target => $node[($n+1)%5], | ||
| 36 | colour => 'black' | ||
| 37 | ); | ||
| 38 | } | ||
| 39 | weaken $_ for(@conn) ; | ||
| 40 | |||
| 41 | $command{nodes_move}->(); | ||
| 42 | } | ||
| 43 | ; | ||
| 44 | my @step; | ||
| 45 | my $repeat; | ||
| 46 | my @borders = (25,25,575,375); | ||
| 47 | $command{nodes_move} = sub { | ||
| 48 | for my $i(0..4){ | ||
| 49 | for (0,1){$step[$i][$_] = ( rand 2)-1} | ||
| 50 | } | ||
| 51 | if ($repeat){$repeat->cancel; | ||
| 52 | undef $repeat;return} | ||
| 53 | $repeat = $mw->repeat(20,\&transform); | ||
| 54 | }; | ||
| 55 | |||
| 56 | sub transform{ | ||
| 57 | for my $n (0..4) { | ||
| 58 | for (0,1) { | ||
| 59 | $coords[$n][$_] += $step[$n][$_]; | ||
| 60 | #print "$coords[$n][$_]\n"; | ||
| 61 | } | ||
| 62 | if ($coords[$n][0]>$borders[2]) { | ||
| 63 | $step[$n][0]= - $step[$n][0]; | ||
| 64 | $coords[$n][0] = $borders[2]; | ||
| 65 | } | ||
| 66 | if ($coords[$n][0]<$borders[0]) { | ||
| 67 | $step[$n][0]= - $step[$n][0]; | ||
| 68 | $coords[$n][0] = $borders[0]; | ||
| 69 | } | ||
| 70 | if ($coords[$n][1]>$borders[3]) { | ||
| 71 | $step[$n][1]= - $step[$n][1]; | ||
| 72 | $coords[$n][1] = $borders[3]; | ||
| 73 | } | ||
| 74 | if ($coords[$n][1]<$borders[1]) { | ||
| 75 | $step[$n][1]= - $step[$n][1]; | ||
| 76 | $coords[$n][1] = $borders[1]; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | } | ||
| 80 | $command{undef_last}=sub { | ||
| 81 | my $item = pop(@node); | ||
| 82 | undef ($item); | ||
| 83 | }; | ||
| 84 | $command{node_set_coord}=sub { | ||
| 85 | $node[1]->set_coords(40,20); | ||
| 86 | }; | ||
| 87 | $command{node_set_text}=sub { | ||
| 88 | |||
| 89 | for my $node (@node){ | ||
| 90 | $node->text($node->text . "\nand more"); | ||
| 91 | } | ||
| 92 | }; | ||
| 93 | $command{node_set_colour}=sub { | ||
| 94 | for my $n(1..3){ | ||
| 95 | my $node = $node[$n]; | ||
| 96 | $node->colour($node->colour eq 'red'? 'white':'red'); | ||
| 97 | } | ||
| 98 | }; | ||
| 99 | $command{conn_directed} = sub { | ||
| 100 | foreach(@conn){$_->arrow('target')} | ||
| 101 | }; | ||
| 102 | $command{conn_undirected} = sub { | ||
| 103 | foreach(@conn){$_->arrow('both')} | ||
| 104 | }; | ||
| 105 | $command{conn_width} = sub { | ||
| 106 | foreach(@conn){$_->width($_->width >2?1:$_->width +1)} | ||
| 107 | }; | ||
| 108 | $command{conn_colour} = sub { | ||
| 109 | foreach(@conn){$_->colour($_->colour eq'red'?'black':'red')} | ||
| 110 | }; | ||
| 111 | |||
| 112 | |||
| 113 | my $frame = $mw->Frame()->pack(-side=>'left'); | ||
| 114 | my $prev; | ||
| 115 | for (sort keys %command){ | ||
| 116 | $frame->Button(-text =>$_, | ||
| 117 | -command=>$command{$_}, | ||
| 118 | -width =>20)->pack; | ||
| 119 | } | ||
| 120 | |||
| 121 | |||
| 122 | |||
| 123 | |||
| 124 | MainLoop; |