Jacquard/Layout/Grid.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Jacquard/Actor/Role/DataPrinter.pm ↑ Summary Jacquard/Actor.pm »
1 1 ▶use Renard::Incunabula::Common::Setup;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Intertangle::Jacquard::Layout::Grid;
3 # ABSTRACT: Grid layout
4 $Intertangle::Jacquard::Layout::Grid::VERSION = '0.001';
5 1 ▶use Mu;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Intertangle::Punchcard::Backend::Kiwisolver::Context;
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use List::AllUtils qw(reduce);
8  
9 ro 'rows'; # TODO PositiveInt
10 ro 'columns'; # TODO PositiveInt
11  
12 lazy context => sub {
13 Intertangle::Punchcard::Backend::Kiwisolver::Context->new;
14 };
15  
16 method _item_no_to_rc($item_no) {
17 ( int($item_no / $self->columns), $item_no % $self->columns );
18 }
19 method _rc_to_item_no($r, $c) {
20 $r * $self->columns + $c;
21 }
22  
23 method create_constraints($actor) {
24 my $items = $actor->children;
25  
26 my @constraints;
27  
28 my @rows_constraints = map { $self->context->new_variable( name => "row.$_" ) } (0..$self->rows-1);
29 my @cols_constraints = map { $self->context->new_variable( name => "col.$_" ) } (0..$self->columns-1);
30  
31 push @constraints, $_ >= 0 for @rows_constraints;
32 push @constraints, $_ >= 0 for @cols_constraints;
33  
34 for my $item_no (0..@$items-1) {
35 my ($row, $col) = $self->_item_no_to_rc($item_no);
36 my $this_item = $items->[$item_no];
37  
38 push @constraints, $this_item->x >= 0;
39 push @constraints, $this_item->y >= 0;
40 push @constraints, $cols_constraints[$col] >= (ref $this_item->width ? $this_item->width->value : $this_item->width);
41 push @constraints, $rows_constraints[$row] >= (ref $this_item->height ? $this_item->height->value : $this_item->height);
42  
43 if( $col > 0 ) {
44 my $item_left0 = $items->[$self->_rc_to_item_no($row,$col-1)];
45 push @constraints, $item_left0->x + $cols_constraints[$col-1] == $this_item->x;
46 }
47 if( $row > 0 ) {
48 my $item_above = $items->[$self->_rc_to_item_no($row-1,$col)];
49 push @constraints, $item_above->y + $rows_constraints[$row-1] == $this_item->y;
50 }
51 }
52  
53 push @constraints, $actor->width == reduce { $a + $b } @cols_constraints;
54 push @constraints, $actor->height == reduce { $a + $b } @rows_constraints;
55  
56 \@constraints;
57 }
58  
59 has _constraints => (
60 is => 'rw',
61 predicate => 1,
62 );
63  
64 method update($actor) {
65 my $solver = $self->context->solver;
66 my $items = $actor->children;
67 my $first_item = $items->[0];
68  
69 if( ! $self->_has_constraints ) {
70 my $constraints = $self->create_constraints( $actor );
71 $self->_constraints( $constraints );
72  
73 for my $constraint (@$constraints) {
74 $solver->add_constraint($constraint);
75 }
76 $solver->add_edit_variable($first_item->x, Intertangle::API::Kiwisolver::Strength::STRONG );
77 $solver->add_edit_variable($first_item->y, Intertangle::API::Kiwisolver::Strength::STRONG );
78 }
79  
80 #$solver->suggest_value($first_item->x, $actor->x->value);
81 #$solver->suggest_value($first_item->y, $actor->y->value);
82 $solver->suggest_value($first_item->x, 0);
83 $solver->suggest_value($first_item->y, 0);
84 $solver->update;
85 }
86  
87 1;
88  
89 __END__
90  
91 =pod
92  
93 =encoding UTF-8
94  
95 =head1 NAME
96  
97 Intertangle::Jacquard::Layout::Grid - Grid layout
98  
99 =head1 VERSION
100  
101 version 0.001
102  
103 =head1 EXTENDS
104  
105 =over 4
106  
107 =item * L<Moo::Object>
108  
109 =back
110  
111 =head1 ATTRIBUTES
112  
113 =head2 rows columns
114  
115 Number of rows / columns.
116  
117 =head1 METHODS
118  
119 =head2 create_constraints
120  
121 ...
122  
123 =head2 update
124  
125 ...
126  
127 =head1 AUTHOR
128  
129 Zakariyya Mughal <zmughal@cpan.org>
130  
131 =head1 COPYRIGHT AND LICENSE
132  
133 This software is copyright (c) 2017 by Zakariyya Mughal.
134  
135 This is free software; you can redistribute it and/or modify it under
136 the same terms as the Perl 5 programming language system itself.
137  
138 =cut
« Jacquard/Actor/Role/DataPrinter.pm ↑ Summary Jacquard/Actor.pm »