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