Add a plugin for displaying random images from a directory.
authorAndrew Ruthven <puck@catalyst.net.nz>
Mon, 22 Sep 2008 03:21:04 +0000 (15:21 +1200)
committerAndrew Ruthven <puck@dirk.wgtn.cat-it.co.nz>
Mon, 22 Sep 2008 03:21:04 +0000 (15:21 +1200)
lib/Display/Plugins/Directory.pm [new file with mode: 0644]

diff --git a/lib/Display/Plugins/Directory.pm b/lib/Display/Plugins/Directory.pm
new file mode 100644 (file)
index 0000000..ea98e73
--- /dev/null
@@ -0,0 +1,93 @@
+package Display::Plugins::Directory;
+
+use base ('Display::Plugin');
+use Clutter;
+use POE;
+use FindBin qw/$Bin/;
+
+my $directory = "$Bin/test_images";
+
+sub new {
+  my $proto = shift;
+  my $class = ref($proto) || $proto;
+
+  my $self = $class->SUPER::new(@_);
+
+  $self->{'last_file'} = '';
+
+  bless ($self, $class);
+  $self->init();
+  return $self;
+}
+
+sub display {
+  my ($self, $kernel) = @_[OBJECT, KERNEL];
+  my $file;
+
+  # Keep on looking until we find a file that exists.
+  do {
+    $file = $self->find_photo();
+
+    return
+      unless defined $file;
+  } until -f $file && $file ne $self->{'last_file'};
+  
+  $self->{'new'}->set_from_file($file);
+
+  if ($self->{'last_file'} eq '') {
+    fade_in($self->{'new'});
+#  } else {
+#warn "Fade out old\n";
+#    my $old_effect = Clutter::EffectTemplate->new_for_duration(1000, 'main::smoothstep_dec' );
+#    my $old = Clutter::Effect->fade($old_effect, $self->{'old'}, 0, $self->can('fade_in'), $self->{'new'});
+#    $old->start();
+  }
+
+#  my $temp = $self->{'old'};
+#  $self->{'old'} = $self->{'new'};
+#  $self->{'new'} = $temp;
+  $self->{'last_file'} = $file;
+
+  $self->{'kernel'}->delay(directory_display, 30);
+}
+
+sub fade_in {
+  my $new_pic = shift;
+
+  my $new_effect = Clutter::EffectTemplate->new_for_duration(1000, 'main::smoothstep_inc');
+  my $new = Clutter::Effect->fade($new_effect, $new_pic, 255);
+  $new->start();
+}
+
+sub find_photo {
+  my $self = shift;
+
+  if (opendir(DIR, $directory)) {
+    my @files = readdir(DIR);
+    closedir DIR;
+
+    return join("/", $directory, $files[rand @files]);
+  } else {
+    warn "Failed to open $directory for reading: $!\n";
+  }
+}
+
+sub init {
+  my $self = shift;
+
+  $self->{'kernel'}->state('directory_display', $self, 'display');
+
+  for my $age ('new', 'old') {
+    $self->{$age} = Clutter::Texture->new();
+    $self->{$age}->set_size($self->{'stage'}->get_size());
+    $self->{$age}->set('keep-aspect-ratio' => 1);
+    $self->{$age}->set('sync-size' => 1);
+    $self->{$age}->set_opacity(0);
+
+    $self->{'stage'}->add($self->{$age});
+  }
+
+  $self->{'kernel'}->yield('directory_display');
+}
+
+1;