]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blobdiff - lib/LaTeX/TikZ/Set/Chain.pm
Reimplement LT::Set::Polyline as a child class of LT::Set::Chain
[perl/modules/LaTeX-TikZ.git] / lib / LaTeX / TikZ / Set / Chain.pm
index 8795de1bb15bfbe0197a9821508f4a7e1e09ef0f..d1022f842d4527513b3b9745a6a83b8d233f7bdf 100644 (file)
@@ -16,6 +16,7 @@ Version 0.02
 our $VERSION = '0.02';
 
 use LaTeX::TikZ::Set::Point;
+use LaTeX::TikZ::Set::Raw;
 
 use LaTeX::TikZ::Interface;
 use LaTeX::TikZ::Functor;
@@ -68,7 +69,7 @@ sub kids { @{$_[0]->_kids} }
 =head2 C<connector>
 
 A code reference that describes how two successive elements of the chain are linked.
-It is called repeatedly with these arguments :
+When the L</path> method is , the connector is run repeatedly with these arguments :
 
 =over 4
 
@@ -88,6 +89,10 @@ The C<$i>-th L<LaTeX::TikZ::Set> object in the chain.
 
 The C<$i+1>-th L<LaTeX::TikZ::Set> object in the chain.
 
+=item *
+
+The L<LaTeX::TikZ::Formatter> object.
+
 =back
 
 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.
@@ -112,11 +117,16 @@ has 'connector' => (
  coerce   => 1,
 );
 
-has '_links' => (
- is       => 'ro',
- isa      => 'ArrayRef[Str]',
- init_arg => undef,
- default  => sub { [ ] },
+=head2 C<cycle>
+
+A boolean that indicates whether the path is a cycle or not.
+
+=cut
+
+has 'cycle' => (
+ is      => 'ro',
+ isa     => 'Bool',
+ default => 0,
 );
 
 =head1 METHODS
@@ -131,68 +141,70 @@ sub add {
  my $set = shift;
 
  $ltsp_tc->assert_valid($_) for @_;
- return $set unless @_;
-
- my $kids  = $set->_kids;
- my $links = $set->_links;
- my $conn  = $set->connector;
 
- push @$kids, shift @_ unless @$kids;
- return $set unless @_;
-
- my $prev  = $kids->[-1];
- for my $i (0 .. $#_) {
-  my $next = $_[$i];
-  my $link = $set->$conn($#$kids, $prev, $next);
-  confess('Invalid connector') unless defined $link and not blessed $link;
-  push @$links, $link;
-  push @$kids,  $next;
-  $prev = $next;
- }
+ push @{$set->_kids}, @_;
 
  $set;
 }
 
-sub BUILD {
- my ($set) = @_;
+=head2 C<path>
+
+=cut
+
+sub path {
+ my ($set, $tikz) = @_;
 
  my @kids  = $set->kids;
- return unless @kids >= 2;
+ return '' unless @kids;
 
- my $links = $set->_links;
  my $conn  = $set->connector;
 
  my $prev  = $kids[0];
+ my $path  = $prev->path($tikz);
+
+ if ($set->cycle) {
+  push @kids, LaTeX::TikZ::Set::Raw->new(
+   content => 'cycle',
+  );
+ }
+
  for my $i (1 .. $#kids) {
-  my $next = $_[$i];
-  my $link = $set->$conn($i - 1, $prev, $next);
+  my $next = $kids[$i];
+  my $link = $set->$conn($i - 1, $prev, $next, $tikz);
   confess('Invalid connector') unless defined $link and not blessed $link;
-  push @$links, $link;
-  $prev = $next;
+  $link    = " $link ";
+  $link    =~ s/\s+/ /g;
+  $path   .= $link . $next->path($tikz);
+  $prev    = $next;
  }
+
+ return $path;
 }
 
-=head2 C<path>
+=head2 C<begin>
 
 =cut
 
-sub path {
+sub begin {
  my $set = shift;
 
- my @kids  = $set->kids;
- return '' unless @kids;
+ my @kids = $set->kids;
+ return undef unless @kids;
 
my $links = $set->_links;
- my $conn  = $set->connector;
$kids[0]->begin;
+}
 
- my $path = $kids[0]->path(@_);
- for my $i (1 .. $#kids) {
-  my $link = ' ' . $links->[$i - 1] . ' ';
-  $link    =~ s/\s+/ /g;
-  $path   .= $link . $kids[$i]->path(@_);
- }
+=head2 C<end>
 
- return $path;
+=cut
+
+sub end {
+ my $set = shift;
+
+ my @kids = $set->kids;
+ return undef unless @kids;
+
+ $kids[-1]->end;
 }
 
 LaTeX::TikZ::Interface->register(
@@ -230,6 +242,7 @@ LaTeX::TikZ::Functor->default_rule(
   $set->new(
    kids      => [ map $_->$functor(@args), $set->kids ],
    connector => $set->connector,
+   cycle     => $set->cycle,
   );
  }
 );