File Coverage

blib/lib/Game/TextMapper/Point/Square.pm
Criterion Covered Total %
statement 59 63 93.6
branch 8 16 50.0
condition 8 15 53.3
subroutine 11 11 100.0
pod 4 5 80.0
total 90 110 81.8


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   109 use Game::TextMapper::Constants qw($dy);
  11         28  
  11         1287  
48              
49 11     11   84 use Game::TextMapper::Point;
  11         25  
  11         111  
50 11     11   439 use Modern::Perl '2018';
  11         82  
  11         208  
51 11     11   5178 use Mojo::Util qw(url_escape);
  11         70  
  11         1121  
52 11     11   83 use Mojo::Base 'Game::TextMapper::Point';
  11         23  
  11         155  
53 11     11   2406 use Encode;
  11         62  
  11         17792  
54              
55             sub pixels {
56 4011     4011 0 19738 my ($self, $offset, $add_x, $add_y) = @_;
57 4011         9487 my $x = $self->x;
58 4011         20595 my $y = $self->y;
59 4011         16791 my $z = $self->z;
60 4011 50       20289 $y += $offset->[$z] if defined $offset->[$z];
61 4011   100     13723 $add_x //= 0;
62 4011   100     10310 $add_y //= 0;
63 4011         39609 return $x * $dy + $add_x, $y * $dy + $add_y;
64             }
65              
66             sub svg_region {
67 1259     1259 1 6918 my ($self, $attributes, $offset) = @_;
68 1259 50       2955 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 12711 my ($self, $offset) = @_;
76 2518         4192 my $data = '';
77 2518         4179 for my $type (@{$self->type}) {
  2518         4967  
78 1483         6748 $data .= sprintf(qq{ \n},
79             $self->pixels($offset),
80             $type);
81             }
82 2518         9510 return $data;
83             }
84              
85             sub svg_coordinates {
86 1259     1259 1 6380 my ($self, $offset) = @_;
87 1259         2927 my $x = $self->x;
88 1259         6217 my $y = $self->y;
89 1259         6156 my $z = $self->z;
90 1259         5442 $y += $offset->[$z];
91 1259         2119 my $data = '';
92 1259         2819 $data .= qq{
93 1259         3520 $data .= sprintf(qq{ x="%.1f" y="%.1f"}, $self->pixels($offset, 0, -0.4 * $dy));
94 1259         2370 $data .= ' ';
95 1259   50     5771 $data .= $self->map->text_attributes || '';
96 1259         10848 $data .= '>';
97 1259         3617 $data .= Game::TextMapper::Point::coord($self->x, $self->y, "."); # original
98 1259         2769 $data .= qq{\n};
99 1259         4114 return $data;
100             }
101              
102             sub svg_label {
103 1259     1259 1 8863 my ($self, $url, $offset) = @_;
104 1259 100       2535 return '' unless defined $self->label;
105 5         46 my $attributes = $self->map->label_attributes;
106 5 50       58 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     35 $url =~ s/\%s/url_escape(encode_utf8($self->label))/e or $url .= url_escape(encode_utf8($self->label)) if $url;
  0         0  
112 5   50     26 my $data = sprintf(qq{ %s},
      50        
113             $self->pixels($offset, 0, 0.4 * $dy),
114             $attributes ||'',
115             $self->map->glow_attributes ||'',
116             $self->label);
117 5 50       126 $data .= qq{} if $url;
118 5   50     18 $data .= sprintf(qq{%s},
119             $self->pixels($offset, 0, 0.4 * $dy),
120             $attributes ||'',
121             $self->label);
122 5 50       60 $data .= qq{} if $url;
123 5         14 $data .= qq{\n};
124 5         26 return $data;
125             }
126              
127             1;