SVG.pm

filter: Statementstmt 43.8% 89 / 203 Branchbran 64.2% 18 / 28 Conditioncond 66.6% 10 / 15 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 73.3% 22 / 30 Podpod 100% 16 / 16 totaltotal 53.1% 160 / 301 CC 26 SCAR 21.5
CC
26
Cov
48%
_just_maze
42.0
to_string
20.0
new
7.0
CRAP 8.6
↑ Summary SVG/HexCells.pm »
1 # SVG maze output
2 # Performs transformation, cleanup, and printing of output of Games::Maze
3  
4 package Games::Maze::SVG;
5  
6 28 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶use Games::Maze;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶use Games::Maze::SVG::Rect;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 28 ▶use Games::Maze::SVG::RectHex;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use Games::Maze::SVG::Hex;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 Games::Maze::SVG - Build mazes in SVG.
18  
19 =head1 VERSION
20  
21 Version 0.90
22  
23 =cut
24  
25 28 our $VERSION = 0.90;
26  
27 =head1 SYNOPSIS
28  
29 Games::Maze::SVG uses the Games::Maze module to create mazes in SVG.
30  
31 use Games::Maze::SVG;
32  
33 my $foo = Games::Maze::SVG->new();
34 ...
35  
36 See Games::Maze::SVG::Manual for more information on using the module.
37  
38 =cut
39  
40 28 ▶use constant SIGN_HEIGHT => 20;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 28 ▶use constant SIDE_MARGIN => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 28 ▶use constant PANEL_WIDTH => 250;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 28 ▶use constant PANEL_MIN_HEIGHT => 365;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 28 my %crumbstyles = (
46 dash => "stroke-width:1px; stroke-dasharray:5px,3px;",
47 dot => "stroke-width:2px; stroke-dasharray:2px,6px;",
48 line => "stroke-width:1px;",
49 none => "visibility:hidden;",
50 );
51  
52 28 my $license = <<'EOL';
53 <metadata>
54 <!--
55 Copyright 2004-2013, G. Wade Johnson
56 Some rights reserved.
57 -->
58 <rdf:RDF xmlns="http://web.resource.org/cc/"
59 xmlns:dc="http://purl.org/dc/elements/1.1/"
60 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
61 <Work rdf:about="">
62 <dc:title>SVG Maze</dc:title>
63 <dc:date>2006</dc:date>
64 <dc:description>An SVG-based Game</dc:description>
65 <dc:creator><Agent>
66 <dc:title>G. Wade Johnson</dc:title>
67 </Agent></dc:creator>
68 <dc:rights><Agent>
69 <dc:title>G. Wade Johnson</dc:title>
70 </Agent></dc:rights>
71 <dc:type rdf:resource="http://purl.org/dc/dcmitype/Interactive" />
72 <license rdf:resource="http://creativecommons.org/licenses/by-sa/2.0/" />
73 </Work>
74  
75 <License rdf:about="http://creativecommons.org/licenses/by-sa/2.0/">
76 <permits rdf:resource="http://web.resource.org/cc/Reproduction" />
77 <permits rdf:resource="http://web.resource.org/cc/Distribution" />
78 <requires rdf:resource="http://web.resource.org/cc/Notice" />
79 <requires rdf:resource="http://web.resource.org/cc/Attribution" />
80 <permits rdf:resource="http://web.resource.org/cc/DerivativeWorks" />
81 <requires rdf:resource="http://web.resource.org/cc/ShareAlike" />
82 </License>
83  
84 </rdf:RDF>
85 </metadata>
86 EOL
87  
88 =head1 FUNCTIONS
89  
90  
91 =cut
92  
93 # ----------------------------------------------
94 # Subroutines
95  
96 =head2 new( $type, %parms )
97  
98 Create a new Games::Maze::SVG object. Supports the following named parameters:
99  
100 Takes one positional parameter that is the maze type: Rect, RectHex, or Hex
101  
102 =over 4
103  
104 =item wallform
105  
106 String naming the wall format. Legal values are bevel, round, roundcorners,
107 and straight. Not all formats work with all maze shapes.
108  
109 =item crumb
110  
111 String describing the breadcrumb design. Legal values are dash,
112 dot, line, and none
113  
114 =item dir
115  
116 Directory in which to find the ecmascript for the maze interactivity. Should
117 either be relative, or in URL form.
118  
119 =item interactive
120  
121 This parameter determines if the maze will be interactive. If the value of the
122 parameter is true (1), the appropriate scripting and support is written into
123 the SVG. If the parameter is omitted or false, no interactive support is
124 provided.
125  
126 =item cols
127  
128 The number of columns used in creating the maze. Default value is 12.
129  
130 =item rows
131  
132 The number of rows used in creating the maze. Default value is 12.
133  
134 =item startcol
135  
136 The column where the entry is found. Default value is random.
137  
138 =item endcol
139  
140 The column where the exit is found. Default value is random.
141  
142 =back
143  
144 =cut
145  
146 sub new
147 35 ▶{
Subroutine new: called CC=7 SCAR=19.5
Pod documented
148 35 my $class = shift;
149  
150 35 ▶ my $shape = shift || 'Rect';
Condition100%2/2
shift() || 'Rect'
l!l
314
MC/DC100%1/1
shift() || 'Rect'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
151  
152 35 my %params = @_;
153  
154 35 ▶ if( exists $params{crumb} && !exists $crumbstyles{ $params{crumb} } )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"crumb"} and not exists $crumbstyles{$params{"crumb"}}) 1 34 35
Condition100%3/3
exists $params{"crumb"} and not exists $crumbstyles{$params{"crumb"}}
!ll&&!rl&&r
3131
MC/DC100%2/2
exists $params{"crumb"} and not exists $crumbstyles{$params{"crumb"}}
exists $params{"crumb"} not exists $crumbstyles{$params{"crumb"}}
Truth table100%3/3
A and not(B)
A= exists $params{"crumb"}B= not exists $crumbstyles{$params{"crumb"}}
ABresult
0X0
100
111
155 {
156 1 croak "Unrecognized breadcrumb style '$params{crumb}'.\n";
157 }
158  
159 34 ▶ return Games::Maze::SVG::Rect->new( @_ ) if 'Rect' eq $shape;
Branch: 1 branch
BranchTrueFalseTotal
if "Rect" eq $shape 17 17 34
160 17 ▶ return Games::Maze::SVG::RectHex->new( @_ ) if 'RectHex' eq $shape;
Branch: 1 branch
BranchTrueFalseTotal
if "RectHex" eq $shape 8 9 17
161 9 ▶ return Games::Maze::SVG::Hex->new( @_ ) if 'Hex' eq $shape;
Branch: 1 branch
BranchTrueFalseTotal
if "Hex" eq $shape 8 1 9
162  
163 1 croak "Unrecognized maze shape '$shape'.\n";
164 }
165  
166 =head2 $m->init_object( %parms )
167  
168 Initializes the maze object with the default values for all mazes. The derived
169 classes should call this method in their constructors.
170  
171 Returns the initial data members as a list.
172  
173 =cut
174  
175 sub init_object
176 33 ▶{
Subroutine init_object: called CC=5 SCAR=16.1
Pod documented
177 33 my %parms = @_;
178  
179 33 my %obj = (
180 mazeparms => {},
181 wallform => 'straight',
182 crumb => 'dash',
183 dir => 'scripts/',
184 );
185 33 ▶ $obj{mazeparms}->{dimensions} = [ $parms{cols} || 12, $parms{rows} || 12, 1 ];
Condition100%4/4
$parms{'cols'} || 12
l!l
231
$parms{'rows'} || 12
l!l
231
MC/DC100%1/1
$parms{'cols'} || 12
$parms{'cols'}
MC/DC100%1/1
$parms{'rows'} || 12
$parms{'rows'}
Truth table100%2/2
A
A= $parms{'cols'}
Aresult
11
00
Truth table100%2/2
A
A= $parms{'rows'}
Aresult
11
00
186 33 ▶ $obj{mazeparms}->{entry} = [ $parms{startcol}, 1, 1 ] if $parms{startcol};
Branch: 1 branch
BranchTrueFalseTotal
if $parms{"startcol"} 1 32 33
187  
188 33 ▶ if( $parms{endcol} )
Branch: 1 branch
BranchTrueFalseTotal
if ($parms{"endcol"}) 1 32 33
189 {
190 1 $obj{mazeparms}->{exit} = [ $parms{endcol}, $obj{mazeparms}->{dimensions}->[1], 1 ];
191 }
192  
193 33 return %obj;
194 }
195  
196 =head2 $m->set_interactive()
197  
198 Method makes the maze interactive.
199  
200 Returns a reference to self for chaining.
201  
202 =cut
203  
204 sub set_interactive
205 1 ▶{
Subroutine set_interactive: called CC=1 SCAR=0.0
Pod documented
206 1 my $self = shift;
207 1 $self->{interactive} = 1;
208 1 return $self;
209 }
210  
211 =head2 $m->set_breadcrumb( $bcs )
212  
213 =over 4
214  
215 =item $bcs
216  
217 String specifying the breadcrumb style. Generates an exception if the
218 breadcrumb style is not recognized.
219  
220 =back
221  
222 Returns a reference to self for chaining.
223  
224 =cut
225  
226 sub set_breadcrumb
227 6 ▶{
Subroutine set_breadcrumb: called CC=3 SCAR=11.0
Pod documented
228 6 my $self = shift;
229 6 my $bcs = shift;
230  
231 6 ▶ return unless defined $bcs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bcs 1 5 6
232  
233 croak "Unrecognized breadcrumb style '$bcs'.\n"
234 5 ▶ unless exists $crumbstyles{$bcs};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $crumbstyles{$bcs} 1 4 5
235 4 $self->{crumb} = $bcs;
236 4 $self->{crumbstyle} = $crumbstyles{$bcs};
237  
238 4 return $self;
239 }
240  
241 =head2 $m->get_crumbstyle()
242  
243 Returns the CSS style for the breadcrumb.
244  
245 =cut
246  
247 sub get_crumbstyle
248 4 ▶{
Subroutine get_crumbstyle: called CC=2 SCAR=8.1
Pod documented
249 4 my $self = shift;
250  
251 4 ▶ return $self->{crumbstyle} ||= $crumbstyles{ $self->{crumb} };
Condition33%1/3
$self->{"crumbstyle"} ||= $crumbstyles{$self->{"crumb"}}
l!l&&r!l&&!r
400
MC/DC0%0/2
$self->{"crumbstyle"} ||= $crumbstyles{$self->{"crumb"}}
$self->{"crumbstyle"} $crumbstyles{$self->{"crumb"}}
Truth table33%1/3
A ||= B
A= $self->{"crumbstyle"}B= $crumbstyles{$self->{"crumb"}}
ABresult
1X1
011
000
252 }
253  
254 =head2 $m->get_script()
255  
256 Method that returns the path to the interactivity script.
257  
258 =cut
259  
260 sub get_script
261 10 ▶{
Subroutine get_script: called CC=1 SCAR=0.0
Pod documented
262 10 my $self = shift;
263  
264 10 return "$self->{dir}$self->{scriptname}";
265 }
266  
267 =head2 $m->to_string()
268  
269 Method that converts the current maze into an SVG string.
270  
271 =cut
272  
273 sub to_string
274 0 ▶{
Subroutine to_string: not called CC=4 SCAR=30.0
Pod documented
275 0 my $self = shift;
276 0 my $maze = Games::Maze->new( %{ $self->{mazeparms} } );
277  
278 0 $maze->make();
279 0 my @rows = map { [ split q{}, $_ ] }
280 split( "\n", $maze->to_ascii() );
281  
282 0 my $crumb = q{};
283 0 my $color = {
284 mazebg => '#ffc',
285 panel => '#ccc',
286 crumb => '#f3f',
287 sprite => 'orange',
288 button => '#ccf',
289 };
290  
291 0 my $crumbstyle = $self->get_crumbstyle();
292  
293 0 $self->transform_grid( \@rows, $self->{wallform} );
294 0 $self->_just_maze( \@rows );
295  
296 0 my ( $xp, $yp ) = $self->convert_start_position( @{ $maze->{entry} } );
297 0 my ( $xe, $ye ) = $self->convert_end_position( @{ $maze->{exit} } );
298 0 my ( $xenter, $yenter ) = $self->convert_sign_position( $xp, $yp );
299 0 my ( $xexit, $yexit ) = $self->convert_sign_position( $xe, $ye );
300  
301 0 my $width = $self->{width} + 2 * SIDE_MARGIN;
302 0 my $height = $self->{height} + 2 * SIGN_HEIGHT;
303 0 my $sprite_def = $self->create_sprite();
304  
305 0 my $output = qq{<?xml version="1.0"?>\n};
306 0 my $offsety = - SIGN_HEIGHT;
307 0 my $offsetx = - SIDE_MARGIN;
308 0 my ( $xme, $yme ) = ( $xp * $self->dx(), $yp * $self->dy() );
309 0 my ( $xcrumb, $ycrumb ) = ( $xme + $self->dx() / 2, $yme + $self->dy() / 2 );
310  
311 0 ▶ my $panelheight = $height > PANEL_MIN_HEIGHT ? $height : PANEL_MIN_HEIGHT;
Branch: 1 branch
BranchTrueFalseTotal
$height > 365 ? : 0 0 0
312 0 ▶ if( $self->{interactive} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'interactive'}) { } 0 0 0
313 {
314 0 my $script = $self->build_all_script();
315 0 my $boardelem = $self->build_board_element( \@rows, $xp, $yp, $xe, $ye );
316  
317 0 my $totalwidth = $width + PANEL_WIDTH;
318 0 $output .= <<"EOH";
319 <svg width="$totalwidth" height="$panelheight"
320 xmlns="http://www.w3.org/2000/svg"
321 xmlns:xlink="http://www.w3.org/1999/xlink"
322 xmlns:maze="http://www.anomaly.org/2005/maze"
323 onload="initialize()">
324 <title>A Playable SVG Maze</title>
325 <desc>This maze was generated using the Games::Maze::SVG Perl
326 module.</desc>
327 $license
328 <defs>
329 <style type="text/css">
330 text { font-family: sans-serif; font-size: 10px; }
331 .panel { fill:$color->{panel}; stroke:none; }
332 .button {
333 cursor: pointer;
334 }
335 .button rect { fill: #33f; stroke: none; filter: url(#bevel);
336 }
337 .button text { text-anchor:middle; fill:#fff; font-weight:bold; }
338 .button polygon { fill:white; stroke:none; }
339 .ctrllabel { text-anchor:middle; font-weight:bold; }
340 #solvedmsg { text-anchor:middle; pointer-events:none; font-size:80px; fill:red;
341 }
342 </style>
343 <filter id="bevel">
344 <feFlood flood-color="#ccf" result="lite-flood"/>
345 <feFlood flood-color="#006" result="dark-flood"/>
346 <feComposite operator="in" in="lite-flood" in2="SourceAlpha"
347 result="lighter"/>
348 <feOffset in="lighter" result="lightedge" dx="-1" dy="-1"/>
349 <feComposite operator="in" in="dark-flood" in2="SourceAlpha"
350 result="darker"/>
351 <feOffset in="darker" result="darkedge" dx="1" dy="1"/>
352 <feMerge>
353 <feMergeNode in="lightedge"/>
354 <feMergeNode in="darkedge"/>
355 <feMergeNode in="SourceGraphic"/>
356 </feMerge>
357 </filter>
358 $script
359 $boardelem
360 </defs>
361 0 <svg x="@{[ PANEL_WIDTH ]}" y="0" width="$width" height="$height"
362 viewBox="$offsetx $offsety $width $height" id="maze">
363 EOH
364 }
365 else
366 {
367 0 $color->{mazebg} = '#fff';
368  
369 0 $output .= <<"EOH";
370 <svg width="$width" height="$height"
371 xmlns="http://www.w3.org/2000/svg"
372 xmlns:xlink="http://www.w3.org/1999/xlink">
373 <title>An SVG Maze</title>
374 <desc>This maze was generated using the Games::Maze::SVG Perl
375 module.</desc>
376 $license
377 <svg x="0" y="0" width="$width" height="$height"
378 viewBox="$offsetx $offsety $width $height" id="maze">
379 EOH
380 }
381  
382 0 $output .= <<"EOH";
383 <defs>
384 <style type="text/css">
385 path { stroke: black; fill: none; }
386 polygon { stroke: black; fill: grey; }
387 #sprite { stroke: grey; stroke-width:0.2px; fill: $color->{sprite}; }
388 .crumbs { fill:none; $crumbstyle }
389 .mazebg { fill:$color->{mazebg}; stroke:none; }
390 text { font-family: sans-serif; font-size: 10px; }
391 .sign text { fill:#fff;text-anchor:middle; font-weight:bold; }
392 .exit rect { fill:red; stroke:none; }
393 .entry rect { fill:green; stroke:none; }
394 </style>
395 <circle id="savemark" r="3" fill="#6f6" stroke="none"/>
396 $sprite_def
397 0 @{[$self->wall_definitions()]}
398 </defs>
399 <rect id="mazebg" class="mazebg" x="$offsetx" y="$offsety" width="100%" height="100%"/>
400  
401 $self->{mazeout}
402 <polyline id="crumb" class="crumbs" stroke="$color->{crumb}" points="$xcrumb,$ycrumb"/>
403 <use id="me" x="$xme" y="$yme" xlink:href="#sprite" visibility="hidden"/>
404  
405 <g transform="translate($xenter,$yenter)" class="entry sign">
406 <rect x="-16" y="-8" width="35" height="16" rx="3" ry="3"/>
407 <text x="2" y="4">Entry</text>
408 </g>
409 <g transform="translate($xexit,$yexit)" class="exit sign">
410 <rect x="-16" y="-8" width="32" height="16" rx="3" ry="3"/>
411 <text x="0" y="4">Exit</text>
412 </g>
413 </svg>
414 EOH
415  
416 0 ▶ if( $self->{interactive} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"interactive"}) 0 0 0
417 {
418 0 my ( $cx, $cy ) = ( ( $self->{width} + PANEL_WIDTH ) / 2, ( 35 + $panelheight / 2 ) );
419 0 $output .= $self->build_control_panel( 0, $panelheight );
420 0 $output .= <<"EOM";
421 <text id="solvedmsg" x="$cx" y="$cy" visibility="hidden">Solved!</text>
422 EOM
423 }
424 0 return $output . "</svg>\n";
425 }
426  
427 =head2 $m->toString()
428  
429 Alias for C<to_string> to deal with inconsistent name from earlier versions.
430  
431 =cut
432  
433 0 ▶sub toString { return $_[0]->to_string(); }
Subroutine toString: not called CC=1 SCAR=6.9
Pod documented
434  
435 =head2 $m->make_board_array( $rows )
436  
437 Build a two-dimensional array of integers that maps the board from
438 the two dimensional matrix of wall descriptions.
439  
440 =cut
441  
442 sub make_board_array
443 14 ▶{
Subroutine make_board_array: called CC=3 SCAR=11.0
Pod documented
444 14 my $self = shift;
445 14 my $rows = shift;
446 14 my @board = ();
447  
448 14 foreach my $row ( @{$rows} )
449 {
450 166 ▶ push @board, [ map { $_ ? 1 : 0 } @{$row} ];
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 1100 1426 2526
451 }
452  
453 14 return \@board;
454 }
455  
456 =head2 $m->get_script_list()
457  
458 Returns a list of script URLs that will be needed by the interactive
459 maze.
460  
461 =cut
462  
463 sub get_script_list
464 6 ▶{
Subroutine get_script_list: called CC=1 SCAR=0.0
Pod documented
465 6 my $self = shift;
466 6 my @scripts = (
467 "$self->{dir}point.es", "$self->{dir}sprite.es",
468 "$self->{dir}maze.es", $self->get_script(),
469 );
470  
471 6 return @scripts;
472 }
473  
474 =head2 $m->build_all_script()
475  
476 Generate the full set of script sections for the maze.
477  
478 =cut
479  
480 sub build_all_script
481 3 ▶{
Subroutine build_all_script: called CC=2 SCAR=6.9
Pod documented
482 3 my $self = shift;
483  
484 3 my $script = q{};
485  
486 3 foreach my $url ( $self->get_script_list() )
487 {
488 12 $script .= qq{ <script type="text/ecmascript" xlink:href="$url"/>\n};
489 }
490  
491 3 $script .= <<"EOS";
492 <script type="text/ecmascript">
493 function push( evt )
494 {
495 var btn = evt.currentTarget;
496 btn.setAttributeNS( null, "opacity", "0.5" );
497 }
498 function release( evt )
499 {
500 var btn = evt.currentTarget;
501 var opval = btn.getAttributeNS( null, "opacity" );
502 if("" != opval &amp;&amp; 1.0 != opval)
503 btn.setAttributeNS( null, "opacity", '1.0' );
504 }
505 </script>
506 EOS
507  
508 3 return $script;
509 }
510  
511 =head2 $m->build_board_element( $rows, $xp, $yp, $xe, $ye )
512  
513 Create the element that describes the board.
514  
515 =over 4
516  
517 =item $rows
518  
519 reference to an array of rows.
520  
521 =item $xp, $yp
522  
523 Starting position
524  
525 =item $xe, $ye
526  
527 Ending position
528  
529 =back
530  
531 =cut
532  
533 sub build_board_element
534 0 ▶{
Subroutine build_board_element: not called CC=2 SCAR=17.9
Pod documented
535 0 my $self = shift;
536 0 my $rows = shift;
537 0 my ( $xp, $yp, $xe, $ye ) = @_;
538  
539 0 my $tilex = $self->dx();
540 0 my $tiley = $self->dy();
541  
542 0 my $board = $self->make_board_array( $rows );
543  
544 0 my $elem .= qq{ <maze:board start="$xp,$yp" end="$xe,$ye" tile="$tilex,$tiley">\n};
545 0 foreach my $row ( @{$board} )
546 {
547 0 $elem .= qq{ } . join( q{}, @{$row} ) . "\n";
548 }
549 0 $elem .= <<'EOS';
550 </maze:board>
551 EOS
552  
553 0 return $elem;
554 }
555  
556 =head2 $m->build_control_panel( $startx, $height )
557  
558 Create the displayable control panel
559  
560 =over 4
561  
562 =item $startx
563  
564 the starting x coordinate for the panel
565  
566 =item $height
567  
568 the height of the maze
569  
570 =back
571  
572 =cut
573  
574 sub build_control_panel
575 0 ▶{
Subroutine build_control_panel: not called CC=1 SCAR=6.9
Pod documented
576 0 my $self = shift;
577 0 my $startx = shift;
578 0 my $height = shift;
579 0 my $panelwidth = PANEL_WIDTH;
580  
581 0 my $offset = 20;
582 0 my $output .= <<"EOB";
583 <g id="control_panel" transform="translate($startx,0)">
584 <rect x="0" y="0" width="$panelwidth" height="$height"
585 class="panel"/>
586 EOB
587 0 $output .= _create_text_button( 'restart', $offset, 20, 50, 20, 'Begin' );
588 0 $output .= _create_text_button( 'save_position', $offset + 60, 20, 50, 20, 'Save' );
589 0 $output .= _create_text_button( 'restore_position', $offset + 120, 20, 50, 20, 'Back' );
590 0 $output .= <<"EOB";
591  
592 <g transform="translate(20,65)">
593 <rect x="-2" y="-2" rx="25" ry="25" width="68" height="68"
594 fill="none" stroke-width="0.5" stroke="black"/>
595 <text x="34" y="-5" class="ctrllabel">Move View</text>
596 EOB
597 0 $output .= _create_view_button( 'maze_up', 22, 0, '10,5 5,15 15,15' );
598 0 $output .= _create_view_button( 'maze_left', 0, 22, '5,10 15,5 15,15' );
599 0 $output .= _create_view_button( 'maze_right', 44, 22, '15,10 5,5 5,15' );
600 0 $output .= _create_view_button( 'maze_down', 22, 44, '10,15 5,5 15,5' );
601 0 $output .= _create_view_button( 'maze_reset', 22, 22, '7,7 7,13 13,13 13,7' );
602  
603 =begin COMMENT
604  
605 $output .= <<"EOB";
606 </g>
607 <g transform="translate(110, 50)">
608 <rect width="82" height="82" x="-1" y="-1" fill="gray" stroke-width="1" stroke="black"/>
609 EOB
610 $output .= $self->_create_thumbnail();
611  
612 =cut
613  
614 0 $output .= <<"EOB";
615 </g>
616  
617 <g class="instruct" transform="translate($offset,165)">
618 <text x="0" y="0">Click Begin button to start</text>
619 <text x="0" y="30">Use the arrow keys to move the sprite</text>
620 <text x="0" y="50">Hold the shift to move quickly.</text>
621 <text x="0" y="70">The mouse must remain over the</text>
622 <text x="0" y="90">maze for the keys to work.</text>
623 <text x="0" y="120">Use arrow buttons to shift the maze</text>
624 <text x="0" y="140">Center button centers view on sprite</text>
625 <text x="0" y="160">Save button saves current position</text>
626 <text x="0" y="180">Back button restores last position</text>
627 </g>
628 </g>
629 EOB
630  
631 0 return $output;
632 }
633  
634 =begin COMMENT
635  
636 # _create_thumbnail
637 #
638 # Create the thumbnail image used to show the player where they are on the
639 # larger field.
640 #
641 sub _create_thumbnail
642 {
643 my $self = shift;
644 my ($x, $y, $wid, $ht) = (0,0,80,80);
645  
646 if($self->{width} > $self->{height})
647 {
648 $ht = int(80 * $self->{height} / $self->{width});
649 $y = (80 - $ht) / 2;
650 }
651 else
652 {
653 $wid = int(80 * $self->{width} / $self->{height});
654 $x = (80 - $wid) / 2;
655 }
656 qq{ <rect x="$x" y="$y" width="$wid" height="$ht" class="mazebg"/>\n};
657 }
658  
659 =cut
660  
661 # _create_text_button
662 #
663 # $function - function name to call
664 # $x - x-coordinate of the button
665 # $y - y-coordinate of the button
666 # $width - width of the button
667 # $height - height of the button
668 # $text - text to be displayed on the button
669  
670 sub _create_text_button
671 0 ▶{
Subroutine _create_text_button: not called CC=1 SCAR=6.9
672 0 my $fn = shift;
673 0 my $x = shift;
674 0 my $y = shift;
675 0 my $width = shift;
676 0 my $height = shift;
677 0 my $text = shift;
678  
679 0 my $tx = $width / 2;
680 0 my $ty = $height / 2 + 5;
681  
682 0 my $output = <<"EOF";
683  
684 <g onclick="$fn()" transform="translate($x,$y)" class="button"
685 onmousedown="push(evt)" onmouseup="release(evt)" onmouseout="release(evt)">
686 <rect x="0" y="0" width="$width" height="$height" rx="5" ry="5"/>
687 <text x="$tx" y="$ty">$text</text>
688 </g>
689 EOF
690  
691 0 return $output;
692 }
693  
694 # _create_view_button
695 #
696 # $function - function name to call
697 # $x - x-coordinate of the button
698 # $y - y-coordinate of the button
699 # $icon - list of points for the icon
700  
701 sub _create_view_button
702 0 ▶{
Subroutine _create_view_button: not called CC=1 SCAR=6.9
703 0 my $fn = shift;
704 0 my $x = shift;
705 0 my $y = shift;
706 0 my $icon = shift;
707  
708 0 my $output = <<"EOF";
709  
710 <g onclick="$fn()" transform="translate($x,$y)" class="button"
711 onmousedown="push(evt)" onmouseup="release(evt)" onmouseout="release(evt)">
712 <rect x="0" y="0" width="20" height="20" rx="5" ry="5"/>
713 <polygon points="$icon"/>
714 </g>
715 EOF
716  
717 0 return $output;
718 }
719  
720 =head2 $m->create_sprite()
721  
722 Create the sprite definition for inclusion in the SVG.
723  
724 =cut
725  
726 sub create_sprite
727 0 ▶{
Subroutine create_sprite: not called CC=1 SCAR=6.9
Pod documented
728 0 my $self = shift;
729 0 my ( $dx2, $dy2 ) = ( $self->dx() / 2, $self->dy() / 2 );
730  
731 0 return qq| |
732 0 . qq|<path id="sprite" d="M0,0 Q$dx2,$dy2 0,@{[$self->dy()]} Q$dx2,$dy2 @{[$self->dx()]},@{[$self->dy()]} Q$dx2,$dy2 @{[$self->dx()]},0 Q$dx2,$dy2 0,0"/>|;
733 }
734  
735 #
736 # Generates just the maze portion of the SVG.
737 #
738 # $dx - The size of the tiles in the X direction.
739 # $dy - The size of the tiles in the Y direction.
740 # $rows - Reference to an array of row data.
741 #
742 # returns a string containing the SVG for the maze description.
743 sub _just_maze
744 0 ▶{
Subroutine _just_maze: not called CC=6 SCAR=37.4
745 0 my $self = shift;
746 0 my $dx = $self->dx();
747 0 my $dy = $self->dy();
748 0 my $rows = shift;
749  
750 0 my $output = q{};
751 0 my ( $maxx, $y ) = ( 0, 0 );
752  
753 0 foreach my $r ( @{$rows} )
754 {
755 0 my $x = 0;
756 0 foreach my $c ( @{$r} )
757 {
758 0 ▶ $output .= qq{ <use x="$x" y="$y" xlink:href="#$c"/>\n}
Branch: 1 branch
BranchTrueFalseTotal
if $c and $c ne "\$" 0 0 0
Condition0%0/3
$c and $c ne "\$"
!ll&&!rl&&r
000
MC/DC0%0/2
$c and $c ne "\$"
$c $c ne "\$"
Truth table0%0/3
A and B
A= $cB= $c ne "\$"
ABresult
0X0
100
111
759 if $c and $c ne q{$};
760 0 $x += $dx;
761 }
762 0 $y += $dy;
763 0 ▶ $maxx = $x if $maxx < $x;
Branch: 1 branch
BranchTrueFalseTotal
if $maxx < $x 0 0 0
764 }
765  
766 0 $self->{width} = $maxx;
767 0 $self->{height} = $y;
768 0 $self->{mazeout} = $output;
769  
770 0 return $self;
771 }
772  
773 =head2 $m->dx()
774  
775 Returns the delta X value for building this maze.
776  
777 =cut
778  
779 sub dx
780 28 ▶{
Subroutine dx: called CC=1 SCAR=0.0
Pod documented
781 28 my $self = shift;
782  
783 28 return $self->{dx};
784 }
785  
786 =head2 $m->dy()
787  
788 Returns the delta Y value for building this maze.
789  
790 =cut
791  
792 sub dy
793 34 ▶{
Subroutine dy: called CC=1 SCAR=0.0
Pod documented
794 34 my $self = shift;
795  
796 34 return $self->{dy};
797 }
798  
799 =head1 AUTHOR
800  
801 G. Wade Johnson, C<< <gwadej@cpan.org> >>
802  
803 =head1 BUGS
804  
805 Please report any bugs or feature requests to
806 C<bug-game-maze-svg@rt.cpan.org>, or through the web interface at
807 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Game-Maze-SVG>.
808 I will be notified, and then you'll automatically be notified of progress on
809 your bug as I make changes.
810  
811 =head1 ACKNOWLEDGEMENTS
812  
813 Thanks go to Valen Johnson and Jason Wood for extensive test play of the
814 mazes.
815  
816 =head1 COPYRIGHT & LICENSE
817  
818 Copyright 2004-2013 G. Wade Johnson, all rights reserved.
819  
820 This program is free software; you can redistribute it and/or modify it
821 under the same terms as Perl itself.
822  
823 =cut
824  
825 28 1;
↑ Summary SVG/HexCells.pm »