dca705345b9eef5d03a438fa0c8ef7784b731386
[HTML5TV.git] / lib / HTML5TV / Slides.pm
1 package HTML5TV::Slides;
2
3 use warnings;
4 use strict;
5
6 use SDL::App;
7 use SDL::Surface;
8 use SDL::Rect;
9 use SDL::Tool::Font;
10
11 use Data::Dump qw/dump/;
12
13 sub new {
14         my $class = shift;
15         my $current_slide = shift || die "need current slide coderef!";
16         my $self = {
17                 last_nr => -42,
18                 current_slide => $current_slide,
19         };
20         bless $self, $class;
21 }
22
23 sub current_slide {
24         my $self = shift;
25         $self->{current_slide}->( shift );
26 }
27
28 sub show {
29         my $self = shift;
30         my $t = shift;
31         my $length = shift;
32         my @subtitles = @_;
33
34         my @slide_paths =
35                 sort {
36                         my $n_a = $1 if $a =~ m{(\d+)};
37                         my $n_b = $1 if $b =~ m{(\d+)};
38                         $n_a <=> $n_b || $a cmp $b
39                 } glob("media/_editing/s/1/*")
40         ;
41
42         my $slide = SDL::Surface->new( -name => $slide_paths[0] );
43         my $w = $slide->width;
44         my $h = $slide->height;
45
46         my @factors = ( qw/ 4 4 4 4 1 2 2 4 4 4 4 / );
47
48         my ( $x, $y ) = ( 0, 0 );
49
50         my $background = SDL::Color->new( -r => 0, -g => 0, -b => 0 );
51         my $overlay_color = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0x88 );
52
53         my $font = SDL::Tool::Font->new(
54                 -normal => 1,
55                 -ttfont => 'media/slides.ttf', # FIXME
56                 -size => 20,
57                 -fg => $overlay_color,
58                 -bg => $background,
59         );
60
61         foreach my $i ( 0 .. $#factors ) {
62
63                 my $factor = $factors[$i] || die "no factor $i in ",dump @factors;
64
65                 my $to = SDL::Rect->new(
66                         -width  => $w / $factor,
67                         -height => $h / $factor,
68                         -x      => $x,
69                         -y      => $y,
70                 );
71
72                 my $pos = $self->current_slide($t) + $i - 5;
73                 my $path = $slide_paths[ $pos ];
74
75                 if ( $pos < 0 || ! $path ) {
76
77                         $self->{app}->fill( $to, $background ) if $self->{app};
78
79                 } else {
80
81                         $path =~ s{/s/[124]/(\D*(\d+))}{/s/$factor/$1};
82                         my $nr = $2;
83
84                         my $slide = SDL::Surface->new( -name => $path );
85
86                         $font->print( $slide, 5, 5, $subtitles[$nr - 1]->[2] || $nr );
87
88                         my $rect = SDL::Rect->new(
89                                 -width  => $slide->width(),
90                                 -height => $slide->height(),
91                                 -x      => 0,
92                                 -y      => 0,
93                         );
94
95 #                       warn "$x $y $path\n";
96
97                         if ( ! $self->{app} ) {
98
99                                 $self->{app} = SDL::App->new(
100                                         -width  => $w,
101                                         -height => ( $h * 2 ) + 20,
102                                         -depth  => 24,
103                                         -title  => 'Slides',
104                                 );
105
106                         }
107
108                         $slide->blit( $rect, $self->{app}, $to );
109
110                 }
111
112                 $self->{app}->update( $to ) if $self->{app};
113
114                 $x += $w / $factor;
115
116                 if ( $x >= $w ) {
117                         $x = 0;
118                         $y += $h / $factor;
119                         $y += 5;
120                 }
121
122         }
123
124         if ( $self->{app} ) {
125
126                 $self->{app}->sync;
127
128                 my $w_1s = $w / $length;
129
130                 my $bar_h = 2;
131
132                 if (0) {
133
134                 my $bar_back = SDL::Color->new( -r => 0x88, -g => 0x88, -b => 0x88 );
135                 my $rect = SDL::Rect->new(
136                         -width  => $w,
137                         -height => $bar_h,
138                         -x      => 0,
139                         -y      => 0,
140                 );
141
142                 $self->{app}->fill( $rect, $bar_back );
143                 $self->{app}->update( $rect );
144
145                 } # background bar
146
147                 my $col_subtitle = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0x88 );
148                 my $col_pos      = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 );
149
150                 foreach my $s ( @subtitles ) {
151                         my $s_x = int( $s->[0] * $w_1s + 0.9 );
152                         my $s_w = int( abs( $s->[1] - $s->[0] ) * $w_1s + 0.9 );
153
154 #                       warn "$s_x $s_w ", $s->[2];
155
156                         my $rect = SDL::Rect->new(
157                                 -width => $s_w,
158                                 -height => $bar_h,
159                                 -x => $s_x,
160                                 -y => 0,
161                         );
162                         $self->{app}->fill( $rect, $col_subtitle );
163 #                       $self->{app}->update( $rect );
164                 }
165
166                 my $rect = SDL::Rect->new(
167                         -width => 1,
168                         -height => $bar_h * 2,
169                         -x => int( $t * $w_1s ),
170                         -y => 0,
171                 );
172                 $self->{app}->fill( $rect, $col_pos );
173 #               $self->{app}->update( $rect );
174
175                 $self->{app}->sync;
176         }
177
178 }
179
180 1;