From: Martin Mares Date: Mon, 1 Nov 2021 11:41:46 +0000 (+0100) Subject: Ported to Gtk3 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;p=anim.git Ported to Gtk3 It hurts. --- diff --git a/AA/UI.pm b/AA/UI.pm index 5abf093..47bad9c 100644 --- a/AA/UI.pm +++ b/AA/UI.pm @@ -45,22 +45,23 @@ my $timer; my $stopped = 0; my $fullscreen = 0; my $movie = 0; -my ($lastw, $lasth, $lastd) = (-1, -1, -1); +my ($lastw, $lasth) = (-1, -1); my ($pixw, $pixh) = (-1, -1); my ($last_scene, $last_frame) = (-1, -1); -sub draw($) { +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 (!$force) { + $area->queue_draw(); + return; + } + + my $w = $area->get_allocated_width; + my $h = $area->get_allocated_height; + # print "Area $w x $h\n"; - if (!defined($pixmap) || $lastw != $w || $lasth != $h || $lastd != $d) { - ($lastw, $lasth, $lastd) = ($w, $h, $d); + if ($lastw != $w || $lasth != $h) { + ($lastw, $lasth) = ($w, $h); if ($w/$h >= 4/3) { $pixh = $h; $pixw = int($h/3*4); @@ -69,23 +70,16 @@ sub draw($) { $pixh = int($w/4*3); } print "Resized to ${w}x${h}, pixmap size ${pixw}x${pixh}\n" if $debug; - - $pixmap = Gtk2::Gdk::Pixmap->new($win, $pixw, $pixh, $d); - $cairo = Gtk2::Gdk::Cairo::Context->create($pixmap); - 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); - } - - $cairo->rectangle(0, 0, 1024, 768); + $cairo->rectangle(0, 0, $w, $h); $cairo->set_source_rgb(0, 0, 0); $cairo->fill; + $cairo->translate(($w - $pixw) / 2, 0); + my $scale = $pixw/1024; + $cairo->scale($scale, $scale); + if (@u_scenes) { print "@@@ Scene $u_scene frame $u_frame\n" if $debug; $u_scenes[$u_scene]->[$u_frame]->Draw($cairo); @@ -98,7 +92,6 @@ sub draw($) { AA->draw($cairo); } - $win->draw_drawable($wgc, $pixmap, 0, 0, 0, 0, $pixw, $pixh); $last_scene = $u_scene; $last_frame = $u_frame; } @@ -133,22 +126,24 @@ sub step() { } sub prepare() { - $window = Gtk2::Window->new ('toplevel'); - $area = Gtk2::DrawingArea->new(); - $area->signal_connect("expose-event" => sub { + $window = Gtk3::Window->new ('toplevel'); + $area = Gtk3::DrawingArea->new(); + $area->signal_connect("draw" => sub { + my ($w, $cr) = @_; + $cairo = $cr; draw(1); if (!defined $timer) { $timer = Glib::Timeout->add(30, sub { $stopped || step(); return 1; }); } }); - $area->set_flags('can-focus'); - # $area->add_events('key-press-mask'); + $area->set_can_focus(1); + $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); + my $k = Gtk3::Gdk::keyval_name($evt->keyval); print "Pressed key $k\n" if $debug; if ($k eq "Escape") { - Gtk2->main_quit; + Gtk3->main_quit; } elsif ($k eq "f") { if ($fullscreen = !$fullscreen) { $window->fullscreen; @@ -224,7 +219,7 @@ sub prepare() { } }); - $window->signal_connect ("delete-event" => sub { Gtk2->main_quit }); + $window->signal_connect ("delete-event" => sub { Gtk3->main_quit }); $window->set_title("Brum"); $window->set_wmclass("brum", "Brum"); $window->set_default_size(640, 480); @@ -261,7 +256,7 @@ sub RunScenario($$) { $u_frame = 0; $u_direction = 0; prepare(); - Gtk2->main; + Gtk3->main; } package AA::UI::PDF; diff --git a/fortune.pl b/fortune.pl index df9f28d..a842b90 100755 --- a/fortune.pl +++ b/fortune.pl @@ -3,7 +3,7 @@ use strict; use warnings; -use Gtk2 -init; +use Gtk3 -init; use Cairo; my $pi = 3.1415926536; @@ -112,16 +112,13 @@ sub para_isec($$$$$$) { } } -my $area = Gtk2::DrawingArea->new(); -my $cairo; +my $area = Gtk3::DrawingArea->new(); my $dh = 620; -sub draw() { - my $win = $area->window; - my $alloc = $area->allocation; - my $w = $alloc->width; - my $h = $alloc->height; +sub draw { + my ($cairo) = @_; + my $w = $area->get_allocated_width; + my $h = $area->get_allocated_height; - $cairo = Gtk2::Gdk::Cairo::Context->create($win); $cairo->rectangle(0, 0, $w, $h); $cairo->set_source_rgb(0, 0, 0); $cairo->fill; @@ -205,12 +202,13 @@ sub draw() { } my $timer; -$area->signal_connect("expose-event" => sub { - draw(); +$area->signal_connect("draw" => sub { + my ($w, $cr) = @_; + draw($cr); }); -my $window = Gtk2::Window->new ('toplevel'); -$window->signal_connect ("delete-event" => sub { Gtk2->main_quit }); +my $window = Gtk3::Window->new ('toplevel'); +$window->signal_connect ("delete-event" => sub { Gtk3->main_quit }); $window->set_title("Brum"); $window->set_wmclass("anim", "Anim"); $window->set_default_size(640, 480); @@ -219,13 +217,13 @@ $window->show_all; $window->fullscreen; $window->signal_connect("key-press-event" => sub { my ($w, $evt) = @_; - my $k = Gtk2::Gdk::keyval_name(0, $evt->keyval); + my $k = Gtk3::Gdk::keyval_name($evt->keyval); if ($k eq "space" || $k eq "Right" || $k eq "Return") { $dh -= 10; - draw(); + $area->queue_draw(); } elsif ($k eq "q" || $k eq "Escape") { - Gtk2->main_quit; + Gtk3->main_quit; } }); -Gtk2->main; +Gtk3->main; diff --git a/goldberg.pl b/goldberg.pl index f0a6a8c..dc10684 100755 --- a/goldberg.pl +++ b/goldberg.pl @@ -5,7 +5,7 @@ use warnings; use utf8; use lib "."; -use Gtk2 -init; +use Gtk3 -init; use Cairo; use AA;