| 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::Square - a square on a map | |||||||||||||||||||||||||||||||||||||||||
| 21 | ||||||||||||||||||||||||||||||||||||||||||
| 22 | =head1 SYNOPSIS | |||||||||||||||||||||||||||||||||||||||||
| 23 | ||||||||||||||||||||||||||||||||||||||||||
| 24 | use Modern::Perl; | |||||||||||||||||||||||||||||||||||||||||
| 25 | use Game::TextMapper::Point::Square; | |||||||||||||||||||||||||||||||||||||||||
| 26 | my $square = Game::TextMapper::Point::Square->new(x => 1, y => 1, z => 0); | |||||||||||||||||||||||||||||||||||||||||
| 27 | say $square->svg_region('', [0]); | |||||||||||||||||||||||||||||||||||||||||
| 28 | # <rect id="square110" x="86.6" y="86.6" width="173.2" height="173.2" /> | |||||||||||||||||||||||||||||||||||||||||
| 29 | ||||||||||||||||||||||||||||||||||||||||||
| 30 | =head1 DESCRIPTION | |||||||||||||||||||||||||||||||||||||||||
| 31 | ||||||||||||||||||||||||||||||||||||||||||
| 32 | This class holds information about a square 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 rectangle at the correct coordinates using these definitions. | |||||||||||||||||||||||||||||||||||||||||
| 36 | ||||||||||||||||||||||||||||||||||||||||||
| 37 | =head1 SEE ALSO | |||||||||||||||||||||||||||||||||||||||||
| 38 | ||||||||||||||||||||||||||||||||||||||||||
| 39 | This is a specialisation of L<Game::TextMapper::Point>. | |||||||||||||||||||||||||||||||||||||||||
| 40 | ||||||||||||||||||||||||||||||||||||||||||
| 41 | The SVG size is determined by C<$dy> from L<Game::TextMapper::Constants>. | |||||||||||||||||||||||||||||||||||||||||
| 42 | ||||||||||||||||||||||||||||||||||||||||||
| 43 | =cut | |||||||||||||||||||||||||||||||||||||||||
| 44 | ||||||||||||||||||||||||||||||||||||||||||
| 45 | package Game::TextMapper::Point::Square; | |||||||||||||||||||||||||||||||||||||||||
| 46 | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 11 | ▶ | use Game::TextMapper::Constants qw($dy); | |||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||
| 48 | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 11 | ▶ | use Game::TextMapper::Point; | |||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||
| 50 | 11 | ▶ | use Modern::Perl '2018'; | |||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||
| 51 | 11 | ▶ | use Mojo::Util qw(url_escape); | |||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||
| 52 | 11 | ▶ | use Mojo::Base 'Game::TextMapper::Point'; | |||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||
| 53 | 11 | ▶ | use Encode; | |||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||
| 54 | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 3956 | ▶ | sub pixels { | |||||||||||||||||||||||||||||||||||||||
|
Subroutine
pixels: called CC=4 SCAR=13.9
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||||||
| 56 | 3956 | my ($self, $offset, $add_x, $add_y) = @_; | ||||||||||||||||||||||||||||||||||||||||
| 57 | 3956 | my $x = $self->x; | ||||||||||||||||||||||||||||||||||||||||
| 58 | 3956 | my $y = $self->y; | ||||||||||||||||||||||||||||||||||||||||
| 59 | 3956 | my $z = $self->z; | ||||||||||||||||||||||||||||||||||||||||
| 60 | 3956 | ▶ | $y += $offset->[$z] if defined $offset->[$z]; | |||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||
| 61 | 3956 | ▶ | $add_x //= 0; | |||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$add_x //= 0
MC/DC100%1/1
$add_x //= 0
$add_x
Truth table100%2/2
A
A= $add_x
| ||||||||||||||||||||||||||||||||||||||||||
| 62 | 3956 | ▶ | $add_y //= 0; | |||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$add_y //= 0
MC/DC100%1/1
$add_y //= 0
$add_y
Truth table100%2/2
A
A= $add_y
| ||||||||||||||||||||||||||||||||||||||||||
| 63 | 3956 | return $x * $dy + $add_x, $y * $dy + $add_y; | ||||||||||||||||||||||||||||||||||||||||
| 64 | } | |||||||||||||||||||||||||||||||||||||||||
| 65 | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 1216 | ▶ | sub svg_region { | |||||||||||||||||||||||||||||||||||||||
|
Subroutine
svg_region: called CC=2 SCAR=7.2
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||
| 67 | 1216 | my ($self, $attributes, $offset) = @_; | ||||||||||||||||||||||||||||||||||||||||
| 68 | 1216 | ▶ | return sprintf(qq{ <rect id="square%s%s%s" x="%.1f" y="%.1f" width="%.1f" height="%.1f" %s />\n}, | |||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||
| 69 | $self->x, $self->y, $self->z != 0 ? $self->z : '', # z 0 is not printed at all for the $id | |||||||||||||||||||||||||||||||||||||||||
| 70 | $self->pixels($offset, -0.5 * $dy, -0.5 * $dy), | |||||||||||||||||||||||||||||||||||||||||
| 71 | $dy, $dy, $attributes); | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | |||||||||||||||||||||||||||||||||||||||||
| 73 | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 2432 | ▶ | sub svg { | |||||||||||||||||||||||||||||||||||||||
|
Subroutine
svg: called CC=2 SCAR=6.9
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||
| 75 | 2432 | my ($self, $offset) = @_; | ||||||||||||||||||||||||||||||||||||||||
| 76 | 2432 | my $data = ''; | ||||||||||||||||||||||||||||||||||||||||
| 77 | 2432 | for my $type (@{$self->type}) { | ||||||||||||||||||||||||||||||||||||||||
| 78 | 1514 | $data .= sprintf(qq{ <use x="%.1f" y="%.1f" xlink:href="#%s" />\n}, | ||||||||||||||||||||||||||||||||||||||||
| 79 | $self->pixels($offset), | |||||||||||||||||||||||||||||||||||||||||
| 80 | $type); | |||||||||||||||||||||||||||||||||||||||||
| 81 | } | |||||||||||||||||||||||||||||||||||||||||
| 82 | 2432 | return $data; | ||||||||||||||||||||||||||||||||||||||||
| 83 | } | |||||||||||||||||||||||||||||||||||||||||
| 84 | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 1216 | ▶ | sub svg_coordinates { | |||||||||||||||||||||||||||||||||||||||
|
Subroutine
svg_coordinates: called CC=2 SCAR=6.9
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||
| 86 | 1216 | my ($self, $offset) = @_; | ||||||||||||||||||||||||||||||||||||||||
| 87 | 1216 | my $x = $self->x; | ||||||||||||||||||||||||||||||||||||||||
| 88 | 1216 | my $y = $self->y; | ||||||||||||||||||||||||||||||||||||||||
| 89 | 1216 | my $z = $self->z; | ||||||||||||||||||||||||||||||||||||||||
| 90 | 1216 | $y += $offset->[$z]; | ||||||||||||||||||||||||||||||||||||||||
| 91 | 1216 | my $data = ''; | ||||||||||||||||||||||||||||||||||||||||
| 92 | 1216 | $data .= qq{ <text text-anchor="middle"}; | ||||||||||||||||||||||||||||||||||||||||
| 93 | 1216 | $data .= sprintf(qq{ x="%.1f" y="%.1f"}, $self->pixels($offset, 0, -0.4 * $dy)); | ||||||||||||||||||||||||||||||||||||||||
| 94 | 1216 | $data .= ' '; | ||||||||||||||||||||||||||||||||||||||||
| 95 | 1216 | ▶ | $data .= $self->map->text_attributes || ''; | |||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->map->text_attributes || ''
MC/DC0%0/1
$self->map->text_attributes || ''
$self->map->text_attributes
Truth table50%1/2
A
A= $self->map->text_attributes
| ||||||||||||||||||||||||||||||||||||||||||
| 96 | 1216 | $data .= '>'; | ||||||||||||||||||||||||||||||||||||||||
| 97 | 1216 | $data .= Game::TextMapper::Point::coord($self->x, $self->y, "."); # original | ||||||||||||||||||||||||||||||||||||||||
| 98 | 1216 | $data .= qq{</text>\n}; | ||||||||||||||||||||||||||||||||||||||||
| 99 | 1216 | return $data; | ||||||||||||||||||||||||||||||||||||||||
| 100 | } | |||||||||||||||||||||||||||||||||||||||||
| 101 | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 1216 | ▶ | sub svg_label { | |||||||||||||||||||||||||||||||||||||||
|
Subroutine
svg_label: called CC=11 SCAR=30.9
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||
| 103 | 1216 | my ($self, $url, $offset) = @_; | ||||||||||||||||||||||||||||||||||||||||
| 104 | 1216 | ▶ | return '' unless defined $self->label; | |||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||
| 105 | 5 | my $attributes = $self->map->label_attributes; | ||||||||||||||||||||||||||||||||||||||||
| 106 | 5 | ▶ | if ($self->size) { | |||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if (not $attributes =~ s/\bfont-size="\d+pt"/'font-size="' . $self->size . 'pt"'/e) { | |||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | $attributes .= ' font-size="' . $self->size . '"'; | ||||||||||||||||||||||||||||||||||||||||
| 109 | } | |||||||||||||||||||||||||||||||||||||||||
| 110 | } | |||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | $url =~ s/\%s/url_escape(encode_utf8($self->label))/e or $url .= url_escape(encode_utf8($self->label)) if $url; | |||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
$url && ($url =~ s/\%s/url_escape(&encode_utf8(scalar $self->label));/eu || ($url .= url_escape(&encode_utf8(scalar $self->label))))
$url =~ s/\%s/url_escape(&encode_utf8(scalar $self->label));/eu or $url .= url_escape(&encode_utf8(scalar $self->label))
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))
| ||||||||||||||||||||||||||||||||||||||||||
| 112 | 5 | my $data = " <g>"; | ||||||||||||||||||||||||||||||||||||||||
| 113 | 5 | ▶ | $data .= sprintf('<text text-anchor="middle" x="%.1f" y="%.1f" %s %s>%s</text>', | |||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$attributes || ''
MC/DC0%0/1
$attributes || ''
$attributes
Truth table50%1/2
A
A= $attributes
| ||||||||||||||||||||||||||||||||||||||||||
| 114 | $self->pixels($offset, 0, 0.4 * $dy), | |||||||||||||||||||||||||||||||||||||||||
| 115 | $attributes ||'', | |||||||||||||||||||||||||||||||||||||||||
| 116 | $self->map->glow_attributes, | |||||||||||||||||||||||||||||||||||||||||
| 117 | $self->label) | |||||||||||||||||||||||||||||||||||||||||
| 118 | if $self->map->glow_attributes; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 5 | ▶ | $data .= qq{<a xlink:href="$url">} if $url; | |||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||
| 120 | 5 | ▶ | $data .= sprintf(qq{<text text-anchor="middle" x="%.1f" y="%.1f" %s>%s</text>}, | |||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$attributes || ''
MC/DC0%0/1
$attributes || ''
$attributes
Truth table50%1/2
A
A= $attributes
| ||||||||||||||||||||||||||||||||||||||||||
| 121 | $self->pixels($offset, 0, 0.4 * $dy), | |||||||||||||||||||||||||||||||||||||||||
| 122 | $attributes ||'', | |||||||||||||||||||||||||||||||||||||||||
| 123 | $self->label); | |||||||||||||||||||||||||||||||||||||||||
| 124 | 5 | ▶ | $data .= "</a>" if $url; | |||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||
| 125 | 5 | $data .= "</g>\n"; | ||||||||||||||||||||||||||||||||||||||||
| 126 | 5 | return $data; | ||||||||||||||||||||||||||||||||||||||||
| 127 | } | |||||||||||||||||||||||||||||||||||||||||
| 128 | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 11 | 1; | ||||||||||||||||||||||||||||||||||||||||