blib/lib/Game/TextMapper/Line.pm

filter: Statementstmt 53.9% 62 / 115 Branchbran 32.5% 13 / 40 Conditioncond 5.5% 1 / 18 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 77.7% 7 / 9 Podpod 50.0% 3 / 6 totaltotal 43.6% 86 / 197 CC 30 SCAR 42.7
CC
30
Cov
44%
svg_label
179.6
svg
13.7
debug
12.0
CRAP 71.4
« blib/lib/Game/TextMapper/Folkesten.pm ↑ Summary blib/lib/auto/share/dist/Game-TextMapper/extract-svg.pl »
1 # Copyright (C) 2009-2022 Alex Schroeder <alex@gnu.org>
2 #
3 # This program is free software: you can redistribute it and/or modify it under
4 # the terms of the GNU Affero General Public License as published by the Free
5 # Software Foundation, either version 3 of the License, or (at your option) any
6 # later version.
7 #
8 # This program is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
11 # details.
12 #
13 # You should have received a copy of the GNU Affero General Public License along
14 # with this program. If not, see <http://www.gnu.org/licenses/>.
15  
16 =encoding utf8
17  
18 =head1 NAME
19  
20 Game::TextMapper::Line - a line between two points
21  
22 =head1 SYNOPSIS
23  
24 use Modern::Perl;
25 use Game::TextMapper::Line::Hex;
26 use Game::TextMapper::Point::Hex;
27 my $line = Game::TextMapper::Line::Hex->new();
28 my $from = Game::TextMapper::Point::Hex->new(x => 1, y => 1, z => 0);
29 my $to = Game::TextMapper::Point::Hex->new(x => 5, y => 3, z => 0);
30 $line->points([$from, $to]);
31 my @line = $line->compute_missing_points;
32 say join(" ", map { $_->str } @line);
33 # (1,1,0) (2,1,0) (3,2,0) (4,2,0) (5,3,0)
34  
35 =head1 DESCRIPTION
36  
37 The line connects two points. This class knows how to compute all the regions
38 between these two points, how to compute the next region along the line, and how
39 to output SVG.
40  
41 In order to do this, the class needs to know how to work with the regions on the
42 map. This is different for hexes and squares. Therefore you should always be
43 using the appropriate Hex or Square class instead.
44  
45 =cut
46  
47 package Game::TextMapper::Line;
48  
49 11 ▶use Modern::Perl '2018';
Subroutine BEGIN: called CC=1 SCAR=0.0
50 11 ▶use Mojo::Util qw(url_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
51 11 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 11 our $debug;
54  
55 =head1 ATTRIBUTES
56  
57 =head2 points
58  
59 An array reference of points using a class derived from
60 L<Game::TextMapper::Point>, i.e. L<Game::TextMapper::Line::Hex> uses
61 L<Game::TextMapper::Point::Hex> and L<Game::TextMapper::Line::Square> uses
62 L<Game::TextMapper::Point::Square>.
63  
64 =cut
65  
66 11 has 'id';
67 11 has 'points';
68 11 has 'offset';
69 11 has 'type';
70 11 has 'label';
71 11 has 'map';
72 11 has 'side';
73 11 has 'start';
74  
75 =head1 METHODS
76  
77 =head2 compute_missing_points
78  
79 Compute the missing points between the points in C<points> and return it.
80  
81 =cut
82  
83 311 ▶sub compute_missing_points {
Subroutine compute_missing_points: called CC=4 SCAR=13.9
Pod documented
84 311 my $self = shift;
85 311 my $i = 0;
86 311 my $current = $self->points->[$i++];
87 311 my $z = $current->z;
88 311 my @result = ($current);
89 311 ▶ while ($self->points->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->points->[$i]) 1410 311 1721
90 1410 $current = $self->one_step($current, $self->points->[$i]);
91 1410 ▶ return unless $z == $current->z; # must all be on the same plane
Branch: 1 branch
BranchTrueFalseTotal
unless $z == $current->z 0 1410 1410
92 1410 push(@result, $current);
93 1410 ▶ $i++ if $current->equal($self->points->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
if $current->equal($self->points->[$i]) 1315 95 1410
94 }
95  
96 311 return @result;
97 }
98  
99 3131 ▶sub partway {
Subroutine partway: called CC=3 SCAR=11.2
Pod undocumented
100 3131 my ($self, $from, $to, $q) = @_;
101 3131 my ($x1, $y1) = $self->pixels($from);
102 3131 my ($x2, $y2) = $self->pixels($to);
103 3131 ▶ $q ||= 1;
Condition50%1/2
$q ||= 1
l!l
31310
MC/DC0%0/1
$q ||= 1
$q
Truth table50%1/2
A
A= $q
Aresult
11
00
104 3131 ▶ return $x1 + ($x2 - $x1) * $q, $y1 + ($y2 - $y1) * $q if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 3131 3131
105 3131 return sprintf("%.1f,%.1f", $x1 + ($x2 - $x1) * $q, $y1 + ($y2 - $y1) * $q);
106 }
107  
108 =head2 svg($offset)
109  
110 This returns an SVG fragment, a string with a C<path>.
111  
112 =cut
113  
114 311 ▶sub svg {
Subroutine svg: called CC=9 SCAR=26.2
Pod documented
115 311 my ($self, $offset) = @_;
116 311 my ($path, $current, $next, $closed);
117 311 $self->offset($offset);
118 311 my @points = $self->compute_missing_points();
119 311 ▶ return '' unless @points;
Branch: 1 branch
BranchTrueFalseTotal
unless @points 0 311 311
120 311 ▶ if ($points[0]->equal($points[$#points])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($points[0]->equal($points[$#points])) 0 311 311
121 0 $closed = 1;
122 }
123  
124 311 ▶ if ($closed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($closed) { } 0 311 311
125 0 for my $i (0 .. $#points - 1) {
126 0 $current = $points[$i];
127 0 $next = $points[$i+1];
128 0 ▶ if (!$path) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $path) { } 0 0 0
129 0 my $a = $self->partway($current, $next, 0.3);
130 0 my $b = $self->partway($current, $next, 0.5);
131 0 my $c = $self->partway($points[$#points-1], $current, 0.7);
132 0 my $d = $self->partway($points[$#points-1], $current, 0.5);
133 0 $path = "M$d C$c $a $b";
134 } else {
135 # continue curve
136 0 my $a = $self->partway($current, $next, 0.3);
137 0 my $b = $self->partway($current, $next, 0.5);
138 0 $path .= " S$a $b";
139 }
140 }
141 } else {
142 311 for my $i (0 .. $#points - 1) {
143 1410 $current = $points[$i];
144 1410 $next = $points[$i+1];
145 1410 ▶ if (!$path) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $path) { } 311 1099 1410
146 # line from a to b; control point a required for following S commands
147 311 my $a = $self->partway($current, $next, 0.3);
148 311 my $b = $self->partway($current, $next, 0.5);
149 311 $path = "M$a C$b $a $b";
150 } else {
151 # continue curve
152 1099 my $a = $self->partway($current, $next, 0.3);
153 1099 my $b = $self->partway($current, $next, 0.5);
154 1099 $path .= " S$a $b";
155 }
156 }
157 # end with a little stub
158 311 $path .= " L" . $self->partway($current, $next, 0.7);
159 }
160  
161 311 my $id = $self->id;
162 311 my $type = $self->type;
163 311 my $attributes = $self->map->path_attributes->{$type};
164 311 my $data = qq{ <path id="$id" $attributes d="$path"/>\n};
165 311 ▶ $data .= $self->debug($closed) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Game::TextMapper::Line::debug 0 311 311
166 311 return $data;
167 }
168  
169 =head2 svg_label
170  
171 This returns an SVG fragment, a group C<g> with C<text> and a C<textPath>
172 element.
173  
174 =cut
175  
176 311 ▶sub svg_label {
Subroutine svg_label: called CC=14 SCAR=51.9
Pod documented
177 311 my ($self) = @_;
178 311 ▶ return '' unless defined $self->label;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->label 311 0 311
179 0 my $id = $self->id;
180 0 my $label = $self->label;
181 0 ▶ my $attributes = $self->map->label_attributes || "";
Condition0%0/2
$self->map->label_attributes || ''
l!l
00
MC/DC0%0/1
$self->map->label_attributes || ''
$self->map->label_attributes
Truth table0%0/2
A
A= $self->map->label_attributes
Aresult
11
00
182 0 ▶ my $glow = $self->map->glow_attributes || "";
Condition0%0/2
$self->map->glow_attributes || ''
l!l
00
MC/DC0%0/1
$self->map->glow_attributes || ''
$self->map->glow_attributes
Truth table0%0/2
A
A= $self->map->glow_attributes
Aresult
11
00
183 0 my $url = $self->map->url;
184 0 ▶ $url =~ s/\%s/url_escape($self->label)/e or $url .= url_escape($self->label) if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 0 0 0
Condition0%0/6
$url && ($url =~ s/\%s/url_escape($self->label);/eu || ($url .= url_escape($self->label)))
!ll&&!rl&&r
000
$url =~ s/\%s/url_escape($self->label);/eu or $url .= url_escape($self->label)
l!l&&r!l&&!r
000
MC/DC0%0/3
$url && ($url =~ s/\%s/url_escape($self->label);/eu || ($url .= url_escape($self->label)))
$url $url =~ s/\%s/url_escape($self->label);/eu $url .= url_escape($self->label)
Truth table0%0/4
A && B or C
A= $urlB= $url =~ s/\%s/url_escape($self->label);/euC= $url .= url_escape($self->label)
ABCresult
0XX0
1000
11X1
1011
185 # Default side is left, but if the line goes from right to left, then "left"
186 # means "upside down", so allow people to control it.
187 0 my $pathAttributes = '';
188 0 ▶ if ($self->side) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->side) { } 0 0 0
elsif ($self->points->[1]->x < $self->points->[0]->x or $#{$self->points;} >= 2 and $self->points->[2]->x < $self->points->[0]->x) { } 0 0 0
189 0 $pathAttributes = ' side="' . $self->side . '"';
190 ▶ } elsif ($self->points->[1]->x < $self->points->[0]->x
Condition0%0/6
$self->points->[1]->x < $self->points->[0]->x or $#{$self->points;} >= 2 and $self->points->[2]->x < $self->points->[0]->x
l!l&&r!l&&!r
000
$#{$self->points;} >= 2 and $self->points->[2]->x < $self->points->[0]->x
!ll&&!rl&&r
000
MC/DC0%0/3
$self->points->[1]->x < $self->points->[0]->x or $#{$self->points;} >= 2 and $self->points->[2]->x < $self->points->[0]->x
$self->points->[1]->x < $self->points->[0]->x $#{$self->points;} >= 2 $self->points->[2]->x < $self->points->[0]->x
Truth table0%0/4
A or B and C
A= $self->points->[1]->x < $self->points->[0]->xB= $#{$self->points;} >= 2C= $self->points->[2]->x < $self->points->[0]->x
ABCresult
1XX1
0111
00X0
0100
191 0 or $#{$self->points} >= 2 and $self->points->[2]->x < $self->points->[0]->x) {
192 0 $pathAttributes = ' side="right"';
193 }
194 0 ▶ if ($self->start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->start) 0 0 0
195 0 $pathAttributes .= ' startOffset="' . $self->start . '"';
196 }
197 0 my $data = qq{ <g>\n};
198 0 ▶ $data .= qq{ <text $attributes $glow><textPath$pathAttributes href='#$id'>$label</textPath></text>\n} if $glow;
Branch: 1 branch
BranchTrueFalseTotal
if $glow 0 0 0
199 0 ▶ $data .= qq{ <a xlink:href="$url">} if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 0 0 0
200 0 $data .= qq{ <text $attributes><textPath$pathAttributes href='#$id'>$label</textPath></text>\n};
201 0 ▶ $data .= qq{ </a>} if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 0 0 0
202 0 $data .= qq{ </g>\n};
203 0 return $data;
204 }
205  
206 0 ▶sub debug {
Subroutine debug: not called CC=3 SCAR=24.8
Pod undocumented
207 0 my ($self, $closed) = @_;
208 0 my ($data, $current, $next);
209 0 my @points = $self->compute_missing_points();
210 0 for my $i (0 .. $#points - 1) {
211 0 $current = $points[$i];
212 0 $next = $points[$i+1];
213 0 $data .= circle($self->pixels($current), 15, $i++);
214 0 $data .= circle($self->partway($current, $next, 0.3), 3, 'a');
215 0 $data .= circle($self->partway($current, $next, 0.5), 5, 'b');
216 0 $data .= circle($self->partway($current, $next, 0.7), 3, 'c');
217 }
218 0 $data .= circle($self->pixels($next), 15, $#points);
219  
220 0 my ($x, $y) = $self->pixels($points[0]); $y += 30;
221 0 ▶ $data .= "<text fill='#000' font-size='20pt' "
Branch: 1 branch
BranchTrueFalseTotal
if $closed 0 0 0
222 . "text-anchor='middle' dominant-baseline='central' "
223 . "x='$x' y='$y'>closed</text>"
224 if $closed;
225  
226 0 return $data;
227 }
228  
229 0 ▶sub circle {
Subroutine circle: not called CC=2 SCAR=17.9
Pod undocumented
230 0 my ($x, $y, $r, $i) = @_;
231 0 my $data = "<circle fill='#666' cx='$x' cy='$y' r='$r'/>";
232 0 ▶ $data .= "<text fill='#000' font-size='20pt' "
Branch: 1 branch
BranchTrueFalseTotal
if $i 0 0 0
233 . "text-anchor='middle' dominant-baseline='central' "
234 . "x='$x' y='$y'>$i</text>" if $i;
235 0 return "$data\n";
236 }
237  
238 =head1 SEE ALSO
239  
240 Lines consist of L<Game::TextMapper::Point> instances.
241  
242 Use either L<Game::TextMapper::Line::Hex> or L<Game::TextMapper::Line::Square>
243 to implement lines.
244  
245 =cut
246  
247 11 1;
« blib/lib/Game/TextMapper/Folkesten.pm ↑ Summary blib/lib/auto/share/dist/Game-TextMapper/extract-svg.pl »