]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Set/Chain.pm
2c640c2821495cddc51ab9cb7a3e3db69bb21035
[perl/modules/LaTeX-TikZ.git] / lib / LaTeX / TikZ / Set / Chain.pm
1 package LaTeX::TikZ::Set::Chain;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 LaTeX::TikZ::Set::Chain - A set object representing a connected path between several objects.
9
10 =head1 VERSION
11
12 Version 0.02
13
14 =cut
15
16 our $VERSION = '0.02';
17
18 use LaTeX::TikZ::Set::Point;
19
20 use LaTeX::TikZ::Interface;
21 use LaTeX::TikZ::Functor;
22
23 use LaTeX::TikZ::Tools;
24
25 use Any::Moose;
26 use Any::Moose 'Util::TypeConstraints' => [ qw<subtype as coerce from via> ];
27
28 =head1 RELATIONSHIPS
29
30 This class consumes the L<LaTeX::TikZ::Set::Path> and L<LaTeX::TikZ::Set::Container> roles, and as such implements the L</path>, L</kids> and L</add> methods.
31
32 =cut
33
34 with qw<
35  LaTeX::TikZ::Set::Path
36  LaTeX::TikZ::Set::Container
37 >;
38
39 =head1 ATTRIBUTES
40
41 =head2 C<kids>
42
43 The L<LaTeX::TikZ::Set::Path> objects that form the chain.
44
45 =cut
46
47 subtype 'LaTeX::TikZ::Set::Chain::Elements'
48      => as 'ArrayRef[LaTeX::TikZ::Set::Path]';
49
50 coerce 'LaTeX::TikZ::Set::Chain::Elements'
51     => from 'ArrayRef[Any]'
52     => via { [ map {
53         blessed($_) && $_->does('LaTeX::TikZ::Set')
54           ? $_
55           : LaTeX::TikZ::Set::Point->new(point => $_)
56        } @$_ ] };
57
58 has '_kids' => (
59  is       => 'ro',
60  isa      => 'LaTeX::TikZ::Set::Chain::Elements',
61  init_arg => 'kids',
62  default  => sub { [ ] },
63  coerce   => 1,
64 );
65
66 sub kids { @{$_[0]->_kids} }
67
68 =head2 C<connector>
69
70 A code reference that describes how two successive elements of the chain are linked.
71 When the L</path> method is , the connector is run repeatedly with these arguments :
72
73 =over 4
74
75 =item *
76
77 The current L<LaTeX::TikZ::Set::Chain> object.
78
79 =item *
80
81 The index C<$i> of the current position in the chain, starting at C<0> for the link between the two first elements.
82
83 =item *
84
85 The C<$i>-th L<LaTeX::TikZ::Set> object in the chain.
86
87 =item *
88
89 The C<$i+1>-th L<LaTeX::TikZ::Set> object in the chain.
90
91 =item *
92
93 The L<LaTeX::TikZ::Formatter> object.
94
95 =back
96
97 You can also pass a string, which will be upgraded to a code reference constantly returning that string ; or an array reference, which will be turned into a code reference returning the C<$i>-th element of the array when asked for the C<$i>-th link.
98
99 =cut
100
101 subtype 'LaTeX::TikZ::Set::Chain::Connector'
102      => as 'CodeRef';
103
104 coerce 'LaTeX::TikZ::Set::Chain::Connector'
105     => from 'Str'
106     => via { my $conn = $_; sub { $conn } };
107
108 coerce 'LaTeX::TikZ::Set::Chain::Connector'
109     => from 'ArrayRef[Str]'
110     => via { my $conns = $_; sub { $conns->[$_[1]] } };
111
112 has 'connector' => (
113  is       => 'ro',
114  isa      => 'LaTeX::TikZ::Set::Chain::Connector',
115  required => 1,
116  coerce   => 1,
117 );
118
119 =head1 METHODS
120
121 =head2 C<add>
122
123 =cut
124
125 my $ltsp_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Set::Path');
126
127 sub add {
128  my $set = shift;
129
130  $ltsp_tc->assert_valid($_) for @_;
131
132  push @{$set->_kids}, @_;
133
134  $set;
135 }
136
137 =head2 C<path>
138
139 =cut
140
141 sub path {
142  my ($set, $tikz) = @_;
143
144  my @kids  = $set->kids;
145  return '' unless @kids;
146
147  my $conn  = $set->connector;
148
149  my $prev  = $kids[0];
150  my $path  = $prev->path($tikz);
151
152  for my $i (1 .. $#kids) {
153   my $next = $kids[$i];
154   my $link = $set->$conn($i - 1, $prev, $next, $tikz);
155   confess('Invalid connector') unless defined $link and not blessed $link;
156   $link    = " $link ";
157   $link    =~ s/\s+/ /g;
158   $path   .= $link . $next->path($tikz);
159   $prev    = $next;
160  }
161
162  return $path;
163 }
164
165 =head2 C<begin>
166
167 =cut
168
169 sub begin {
170  my $set = shift;
171
172  my @kids = $set->kids;
173  return undef unless @kids;
174
175  $kids[0]->begin;
176 }
177
178 =head2 C<end>
179
180 =cut
181
182 sub end {
183  my $set = shift;
184
185  my @kids = $set->kids;
186  return undef unless @kids;
187
188  $kids[-1]->end;
189 }
190
191 LaTeX::TikZ::Interface->register(
192  join => sub {
193   shift;
194   my $conn = shift;
195
196   __PACKAGE__->new(
197    kids      => \@_,
198    connector => $conn,
199   );
200  },
201  chain => sub {
202   shift;
203   confess("The 'chain' command expects an odd number of arguments")
204                                                                   unless @_ % 2;
205
206   my @kids = shift;
207   my @links;
208   for (my $i = 0; $i < @_; $i += 2) {
209    push @links, $_[$i];
210    push @kids,  $_[$i + 1];
211   }
212
213   __PACKAGE__->new(
214    kids      => \@kids,
215    connector => \@links,
216   );
217  }
218 );
219
220 LaTeX::TikZ::Functor->default_rule(
221  (__PACKAGE__) => sub {
222   my ($functor, $set, @args) = @_;
223   $set->new(
224    kids      => [ map $_->$functor(@args), $set->kids ],
225    connector => $set->connector,
226   );
227  }
228 );
229
230 __PACKAGE__->meta->make_immutable;
231
232 =head1 SEE ALSO
233
234 L<LaTeX::TikZ>, L<LaTeX::TikZ::Set::Path>.
235
236 =head1 AUTHOR
237
238 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
239
240 You can contact me by mail or on C<irc.perl.org> (vincent).
241
242 =head1 BUGS
243
244 Please report any bugs or feature requests to C<bug-latex-tikz at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=LaTeX-TikZ>.
245 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
246
247 =head1 SUPPORT
248
249 You can find documentation for this module with the perldoc command.
250
251     perldoc LaTeX::TikZ
252
253 =head1 COPYRIGHT & LICENSE
254
255 Copyright 2011 Vincent Pit, all rights reserved.
256
257 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
258
259 =cut
260
261 1; # End of LaTeX::TikZ::Set::Chain