« blib/lib/App/Asciio/Actions/ElementsManipulation.pm
↑ Summary
blib/lib/App/Asciio/stripes/editable_box2.pm »
| 1 | |||
| 2 | package App::Asciio::stripes::group ; | ||
| 3 | use base App::Asciio::stripes::stripes ; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use List::Util qw(min max) ; | ||
| 9 | use List::MoreUtils qw(minmax) ; | ||
| 10 | use Module::Util qw(find_installed) ; | ||
| 11 | use File::Basename ; | ||
| 12 | |||
| 13 | use App::Asciio::String ; | ||
| 14 | |||
| 15 | #----------------------------------------------------------------------------- | ||
| 16 | |||
| 17 | sub new | ||
| 18 | { | ||
| 19 | my ($class, $elements, $connections, $as_one_strip, $asciio_handle) = @_ ; | ||
| 20 | |||
| 21 | my @stripes ; | ||
| 22 | |||
| 23 | my ($total_width, $total_height) = (0, 0) ; | ||
| 24 | my ($min_x, $min_y, $max_x, $max_y) = (10_000, 10_000, 0, 0) ; | ||
| 25 | |||
| 26 | my ($min_ex) = min( map { $_->{X} } @$elements) ; | ||
| 27 | my ($min_ey) = min( map { $_->{Y} } @$elements) ; | ||
| 28 | |||
| 29 | # X Y are not the minimum coordinate of the element, | ||
| 30 | # there is a case where the arrow is reversed | ||
| 31 | # X_OFFSET and Y_OFFSET may be negative | ||
| 32 | for my $element (@{$elements}) | ||
| 33 | { | ||
| 34 | for my $stripe (@{$element->get_stripes()}) | ||
| 35 | { | ||
| 36 | $min_ex = min($min_ex, $element->{X} + $stripe->{X_OFFSET}) ; | ||
| 37 | $min_ey = min($min_ey, $element->{Y} + $stripe->{Y_OFFSET}) ; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | for my $element (@{$elements}) | ||
| 42 | { | ||
| 43 | delete $element->{CACHE} ; | ||
| 44 | |||
| 45 | my $element_offset_x = $element->{X} - $min_ex ; | ||
| 46 | my $element_offset_y = $element->{Y} - $min_ey ; | ||
| 47 | |||
| 48 | for my $stripe (@{$element->get_stripes()}) | ||
| 49 | { | ||
| 50 | my $text = $stripe->{TEXT} ; | ||
| 51 | |||
| 52 | my $width = max( map{ unicode_length($_) } split("\n", $text)) ; | ||
| 53 | |||
| 54 | my $height = ($text =~ tr[\n][\n]) + 1 ; | ||
| 55 | |||
| 56 | if(! $as_one_strip) | ||
| 57 | { | ||
| 58 | my @background_color ; | ||
| 59 | @background_color = ("BACKGROUND" => $element->{COLORS}{BACKGROUND}) if defined $element->{COLORS}{BACKGROUND} ; | ||
| 60 | @background_color = ("BACKGROUND" => $stripe->{BACKGROUND}) if defined $stripe->{BACKGROUND} ; | ||
| 61 | |||
| 62 | my @foreground_color ; | ||
| 63 | @foreground_color = ("FOREGROUND" => $element->{COLORS}{FOREGROUND}) if defined $element->{COLORS}{FOREGROUND} ; | ||
| 64 | @foreground_color = ("FOREGROUND" => $stripe->{FOREGROUND}) if defined $stripe->{FOREGROUND} ; | ||
| 65 | |||
| 66 | push @stripes, | ||
| 67 | { | ||
| 68 | TEXT => $text, | ||
| 69 | X_OFFSET => $stripe->{X_OFFSET} + $element_offset_x, | ||
| 70 | Y_OFFSET => $stripe->{Y_OFFSET} + $element_offset_y, | ||
| 71 | WIDTH => $width, | ||
| 72 | HEIGHT => $height , | ||
| 73 | @background_color, | ||
| 74 | @foreground_color, | ||
| 75 | } ; | ||
| 76 | } | ||
| 77 | |||
| 78 | ($total_width) = max($total_width, $stripe->{X_OFFSET} + $width + $element_offset_x) ; | ||
| 79 | ($total_height) = max($total_height, $stripe->{Y_OFFSET} + $height + $element_offset_y) ; | ||
| 80 | |||
| 81 | $min_x = min($min_x, $stripe->{X_OFFSET} + $element_offset_x) ; | ||
| 82 | $max_x = max($max_x, $stripe->{X_OFFSET} + $width + $element_offset_x) ; | ||
| 83 | $min_y = min($min_y, $stripe->{Y_OFFSET} + $element_offset_y) ; | ||
| 84 | $max_y = max($max_y, $stripe->{Y_OFFSET} + $height + $element_offset_y) ; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | $total_width -= $min_x ; | ||
| 89 | $total_height -= $min_y ; | ||
| 90 | |||
| 91 | if ($as_one_strip) | ||
| 92 | { | ||
| 93 | my $asciio = App::Asciio->new() ; | ||
| 94 | $asciio->add_elements(@{$elements}) ; | ||
| 95 | $asciio->{USE_CROSS_MODE} = $asciio_handle->{USE_CROSS_MODE} if(defined $asciio_handle->{USE_CROSS_MODE}) ; | ||
| 96 | |||
| 97 | my $text = $asciio->transform_elements_to_ascii_buffer() ; | ||
| 98 | |||
| 99 | my $cropped_text = '' ; | ||
| 100 | for my $line (split /\n/, $text) | ||
| 101 | { | ||
| 102 | $line =~ s/^\s{$min_ex}// ; | ||
| 103 | $cropped_text .= sprintf "%-${total_width}s\n", $line ; | ||
| 104 | } | ||
| 105 | |||
| 106 | $text = $cropped_text ; | ||
| 107 | |||
| 108 | @stripes = | ||
| 109 | ({ | ||
| 110 | TEXT => $text, | ||
| 111 | X_OFFSET => 0, | ||
| 112 | Y_OFFSET => 0, | ||
| 113 | WIDTH => $total_width, | ||
| 114 | HEIGHT => $total_height, | ||
| 115 | }) ; | ||
| 116 | } | ||
| 117 | |||
| 118 | return bless({ | ||
| 119 | STRIPES => \@stripes, | ||
| 120 | EXTENTS => [$min_x, $min_y, $max_x, $max_y], | ||
| 121 | EX => $min_ex, | ||
| 122 | EY => $min_ey, | ||
| 123 | WIDTH => $total_width, | ||
| 124 | HEIGHT => $total_height, | ||
| 125 | ELEMENTS => $elements, | ||
| 126 | CONNECTIONS => $connections, | ||
| 127 | }, __PACKAGE__), | ||
| 128 | $min_ex, | ||
| 129 | $min_ey ; | ||
| 130 | } | ||
| 131 | |||
| 132 | #----------------------------------------------------------------------------- | ||
| 133 | |||
| 134 | 1 ; |