File Coverage

blib/lib/Game/TextMapper/Point/Square.pm
Criterion Covered Total %
statement 60 64 93.7
branch 9 18 50.0
condition 7 13 53.8
subroutine 11 11 100.0
pod 4 5 80.0
total 91 111 81.9


line stmt bran cond sub pod time code
1             # Copyright (C) 2009-2022 Alex Schroeder
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 .
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             #
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.
40              
41             The SVG size is determined by C<$dy> from L.
42              
43             =cut
44              
45             package Game::TextMapper::Point::Square;
46              
47 11     11   62 use Game::TextMapper::Constants qw($dy);
  11         23  
  11         1063  
48              
49 11     11   58 use Game::TextMapper::Point;
  11         17  
  11         73  
50 11     11   292 use Modern::Perl '2018';
  11         122  
  11         129  
51 11     11   3701 use Mojo::Util qw(url_escape);
  11         26  
  11         656  
52 11     11   52 use Mojo::Base 'Game::TextMapper::Point';
  11         17  
  11         56  
53 11     11   1775 use Encode;
  11         17  
  11         10843  
54              
55             sub pixels {
56 3975     3975 0 14048 my ($self, $offset, $add_x, $add_y) = @_;
57 3975         5920 my $x = $self->x;
58 3975         12285 my $y = $self->y;
59 3975         11528 my $z = $self->z;
60 3975 50       12853 $y += $offset->[$z] if defined $offset->[$z];
61 3975   100     6970 $add_x //= 0;
62 3975   100     6588 $add_y //= 0;
63 3975         21802 return $x * $dy + $add_x, $y * $dy + $add_y;
64             }
65              
66             sub svg_region {
67 1249     1249 1 4971 my ($self, $attributes, $offset) = @_;
68 1249 50       2309 return sprintf(qq{ \n},
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             sub svg {
75 2498     2498 1 6793 my ($self, $offset) = @_;
76 2498         2614 my $data = '';
77 2498         2424 for my $type (@{$self->type}) {
  2498         3013  
78 1467         3828 $data .= sprintf(qq{ \n},
79             $self->pixels($offset),
80             $type);
81             }
82 2498         5369 return $data;
83             }
84              
85             sub svg_coordinates {
86 1249     1249 1 3409 my ($self, $offset) = @_;
87 1249         1510 my $x = $self->x;
88 1249         3601 my $y = $self->y;
89 1249         3380 my $z = $self->z;
90 1249         3142 $y += $offset->[$z];
91 1249         1226 my $data = '';
92 1249         1437 $data .= qq{
93 1249         1688 $data .= sprintf(qq{ x="%.1f" y="%.1f"}, $self->pixels($offset, 0, -0.4 * $dy));
94 1249         1456 $data .= ' ';
95 1249   50     1647 $data .= $self->map->text_attributes || '';
96 1249         5494 $data .= '>';
97 1249         1570 $data .= Game::TextMapper::Point::coord($self->x, $self->y, "."); # original
98 1249         1656 $data .= qq{\n};
99 1249         2182 return $data;
100             }
101              
102             sub svg_label {
103 1249     1249 1 7190 my ($self, $url, $offset) = @_;
104 1249 100       2150 return '' unless defined $self->label;
105 5         22 my $attributes = $self->map->label_attributes;
106 5 50       30 if ($self->size) {
107 0 0       0 if (not $attributes =~ s/\bfont-size="\d+pt"/'font-size="' . $self->size . 'pt"'/e) {
  0         0  
108 0         0 $attributes .= ' font-size="' . $self->size . '"';
109             }
110             }
111 5 50 0     19 $url =~ s/\%s/url_escape(encode_utf8($self->label))/e or $url .= url_escape(encode_utf8($self->label)) if $url;
  0         0  
112 5         7 my $data = " ";
113 5 50 50     10 $data .= sprintf('%s',
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 50       55 $data .= qq{} if $url;
120 5   50     10 $data .= sprintf(qq{%s},
121             $self->pixels($offset, 0, 0.4 * $dy),
122             $attributes ||'',
123             $self->label);
124 5 50       31 $data .= "" if $url;
125 5         7 $data .= "\n";
126 5         12 return $data;
127             }
128              
129             1;