| 1 | |||
| 2 | package App::Asciio::stripes::if_box ; | ||
| 3 | use base App::Asciio::stripes::single_stripe ; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use List::Util qw(min max) ; | ||
| 9 | use Readonly ; | ||
| 10 | use Clone ; | ||
| 11 | |||
| 12 | use App::Asciio::String ; | ||
| 13 | |||
| 14 | #----------------------------------------------------------------------------- | ||
| 15 | |||
| 16 | sub new | ||
| 17 | { | ||
| 18 | my ($class, $element_definition) = @_ ; | ||
| 19 | |||
| 20 | my $self = bless {}, __PACKAGE__ ; | ||
| 21 | |||
| 22 | $self->setup | ||
| 23 | ( | ||
| 24 | $element_definition->{TEXT_ONLY}, | ||
| 25 | 1, 1, | ||
| 26 | $element_definition->{RESIZABLE}, | ||
| 27 | $element_definition->{EDITABLE}, | ||
| 28 | ) ; | ||
| 29 | |||
| 30 | return $self ; | ||
| 31 | } | ||
| 32 | |||
| 33 | #----------------------------------------------------------------------------- | ||
| 34 | |||
| 35 | sub setup | ||
| 36 | { | ||
| 37 | my ($self, $text_only, $end_x, $end_y, $resizable, $editable) = @_ ; | ||
| 38 | |||
| 39 | # $end_x, $end_y are used if we want to keep a box size constant if the included text gets smaller | ||
| 40 | # if_boxes automatically fit to their content (so far) so those variables are not used | ||
| 41 | |||
| 42 | $text_only = '' unless defined $text_only ; | ||
| 43 | |||
| 44 | my ($text_width, @lines) = (0) ; | ||
| 45 | |||
| 46 | for my $line (split("\n", $text_only)) | ||
| 47 | { | ||
| 48 | $text_width = max($text_width, unicode_length($line)) ; | ||
| 49 | push @lines, $line ; | ||
| 50 | } | ||
| 51 | |||
| 52 | my $number_of_lines = scalar(@lines) ; | ||
| 53 | my $text_of_lines = $number_of_lines ; | ||
| 54 | |||
| 55 | my $lines_to_add = ($number_of_lines + 1) % 2 ; # always odd | ||
| 56 | unshift @lines, map {''} (1 .. $lines_to_add / 2) ; | ||
| 57 | push @lines, map {''} (1 .. $lines_to_add / 2) ; | ||
| 58 | |||
| 59 | $number_of_lines += $lines_to_add ; | ||
| 60 | |||
| 61 | my $half_the_lines = int($number_of_lines / 2) ; | ||
| 62 | |||
| 63 | my $extra_width = 2 + $half_the_lines ; | ||
| 64 | my $extra_height = 2 ; | ||
| 65 | |||
| 66 | my $text = ' ' x ($half_the_lines + 1). '.' . '-' x $text_width . '.' . "\n" ; | ||
| 67 | |||
| 68 | my @top_lines = (splice @lines, 0, $number_of_lines / 2) ; | ||
| 69 | |||
| 70 | my $left_indentation = $half_the_lines ; | ||
| 71 | my $inside_indentation = 0 ; | ||
| 72 | |||
| 73 | for my $line (@top_lines) | ||
| 74 | { | ||
| 75 | my $padding = ' ' x ($text_width - unicode_length($line)) ; | ||
| 76 | |||
| 77 | $text .= ' ' x $left_indentation . '/ ' . ' ' x $inside_indentation . $line . $padding . ' ' x $inside_indentation. ' \\' . "\n" ; | ||
| 78 | $left_indentation-- ; | ||
| 79 | $inside_indentation++ ; | ||
| 80 | } | ||
| 81 | |||
| 82 | my $center_line = shift @lines || '' ; | ||
| 83 | my $padding = ' ' x ($text_width - unicode_length($center_line)) ; | ||
| 84 | |||
| 85 | $center_line = '( ' . ' ' x $inside_indentation . $center_line . $padding . ' ' x $inside_indentation . ' )' ; | ||
| 86 | my $width = unicode_length($center_line) ; | ||
| 87 | $text .= $center_line . "\n" ; | ||
| 88 | |||
| 89 | $left_indentation = 1 ; | ||
| 90 | $inside_indentation-- ; | ||
| 91 | |||
| 92 | my @bottom_lines = @lines ; | ||
| 93 | push @bottom_lines, '' for (1 .. scalar(@top_lines) - scalar(@bottom_lines)) ; | ||
| 94 | |||
| 95 | for my $line (@bottom_lines) | ||
| 96 | { | ||
| 97 | my $padding = ' ' x ($text_width - unicode_length($line)) ; | ||
| 98 | |||
| 99 | $text .= ' ' x $left_indentation . '\\ ' . ' ' x $inside_indentation . $line . $padding . ' ' x $inside_indentation . ' /' . "\n" ; | ||
| 100 | $left_indentation++ ; | ||
| 101 | $inside_indentation-- ; | ||
| 102 | } | ||
| 103 | |||
| 104 | $text .= ' ' x ($half_the_lines + 1) . q{'} . '-' x $text_width . q{'} . "\n" ; | ||
| 105 | my $height = $text =~ tr[\n][\n] ; | ||
| 106 | |||
| 107 | $self->set | ||
| 108 | ( | ||
| 109 | TEXT => $text, | ||
| 110 | WIDTH => $width, | ||
| 111 | HEIGHT => $number_of_lines + 2, | ||
| 112 | TEXT_ONLY => $text_only, | ||
| 113 | TEXT_BEGIN_X => int((($text_of_lines+2)/2)+1), | ||
| 114 | TEXT_BEGIN_Y => 1, | ||
| 115 | RESIZABLE => $resizable, | ||
| 116 | EDITABLE => $editable, | ||
| 117 | STRIPES => [ {X_OFFSET => 0, Y_OFFSET => 0, WIDTH => $width, HEIGHT => $height, TEXT => $text} ], | ||
| 118 | EXTENTS => [0, 0, $width, $height], | ||
| 119 | ) ; | ||
| 120 | } | ||
| 121 | |||
| 122 | #----------------------------------------------------------------------------- | ||
| 123 | |||
| 124 | sub get_selection_action | ||
| 125 | { | ||
| 126 | my ($self, $x, $y) = @_ ; | ||
| 127 | |||
| 128 | ($x == $self->{WIDTH} - 1 && $y == $self->{HEIGHT} - 1) | ||
| 129 | ? 'resize' | ||
| 130 | : 'move' ; | ||
| 131 | } | ||
| 132 | |||
| 133 | #----------------------------------------------------------------------------- | ||
| 134 | |||
| 135 | sub match_connector | ||
| 136 | { | ||
| 137 | my ($self, $x, $y) = @_ ; | ||
| 138 | |||
| 139 | my $middle_width = int($self->{WIDTH} / 2) ; | ||
| 140 | my $middle_height = int($self->{HEIGHT} / 2) ; | ||
| 141 | |||
| 142 | if($x == $middle_width && $y == -1) | ||
| 143 | { | ||
| 144 | return {X => $x, Y => $y, NAME => 'top_center'} ; | ||
| 145 | } | ||
| 146 | elsif($x == $middle_width && $y == $self->{HEIGHT}) | ||
| 147 | { | ||
| 148 | return {X => $x, Y => $y, NAME => 'bottom_center'} ; | ||
| 149 | } | ||
| 150 | if($x == -1 && $y == $middle_height) | ||
| 151 | { | ||
| 152 | return {X => $x, Y => $y, NAME => 'left_center'} ; | ||
| 153 | } | ||
| 154 | elsif($x == $self->{WIDTH} && $y == $middle_height) | ||
| 155 | { | ||
| 156 | return {X => $x, Y => $y, NAME => 'right_center'} ; | ||
| 157 | } | ||
| 158 | elsif($x >= 0 && $x < $self->{WIDTH} && $y >= 0 && $y < $self->{HEIGHT}) | ||
| 159 | { | ||
| 160 | return {X => $middle_width, Y => -1, NAME => 'to_be_optimized'} ; | ||
| 161 | } | ||
| 162 | else | ||
| 163 | { | ||
| 164 | return ; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | #----------------------------------------------------------------------------- | ||
| 169 | |||
| 170 | sub get_connection_points | ||
| 171 | { | ||
| 172 | my ($self) = @_ ; | ||
| 173 | my $middle_width = int($self->{WIDTH} / 2) ; | ||
| 174 | my $middle_height = int($self->{HEIGHT} / 2) ; | ||
| 175 | |||
| 176 | return | ||
| 177 | ( | ||
| 178 | {X => $middle_width, Y => -1, NAME => 'top_center'}, | ||
| 179 | {X => $middle_width, Y => $self->{HEIGHT}, NAME => 'bottom_center'}, | ||
| 180 | {X => -1, Y => $middle_height, NAME => 'left_center'}, | ||
| 181 | {X => $self->{WIDTH}, Y => $middle_height, NAME => 'right_center'}, | ||
| 182 | ) ; | ||
| 183 | } | ||
| 184 | |||
| 185 | #----------------------------------------------------------------------------- | ||
| 186 | |||
| 187 | sub get_named_connection | ||
| 188 | { | ||
| 189 | my ($self, $name) = @_ ; | ||
| 190 | my $middle_width = int($self->{WIDTH} / 2) ; | ||
| 191 | my $middle_height = int($self->{HEIGHT} / 2) ; | ||
| 192 | |||
| 193 | if($name eq 'top_center') | ||
| 194 | { | ||
| 195 | return( {X => $middle_width, Y => -1, NAME => 'top_center'} ) ; | ||
| 196 | } | ||
| 197 | elsif($name eq 'bottom_center') | ||
| 198 | { | ||
| 199 | return( {X => $middle_width, Y => $self->{HEIGHT}, NAME => 'bottom_center'} ) ; | ||
| 200 | } | ||
| 201 | elsif($name eq 'left_center') | ||
| 202 | { | ||
| 203 | return {X => -1, Y => $middle_height, NAME => 'left_center'}, | ||
| 204 | } | ||
| 205 | elsif($name eq 'right_center') | ||
| 206 | { | ||
| 207 | return {X => $self->{WIDTH}, Y => $middle_height, NAME => 'right_center'}, | ||
| 208 | } | ||
| 209 | else | ||
| 210 | { | ||
| 211 | return ; | ||
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 | #----------------------------------------------------------------------------- | ||
| 216 | |||
| 217 | sub resize | ||
| 218 | { | ||
| 219 | my ($self, $reference_x, $reference_y, $new_x, $new_y) = @_ ; | ||
| 220 | |||
| 221 | return(0, 0, $self->{WIDTH}, $self->{HEIGHT}) ; | ||
| 222 | } | ||
| 223 | |||
| 224 | #----------------------------------------------------------------------------- | ||
| 225 | |||
| 226 | sub get_text { my ($self) = @_ ; return($self->{TEXT_ONLY}) ; } | ||
| 227 | |||
| 228 | #----------------------------------------------------------------------------- | ||
| 229 | |||
| 230 | sub set_text | ||
| 231 | { | ||
| 232 | my ($self, $text) = @_ ; | ||
| 233 | |||
| 234 | $text = 'edit_me' if($text eq '') ; | ||
| 235 | |||
| 236 | $self->setup($text, $self->{WIDTH}, $self->{HEIGHT}, $self->{RESIZABLE}, $self->{EDITABLE}) ; | ||
| 237 | } | ||
| 238 | |||
| 239 | #----------------------------------------------------------------------------- | ||
| 240 | |||
| 241 | sub edit | ||
| 242 | { | ||
| 243 | my ($self, $asciio) = @_ ; | ||
| 244 | |||
| 245 | return unless $self->{EDITABLE} ; | ||
| 246 | |||
| 247 | my ($text) = $asciio->display_edit_dialog('if object', $self->{TEXT_ONLY}, $asciio, $self->{X}, $self->{Y}, $self->{TEXT_BEGIN_X}, $self->{TEXT_BEGIN_Y}) ; | ||
| 248 | |||
| 249 | $text //= $self->{TEXT} ; | ||
| 250 | |||
| 251 | my $tab_as_space = $asciio->{TAB_AS_SPACES} ; | ||
| 252 | $text =~ s/\t/$tab_as_space/g ; | ||
| 253 | |||
| 254 | $self->set_text($text) ; | ||
| 255 | } | ||
| 256 | |||
| 257 | #----------------------------------------------------------------------------- | ||
| 258 | |||
| 259 | 1 ; |