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

filter: Statementstmt 91.3% 63 / 69 Branchbran 60.0% 12 / 20 Conditioncond 66.6% 16 / 24 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 100% 11 / 11 Podpod 83.3% 5 / 6 totaltotal 78.8% 112 / 142 CC 21 SCAR 20.1
CC
21
Cov
81%
svg_label
13.7
svg_region
8.3
pixels
4.0
CRAP 7.4
« blib/lib/Game/TextMapper/Command/render.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 sub corners {
67 1414 ▶ return @hex;
Subroutine corners: called CC=1 SCAR=0.0
Pod documented
68 }
69  
70 sub pixels {
71 12358 ▶ my ($self, $offset, $add_x, $add_y) = @_;
Subroutine pixels: called CC=4 SCAR=13.9
Pod undocumented
72 12358 my $x = $self->x;
73 12358 my $y = $self->y;
74 12358 my $z = $self->z;
75 12358 ▶ $y += $offset->[$z] if defined $offset->[$z];
Branch: 1 branch
BranchTrueFalseTotal
if defined $offset->[$z] 12358 0 12358
76 12358 ▶ $add_x //= 0;
Condition100%2/2
$add_x //= 0
l!l
97502608
MC/DC100%1/1
$add_x //= 0
$add_x
Truth table100%2/2
A
A= $add_x
Aresult
11
00
77 12358 ▶ $add_y //= 0;
Condition100%2/2
$add_y //= 0
l!l
97502608
MC/DC100%1/1
$add_y //= 0
$add_y
Truth table100%2/2
A
A= $add_y
Aresult
11
00
78 12358 return $x * $dx * 3/2 + $add_x, $y * $dy - $x%2 * $dy/2 + $add_y;
79 }
80  
81 sub svg_region {
82 1382 ▶ my ($self, $attributes, $offset) = @_;
Subroutine svg_region: called CC=6 SCAR=21.2
Pod documented
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 sub svg {
99 2764 ▶ my ($self, $offset) = @_;
Subroutine svg: called CC=2 SCAR=6.9
Pod documented
100 2764 my $data = '';
101 2764 for my $type (@{$self->type}) {
102 2571 $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 sub svg_coordinates {
109 1382 ▶ my ($self, $offset) = @_;
Subroutine svg_coordinates: called CC=2 SCAR=6.9
Pod documented
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 sub svg_label {
121 1382 ▶ my ($self, $url, $offset) = @_;
Subroutine svg_label: called CC=11 SCAR=26.2
Pod documented
122 1382 ▶ return '' unless defined $self->label;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->label 1344 38 1382
123 38 my $attributes = $self->map->label_attributes;
124 38 ▶ if ($self->size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->size) 0 38 38
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 38 ▶ $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 37 38
Condition50%3/6
$url && ($url =~ s/\%s/url_escape(&encode_utf8(scalar $self->label));/eu || ($url .= url_escape(&encode_utf8(scalar $self->label))))
!ll
3710
$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 38 ▶ my $data = sprintf(qq{ <g><text text-anchor="middle" x="%.1f" y="%.1f" %s %s>}
Condition75%3/4
$attributes || ''
l!l
371
$self->map->glow_attributes || ''
l!l
038
MC/DC100%1/1
$attributes || ''
$attributes
MC/DC0%0/1
$self->map->glow_attributes || ''
$self->map->glow_attributes
Truth table100%2/2
A
A= $attributes
Aresult
11
00
Truth table50%1/2
A
A= $self->map->glow_attributes
Aresult
11
00
131 . $self->label
132 . qq{</text>},
133 $self->pixels($offset, 0, $dy * 0.4),
134 $attributes ||'',
135 $self->map->glow_attributes ||'');
136 38 ▶ $data .= qq{<a xlink:href="$url">} if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 1 37 38
137 38 ▶ $data .= sprintf(qq{<text text-anchor="middle" x="%.1f" y="%.1f" %s>}
Condition100%2/2
$attributes || ''
l!l
371
MC/DC100%1/1
$attributes || ''
$attributes
Truth table100%2/2
A
A= $attributes
Aresult
11
00
138 . $self->label
139 . qq{</text>},
140 $self->pixels($offset, 0, $dy * 0.4),
141 $attributes ||'');
142 38 ▶ $data .= qq{</a>} if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 1 37 38
143 38 $data .= qq{</g>\n};
144 38 return $data;
145 }
146  
147 =head1 SEE ALSO
148  
149 This is a specialisation of L<Game::TextMapper::Point>.
150  
151 The SVG size is determined by C<$dx> and C<$dy> from
152 L<Game::TextMapper::Constants>.
153  
154 =cut
155  
156 11 1;
« blib/lib/Game/TextMapper/Command/render.pm ↑ Summary blib/lib/Game/TextMapper/Point.pm »