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