]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Set/Rectangle.pm
Remove magic LaTeX::TikZ::Interface->import
[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   @_ = (
67    from => $_[0],
68    to   => $_[1],
69   );
70  } else {
71   my %args = @_;
72   if (not exists $args{to} and exists $args{from}) {
73    confess(<<'   MSG') unless exists $args{width} and exists $args{height};
74 Attributes 'width' and 'height' are required when 'to' was not given
75    MSG
76    $args{from} = $tc1->coerce($args{from});
77    $meta->find_attribute_by_name($_)->type_constraint->assert_valid($args{$_})
78                                                       for qw/from width height/;
79    my $p = $args{from}->point;
80    $args{to} = LaTeX::TikZ::Point->new(
81     x => $p->x + $args{width},
82     y => $p->y + $args{height},
83    );
84    @_ = %args;
85   }
86  }
87
88  $class->$orig(@_);
89 };
90
91 LaTeX::TikZ::Interface->register(
92  rectangle => sub {
93   shift;
94   my ($p, $q) = @_;
95
96   my $is_relative = !blessed($q) && ref($q) eq 'HASH';
97
98   __PACKAGE__->new(
99    from => $p,
100    ($is_relative ? (map +($_ => $q->{$_}), qw/width height/) : (to => $q)),
101   );
102  },
103 );
104
105 LaTeX::TikZ::Functor->default_rule(
106  (__PACKAGE__) => sub {
107   my ($functor, $set, @args) = @_;
108   $set->new(map { $_ => $set->$_->$functor(@args) } qw/from to/)
109  }
110 );
111
112 __PACKAGE__->meta->make_immutable;
113
114 =head1 AUTHOR
115
116 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
117
118 You can contact me by mail or on C<irc.perl.org> (vincent).
119
120 =head1 BUGS
121
122 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>.
123 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
124
125 =head1 SUPPORT
126
127 You can find documentation for this module with the perldoc command.
128
129     perldoc LaTeX::TikZ
130
131 =head1 COPYRIGHT & LICENSE
132
133 Copyright 2010 Vincent Pit, all rights reserved.
134
135 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
136
137 =cut
138
139 1; # End of LaTeX::TikZ::Set::Rectangle