File Coverage

blib/lib/Game/TextMapper/Line.pm
Criterion Covered Total %
statement 52 105 49.5
branch 11 38 28.9
condition 1 15 6.6
subroutine 7 9 77.7
pod 3 6 50.0
total 74 173 42.7


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::Line - a line between two points
21              
22             =head1 SYNOPSIS
23              
24             use Modern::Perl;
25             use Game::TextMapper::Line::Hex;
26             use Game::TextMapper::Point::Hex;
27             my $line = Game::TextMapper::Line::Hex->new();
28             my $from = Game::TextMapper::Point::Hex->new(x => 1, y => 1, z => 0);
29             my $to = Game::TextMapper::Point::Hex->new(x => 5, y => 3, z => 0);
30             $line->points([$from, $to]);
31             my @line = $line->compute_missing_points;
32             say join(" ", map { $_->str } @line);
33             # (1,1,0) (2,1,0) (3,2,0) (4,2,0) (5,3,0)
34              
35             =head1 DESCRIPTION
36              
37             The line connects two points. This class knows how to compute all the regions
38             between these two points, how to compute the next region along the line, and how
39             to output SVG.
40              
41             In order to do this, the class needs to know how to work with the regions on the
42             map. This is different for hexes and squares. Therefore you should always be
43             using the appropriate Hex or Square class instead.
44              
45             =cut
46              
47             package Game::TextMapper::Line;
48              
49 11     11   69 use Modern::Perl '2018';
  11         19  
  11         63  
50 11     11   3257 use Mojo::Util qw(url_escape);
  11         37  
  11         649  
51 11     11   52 use Mojo::Base -base;
  11         14  
  11         81  
