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