SWF/Key.pm untested

filter: Statementstmt 0.0% 0 / 121 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 146 CC 3 SCAR 14.6
CC
3
Cov
0%
indicator
12.0
new
2.0
draw_arc
2.0
CRAP 4.3
1 package Alzabo::Display::SWF::Key;
2  
3 use strict;
4 use warnings;
5 use fields qw/r segments name linestyle_up linestyle_over fill1 fill2 opac/;
6  
7 use SWF::Sprite;
8 use SWF::Shape;
9 use SWF::Button;
10 use SWF::Action;
11 our $VERSION = '0.01';
12  
13 my $PI = 3.1415926535;
14  
15 sub new {
16 my $pkg = shift;
17 my $self = { @_ };
18 bless $self, $pkg;
19 return $self;
20 }
21  
22 sub indicator {
23 my ($self, $fk) = @_;
24 our ($r, $segments) = ($self->{r}, $self->{segments});
25 my $c = new SWF::Sprite();
26 my $b = new SWF::Button();
27 my $s = new SWF::Shape();
28 $s->setLineStyle( @{ $self->{linestyle_up} } );
29 $s->movePenTo($r, 0);
30 my $start = 0;
31 #print STDERR "drawing BUTTON_UP shape ...\n";
32 for ( my $end = 360/$segments; $end <= 360; $end += 360/$segments ) {
33 # Calculate the midpoint angle. The control point
34 # lies on this line
35 my $midpoint = $start+($end-$start)/2;
36 # draw_arc() draws a circular arc between 2 points
37 draw_arc(
38 $s,
39 $r*cos(radians($start)), $r*sin(radians($start)),
40 $r*cos(radians($end )), $r*sin(radians($end )),
41 radians($midpoint)
42 );
43 $start = $end;
44 }
45 $b->addShape($s, SWF::Button::SWFBUTTON_UP);
46  
47 my $t = new SWF::Shape();
48 $t->setLineStyle( @{ $self->{linestyle_over} } ); #, $self->{opac} );
49 $t->movePenTo($r, 0);
50 $start = 0;
51 #print STDERR "drawing my BUTTON_OVER circle ...\n";
52 for ( my $end = 360/$segments; $end <= 360; $end += 360/$segments ) {
53 # Calculate the midpoint angle. The control point
54 # lies on this line
55 my $midpoint = $start+($end-$start)/2;
56 # draw_arc() draws a circular arc between 2 points
57 draw_arc(
58 $t,
59 $r*cos(radians($start)), $r*sin(radians($start)),
60 $r*cos(radians($end )), $r*sin(radians($end )),
61 radians($midpoint)
62 );
63 $start = $end;
64 }
65  
66 scalar @{ $fk } ? $b->addShape($t, SWF::Button::SWFBUTTON_OVER)
67 : $b->addShape($s, SWF::Button::SWFBUTTON_OVER);
68  
69 my $fks = new SWF::Shape();
70 $fks->setLineStyle( @{ $self->{linestyle_over} } , $self->{opac} );
71 for my $fk ( @{ $fk } ) {
72 my ($x, $y) = @{ $fk->{xy} };
73 my $l = sqrt( $x*$x + $y*$y);
74 my $z = $r+4;
75 $fks->movePenTo($x*$z/$l, $y*$z/$l);
76 #print STDERR "drawing Line to $fk->{name} ($x, $y)\n";
77 $fks->drawLineTo($x*($l-$z)/$l, $y*($l-$z)/$l);
78 #$fks->movePenTo($x+$r, $y);
79 #$start = 0;
80 #print STDERR "drawing other BUTTON_OVER circle ...\n";
81 #for ( my $end = 360/$segments; $end <= 360; $end += 360/$segments ) {
82 # my $midpoint = $start+($end-$start)/2;
83 # draw_arc(
84 # $fks,
85 # $r*cos(radians($start)), $r*sin(radians($start)),
86 # $r*cos(radians($end )), $r*sin(radians($end )),
87 # radians($midpoint), $x, $y
88 # );
89 # $start = $end;
90 #}
91 }
92  
93 my $f1 = new SWF::Shape();
94 $f1->setLineStyle( 0, @{ $self->{linestyle_up} } );
95 $f1->setLeftFill($f1->addFill( @{ $self->{fill1} } ));
96 $f1->movePenTo($r, 0);
97 $start = 0;
98 #print STDERR "drawing BUTTON_HIT circle ...\n";
99 for ( my $end = 360/$segments; $end <= 360 ; $end += 360/$segments ) {
100 my $midpoint = $start+($end-$start)/2;
101 draw_arc(
102 $f1,
103 $r*cos(radians($start)), $r*sin(radians($start)),
104 $r*cos(radians($end )), $r*sin(radians($end )),
105 radians($midpoint)
106 );
107 $start = $end;
108 }
109  
110 $c->add($f1);
111 $b->addShape($f1, SWF::Button::SWFBUTTON_HIT);
112  
113 if ( defined $self->{fill2} ) {
114 my $f2 = new SWF::Shape();
115 $f2->setLineStyle( 0, @{ $self->{linestyle_up} } );
116 $f2->setLeftFill($f2->addFill( @{ $self->{fill2} } ));
117 $f2->movePenTo(-$r, 0);
118 $start = 180;
119 #print STDERR "drawing half circle ...\n";
120 for ( my $end = 180 + 360/$segments; $end <= 360 ; $end += 360/$segments )
121 {
122 my $midpoint = $start+($end-$start)/2;
123 draw_arc(
124 $f2,
125 $r*cos(radians($start)), $r*sin(radians($start)),
126 $r*cos(radians($end )), $r*sin(radians($end )),
127 radians($midpoint)
128 );
129 $start = $end;
130 }
131 $f2->drawLine(-2*$r, 0);
132 $c->add($f2);
133 }
134  
135 my $bi = $c->add($b);
136 $b->setAction(
137 new SWF::Action("nextFrame();"),
138 SWF::Button::SWFBUTTON_MOUSEOVER
139 );
140 $b->setAction(
141 new SWF::Action("play();"),
142 SWF::Button::SWFBUTTON_MOUSEOUT
143 );
144 $c->nextFrame;
145 $c->add(new SWF::Action("stop();"));
146 $c->nextFrame;
147 my $fki = $c->add($fks);
148 $c->add(new SWF::Action("swapDepths(2);"));#
149 $c->nextFrame;
150 return $c;
151 }
152  
153 sub draw_arc {
154 # Take a shape, a start coordinate, end coordinate and
155 # pre-computed mid-point angle as arguments
156 our $r;
157 my ($s, $x1, $y1, $x2, $y2, $angle, $x, $y) = @_;
158 $x ||= 0;
159 $y ||= 0;
160 my $cx = 2*($r*cos($angle)-.25*$x1-.25*$x2);
161 my $cy = 2*($r*sin($angle)-.25*$y1-.25*$y2);
162 #printf STDERR "Angle %.2f :: %.2f, %.2f :: %.2f, %.2f :: %.2f, %.2f\n",
163 # $angle, $x1, $y1, $x2, $y2, $cx, $cy;
164  
165 # Draw the curve on the Shape
166  
167 $s->drawCurveTo($x+$cx, $y+$cy, $x+$x2, $y+$y2);
168 #$s->drawCurve($cx-$x1, $cy-$y1, $x2-$cx, $y2-$cy);
169 }
170  
171 sub radians { return ($_[0]/180)*$PI }
172  
173 1;