File Coverage

blib/lib/Game/TextMapper/Point/Square.pm
Criterion Covered Total %
statement 56 60 93.3
branch 6 12 50.0
condition 4 11 36.3
subroutine 9 9 100.0
pod 4 4 100.0
total 79 96 82.2


line stmt bran cond sub pod time code
1             # Copyright (C) 2009-2021 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 1     1   7 use Game::TextMapper::Constants qw($dy);
  1         2  
  1         72  
48              
49 1     1   6 use Game::TextMapper::Point;
  1         2  
  1         7  
50 1     1   26 use Modern::Perl '2018';
  1         1  
  1         12  
51 1     1   210 use Mojo::Util qw(url_escape);
  1         2  
  1         50  
52 1     1   5 use Mojo::Base 'Game::TextMapper::Point';
  1         9  
  1         8  
53              
54             sub svg_region {
55 1136     1136 1 3944 my ($self, $attributes, $offset) = @_;
56 1136         1673 return sprintf(qq{ \n},
57             $self->x, $self->y, $self->z,
58             ($self->x - 0.5) * $dy,
59             ($self->y + $offset->[$self->z] - 0.5) * $dy,
60             $dy, $dy, $attributes);
61             }
62              
63             sub svg {
64 2272     2272 1 7399 my ($self, $offset) = @_;
65 2272         3436 my $x = $self->x;
66 2272         7709 my $y = $self->y;
67 2272         7639 my $z = $self->z;
68 2272         7388 $y += $offset->[$z];
69 2272         2737 my $data = '';
70 2272         2372 for my $type (@{$self->type}) {
  2272         3245  
71 1493         10174 $data .= sprintf(qq{ \n},
72             $x * $dy,
73             $y * $dy, # square
74             $type);
75             }
76 2272         6767 return $data;
77             }
78              
79             sub svg_coordinates {
80 1136     1136 1 3931 my ($self, $offset) = @_;
81 1136         1704 my $x = $self->x;
82 1136         4384 my $y = $self->y;
83 1136         3871 my $z = $self->z;
84 1136         4069 $y += $offset->[$z];
85 1136         1415 my $data = '';
86 1136         1613 $data .= qq{
87 1136         5271 $data .= sprintf(qq{ x="%.1f" y="%.1f"},
88             $x * $dy,
89             ($y - 0.4) * $dy); # square
90 1136         1425 $data .= ' ';
91 1136   50     1835 $data .= $self->map->text_attributes || '';
92 1136         6476 $data .= '>';
93 1136         1909 $data .= Game::TextMapper::Point::coord($self->x, $self->y, "."); # original
94 1136         2125 $data .= qq{\n};
95 1136         2857 return $data;
96             }
97              
98             sub svg_label {
99 1136     1136 1 5453 my ($self, $url, $offset) = @_;
100 1136 100       1643 return '' unless defined $self->label;
101 5         26 my $attributes = $self->map->label_attributes;
102 5 50       31 if ($self->size) {
103 0 0       0 if (not $attributes =~ s/\bfont-size="\d+pt"/'font-size="' . $self->size . 'pt"'/e) {
  0         0  
104 0         0 $attributes .= ' font-size="' . $self->size . '"';
105             }
106             }
107 5 50 0     21 $url =~ s/\%s/url_escape($self->label)/e or $url .= url_escape($self->label) if $url;
  0         0  
108 5         11 my $x = $self->x;
109 5         18 my $y = $self->y;
110 5         18 my $z = $self->z;
111 5         20 $y += $offset->[$z];
112 5   50     13 my $data = sprintf(qq{ }
      50        
113             . $self->label
114             . qq{},
115             $x * $dy,
116             ($y + 0.4) * $dy, # square
117             $attributes ||'',
118             $self->map->glow_attributes ||'');
119 5 50       80 $data .= qq{} if $url;
120 5   50     13 $data .= sprintf(qq{}
121             . $self->label
122             . qq{},
123             $x * $dy,
124             ($y + 0.4) * $dy, # square
125             $attributes ||'');
126 5 50       45 $data .= qq{} if $url;
127 5         8 $data .= qq{\n};
128 5         17 return $data;
129             }
130              
131             1;