| 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 | 1 | $Intertangle::Jacquard::Layout::Grid::VERSION = '0.002'; | |||||||||
| 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=0.0
| |||||||||||
| 7 | 1 | ▶ | use List::AllUtils qw(reduce); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 1 | ro 'rows'; # TODO PositiveInt | |||||||||
| 10 | 1 | ro 'columns'; # TODO PositiveInt | |||||||||
| 11 | |||||||||||
| 12 | 0 | ▶ | lazy context => sub { | ||||||||
|
Subroutine
_build_context: not called CC=1 SCAR=6.9
| |||||||||||
| 13 | 0 | Intertangle::Punchcard::Backend::Kiwisolver::Context->new; | |||||||||
| 14 | 1 | }; | |||||||||
| 15 | |||||||||||
| 16 | 0 | ▶ | method _item_no_to_rc($item_no) { | ||||||||
|
Subroutine
_item_no_to_rc: not called CC=1 SCAR=6.9
| |||||||||||
| 17 | 0 | ( int($item_no / $self->columns), $item_no % $self->columns ); | |||||||||
| 18 | 1 | } | |||||||||
| 19 | 0 | ▶ | method _rc_to_item_no($r, $c) { | ||||||||
|
Subroutine
_rc_to_item_no: not called CC=1 SCAR=6.9
| |||||||||||
| 20 | 0 | $r * $self->columns + $c; | |||||||||
| 21 | 1 | } | |||||||||
| 22 | |||||||||||
| 23 | 0 | ▶ | method create_constraints($actor) { | ||||||||
|
Subroutine
create_constraints: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||
| 24 | 0 | my $items = $actor->children; | |||||||||
| 25 | |||||||||||
| 26 | 0 | my @constraints; | |||||||||
| 27 | |||||||||||
| 28 | 0 | my @rows_constraints = map { $self->context->new_variable( name => "row.$_" ) } (0..$self->rows-1); | |||||||||
| 29 | 0 | my @cols_constraints = map { $self->context->new_variable( name => "col.$_" ) } (0..$self->columns-1); | |||||||||
| 30 | |||||||||||
| 31 | 0 | push @constraints, $_ >= 0 for @rows_constraints; | |||||||||
| 32 | 0 | push @constraints, $_ >= 0 for @cols_constraints; | |||||||||
| 33 | |||||||||||
| 34 | 0 | for my $item_no (0..@$items-1) { | |||||||||
| 35 | 0 | my ($row, $col) = $self->_item_no_to_rc($item_no); | |||||||||
| 36 | 0 | my $this_item = $items->[$item_no]; | |||||||||
| 37 | |||||||||||
| 38 | 0 | push @constraints, $this_item->x >= 0; | |||||||||
| 39 | 0 | push @constraints, $this_item->y >= 0; | |||||||||
| 40 | 0 | ▶ | push @constraints, $cols_constraints[$col] >= (ref $this_item->width ? $this_item->width->value : $this_item->width); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 0 | ▶ | push @constraints, $rows_constraints[$row] >= (ref $this_item->height ? $this_item->height->value : $this_item->height); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | |||||||||||
| 43 | 0 | ▶ | if( $col > 0 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 0 | my $item_left0 = $items->[$self->_rc_to_item_no($row,$col-1)]; | |||||||||
| 45 | 0 | push @constraints, $item_left0->x + $cols_constraints[$col-1] == $this_item->x; | |||||||||
| 46 | } | ||||||||||
| 47 | 0 | ▶ | if( $row > 0 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 0 | my $item_above = $items->[$self->_rc_to_item_no($row-1,$col)]; | |||||||||
| 49 | 0 | push @constraints, $item_above->y + $rows_constraints[$row-1] == $this_item->y; | |||||||||
| 50 | } | ||||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 0 | ▶ | push @constraints, $actor->width == reduce { $a + $b } @cols_constraints; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 54 | 0 | ▶ | push @constraints, $actor->height == reduce { $a + $b } @rows_constraints; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 55 | |||||||||||
| 56 | 0 | \@constraints; | |||||||||
| 57 | 1 | } | |||||||||
| 58 | |||||||||||
| 59 | 1 | has _constraints => ( | |||||||||
| 60 | is => 'rw', | ||||||||||
| 61 | predicate => 1, | ||||||||||
| 62 | ); | ||||||||||
| 63 | |||||||||||
| 64 | 0 | ▶ | method update($actor) { | ||||||||
|
Subroutine
update: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 65 | 0 | my $solver = $self->context->solver; | |||||||||
| 66 | 0 | my $items = $actor->children; | |||||||||
| 67 | 0 | my $first_item = $items->[0]; | |||||||||
| 68 | |||||||||||
| 69 | 0 | ▶ | if( ! $self->_has_constraints ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 0 | my $constraints = $self->create_constraints( $actor ); | |||||||||
| 71 | 0 | $self->_constraints( $constraints ); | |||||||||
| 72 | |||||||||||
| 73 | 0 | for my $constraint (@$constraints) { | |||||||||
| 74 | 0 | $solver->add_constraint($constraint); | |||||||||
| 75 | } | ||||||||||
| 76 | 0 | $solver->add_edit_variable($first_item->x, Graphics::Layout::Kiwisolver::Strength::STRONG ); | |||||||||
| 77 | 0 | $solver->add_edit_variable($first_item->y, Graphics::Layout::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 | 0 | $solver->suggest_value($first_item->x, 0); | |||||||||
| 83 | 0 | $solver->suggest_value($first_item->y, 0); | |||||||||
| 84 | 0 | $solver->update; | |||||||||
| 85 | 1 | } | |||||||||
| 86 | |||||||||||
| 87 | 1 | 1; | |||||||||
| 88 | |||||||||||
| 89 | __END__ | ||||||||||