File: pixbufs.pl

package info (click to toggle)
libgtk2-perl 1%3A1.140-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,808 kB
  • ctags: 609
  • sloc: perl: 14,245; ansic: 118; makefile: 70
file content (227 lines) | stat: -rw-r--r-- 5,927 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/perl -w
#
# Pixbufs
#
# A GdkPixbuf represents an image, normally in RGB or RGBA format.
# Pixbufs are normally used to load files from disk and perform
# image scaling.
#
# This demo is not all that educational, but looks cool. It was written
# by Extreme Pixbuf Hacker Federico Mena Quintero. It also shows
# off how to use GtkDrawingArea to do a simple animation.
#
# Look at the Image demo for additional pixbuf usage examples.
#
#

package pixbufs;

use Glib qw(TRUE FALSE);
use Gtk2;
use strict;

chdir '../gtk-demo';

use constant FRAME_DELAY => 50;

use constant BACKGROUND_NAME => "background.jpg";

my @image_names = (
  "apple-red.png",
  "gnome-applets.png",
  "gnome-calendar.png",
  "gnome-foot.png",
  "gnome-gmush.png",
  "gnome-gimp.png",
  "gnome-gsame.png",
  "gnu-keys.png"
);

# demo window
my $window = undef;

# Current frame
my $frame;

# Background image
my $background;
my ($back_width, $back_height);

# Images
my @images;

# Widgets
my $da;

# Loads the images for the demo.
# croaks if anything goes wrong.
sub load_pixbufs {
  return TRUE if $background; # already loaded earlier

  # demo_find_file() looks in the the current directory first,
  # so you can run gtk-demo without installing GTK, then looks
  # in the location where the file is installed.
  #
  my $filename = main::demo_find_file (BACKGROUND_NAME);

  $background = Gtk2::Gdk::Pixbuf->new_from_file ($filename);

  $back_width = $background->get_width;
  $back_height = $background->get_height;

  foreach my $i (@image_names) {
      push @images, Gtk2::Gdk::Pixbuf->new_from_file (
	      		main::demo_find_file ($i));
  }

  return TRUE;
}

# Expose callback for the drawing area
sub expose_cb {
  my ($widget, $event) = @_;

  # the C code that this replaces used gdk_pixbuf_get_pixels and
  # gdk_draw_rgb_image_dithalign, with pointer math to find the
  # correct index in the image data; that doesn't work well with
  # perl scalars, and besides, the GdkPixbuf method render_to_drawable
  # exists for this very purpose.
  $frame->render_to_drawable ($widget->window, $widget->style->black_gc,
                              $event->area->x, $event->area->y,
                              $event->area->x, $event->area->y,
                              $event->area->width, $event->area->height,
			      'normal',
                              $event->area->x, $event->area->y);

  return TRUE;
}

use constant CYCLE_LEN => 60;
use constant G_PI => 3.141529;
use POSIX;
sub MAX { $_[0] > $_[1] ? $_[0] : $_[1] }
sub MIN { $_[0] < $_[1] ? $_[0] : $_[1] }

my $frame_num = 0;

# Timeout handler to regenerate the frame
sub timeout {
  $background->copy_area (0, 0, $back_width, $back_height, $frame, 0, 0);

  my $f = ($frame_num % CYCLE_LEN) / CYCLE_LEN;

  my $xmid = $back_width / 2.0;
  my $ymid = $back_height / 2.0;

  my $radius = MIN ($xmid, $ymid) / 2.0;

  for (my $i = 0; $i < @images; $i++) {
      my $ang = 2.0 * G_PI * $i / @images - $f * 2.0 * G_PI;

      my $iw = $images[$i]->get_width;
      my $ih = $images[$i]->get_height;

      my $r = $radius + ($radius / 3.0) * sin ($f * 2.0 * G_PI);

      my $xpos = POSIX::floor ($xmid + $r * cos ($ang) - $iw / 2.0 + 0.5);
      my $ypos = POSIX::floor ($ymid + $r * sin ($ang) - $ih / 2.0 + 0.5);

      my $k = ($i & 1) ? sin ($f * 2.0 * G_PI) : cos ($f * 2.0 * G_PI);
      $k = 2.0 * $k * $k;
      $k = MAX (0.25, $k);

      my $r1 = Gtk2::Gdk::Rectangle->new ($xpos, $ypos, $iw * $k, $ih * $k);

      my $r2 = Gtk2::Gdk::Rectangle->new (0, 0, $back_width, $back_height);

      my $dest = $r1->intersect ($r2);
      if ($dest) {
	$images[$i]->composite ($frame,
			        $dest->x, $dest->y,
			        $dest->width, $dest->height,
			        $xpos, $ypos,
			        $k, $k,
			        'nearest',
			      (($i & 1)
			       ? MAX (127, abs (255 * sin ($f * 2.0 * G_PI)))
			       : MAX (127, abs (255 * cos ($f * 2.0 * G_PI)))));
      }
  }

  $da->queue_draw;

  $frame_num++;
  return TRUE;
}

my $timeout_id;

sub cleanup_callback {
  Glib::Source->remove ($timeout_id);
  $timeout_id = 0;
}

sub do {
  if (!$window) {
      $window = Gtk2::Window->new;
      $window->set_title ("Pixbufs");
      $window->set_resizable (FALSE);

      $window->signal_connect (destroy => sub {$window=undef; 1});
      $window->signal_connect (destroy => \&cleanup_callback);


      eval { load_pixbufs; };
      if ($@) {
	  my $dialog = Gtk2::MessageDialog->new ($window,
					   'destroy-with-parent',
					   'error',
					   'close',
					   "Failed to load an image: $@");

	  $dialog->signal_connect (response => sub { $_[0]->destroy; 1 });

	  $dialog->show;
      } else {
	  $window->set_size_request ($back_width, $back_height);

	  $frame = Gtk2::Gdk::Pixbuf->new ('rgb', FALSE, 8, $back_width, $back_height);

	  $da = Gtk2::DrawingArea->new;

	  $da->signal_connect (expose_event => \&expose_cb);

	  $window->add ($da);

	  $timeout_id = Glib::Timeout->add (FRAME_DELAY, \&timeout);
    }
  }

  if (!$window->visible) {
      $window->show_all;
  } else {
      $window->destroy;
      $window = undef;
  }

  return $window;
}

1;
__END__
Copyright (C) 2003 by the gtk2-perl team (see the file AUTHORS for the
full list)

This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Library General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option) any
later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU Library General Public License for more
details.

You should have received a copy of the GNU Library General Public License along
with this library; if not, write to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA  02111-1307  USA.