Plagger/Plugin/Publish/SWF.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 147 CC 4 SCAR 15.5
CC
4
Cov
0%
feed
12.0
fold_body
6.0
register
2.0
CRAP 4.7
1 package Plagger::Plugin::Publish::SWF;
2  
3 use strict;
4 use base qw(Plagger::Plugin);
5 use File::Spec;
6 use SWF::Builder;
7  
8 sub register {
9 my($self, $context) = @_;
10 $context->register_hook(
11 $self,
12 'publish.feed' => \&feed,
13 );
14 }
15  
16 sub feed {
17 my($self, $context, $args) = @_;
18 my $dir = $self->conf->{dir} || 'swf';
19 unless (-e $dir && -d _) {
20 mkdir $dir, 0755 or $context->error("mkdir $dir: $!");
21 }
22  
23 my $file = File::Spec->catfile($dir, $args->{feed}->id . ".swf");
24 unless ($self->conf->{font}) {
25 $context->error("'font' config is missing");
26 }
27 my $movie = $self->create_stage($context, $args);
28 $movie->save($file);
29 $context->log(info => "SWF file saved as $file");
30  
31 return;
32 }
33  
34 sub create_stage {
35 my($self, $context, $args) = @_;
36 my $bgcolor = $self->conf->{bgcolor} || 'ffffff';
37 my $width = $self->conf->{width} || 500;
38 my $height = $self->conf->{height} || 500;
39 my $movie = SWF::Builder->new (
40 FrameRate => 12,
41 FrameSize => [0, 0,$width,$height],
42 BackgroundColor => $bgcolor
43 );
44  
45 my $new_mc = $movie->new_movie_clip;
46 my $shape = $new_mc->new_shape;
47 $shape->fillstyle($bgcolor);
48 $shape->lineto(0,0)->lineto(0,$height)->lineto($width,$height)->lineto($width,0)->lineto(0,0);
49 $shape->place;
50 my $new_mc_ins = $new_mc->place;
51 $new_mc_ins->on('Press')->compile('_root.nextPage();');
52  
53 my $new_pre_mc = $movie->new_movie_clip;
54 my $pre_shape = $new_pre_mc->new_shape;
55 my $color = $self->conf->{color} || '000000';
56 $pre_shape->fillstyle($color);
57 $pre_shape->lineto(0,0)->lineto(0,$height)->lineto($width,$height)->lineto($width,0)->lineto(0,0);
58 $pre_shape->place;
59 $new_pre_mc->place->name('pre_mc');
60  
61 my $page = 0;
62 for my $entry ($args->{feed}->entries) {
63 $self->create_page($movie, ++$page, $entry->title_text, $entry->body_text);
64 }
65  
66 $movie->frame_action(1)->compile( <<AS_END );
67 this.page = 0;
68 this.total = $page;
69 nextPage();
70 _root.pre_mc._visible = false;
71 oListener = new Object;
72 oListener.onKeyDown = function () {
73 if(Key.isDown(74)){
74 _root.nextPage();
75 }
76 if(Key.isDown(75)){
77 _root.prePage();
78 }
79 }
80 Key.addListener(oListener);
81 function prePage() {
82 this._hidePage();
83 this.page--;
84 entry_name = 'entry_text' + this.page.toString();
85 title_name = 'title_text' + this.page.toString();
86 if(!this[entry_name]){
87 this.page = this.total;
88 entry_name = 'entry_text' + this.page.toString();
89 title_name = 'title_text' + this.page.toString();
90 }
91 this[entry_name].onEnterFrame = function () {
92 this._alpha += 6;
93 this._visible = true;
94 }
95 this[title_name].onEnterFrame = function () {
96 this._alpha += 6;
97 this._visible = true;
98 }
99 this[title_name].play();
100 this[entry_name].play();
101 }
102 function nextPage() {
103 this._hidePage();
104 this.page++;
105 entry_name = 'entry_text' + this.page.toString();
106 title_name = 'title_text' + this.page.toString();
107 if(!this[entry_name]){
108 this.page = 1;
109 entry_name = 'entry_text' + this.page.toString();
110 title_name = 'title_text' + this.page.toString();
111 }
112 this[entry_name].onEnterFrame = function () {
113 this._alpha += 6;
114 this._visible = true;
115 }
116 this[title_name].onEnterFrame = function () {
117 this._alpha += 6;
118 this._visible = true;
119 }
120 this[title_name].play();
121 this[entry_name].play();
122 }
123 function _hidePage() {
124 var title_name = 'title_text' + this.page.toString();
125 var entry_name = 'entry_text' + this.page.toString();
126 this[title_name].onEnterFrame = function () {}
127 this[title_name]._alpha = 0;
128 this[title_name]._visible = false;
129 this[title_name].stop();
130 this[entry_name].onEnterFrame = function () {}
131 this[entry_name]._alpha = 0;
132 this[entry_name]._visible = false;
133 this[entry_name].stop();
134 }
135 AS_END
136  
137 $movie;
138 }
139  
140 sub create_page {
141 my($self, $movie, $page, $title, $body) = @_;
142  
143 $self->log(debug => "Creating page $page ($title)");
144  
145 my $font = $self->conf->{font};
146 my $color = $self->conf->{color} || '000000';
147 my $title_size = $self->conf->{title_size} || 32;
148 my $body_size = $self->conf->{body_size} || 24;
149  
150 $body = $self->fold_body($body, $self->conf->{linefeed});
151  
152 my $entry_name = 'entry_text'.$page;
153 my $title_name = 'title_text'.$page;
154 $font = $movie->new_font($font);
155  
156 my $title_text_mc = $movie->new_movie_clip;
157 my $title_ins = $title_text_mc->new_static_text($font);
158 $title_ins->size($title_size)->color($color)->text(Encode::decode_utf8($title))->place;
159 my $title_text_ins = $title_text_mc->place;
160 $title_text_ins->on('Load')->compile('this._visible=false;this._alpha=0;');
161 $title_text_ins->name($title_name);
162 $title_text_ins->moveto(10,10);
163  
164 my $entry_text_mc = $movie->new_movie_clip;
165 my $entry_ins = $entry_text_mc->new_static_text($font);
166 $entry_ins->size($body_size)->color($color)->text(Encode::decode_utf8($body))->place;
167 my $entry_text_ins = $entry_text_mc->place;
168 $entry_text_ins->name($entry_name);
169 $entry_text_ins->on('Initialize')->compile('this._visible=false;this._alpha=0;');
170 $entry_text_ins->moveto(10,50);
171 }
172  
173 sub fold_body {
174 my($self, $str, $length) = @_;
175  
176 $length ||= 30;
177  
178 if (eval { require Text::WrapI18N }) {
179 local $Text::WrapI18N::columns = $length;
180 return Text::WrapI18N::wrap('', '', Encode::encode_utf8($str));
181 } else {
182 require Text::Wrap;
183 local $Text::Wrap::columns = $length + 1;
184 return Text::Wrap::wrap('', '', $str);
185 }
186 }
187  
188 1;
189  
190  
191 __END__