blib/lib/Game/TextMapper/Point/Hex.pm

filter: Statementstmt 91.4% 64 / 70 Branchbran 59.0% 13 / 22 Conditioncond 59.0% 13 / 22 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 100% 11 / 11 Podpod 83.3% 5 / 6 totaltotal 77.4% 110 / 142 CC 21 SCAR 20.7
CC
21
Cov
79%
svg_label
15.2
svg_region
8.3
pixels
4.0
CRAP 8.0
« blib/lib/Game/TextMapper/Point/Square.pm ↑ Summary blib/lib/Game/TextMapper/Point.pm »
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::Point::Hex - a hex on a map
21  
22 =head1 SYNOPSIS
23  
24 use Modern::Perl;
25 use Game::TextMapper::Point::Hex;
26 my $hex = Game::TextMapper::Point::Hex->new(x => 1, y => 1, z => 0);
27 say $hex->svg_region('', [0]);
28 # <polygon id="hex110" points="50.0,86.6 100.0,173.2 200.0,173.2 250.0,86.6 200.0,0.0 100.0,0.0" />
29  
30 =head1 DESCRIPTION
31  
32 This class holds information about a hex region: coordinates, a label, and
33 types. Types are the kinds of symbols that can be found in the region: a keep, a
34 tree, a mountain. They correspond to SVG definitions. The class knows how to
35 draw a SVG polygon at the correct coordinates using these definitions.
36  
37 For attributes and methods, see L<Game::TextMapper::Point>.
38  
39 =head2 Additional Methods
40  
41 =cut
42  
43 package Game::TextMapper::Point::Hex;
44  
45 11 ▶use Game::TextMapper::Constants qw($dx $dy);
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 11 ▶use Modern::Perl '2018';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 11 ▶use Mojo::Util qw(url_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
49 11 ▶use Encode qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
50 11 ▶use Mojo::Base 'Game::TextMapper::Point';
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 =head3 corners
53  
54 Return the relative SVG coordinates of the points making up the shape, i.e. six
55 for L<Game::TextMapper::Point::Hex> and four for
56 L<Game::TextMapper::Point::Square>.
57  
58 The SVG coordinates are arrays with x and y coordinates relative to the center
59 of the shape.
60  
61 =cut
62  
63 11 my @hex = ([-$dx, 0], [-$dx/2, $dy/2], [$dx/2, $dy/2],
64 [$dx, 0], [$dx/2, -$dy/2], [-$dx/2, -$dy/2]);
65  
66 1411 ▶sub corners {
Subroutine corners: called CC=1 SCAR=0.0
Pod documented
67 1411 return @hex;
68 }
69  
70 12325 ▶sub pixels {
Subroutine pixels: called CC=4 SCAR=13.9
Pod undocumented
71 12325 my ($self, $offset, $add_x, $add_y) = @_;
72 12325 my $x = $self->x;
73 12325 my $y = $self->y;
74 12325 my $z = $self->z;
75 12325 ▶ $y += $offset->[$z] if defined $offset->[$z];
Branch: 1 branch
BranchTrueFalseTotal
if defined $offset->[$z] 12325 0 12325
76 12325 ▶ $add_x //= 0;
Condition100%2/2
$add_x //= 0
l!l
97112614
MC/DC100%1/1
$add_x //= 0
$add_x
Truth table100%2/2
A
A= $add_x
Aresult
11
00
77 12325 ▶ $add_y //= 0;
Condition100%2/2
$add_y //= 0
l!l
97112614
MC/DC100%1/1
$add_y //= 0
$add_y
Truth table100%2/2
A
A= $add_y
Aresult
11
00
78 12325 return $x * $dx * 3/2 + $add_x, $y * $dy - $x%2 * $dy/2 + $add_y;
79 }
80  
81 1382 ▶sub svg_region {
Subroutine svg_region: called CC=6 SCAR=21.2
Pod documented
82 1382 my ($self, $attributes, $offset) = @_;
83 1382 my $x = $self->x;
84 1382 my $y = $self->y;
85 1382 my $z = $self->z;
86 1382 my $id = "hex";
87 1382 ▶ if ($x < 100 and $y < 100 and $z < 100) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 100 and $y < 100 and $z < 100) { } 1382 0 1382
Condition33%2/6
$x < 100 and $y < 100 and $z < 100
!ll&&!rl&&r
001382
$x < 100 and $y < 100
!ll&&!rl&&r
001382
MC/DC0%0/3
$x < 100 and $y < 100 and $z < 100
$x < 100 $y < 100 $z < 100
Truth table25%1/4
A and B and C
A= $x < 100B= $y < 100C= $z < 100
ABCresult
0XX0
10X0
1100
1111
88 1382 $id .= "$x$y";
89 1382 ▶ $id .= $z if $z != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $z != 0 0 1382 1382
90 } else {
91 0 $id .= "$x.$y";
92 0 ▶ $id .= ".$z" if $z != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $z != 0 0 0 0
93 }
94 1382 my $points = join(" ", map { sprintf("%.1f,%.1f", $self->pixels($offset, @$_)) } $self->corners());
95 1382 return qq{ <polygon id="$id" $attributes points="$points" />\n}
96 }
97  
98 2764 ▶sub svg {
Subroutine svg: called CC=2 SCAR=6.9
Pod documented
99 2764 my ($self, $offset) = @_;
100 2764 my $data = '';
101 2764 for my $type (@{$self->type}) {
102 2578 $data .= sprintf(qq{ <use x="%.1f" y="%.1f" xlink:href="#%s" />\n},
103 $self->pixels($offset), $type);
104 }
105 2764 return $data;
106 }
107  
108 1382 ▶sub svg_coordinates {
Subroutine svg_coordinates: called CC=2 SCAR=6.9
Pod documented
109 1382 my ($self, $offset) = @_;
110 1382 my $data = qq{ <text text-anchor="middle"};
111 1382 $data .= sprintf(qq{ x="%.1f" y="%.1f"}, $self->pixels($offset, 0, -$dy * 0.4));
112 1382 $data .= ' ';
113 1382 ▶ $data .= $self->map->text_attributes || '';
Condition100%2/2
$self->map->text_attributes || ''
l!l
13802
MC/DC100%1/1
$self->map->text_attributes || ''
$self->map->text_attributes
Truth table100%2/2
A
A= $self->map->text_attributes
Aresult
11
00
114 1382 $data .= '>';
115 1382 $data .= Game::TextMapper::Point::coord($self->x, $self->y, ".");
116 1382 $data .= qq{</text>\n};
117 1382 return $data;
118 }
119  
120 1382 ▶sub svg_label {
Subroutine svg_label: called CC=11 SCAR=27.2
Pod documented
121 1382 my ($self, $url, $offset) = @_;
122 1382 ▶ return '' unless defined $self->label;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->label 1345 37 1382
123 37 my $attributes = $self->map->label_attributes;
124 37 ▶ if ($self->size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->size) 0 37 37
125 0 ▶ if (not $attributes =~ s/\bfont-size="\d+pt"/'font-size="' . $self->size . 'pt"'/e) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attributes =~ s/\bfont-size="\d+pt"/'font-size="' . $self->size . 'pt"';/eu) 0 0 0
126 0 $attributes .= ' font-size="' . $self->size . '"';
127 }
128 }
129 0 ▶ $url =~ s/\%s/url_escape(encode_utf8($self->label))/e or $url .= url_escape(encode_utf8($self->label)) if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 1 36 37
Condition50%3/6
$url && ($url =~ s/\%s/url_escape(&encode_utf8(scalar $self->label));/eu || ($url .= url_escape(&encode_utf8(scalar $self->label))))
!ll
3610
$url =~ s/\%s/url_escape(&encode_utf8(scalar $self->label));/eu or $url .= url_escape(&encode_utf8(scalar $self->label))
l!l
010
MC/DC0%0/3
$url && ($url =~ s/\%s/url_escape(&encode_utf8(scalar $self->label));/eu || ($url .= url_escape(&encode_utf8(scalar $self->label))))
$url $url =~ s/\%s/url_escape(&encode_utf8(scalar $self->label));/eu $url .= url_escape(&encode_utf8(scalar $self->label))
Truth table0%0/4
A && B or C
A= $urlB= $url =~ s/\%s/url_escape(&encode_utf8(scalar $self->label));/euC= $url .= url_escape(&encode_utf8(scalar $self->label))
ABCresult
0XX0
1000
11X1
1011
130 37 my $data = " <g>";
131 37 ▶ $data .= sprintf('<text text-anchor="middle" x="%.1f" y="%.1f" %s %s>'
Branch: 1 branch
BranchTrueFalseTotal
if $self->map->glow_attributes 0 37 37
Condition0%0/2
$attributes || ''
l!l
00
MC/DC0%0/1
$attributes || ''
$attributes
Truth table0%0/2
A
A= $attributes
Aresult
11
00
132 . $self->label
133 . '</text>',
134 $self->pixels($offset, 0, $dy * 0.4),
135 $attributes ||'',
136 $self->map->glow_attributes)
137 if $self->map->glow_attributes;
138 37 ▶ $data .= qq{<a xlink:href="$url">} if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 1 36 37
139 37 ▶ $data .= sprintf('<text text-anchor="middle" x="%.1f" y="%.1f" %s>'
Condition100%2/2
$attributes || ''
l!l
361
MC/DC100%1/1
$attributes || ''
$attributes
Truth table100%2/2
A
A= $attributes
Aresult
11
00
140 . $self->label
141 . '</text>',
142 $self->pixels($offset, 0, $dy * 0.4),
143 $attributes ||'');
144 37 ▶ $data .= "</a>" if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 1 36 37
145 37 $data .= "</g>\n";
146 37 return $data;
147 }
148  
149 =head1 SEE ALSO
150  
151 This is a specialisation of L<Game::TextMapper::Point>.
152  
153 The SVG size is determined by C<$dx> and C<$dy> from
154 L<Game::TextMapper::Constants>.
155  
156 =cut
157  
158 11 1;
« blib/lib/Game/TextMapper/Point/Square.pm ↑ Summary blib/lib/Game/TextMapper/Point.pm »