From: Vincent Pit Date: Tue, 1 Feb 2011 16:03:40 +0000 (+0100) Subject: Reimplement LT::Set::Polyline as a child class of LT::Set::Chain X-Git-Tag: rt87282~12 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FLaTeX-TikZ.git;a=commitdiff_plain;h=7f9973057cc57660599aa4a093718cf343a48198 Reimplement LT::Set::Polyline as a child class of LT::Set::Chain Also move the "cycle" attribute to LT::Set::Chain. --- diff --git a/lib/LaTeX/TikZ/Set/Arc.pm b/lib/LaTeX/TikZ/Set/Arc.pm index 0ef7e04..38fff77 100644 --- a/lib/LaTeX/TikZ/Set/Arc.pm +++ b/lib/LaTeX/TikZ/Set/Arc.pm @@ -72,7 +72,7 @@ LaTeX::TikZ::Interface->register( $set->clip( LaTeX::TikZ::Set::Polyline->new( points => [ $ua, @outside, $ub ], - closed => 1, + cycle => 1, ), ); }, diff --git a/lib/LaTeX/TikZ/Set/Chain.pm b/lib/LaTeX/TikZ/Set/Chain.pm index 2c640c2..d1022f8 100644 --- a/lib/LaTeX/TikZ/Set/Chain.pm +++ b/lib/LaTeX/TikZ/Set/Chain.pm @@ -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; @@ -116,6 +117,18 @@ has 'connector' => ( coerce => 1, ); +=head2 C + +A boolean that indicates whether the path is a cycle or not. + +=cut + +has 'cycle' => ( + is => 'ro', + isa => 'Bool', + default => 0, +); + =head1 METHODS =head2 C @@ -149,6 +162,12 @@ sub path { 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 = $kids[$i]; my $link = $set->$conn($i - 1, $prev, $next, $tikz); @@ -223,6 +242,7 @@ LaTeX::TikZ::Functor->default_rule( $set->new( kids => [ map $_->$functor(@args), $set->kids ], connector => $set->connector, + cycle => $set->cycle, ); } ); diff --git a/lib/LaTeX/TikZ/Set/Polyline.pm b/lib/LaTeX/TikZ/Set/Polyline.pm index 987c321..e8e0ee0 100644 --- a/lib/LaTeX/TikZ/Set/Polyline.pm +++ b/lib/LaTeX/TikZ/Set/Polyline.pm @@ -25,11 +25,11 @@ use Any::Moose 'Util::TypeConstraints'; =head1 RELATIONSHIPS -This class consumes the L role, and as such implements the L method. +This class is a subclass of L, and as such inherits its C method. =cut -with 'LaTeX::TikZ::Set::Path'; +extends 'LaTeX::TikZ::Set::Chain'; subtype 'LaTeX::TikZ::Set::Polyline::Vertices' => as 'ArrayRef[LaTeX::TikZ::Set::Point]' @@ -44,11 +44,11 @@ coerce 'LaTeX::TikZ::Set::Polyline::Vertices' =head2 C -The list of the successive vertices of the path. +The list of L objects (or scalars that coerce into such objects) that make the successive vertices of the path. =cut -has '_points' => ( +has '+_kids' => ( is => 'ro', isa => 'LaTeX::TikZ::Set::Polyline::Vertices', init_arg => 'points', @@ -56,58 +56,16 @@ has '_points' => ( coerce => 1, ); -sub points { @{$_[0]->_points} } +sub points { @{$_[0]->_kids} } -=head2 C +around 'BUILDARGS' => sub { + my ($orig, $class, %args) = @_; -A boolean that indicates whether the path is closed or not. - -=cut - -has 'closed' => ( - is => 'ro', - isa => 'Bool', - default => 0, -); - -=head1 METHODS - -=head2 C - -=cut - -sub path { - my $set = shift; - - join ' -- ', map($_->path(@_), $set->points), - ($set->closed ? 'cycle' : ()); -} - -=head2 C - -=cut - -sub begin { - my $set = shift; - - my @points = $set->points; - return undef unless @points; - - $points[0]->begin; -} - -=head2 C - -=cut - -sub end { - my $set = shift; - - my @points = $set->points; - return undef unless @points; - - $points[-1]->end; -} + $class->$orig( + %args, + connector => '--', + ); +}; LaTeX::TikZ::Interface->register( polyline => sub { @@ -118,25 +76,18 @@ LaTeX::TikZ::Interface->register( closed_polyline => sub { shift; - __PACKAGE__->new(points => \@_, closed => 1); - }, -); - -LaTeX::TikZ::Functor->default_rule( - (__PACKAGE__) => sub { - my ($functor, $set, @args) = @_; - $set->new( - points => [ map $_->$functor(@args), $set->points ], - closed => $set->closed, + __PACKAGE__->new( + points => \@_, + cycle => 1 ); - } + }, ); __PACKAGE__->meta->make_immutable; =head1 SEE ALSO -L, L. +L, L. =head1 AUTHOR