]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Set/Rectangle.pm
5c20fa8e95a0a769d3c84ce9cbfd085fa66ba3b9
[perl/modules/LaTeX-TikZ.git] / lib / LaTeX / TikZ / Set / Rectangle.pm
1 package LaTeX::TikZ::Set::Rectangle;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 LaTeX::TikZ::Set::Rectangle - A set object representing a rectangle.
9
10 =head1 VERSION
11
12 Version 0.01
13
14 =cut
15
16 our $VERSION = '0.01';
17
18 use LaTeX::TikZ::Set::Point;
19
20 use LaTeX::TikZ::Interface;
21 use LaTeX::TikZ::Functor;
22
23 use Any::Moose;
24
25 with 'LaTeX::TikZ::Set::Op';
26
27 has 'from' => (
28  is       => 'ro',
29  isa      => 'LaTeX::TikZ::Set::Point',
30  required => 1,
31  coerce   => 1,
32 );
33
34 has 'to' => (
35  is       => 'ro',
36  isa      => 'LaTeX::TikZ::Set::Point',
37  required => 1,
38  coerce   => 1,
39 );
40
41 has 'width' => (
42  is  => 'ro',
43  isa => 'Num',
44 );
45
46 has 'height' => (
47  is  => 'ro',
48  isa => 'Num',
49 );
50
51 sub path {
52  my $set = shift;
53
54  $set->from->path(@_) . ' rectangle ' . $set->to->path(@_);
55 }
56
57 my $meta = __PACKAGE__->meta;
58 my $tc1  = $meta->find_attribute_by_name('from')->type_constraint;
59 my $tc2  = $meta->find_attribute_by_name('to')->type_constraint;
60
61 around 'BUILDARGS' => sub {
62  my $orig  = shift;
63  my $class = shift;
64
65  if (@_ == 2 and $tc1->check($_[0]) and $tc2->check($_[1])) {
66   my ($from, $to) = @_;
67   @_ = (
68    from   => $from,
69    to     => $to,
70    width  => $to->x - $from->x,
71    height => $to->y - $from->y,
72   );
73  } else {
74   my %args = @_;
75   if (not exists $args{to} and exists $args{from}) {
76    confess(<<'   MSG') unless exists $args{width} and exists $args{height};
77 Attributes 'width' and 'height' are required when 'to' was not given
78    MSG
79    $args{from} = $tc1->coerce($args{from});
80    $meta->find_attribute_by_name($_)->type_constraint->assert_valid($args{$_})
81                                                       for qw/from width height/;
82    my $p = $args{from}->point;
83    $args{to} = LaTeX::TikZ::Point->new(
84     x => $p->x + $args{width},
85     y => $p->y + $args{height},
86    );
87    @_ = %args;
88   }
89  }
90
91  $class->$orig(@_);
92 };
93
94 LaTeX::TikZ::Interface->register(
95  rectangle => sub {
96   shift;
97   my ($p, $q) = @_;
98
99   my $is_relative = !blessed($q) && ref($q) eq 'HASH';
100
101   __PACKAGE__->new(
102    from => $p,
103    ($is_relative ? (map +($_ => $q->{$_}), qw/width height/) : (to => $q)),
104   );
105  },
106 );
107
108 LaTeX::TikZ::Functor->default_rule(
109  (__PACKAGE__) => sub {
110   my ($functor, $set, @args) = @_;
111   $set->new(map { $_ => $set->$_->$functor(@args) } qw/from to/)
112  }
113 );
114
115 __PACKAGE__->meta->make_immutable;
116
117 =head1 AUTHOR
118
119 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
120
121 You can contact me by mail or on C<irc.perl.org> (vincent).
122
123 =head1 BUGS
124
125 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>.
126 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
127
128 =head1 SUPPORT
129
130 You can find documentation for this module with the perldoc command.
131
132     perldoc LaTeX::TikZ
133
134 =head1 COPYRIGHT & LICENSE
135
136 Copyright 2010 Vincent Pit, all rights reserved.
137
138 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
139
140 =cut
141
142 1; # End of LaTeX::TikZ::Set::Rectangle