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   76 use Game::TextMapper::Constants qw($dy);
  11         20  
  11         1197  
48              
49 11     11   76 use Game::TextMapper::Point;
  11         19  
  11         76  
50 11     11   383 use Modern::Perl '2018';
  11         95  
  11         145  
51 11     11   4084 use Mojo::Util qw(url_escape);
  11         50  
  11         742  
52 11     11   99 use Mojo::Base 'Game::TextMapper::Point';
  11         20  
  11         77  
53 11     11   2190 use Encode;
  11         36  
  11         13509  
54              
55             sub pixels {
56 3988     3988 0 13050 my ($self, $offset, $add_x, $add_y) = @_;
57 3988         5791 my $x = $self->x;
58 3988         11692 my $y = $self->y;
59 3988         11332 my $z = $self->z;
60 3988 50       12815 $y += $offset->[$z] if defined $offset->[$z];
61 3988   100     7080 $add_x //= 0;
62 3988   100     6564 $add_y //= 0;
63 3988         24393 return $x * $dy + $add_x, $y * $dy + $add_y;
64             }
65              
66             sub svg_region {
67 1259     1259 1 4775 my ($self, $attributes, $offset) = @_;
68 1259 50       1933 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 2518     2518 1 6835 my ($self, $offset) = @_;
76 2518         2464 my $data = '';
77 2518         2315 for my $type (@{$self->type}) {
  2518         2964  
78 1460         3757 $data .= sprintf(qq{ \n},
79             $self->pixels($offset),
80             $type);
81             }
82 2518         5601 return $data;
83             }
84              
85             sub svg_coordinates {
86 1259     1259 1 4162 my ($self, $offset) = @_;
87 1259         1771 my $x = $self->x;
88 1259         3961 my $y = $self->y;
89 1259         3843 my $z = $self->z;
90 1259         3703 $y += $offset->[$z];
91 1259         1378 my $data = '';
92 1259         1643 $data .= qq{
93 1259         2080 $data .= sprintf(qq{ x="%.1f" y="%.1f"}, $self->pixels($offset, 0, -0.4 * $dy));
94 1259         1725 $data .= ' ';
95 1259   50     2129 $data .= $self->map->text_attributes || '';
96 1259         6718 $data .= '>';
97 1259         1929 $data .= Game::TextMapper::Point::coord($self->x, $self->y, "."); # original
98 1259         1877 $data .= qq{\n};
99 1259         2706 return $data;
100             }
101              
102             sub svg_label {
103 1259     1259 1 6443 my ($self, $url, $offset) = @_;
104 1259 100       1895 return '' unless defined $self->label;
105 5         41 my $attributes = $self->map->label_attributes;
106 5 50       60 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     30 $url =~ s/\%s/url_escape(encode_utf8($self->label))/e or $url .= url_escape(encode_utf8($self->label)) if $url;
  0         0  
112 5         14 my $data = " ";
113 5 50 50     13 $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       139 $data .= qq{} if $url;
120 5   50     20 $data .= sprintf(qq{%s},
121             $self->pixels($offset, 0, 0.4 * $dy),
122             $attributes ||'',
123             $self->label);
124 5 50       66 $data .= "" if $url;
125 5         12 $data .= "\n";
126 5         27 return $data;
127             }
128              
129             1;