52              
53             our $debug;
54              
55             =head1 ATTRIBUTES
56              
57             =head2 points
58              
59             An array reference of points using a class derived from
60             L, i.e. L uses
61             L and L uses
62             L.
63              
64             =cut
65              
66             has 'id';
67             has 'points';
68             has 'offset';
69             has 'type';
70             has 'label';
71             has 'map';
72             has 'side';
73             has 'start';
74              
75             =head1 METHODS
76              
77             =head2 compute_missing_points
78              
79             Compute the missing points between the points in C and return it.
80              
81             =cut
82              
83             sub compute_missing_points {
84 321     321 1 642 my $self = shift;
85 321         517 my $i = 0;
86 321         739 my $current = $self->points->[$i++];
87 321         1548 my $z = $current->z;
88 321         1425 my @result = ($current);
89 321         712 while ($self->points->[$i]) {
90 1820         38116 $current = $self->one_step($current, $self->points->[$i]);
91 1820 50       6582 return unless $z == $current->z; # must all be on the same plane
92 1820         9647 push(@result, $current);
93 1820 100       4449 $i++ if $current->equal($self->points->[$i]);
94             }
95              
96 321         7597 return @result;
97             }
98              
99             sub partway {
100 3961     3961 0 8037 my ($self, $from, $to, $q) = @_;
101 3961         9666 my ($x1, $y1) = $self->pixels($from);
102 3961         9510 my ($x2, $y2) = $self->pixels($to);
103 3961   50     8514 $q ||= 1;
104 3961 50       7436 return $x1 + ($x2 - $x1) * $q, $y1 + ($y2 - $y1) * $q if wantarray;
105 3961         28497 return sprintf("%.1f,%.1f", $x1 + ($x2 - $x1) * $q, $y1 + ($y2 - $y1) * $q);
106             }
107              
108             =head2 svg($offset)
109              
110             This returns an SVG fragment, a string with a C.
111              
112             =cut
113              
114             sub svg {
115 321     321 1 1749 my ($self, $offset) = @_;
116 321         508 my ($path, $current, $next, $closed);
117 321         802 $self->offset($offset);
118 321         2195 my @points = $self->compute_missing_points();
119 321 50       863 return '' unless @points;
120 321 50       920 if ($points[0]->equal($points[$#points])) {
121 0         0 $closed = 1;
122             }
123              
124 321 50       3092 if ($closed) {
125 0         0 for my $i (0 .. $#points - 1) {
126 0         0 $current = $points[$i];
127 0         0 $next = $points[$i+1];
128 0 0       0 if (!$path) {
129 0         0 my $a = $self->partway($current, $next, 0.3);
130 0         0 my $b = $self->partway($current, $next, 0.5);
131 0         0 my $c = $self->partway($points[$#points-1], $current, 0.7);
132 0         0 my $d = $self->partway($points[$#points-1], $current, 0.5);
133 0         0 $path = "M$d C$c $a $b";
134             } else {
135             # continue curve
136 0         0 my $a = $self->partway($current, $next, 0.3);
137 0         0 my $b = $self->partway($current, $next, 0.5);
138 0         0 $path .= " S$a $b";
139             }
140             }
141             } else {
142 321         903 for my $i (0 .. $#points - 1) {
143 1820         3213 $current = $points[$i];
144 1820         3195 $next = $points[$i+1];
145 1820 100       3513 if (!$path) {
146             # line from a to b; control point a required for following S commands
147 321         797 my $a = $self->partway($current, $next, 0.3);
148 321         879 my $b = $self->partway($current, $next, 0.5);
149 321         955 $path = "M$a C$b $a $b";
150             } else {
151             # continue curve
152 1499         3340 my $a = $self->partway($current, $next, 0.3);
153 1499         3567 my $b = $self->partway($current, $next, 0.5);
154 1499         4666 $path .= " S$a $b";
155             }
156             }
157             # end with a little stub
158 321         710 $path .= " L" . $self->partway($current, $next, 0.7);
159             }
160              
161 321         897 my $id = $self->id;
162 321         1648 my $type = $self->type;
163 321         1521 my $attributes = $self->map->path_attributes->{$type};
164 321         3399 my $data = qq{ \n};
165 321 50       765 $data .= $self->debug($closed) if $debug;
166 321         3267 return $data;
167             }
168              
169             =head2 svg_label
170              
171             This returns an SVG fragment, a group C with C and a C
172             element.
173              
174             =cut
175              
176             sub svg_label {
177 321     321 1 1109 my ($self) = @_;
178 321 50       443 return '' unless defined $self->label;
179 0           my $id = $self->id;
180 0           my $label = $self->label;
181 0   0       my $attributes = $self->map->label_attributes || "";
182 0   0       my $glow = $self->map->glow_attributes || "";
183 0           my $url = $self->map->url;
184 0 0 0       $url =~ s/\%s/url_escape($self->label)/e or $url .= url_escape($self->label) if $url;
  0            
185             # Default side is left, but if the line goes from right to left, then "left"
186             # means "upside down", so allow people to control it.
187 0           my $pathAttributes = '';
188 0 0 0       if ($self->side) {
    0 0        
189 0           $pathAttributes = ' side="' . $self->side . '"';
190             } elsif ($self->points->[1]->x < $self->points->[0]->x
191 0           or $#{$self->points} >= 2 and $self->points->[2]->x < $self->points->[0]->x) {
192 0           $pathAttributes = ' side="right"';
193             }
194 0 0         if ($self->start) {
195 0           $pathAttributes .= ' startOffset="' . $self->start . '"';
196             }
197 0           my $data = qq{ \n};
198 0 0         $data .= qq{ $label\n} if $glow;
199 0 0         $data .= qq{ } if $url;
200 0           $data .= qq{ $label\n};
201 0 0         $data .= qq{ } if $url;
202 0           $data .= qq{ \n};
203 0           return $data;
204             }
205              
206             sub debug {
207 0     0 0   my ($self, $closed) = @_;
208 0           my ($data, $current, $next);
209 0           my @points = $self->compute_missing_points();
210 0           for my $i (0 .. $#points - 1) {
211 0           $current = $points[$i];
212 0           $next = $points[$i+1];
213 0           $data .= circle($self->pixels($current), 15, $i++);
214 0           $data .= circle($self->partway($current, $next, 0.3), 3, 'a');
215 0           $data .= circle($self->partway($current, $next, 0.5), 5, 'b');
216 0           $data .= circle($self->partway($current, $next, 0.7), 3, 'c');
217             }
218 0           $data .= circle($self->pixels($next), 15, $#points);
219              
220 0           my ($x, $y) = $self->pixels($points[0]); $y += 30;
  0            
221 0 0         $data .= "
222             . "text-anchor='middle' dominant-baseline='central' "
223             . "x='$x' y='$y'>closed"
224             if $closed;
225              
226 0           return $data;
227             }
228              
229             sub circle {
230 0     0 0   my ($x, $y, $r, $i) = @_;
231 0           my $data = "";
232 0 0         $data .= "
233             . "text-anchor='middle' dominant-baseline='central' "
234             . "x='$x' y='$y'>$i" if $i;
235 0           return "$data\n";
236             }
237              
238             =head1 SEE ALSO
239              
240             Lines consist of L instances.
241              
242             Use either L or L
243             to implement lines.
244              
245             =cut
246              
247             1;