| 1 | use Renard::Incunabula::Common::Setup; | ||
| 2 | package Intertangle::Jacquard::Layout::Box; | ||
| 3 | # ABSTRACT: Box layout | ||
| 4 | $Intertangle::Jacquard::Layout::Box::VERSION = '0.002'; | ||
| 5 | use Mu; | ||
| 6 | use Intertangle::Punchcard::Backend::Kiwisolver::Context; | ||
| 7 | use Intertangle::Punchcard::Attributes; | ||
| 8 | |||
| 9 | ro 'margin'; # TODO PositiveOrZeroInt | ||
| 10 | |||
| 11 | variable 'outer_x'; | ||
| 12 | variable 'outer_y'; | ||
| 13 | |||
| 14 | lazy context => sub { | ||
| 15 | Intertangle::Punchcard::Backend::Kiwisolver::Context->new; | ||
| 16 | }; | ||
| 17 | |||
| 18 | method create_constraints($actor) { | ||
| 19 | my $items = $actor->children; | ||
| 20 | |||
| 21 | my @constraints; | ||
| 22 | |||
| 23 | my $inner_x = $self->context->new_variable( name => "inner.x" ); | ||
| 24 | my $inner_y = $self->context->new_variable( name => "inner.y" ); | ||
| 25 | my $inner_width = $self->context->new_variable( name => "inner.width" ); | ||
| 26 | my $inner_height = $self->context->new_variable( name => "inner.height" ); | ||
| 27 | |||
| 28 | for my $item_no (0..@$items-1) { | ||
| 29 | my $this_item = $items->[$item_no]; | ||
| 30 | |||
| 31 | push @constraints, $this_item->x >= $inner_x; | ||
| 32 | push @constraints, $this_item->y >= $inner_y; | ||
| 33 | |||
| 34 | push @constraints, $inner_width >= $this_item->width; | ||
| 35 | push @constraints, $inner_height >= $this_item->height; | ||
| 36 | } | ||
| 37 | |||
| 38 | push @constraints, $self->outer_x + $self->margin == $inner_x; | ||
| 39 | push @constraints, $self->outer_y + $self->margin == $inner_y; | ||
| 40 | |||
| 41 | push @constraints, $actor->width == $inner_width + 2 * $self->margin; | ||
| 42 | push @constraints, $actor->height == $inner_height + 2 * $self->margin; | ||
| 43 | |||
| 44 | \@constraints; | ||
| 45 | } | ||
| 46 | |||
| 47 | has _constraints => ( | ||
| 48 | is => 'rw', | ||
| 49 | predicate => 1, | ||
| 50 | ); | ||
| 51 | |||
| 52 | method update($actor) { | ||
| 53 | my $solver = $self->context->solver; | ||
| 54 | my $items = $actor->children; | ||
| 55 | my $first_item = $items->[0]; | ||
| 56 | |||
| 57 | if( ! $self->_has_constraints ) { | ||
| 58 | my $constraints = $self->create_constraints( $actor ); | ||
| 59 | $self->_constraints( $constraints ); | ||
| 60 | |||
| 61 | for my $constraint (@$constraints) { | ||
| 62 | $solver->add_constraint($constraint); | ||
| 63 | } | ||
| 64 | |||
| 65 | $solver->add_edit_variable($self->outer_x, Graphics::Layout::Kiwisolver::Strength::STRONG ); | ||
| 66 | $solver->add_edit_variable($self->outer_y, Graphics::Layout::Kiwisolver::Strength::STRONG ); | ||
| 67 | } | ||
| 68 | |||
| 69 | $solver->suggest_value( $self->outer_x, 0); | ||
| 70 | $solver->suggest_value( $self->outer_y, 0); | ||
| 71 | $solver->update; | ||
| 72 | } | ||
| 73 | |||
| 74 | 1; | ||
| 75 | |||
| 76 | __END__ |