| 1 | #!/usr/bin/perl | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Tk; | ||
| 5 | use Data::Dumper; | ||
| 6 | require Tk::GraphItems::TextBox; | ||
| 7 | require Tk::GraphItems::Connector; | ||
| 8 | |||
| 9 | my $mw = tkinit(); | ||
| 10 | my$text; | ||
| 11 | $mw->Label(-textvariable=>\$text)->pack; | ||
| 12 | my $s_can = $mw -> Scrolled('Canvas', | ||
| 13 | -scrollregion=>[0,0,200,700], | ||
| 14 | )->pack(-side =>'right', | ||
| 15 | -fill =>'both', | ||
| 16 | -expand=>1); | ||
| 17 | my $can = $s_can->Subwidget('scrolled'); | ||
| 18 | |||
| 19 | my @node; | ||
| 20 | my @conn; | ||
| 21 | my ($tx,$ty) = (100,100); | ||
| 22 | my %command; | ||
| 23 | $command{build_items}=sub{ | ||
| 24 | my ($x,$y) = (50,20); | ||
| 25 | for my $n(0..4){ | ||
| 26 | $node[$n] =Tk::GraphItems::TextBox->new(canvas => $can, | ||
| 27 | text => "object $n", | ||
| 28 | 'x' => $x=($x%200)+40, | ||
| 29 | 'y' => $y+=100); | ||
| 30 | } | ||
| 31 | ($tx,$ty)=(50,200); | ||
| 32 | $node[5] = Tk::GraphItems::TextBox->new(canvas=>$can, | ||
| 33 | text =>"object\ntied x_y", | ||
| 34 | 'x'=>\$tx, | ||
| 35 | 'y'=>\$ty); | ||
| 36 | for my $n(0..4){ | ||
| 37 | $conn[$n] = Tk::GraphItems::Connector->new( | ||
| 38 | source =>$node[$n], | ||
| 39 | target =>$node[($n+1)%5], | ||
| 40 | colour =>'black', | ||
| 41 | ); | ||
| 42 | } | ||
| 43 | |||
| 44 | weaken $_ for(@conn) ; | ||
| 45 | |||
| 46 | Tk::GraphItems::Connector->new( | ||
| 47 | source=>$node[5], | ||
| 48 | target=>$node[3], | ||
| 49 | ); | ||
| 50 | $conn[0]->bind_class('<3>',sub{my $col = $_[0]->colour; | ||
| 51 | $col = $col eq 'red' ? 'black':'red'; | ||
| 52 | $_[0]->colour($col); | ||
| 53 | } | ||
| 54 | ); | ||
| 55 | |||
| 56 | }; | ||
| 57 | |||
| 58 | $command{nodes_move}= sub{ | ||
| 59 | for (1..2){$node[$_]->move(20,5)}; | ||
| 60 | $_ += 10 for ($tx,$ty); | ||
| 61 | }; | ||
| 62 | $command{undef_last}=sub { | ||
| 63 | my $item = pop(@node); | ||
| 64 | undef ($item); | ||
| 65 | }; | ||
| 66 | $command{node_set_coord}=sub { | ||
| 67 | $node[1]->set_coords(40,20); | ||
| 68 | }; | ||
| 69 | $command{node_set_text}=sub { | ||
| 70 | for my $n(2..4){ | ||
| 71 | my $node = $node[$n]; | ||
| 72 | $node->text($node->text . "\nand more"); | ||
| 73 | } | ||
| 74 | }; | ||
| 75 | $command{node_set_colour}=sub { | ||
| 76 | for my $n(1..3){ | ||
| 77 | my $node = $node[$n]; | ||
| 78 | $node->colour($node->colour eq 'red'? 'white':'red'); | ||
| 79 | } | ||
| 80 | }; | ||
| 81 | $command{conn_directed} = sub { | ||
| 82 | foreach(@conn){$_->arrow('target')} | ||
| 83 | }; | ||
| 84 | $command{conn_undirected} = sub { | ||
| 85 | foreach(@conn){$_->arrow('both')} | ||
| 86 | }; | ||
| 87 | $command{conn_width} = sub { | ||
| 88 | foreach(@conn){$_->width($_->width >2?1:$_->width +1)} | ||
| 89 | }; | ||
| 90 | $command{conn_colour} = sub { | ||
| 91 | foreach(@conn){$_->colour($_->colour eq'red'?'black':'red')} | ||
| 92 | }; | ||
| 93 | |||
| 94 | my $frame = $mw->Frame()->pack(-side=>'left'); | ||
| 95 | my $prev; | ||
| 96 | for (sort keys %command){ | ||
| 97 | $frame->Button(-text =>$_, | ||
| 98 | -command=>$command{$_}, | ||
| 99 | -width =>20)->pack; | ||
| 100 | } | ||
| 101 | |||
| 102 | MainLoop; |