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/Schroeder/Archipelago.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 sub compute_missing_points {
84 352 ▶ my $self = shift;
Subroutine compute_missing_points: called CC=4 SCAR=13.9
Pod documented
85 352 my $i = 0;
86 352 my $current = $self->points->[$i++];
87 352 my $z = $current->z;
88 352 my @result = ($current);
89 352 ▶ while ($self->points->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->points->[$i]) 4531 352 4883
90 4531 $current = $self->one_step($current, $self->points->[$i]);
91 4531 ▶ return unless $z == $current->z; # must all be on the same plane
Branch: 1 branch
BranchTrueFalseTotal
unless $z == $current->z 0 4531 4531
92 4531 push(@result, $current);
93 4531 ▶ $i++ if $current->equal($self->points->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
if $current->equal($self->points->[$i]) 4431 100 4531
94 }
95  
96 352 return @result;
97 }
98  
99 sub partway {
100 9414 ▶ my ($self, $from, $to, $q) = @_;
Subroutine partway: called CC=3 SCAR=11.2
Pod undocumented
101 9414 my ($x1, $y1) = $self->pixels($from);
102 9414 my ($x2, $y2) = $self->pixels($to);
103 9414 ▶ $q ||= 1;
Condition50%1/2
$q ||= 1
l!l
94140
MC/DC0%0/1
$q ||= 1
$q
Truth table50%1/2
A
A= $q
Aresult
11
00
104 9414 ▶ return $x1 + ($x2 - $x1) * $q, $y1 + ($y2 - $y1) * $q if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 9414 9414
105 9414 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 sub svg {
115 352 ▶ my ($self, $offset) = @_;
Subroutine svg: called CC=9 SCAR=26.2
Pod documented
116 352 my ($path, $current, $next, $closed);
117 352 $self->offset($offset);
118 352 my @points = $self->compute_missing_points();
119 352 ▶ return '' unless @points;
Branch: 1 branch
BranchTrueFalseTotal
unless @points 0 352 352
120 352 ▶ if ($points[0]->equal($points[$#points])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($points[0]->equal($points[$#points])) 0 352 352
121 0 $closed = 1;
122 }
123  
124 352 ▶ if ($closed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($closed) { } 0 352 352
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 352 for my $i (0 .. $#points - 1) {
143 4531 $current = $points[$i];
144 4531 $next = $points[$i+1];
145 4531 ▶ if (!$path) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $path) { } 352 4179 4531
146 # line from a to b; control point a required for following S commands
147 352 my $a = $self->partway($current, $next, 0.3);
148 352 my $b = $self->partway($current, $next, 0.5);
149 352 $path = "M$a C$b $a $b";
150 } else {
151 # continue curve
152 4179 my $a = $self->partway($current, $next, 0.3);
153 4179 my $b = $self->partway($current, $next, 0.5);
154 4179 $path .= " S$a $b";
155 }
156 }
157 # end with a little stub
158 352 $path .= " L" . $self->partway($current, $next, 0.7);
159 }
160  
161 352 my $id = $self->id;
162 352 my $type = $self->type;
163 352 my $attributes = $self->map->path_attributes->{$type};
164 352 my $data = qq{ <path id="$id" $attributes d="$path"/>\n};
165 352 ▶ $data .= $self->debug($closed) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Game::TextMapper::Line::debug 0 352 352
166 352 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 sub svg_label {
177 352 ▶ my ($self) = @_;
Subroutine svg_label: called CC=14 SCAR=51.9
Pod documented
178 352 ▶ return '' unless defined $self->label;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->label 352 0 352
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 sub debug {
207 0 ▶ my ($self, $closed) = @_;
Subroutine debug: not called CC=3 SCAR=24.8
Pod undocumented
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 sub circle {
230 0 ▶ my ($x, $y, $r, $i) = @_;
Subroutine circle: not called CC=2 SCAR=17.9
Pod undocumented
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/Schroeder/Archipelago.pm ↑ Summary blib/lib/auto/share/dist/Game-TextMapper/extract-svg.pl »