use Cairo;
use AA;
+use AA::Gfx;
+use AA::UI;
use AA::Graph;
use AA::Net;
use AA::Anim;
-my $v = AA::NetV->new('v');
-$v->Set('x', 100);
-$v->Set('y0', 100);
-$v->Set('label', '1');
-
-my $w = AA::NetV->new('w');
-$w->Bind('x', AA::Anim->ticker(200, 600, 200));
-$w->Set('y0', 100);
-$w->Set('label', '2');
-
-my $e = AA::Edge->new('e', $v, $w);
-$e->Set('arrow', 1);
-
-$v->Set("h", 1);
-$w->Set("h", 2);
-
-AA->calculate;
-
-### G T K ###
-
-my $area = Gtk2::DrawingArea->new();
-my $cairo;
-
-my $pixmap;
-my ($lastw, $lasth, $lastd) = (-1, -1, -1);
-my ($pixw, $pixh) = (-1, -1);
-sub draw($) {
- my ($force) = @_;
- my $win = $area->window;
- my $wgc = Gtk2::Gdk::GC->new($win);
- my $alloc = $area->allocation;
- my $w = $alloc->width;
- my $h = $alloc->height;
- my $d = $win->get_depth;
- # print "Area $w x $h x $d, xx=$xx\n";
-
- if (!defined($pixmap) || $lastw != $w || $lasth != $h || $lastd != $d) {
- ($lastw, $lasth, $lastd) = ($w, $h, $d);
- if ($w/$h >= 4/3) {
- $pixh = $h;
- $pixw = int($h/3*4);
- } else {
- $pixw = $w;
- $pixh = int($w/4*3);
- }
- print "Resized to ${w}x${h}, pixmap size ${pixw}x${pixh}\n";
-
- $pixmap = Gtk2::Gdk::Pixmap->new($win, $pixw, $pixh, $d);
- $cairo = Gtk2::Gdk::Cairo::Context->create($pixmap);
- $cairo->select_font_face('URW Palladio L', 'normal', 'normal');
- my $scale = $pixw/1024;
- $cairo->scale($scale, $scale);
- }
-
- if ($force) {
- $wgc->set_rgb_background(0);
- $wgc->set_rgb_foreground(0);
- $win->draw_rectangle($wgc, 1, 0, 0, $w, $h);
- }
+my $ui = AA::UI->new('');
+
+my $back = AA::Background->new('bg');
+
+my $title = AA::Label->new('title');
+$title->MultiSet(
+ 'text' => 'Goldbergův algoritmus',
+ 'size' => 60,
+ 'x' => 1024/2,
+ 'y' => 80,
+);
+
+my @v;
+for (my $i=0; $i<5; $i++) {
+ my $v = AA::NetV->new("v$i");
+ $v->MultiSet(
+ 'x' => 100 + 180*$i,
+ 'y0' => 600,
+ 'label' => ($i == 0) ? "Z" : ($i == 4) ? "S" : $i,
+ 'h' => ($i == 0) ? 5 : 0,
+ );
+ push @v, $v;
+}
- $cairo->rectangle(0, 0, 1024, 768);
- $cairo->set_source_rgb(0, 0, 0);
- $cairo->fill;
+my @e;
+for (my $i=0; $i<4; $i++) {
+ my $e = AA::NetE->new("e$i", $v[$i], $v[$i+1]);
+ $e->Set('c', 5);
+ $e->Set('f', 0);
+ push @e, $e;
+}
- AA->draw($cairo);
+for (my $i=1; $i<@v; $i++) {
+ my $ii = $i; # need to keep a copy in the current closure
+ $v[$i]->Bind('xs', sub { $e[$ii-1]->Get('f') - (($ii == @v-1) ? 0 : $e[$ii]->Get('f')) });
+}
+$v[4]->{XS}->Set('color', [0,1,0]);
- $win->draw_drawable($wgc, $pixmap, 0, 0, 0, 0, $pixw, $pixh);
+sub flow($$) {
+ my ($i,$df) = @_;
+ $e[$i]->Set('f', $e[$i]->Get('f') + $df);
}
-sub step() {
- AA::Anim->step;
- draw(0);
+sub up($) {
+ my ($i) = @_;
+ $v[$i]->Set('h', $v[$i]->Get('h') + 1);
}
-my $window = Gtk2::Window->new ('toplevel');
+sub hint($) {
+ my ($h) = @_;
+ for (my $i=0; $i<@v; $i++) {
+ $v[$i]->Set('hint', $i == $h);
+ }
+}
-my $timer;
-my $stopped = 1;
-my $fullscreen = 0;
-$area->signal_connect("expose-event" => sub {
- draw(1);
- if (!defined $timer) {
- $timer = Glib::Timeout->add(30, sub { $stopped || step(); return 1; });
+my $scenario;
+
+if (!@ARGV || $ARGV[0] eq "1") {
+ $scenario = [
+ sub { flow(0, 5); hint(1); },
+ sub { up(1); },
+ sub { flow(1, 5); hint(2); },
+ sub { up(2); },
+ sub { flow(2, 5); hint(3); },
+ sub { up(3); },
+ sub { flow(3, 5); hint(-1); },
+ ];
+} elsif ($ARGV[0] eq "2") {
+ $title->Set('text', "Goldberg v úzkých");
+ for (my $i=0; $i<@v-1; $i++) {
+ $e[$i]->Set('c', 5-$i);
}
-});
-$area->set_flags('can-focus');
-# $area->add_events('key-press-mask');
-$area->signal_connect('key-press-event' => sub {
- my ($w, $evt) = @_;
- my $k = Gtk2::Gdk::keyval_name(0, $evt->keyval);
- print "Pressed key $k\n";
- if ($k eq "Escape") {
- Gtk2->main_quit;
- } elsif ($k eq "space") {
- $stopped = !$stopped;
- } elsif ($k eq "f") {
- if ($fullscreen = !$fullscreen) {
- $window->fullscreen;
- } else {
- $window->unfullscreen;
- }
+ $scenario = [
+ sub { flow(0, 5); hint(1); },
+ sub { up(1); },
+ sub { flow(1, 4); hint(2); },
+ sub { up(2); },
+ sub { flow(2, 3); hint(3); },
+ sub { up(3); },
+ sub { flow(3, 2); },
+ sub { up(3); },
+ sub { flow(2, -1); hint(2); },
+ sub { up(2); },
+ sub { flow(1, -1); hint(1); },
+ sub { up(1); },
+ sub { up(1); },
+ sub { flow(1, 1); },
+ sub { up(1); },
+ sub { up(1); },
+ sub { up(1); },
+ sub { flow(0, -1); hint(2); },
+ sub { up(2); },
+ sub { flow(2, 1); hint(3); },
+ sub { up(3); },
+ sub { up(3); },
+ sub { flow(2, -1); hint(2); },
+ sub { up(2); },
+ sub { up(2); },
+ sub { flow(2, 1); hint(3); },
+ sub { up(3); },
+ sub { up(3); },
+ sub { flow(2, -1); hint(2); },
+ sub { up(2); },
+ sub { up(2); },
+ sub { flow(2, 1); },
+ sub { flow(1, -1); hint(1); },
+ sub { flow(0, -1); hint(3); },
+ sub { up(3); },
+ sub { up(3); },
+ sub { flow(2, -1); hint(2); },
+ sub { flow(1, -1); hint(1); },
+ sub { flow(0, -1); hint(-1); },
+ ];
+} elsif ($ARGV[0] eq "3") {
+ $title->Set('text', "Goldberg s nejvyšším vrcholem");
+ for (my $i=0; $i<@v-1; $i++) {
+ $e[$i]->Set('c', 5-$i);
}
-});
-
-$window->signal_connect ("delete-event" => sub { Gtk2->main_quit });
-$window->set_title("Brum");
-$window->set_wmclass("brum", "Brum");
-$window->set_default_size(640, 480);
-$window->add ($area);
-$window->show_all;
+ $scenario = [
+ sub { flow(0, 5); hint(1); },
+ sub { up(1); },
+ sub { flow(1, 4); },
+ sub { up(1); },
+ sub { up(1); },
+ sub { up(1); },
+ sub { up(1); },
+ sub { up(1); },
+ sub { flow(0, -1); hint(2); },
+ sub { up(2); },
+ sub { flow(2, 3); },
+ sub { up(2); },
+ sub { up(2); },
+ sub { up(2); },
+ sub { up(2); },
+ sub { up(2); },
+ sub { up(2); },
+ sub { flow(1, -1); hint(1); },
+ sub { flow(0, -1); hint(3); },
+ sub { up(3); },
+ sub { flow(3, 2); },
+ sub { up(3); },
+ sub { up(3); },
+ sub { up(3); },
+ sub { up(3); },
+ sub { up(3); },
+ sub { up(3); },
+ sub { up(3); },
+ sub { flow(2, -1); hint(2); },
+ sub { flow(1, -1); hint(1); },
+ sub { flow(0, -1); hint(-1); },
+ ];
+} else {
+ die "What do you want?\n";
+}
-Gtk2->main;
+$ui->RunScenario($scenario, 1);