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::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
|
1
|
|
|
1
|
|
7
|
use Modern::Perl '2018'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
50
|
1
|
|
|
1
|
|
110
|
use Mojo::Util qw(url_escape); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
53
|
|
51
|
1
|
|
|
1
|
|
6
|
use Mojo::Base -base; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
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
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head1 METHODS |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head2 compute_missing_points |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
Compute the missing points between the points in C and return it. |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=cut |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub compute_missing_points { |
82
|
357
|
|
|
357
|
1
|
449
|
my $self = shift; |
83
|
357
|
|
|
|
|
417
|
my $i = 0; |
84
|
357
|
|
|
|
|
629
|
my $current = $self->points->[$i++]; |
85
|
357
|
|
|
|
|
1511
|
my $z = $current->z; |
86
|
357
|
|
|
|
|
1374
|
my @result = ($current); |
87
|
357
|
|
|
|
|
665
|
while ($self->points->[$i]) { |
88
|
2508
|
|
|
|
|
37699
|
$current = $self->one_step($current, $self->points->[$i]); |
89
|
2508
|
50
|
|
|
|
8531
|
return unless $z == $current->z; # must all be on the same plane |
90
|
2508
|
|
|
|
|
9243
|
push(@result, $current); |
91
|
2508
|
100
|
|
|
|
3875
|
$i++ if $current->equal($self->points->[$i]); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
357
|
|
|
|
|
6245
|
return @result; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub partway { |
98
|
5373
|
|
|
5373
|
0
|
7624
|
my ($self, $from, $to, $q) = @_; |
99
|
5373
|
|
|
|
|
8995
|
my ($x1, $y1) = $self->pixels($from); |
100
|
5373
|
|
|
|
|
9227
|
my ($x2, $y2) = $self->pixels($to); |
101
|
5373
|
|
50
|
|
|
8602
|
$q ||= 1; |
102
|
5373
|
50
|
|
|
|
7451
|
return $x1 + ($x2 - $x1) * $q, $y1 + ($y2 - $y1) * $q if wantarray; |
103
|
5373
|
|
|
|
|
26870
|
return sprintf("%.1f,%.1f", $x1 + ($x2 - $x1) * $q, $y1 + ($y2 - $y1) * $q); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head2 svg($offset) |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
This returns an SVG fragment, a string with a C. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=cut |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub svg { |
113
|
357
|
|
|
357
|
1
|
1390
|
my ($self, $offset) = @_; |
114
|
357
|
|
|
|
|
484
|
my ($path, $current, $next, $closed); |
115
|
357
|
|
|
|
|
788
|
$self->offset($offset); |
116
|
357
|
|
|
|
|
1708
|
my @points = $self->compute_missing_points(); |
117
|
357
|
50
|
|
|
|
679
|
return '' unless @points; |
118
|
357
|
50
|
|
|
|
777
|
if ($points[0]->equal($points[$#points])) { |
119
|
0
|
|
|
|
|
0
|
$closed = 1; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
357
|
50
|
|
|
|
2541
|
if ($closed) { |
123
|
0
|
|
|
|
|
0
|
for my $i (0 .. $#points - 1) { |
124
|
0
|
|
|
|
|
0
|
$current = $points[$i]; |
125
|
0
|
|
|
|
|
0
|
$next = $points[$i+1]; |
126
|
0
|
0
|
|
|
|
0
|
if (!$path) { |
127
|
0
|
|
|
|
|
0
|
my $a = $self->partway($current, $next, 0.3); |
128
|
0
|
|
|
|
|
0
|
my $b = $self->partway($current, $next, 0.5); |
129
|
0
|
|
|
|
|
0
|
my $c = $self->partway($points[$#points-1], $current, 0.7); |
130
|
0
|
|
|
|
|
0
|
my $d = $self->partway($points[$#points-1], $current, 0.5); |
131
|
0
|
|
|
|
|
0
|
$path = "M$d C$c $a $b"; |
132
|
|
|
|
|
|
|
} else { |
133
|
|
|
|
|
|
|
# continue curve |
134
|
0
|
|
|
|
|
0
|
my $a = $self->partway($current, $next, 0.3); |
135
|
0
|
|
|
|
|
0
|
my $b = $self->partway($current, $next, 0.5); |
136
|
0
|
|
|
|
|
0
|
$path .= " S$a $b"; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
} else { |
140
|
357
|
|
|
|
|
695
|
for my $i (0 .. $#points - 1) { |
141
|
2508
|
|
|
|
|
3202
|
$current = $points[$i]; |
142
|
2508
|
|
|
|
|
3018
|
$next = $points[$i+1]; |
143
|
2508
|
100
|
|
|
|
3490
|
if (!$path) { |
144
|
|
|
|
|
|
|
# line from a to b; control point a required for following S commands |
145
|
357
|
|
|
|
|
610
|
my $a = $self->partway($current, $next, 0.3); |
146
|
357
|
|
|
|
|
678
|
my $b = $self->partway($current, $next, 0.5); |
147
|
357
|
|
|
|
|
920
|
$path = "M$a C$b $a $b"; |
148
|
|
|
|
|
|
|
} else { |
149
|
|
|
|
|
|
|
# continue curve |
150
|
2151
|
|
|
|
|
3248
|
my $a = $self->partway($current, $next, 0.3); |
151
|
2151
|
|
|
|
|
3710
|
my $b = $self->partway($current, $next, 0.5); |
152
|
2151
|
|
|
|
|
4447
|
$path .= " S$a $b"; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
# end with a little stub |
156
|
357
|
|
|
|
|
616
|
$path .= " L" . $self->partway($current, $next, 0.7); |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
357
|
|
|
|
|
872
|
my $id = $self->id; |
160
|
357
|
|
|
|
|
1461
|
my $type = $self->type; |
161
|
357
|
|
|
|
|
1358
|
my $attributes = $self->map->path_attributes->{$type}; |
162
|
357
|
|
|
|
|
2644
|
my $data = qq{ \n}; |
163
|
357
|
50
|
|
|
|
770
|
$data .= $self->debug($closed) if $debug; |
164
|
357
|
|
|
|
|
2165
|
return $data; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head2 svg_label |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
This returns an SVG fragment, a group C with C and a C |
170
|
|
|
|
|
|
|
element. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=cut |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub svg_label { |
175
|
357
|
|
|
357
|
1
|
1129
|
my ($self) = @_; |
176
|
357
|
50
|
|
|
|
472
|
return '' unless defined $self->label; |
177
|
0
|
|
|
|
|
|
my $id = $self->id; |
178
|
0
|
|
|
|
|
|
my $label = $self->label; |
179
|
0
|
|
0
|
|
|
|
my $attributes = $self->map->label_attributes || ""; |
180
|
0
|
|
0
|
|
|
|
my $glow = $self->map->glow_attributes || ""; |
181
|
0
|
|
|
|
|
|
my $url = $self->map->url; |
182
|
0
|
0
|
0
|
|
|
|
$url =~ s/\%s/url_escape($self->label)/e or $url .= url_escape($self->label) if $url; |
|
0
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
# default is left, but if the line goes from right to left, then "left" means "upside down" |
184
|
0
|
|
|
|
|
|
my $side = ''; |
185
|
0
|
0
|
0
|
|
|
|
if ($self->points->[1]->x < $self->points->[0]->x |
|
|
|
0
|
|
|
|
|
186
|
0
|
|
|
|
|
|
or $#{$self->points} >= 2 and $self->points->[2]->x < $self->points->[0]->x) { |
187
|
0
|
|
|
|
|
|
$side = ' side="right"'; |
188
|
|
|
|
|
|
|
} |
189
|
0
|
|
|
|
|
|
my $data = qq{ \n}; |
190
|
0
|
0
|
|
|
|
|
$data .= qq{ $label\n} if $glow; |
191
|
0
|
0
|
|
|
|
|
$data .= qq{ } if $url; |
192
|
0
|
|
|
|
|
|
$data .= qq{ $label\n}; |
193
|
0
|
0
|
|
|
|
|
$data .= qq{ } if $url; |
194
|
0
|
|
|
|
|
|
$data .= qq{ \n}; |
195
|
0
|
|
|
|
|
|
return $data; |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
sub debug { |
199
|
0
|
|
|
0
|
0
|
|
my ($self, $closed) = @_; |
200
|
0
|
|
|
|
|
|
my ($data, $current, $next); |
201
|
0
|
|
|
|
|
|
my @points = $self->compute_missing_points(); |
202
|
0
|
|
|
|
|
|
for my $i (0 .. $#points - 1) { |
203
|
0
|
|
|
|
|
|
$current = $points[$i]; |
204
|
0
|
|
|
|
|
|
$next = $points[$i+1]; |
205
|
0
|
|
|
|
|
|
$data .= circle($self->pixels($current), 15, $i++); |
206
|
0
|
|
|
|
|
|
$data .= circle($self->partway($current, $next, 0.3), 3, 'a'); |
207
|
0
|
|
|
|
|
|
$data .= circle($self->partway($current, $next, 0.5), 5, 'b'); |
208
|
0
|
|
|
|
|
|
$data .= circle($self->partway($current, $next, 0.7), 3, 'c'); |
209
|
|
|
|
|
|
|
} |
210
|
0
|
|
|
|
|
|
$data .= circle($self->pixels($next), 15, $#points); |
211
|
|
|
|
|
|
|
|
212
|
0
|
|
|
|
|
|
my ($x, $y) = $self->pixels($points[0]); $y += 30; |
|
0
|
|
|
|
|
|
|
213
|
0
|
0
|
|
|
|
|
$data .= "
|
214
|
|
|
|
|
|
|
. "text-anchor='middle' dominant-baseline='central' " |
215
|
|
|
|
|
|
|
. "x='$x' y='$y'>closed" |
216
|
|
|
|
|
|
|
if $closed; |
217
|
|
|
|
|
|
|
|
218
|
0
|
|
|
|
|
|
return $data; |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
sub circle { |
222
|
0
|
|
|
0
|
0
|
|
my ($x, $y, $r, $i) = @_; |
223
|
0
|
|
|
|
|
|
my $data = ""; |
224
|
0
|
0
|
|
|
|
|
$data .= "
|
225
|
|
|
|
|
|
|
. "text-anchor='middle' dominant-baseline='central' " |
226
|
|
|
|
|
|
|
. "x='$x' y='$y'>$i" if $i; |
227
|
0
|
|
|
|
|
|
return "$data\n"; |
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=head1 SEE ALSO |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
Lines consist of L instances. |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
Use either L or L |
235
|
|
|
|
|
|
|
to implement lines. |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=cut |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
1; |