Create a new base object for the photo classes which contains common code.
[picture-display.git] / lib / Display / Plugins / Photo.pm
1 package Display::Plugins::Photo;
2
3 use base ('Display::Plugin');
4 use Clutter;
5 use POE::Session;
6 use strict;
7
8 my $delay = 10;
9
10 sub new {
11   my $proto = shift;
12   my $class = ref($proto) || $proto;
13
14   my $self = $class->SUPER::new(@_);
15
16   $self->{'last_file'} = '';
17
18   bless ($self, $class);
19
20   $self->{'kernel'}->state('display_photo', $self);
21
22   if ($self->can('find_photo')) {
23     $self->{'kernel'}->state('find_photo', $self);
24     $self->init_delay($delay, 'find_photo');
25   }
26
27   for my $age ('new', 'old') {
28     $self->{$age} = Clutter::Texture->new();
29     $self->{$age}->set('request_mode' => 'width-for-height');
30     $self->{$age}->set('keep-aspect-ratio' => 1);
31     $self->{$age}->set('sync-size' => 1);
32     $self->{$age}->set_opacity(0);
33
34     $self->{'stage'}->add($self->{$age});
35   }
36
37   return $self;
38 }
39
40 sub display_photo {
41   my ($self, $kernel, $file) = @_[OBJECT, KERNEL, ARG0];
42
43   # Keep on looking until we find a file that exists.
44   if (! -f $file || $file eq $self->{'last_file'}) {
45     $kernel->yield('find_photo');
46     return;
47   } 
48
49   #warn "Loading $file\n";
50   $self->{'new'}->set_opacity(0);
51   $self->{'new'}->set_size(-1, -1);
52   $self->{'new'}->set_from_file($file);
53   $self->{'new'}->set_opacity(0);
54   $self->{'new'}->set_height($self->{'stage'}->get_height());
55   $self->{'new'}->set_anchor_point(
56     $self->{'new'}->get_width / 2,
57     $self->{'new'}->get_height / 2
58   );
59   $self->{'new'}->set_position(
60     $self->{'stage'}->get_width / 2,
61     $self->{'stage'}->get_height / 2
62   );
63   #$self->{'stage'}->add($self->{'new'});
64
65 #warn "Going to show $file\n";
66   if ($self->{'last_file'} eq '') {
67     fade_in(undef, $self);
68   } else {
69     $self->fade_out();
70   }
71
72   $self->{'last_file'} = $file;
73   $self->delay();
74 }
75
76 sub fade_in {
77   my ($old_timeline, $self) = @_;
78
79   my $new_effect = Clutter::EffectTemplate->new_for_duration(1000, 'main::smoothstep_inc');
80   my $new = Clutter::Effect->fade($new_effect, $self->{'new'}, 255, $self->can('post_fade_in'), $self);
81   $new->start();
82
83 #  my $timeline = Clutter::Timeline->new_for_duration(1000);
84 #  $timeline->signal_connect('completed', $self->can('post_fade_in'), $self);
85 #  my $alpha    = Clutter::Alpha->new($timeline, 'main::smoothstep_inc');
86 #  my $opacity  = Clutter::Behaviour::Opacity->new($alpha, 0, 255);
87 #  $opacity->apply($self->{'new'});
88 #warn "Setup timeline, time to start it.\n";
89 #  $timeline->start();
90 }
91
92 sub post_fade_in {
93   my ($old_timeline, $self) = @_;
94
95   my $temp = $self->{'old'};
96   $self->{'old'} = $self->{'new'};
97   $self->{'new'} = $temp;
98 }
99
100 sub fade_out {
101   my ($self) = @_;
102
103   my $old_effect = Clutter::EffectTemplate->new_for_duration(1000, 'main::smoothstep_inc' );
104   my $old = Clutter::Effect->fade($old_effect, $self->{'old'}, 0, $self->can('fade_in'), $self);
105   $old->start();
106
107 #  my $timeline = Clutter::Timeline->new_for_duration(1000);
108 #  $timeline->signal_connect('completed', \&fade_in, $self);
109 #  my $alpha    = Clutter::Alpha->new($timeline, 'main::smoothstep_dec');
110 #  my $opacity  = Clutter::Behaviour::Opacity->new($alpha, 255, 0);
111 #  $opacity->apply($self->{'old'});
112 #  $timeline->start();
113 }
114
115 1;