blib/lib/App/Asciio/stripes/editable_box2.pm untested

filter: Statementstmt 0.0% 0 / 209 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 51 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 0.0% 0 / 32 Podpod 0.0% 0 / 24 totaltotal 0.0% 0 / 414 CC 36 SCAR 35.5
CC
36
Cov
0%
get_box_frame_elements
110.0
setup
90.0
match_connector
20.0
CRAP 34.9
1  
2 package App::Asciio::stripes::editable_box2 ;
3 use parent qw/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 Readonly my $DEFAULT_BOX_TYPE =>
17 [
18 [1, 'top', '.', '-', '.', 1, ],
19 [0, 'title separator', '|', '-', '|', 1, ],
20 [1, 'body separator', '| ', '|', ' |', 1, ],
21 [1, 'bottom', '\'', '-', '\'', 1, ],
22 [1, 'fill-character', '', ' ', '', 1, ],
23 ] ;
24  
25 sub new
26 {
27 my ($class, $element_definition) = @_ ;
28  
29 my $self = bless {}, __PACKAGE__ ;
30  
31 $self->setup
32 (
33 $element_definition->{TEXT_ONLY},
34 $element_definition->{TITLE},
35 $element_definition->{BOX_TYPE} // Clone::clone($DEFAULT_BOX_TYPE),
36 1, 1,
37 $element_definition->{RESIZABLE},
38 $element_definition->{EDITABLE},
39 $element_definition->{AUTO_SHRINK},
40 ) ;
41  
42 return $self ;
43 }
44  
45 #-----------------------------------------------------------------------------
46  
47 sub setup
48 {
49 my ($self, $text_only, $title_text, $box_type, $end_x, $end_y, $resizable, $editable, $auto_shrink) = @_ ;
50  
51 my $fill_char = ' ';
52  
53 if($box_type->[4][3])
54 {
55 $fill_char = substr($box_type->[4][3], 0, 1);
56 }
57  
58 my ($text_width, @lines) = (0) ;
59  
60 for my $line (split("\n", $text_only))
61 {
62 $text_width = max($text_width, unicode_length($line)) ;
63 push @lines, $line ;
64 }
65  
66 my ($title_width, @title_lines) = (0) ;
67  
68 $title_text = '' unless defined $title_text ;
69  
70 for my $title_line (split("\n", $title_text))
71 {
72 $title_width = max($title_width, unicode_length($title_line)) ;
73 push @title_lines, $title_line ;
74 }
75  
76 my ($extra_width, $extra_height) = get_box_frame_size_overhead($box_type) ;
77  
78 my $display_title = (defined $title_text and $title_text ne '') ? 1 : 0 ;
79 $text_width = max($text_width, $title_width) if $display_title;
80  
81 if($auto_shrink)
82 {
83 ($end_x, $end_y) = (-5, -5) ;
84 }
85  
86 $end_x = max($end_x, $text_width + $extra_width, $title_width + $extra_width) ;
87 $end_y = max($end_y, scalar(@lines) + $extra_height + scalar(@title_lines)) ;
88  
89 my ($box_top, $box_left, $box_right, $box_bottom, $title_separator, $title_left, $title_right) = get_box_frame_elements($box_type, $end_x) ;
90  
91 my $text = $box_top ;
92  
93 for my $title_line (@title_lines)
94 {
95 my $pading = ($end_x - (unicode_length($title_left . $title_line . $title_right))) ;
96 my $left_pading = int($pading / 2) ;
97 my $right_pading = $pading - $left_pading ;
98
99 $text .= $title_left . ($fill_char x $left_pading) . $title_line . ($fill_char x $right_pading) . $title_right ."\n" ;
100 }
101  
102 $text .= $title_separator ;
103  
104 for my $line (@lines)
105 {
106 $text .= $box_left . $line . ($fill_char x ($end_x - (unicode_length($line) + $extra_width))) . $box_right . "\n" ;
107 }
108
109 for (1 .. ($end_y - (@lines + $extra_height + @title_lines)))
110 {
111 $text .= $box_left . ($fill_char x ($end_x - $extra_width)) . $box_right . "\n" ;
112 }
113  
114 $text .= $box_bottom ;
115  
116 my ($text_begin_x, $text_begin_y, $title_separator_exist) = (0, 0, 0) ;
117 $text_begin_y++ if($box_top) ;
118 $text_begin_x = unicode_length($box_left);
119 $title_separator_exist = 1 if($title_separator);
120  
121 unless (defined $self->{CONNECTORS})
122 {
123 for my $connector
124 (
125 # X SCALE_X OFFSET_X Y SCALE_Y OFFSET_Y NAME
126 [ 0, 50, 0 , -1, -1, 0 , 'top_center' ],
127 [ 0, 50, 0 , 0, 100, 0 , 'bottom_center' ],
128 [ -1, -1, 0 , 0, 50, 0 , 'left_center' ],
129 [ 0, 100, 0 , 0, 50, 0 , 'right_center' ],
130 )
131 {
132 $self->add_connector($connector) ;
133 }
134 }
135  
136 my $connectors = $self->scale_connectors($self->{CONNECTORS}, $end_x, $end_y,) ;
137  
138 $self->set
139 (
140 TEXT => $text,
141 TITLE => $title_text,
142 WIDTH => $end_x,
143 HEIGHT => $end_y,
144 TEXT_ONLY => $text_only,
145 TEXT_BEGIN_X => $text_begin_x,
146 TEXT_BEGIN_Y => $text_begin_y,
147 TITLE_SEPARATOR_EXIST => $title_separator_exist,
148 BOX_TYPE => $box_type,
149 RESIZABLE => $resizable,
150 EDITABLE => $editable,
151 AUTO_SHRINK => $auto_shrink,
152 STRIPES => [ {X_OFFSET => 0, Y_OFFSET => 0, WIDTH => $end_x, HEIGHT => $end_y, TEXT => $text } ],
153 EXTENTS => [ 0, 0, $end_x, $end_y ],
154 CONNECTORS => $connectors,
155 ) ;
156 }
157  
158 #-----------------------------------------------------------------------------
159  
160 sub scale_connectors
161 {
162 my ($self, $connectors, $width, $height) = @_ ;
163  
164 for my $connector ($connectors->@*)
165 {
166 $connector->{X} = int($width * $connector->{SCALE_X} / 100) + $connector->{OFFSET_X} if ($connector->{SCALE_X} >= 0) ;
167 $connector->{Y} = int($height * $connector->{SCALE_Y} / 100) + $connector->{OFFSET_Y} if ($connector->{SCALE_Y} >= 0) ;
168 }
169  
170 return $connectors ;
171 }
172  
173 #-----------------------------------------------------------------------------
174  
175 use Readonly ;
176  
177 Readonly my $TOP => 0 ;
178 Readonly my $TITLE_SEPARATOR => 1 ;
179 Readonly my $BODY_SEPARATOR => 2 ;
180 Readonly my $BOTTOM => 3;
181  
182 Readonly my $DISPLAY => 0 ;
183 Readonly my $NAME => 1 ;
184 Readonly my $LEFT => 2 ;
185 Readonly my $BODY => 3 ;
186 Readonly my $RIGHT => 4 ;
187  
188 sub get_box_frame_size_overhead
189 {
190 my ($box_type) = @_ ;
191  
192 my @displayed_elements = grep { $_->[$DISPLAY] } @{$box_type} ;
193  
194 my $extra_width = $box_type->[$BODY_SEPARATOR][$DISPLAY]
195 ? max(0, map { unicode_length($_) } map {$_->[$LEFT]} @displayed_elements)
196 + max(0, map { unicode_length($_) } map {$_->[$RIGHT]} @displayed_elements)
197 : 0 ;
198  
199 my $extra_height = 0 ;
200  
201 for ($TOP, $TITLE_SEPARATOR, $BOTTOM)
202 {
203 $extra_height++ if defined $box_type->[$_][$DISPLAY] && $box_type->[$_][$DISPLAY] ;
204 }
205  
206 return($extra_width, $extra_height) ;
207 }
208  
209 sub get_box_frame_elements
210 {
211 my ($box_type, $width) = @_ ;
212  
213 my ($box_top, $box_left, $box_right, $box_bottom, $title_separator, $title_left, $title_right) = map {''} (1 .. 7) ;
214  
215 if($box_type->[$TOP][$DISPLAY])
216 {
217 my $box_left_and_right_length = unicode_length($box_type->[$TOP][$LEFT]) + unicode_length($box_type->[$TOP][$RIGHT]) ;
218 $box_top = $box_type->[$TOP][$LEFT]
219 . ($box_type->[$TOP][$BODY] x ($width - $box_left_and_right_length))
220 . $box_type->[$TOP][$RIGHT]
221 . "\n" ;
222 }
223  
224 $title_left = $box_type->[$TITLE_SEPARATOR][$LEFT] if($box_type->[$BODY_SEPARATOR][$DISPLAY]) ;
225 $title_right = $box_type->[$TITLE_SEPARATOR][$RIGHT] if($box_type->[$BODY_SEPARATOR][$DISPLAY]) ;
226  
227 if($box_type->[$TITLE_SEPARATOR][$DISPLAY])
228 {
229 my $title_left_and_right_length = unicode_length($title_left) + unicode_length($title_right) ;
230
231 my $title_separator_body = $box_type->[$TITLE_SEPARATOR][$BODY] ;
232 $title_separator_body = ' ' unless defined $title_separator_body ;
233 $title_separator_body = ' ' if $title_separator_body eq '' ;
234
235 $title_separator = $title_left
236 . ($title_separator_body x ($width - $title_left_and_right_length))
237 . $title_right
238 . "\n" ;
239 }
240  
241 $box_left = $box_type->[$BODY_SEPARATOR][$LEFT] if($box_type->[$BODY_SEPARATOR][$DISPLAY]) ;
242 $box_right = $box_type->[$BODY_SEPARATOR][$RIGHT] if($box_type->[$BODY_SEPARATOR][$DISPLAY]) ;
243  
244 if($box_type->[$BOTTOM][$DISPLAY])
245 {
246 my $box_left_and_right_length = unicode_length($box_type->[$BOTTOM][$LEFT]) + unicode_length($box_type->[$BOTTOM][$RIGHT]) ;
247 $box_bottom = $box_type->[$BOTTOM][$LEFT]
248 . ($box_type->[$BOTTOM][$BODY] x ($width - $box_left_and_right_length))
249 . $box_type->[$BOTTOM][$RIGHT] ;
250 }
251  
252 return ($box_top, $box_left, $box_right, $box_bottom, $title_separator, $title_left, $title_right) ;
253 }
254  
255 #-----------------------------------------------------------------------------
256  
257 sub get_selection_action
258 {
259 my ($self, $x, $y) = @_ ;
260 my $action ;
261  
262 if($self->{AUTO_SHRINK})
263 {
264 $action = 'move'
265 }
266 else
267 {
268 $action = ($x == $self->{WIDTH} - 1 && $y == $self->{HEIGHT} - 1)
269 ? 'resize'
270 : 'move' ;
271 }
272  
273 return($action) ;
274 }
275  
276 #-----------------------------------------------------------------------------
277  
278 sub add_connector
279 {
280 my ($self, $connector) = @_ ;
281 my ($x, $scale_x, $offset_x, $y, $scale_y, $offset_y, $name) = $connector->@* ;
282  
283 $self->remove_connector($name) ;
284  
285 push $self->{CONNECTORS}->@*,
286 {
287 X => $x, SCALE_X => $scale_x, OFFSET_X => $offset_x,
288 Y => $y, SCALE_Y => $scale_y, OFFSET_Y => $offset_y,
289 NAME => $name
290 } ;
291 }
292  
293 #-----------------------------------------------------------------------------
294  
295 sub remove_connector
296 {
297 my ($self, $name) = @_ ;
298  
299 $self->{CONNECTORS} = [ grep { $_->{NAME} ne $name } $self->{CONNECTORS}->@* ] ;
300 }
301  
302 #-----------------------------------------------------------------------------
303  
304 sub match_connector
305 {
306 my ($self, $x, $y) = @_ ;
307  
308 for my $connector( @{$self->{CONNECTORS}})
309 {
310 return($connector) if( $x == $connector->{X} && $y == $connector->{Y}) ;
311 }
312  
313 if($self->is_optimize_enabled() && $x >= 0 && $x < $self->{WIDTH} && $y >= 0 && $y < $self->{HEIGHT})
314 {
315 return {X => -1, Y => -1, NAME => 'to_be_optimized'} ;
316 }
317
318 if($self->{ALLOW_BORDER_CONNECTION} && $x >= -1 && $x <= $self->{WIDTH} && $y >= -1 && $y <= $self->{HEIGHT})
319 {
320 return {X => $x, Y => $y, NAME => 'border'} ;
321 }
322  
323 return ;
324 }
325  
326 #-----------------------------------------------------------------------------
327  
328 sub get_connection_points
329 {
330 my ($self) = @_ ;
331  
332 $self->{CONNECTORS}->@*,
333 }
334  
335 #-----------------------------------------------------------------------------
336  
337 sub get_extra_points
338 {
339 my ($self) = @_ ;
340  
341 if($self->{RESIZABLE} && ! $self->is_auto_shrink())
342 {
343 return {X => $self->{WIDTH} - 1, Y => $self->{HEIGHT} - 1, NAME => 'resize'} ;
344 }
345 else
346 {
347 return ;
348 }
349 }
350  
351 #-----------------------------------------------------------------------------
352  
353 sub get_named_connection
354 {
355 my ($self, $name) = @_ ;
356  
357 for my $connector( $self->{CONNECTORS}->@*)
358 {
359 return $connector if $name eq $connector->{NAME} ;
360 }
361  
362 return ;
363 }
364  
365 #-----------------------------------------------------------------------------
366  
367 sub allow_border_connection { my($self, $allow) = @_ ; $self->{ALLOW_BORDER_CONNECTION} = $allow ; }
368 sub is_border_connection_allowed { my($self) = @_ ; return $self->{ALLOW_BORDER_CONNECTION} ; }
369  
370 #-----------------------------------------------------------------------------
371  
372 sub flip_auto_shrink { my($self) = @_ ; $self->{AUTO_SHRINK} ^= 1 ; }
373 sub is_auto_shrink { my($self) = @_ ; return $self->{AUTO_SHRINK} ; }
374  
375 #-----------------------------------------------------------------------------
376  
377 sub resize
378 {
379 my ($self, $reference_x, $reference_y, $new_x, $new_y) = @_ ;
380  
381 return(0, 0, $self->{WIDTH}, $self->{HEIGHT}) unless $self->{RESIZABLE} ;
382  
383 my $new_end_x = $new_x ;
384 my $new_end_y = $new_y ;
385  
386 if($reference_x == -1 && $reference_y == -1)
387 {
388 $self->setup
389 (
390 $self->{TEXT_ONLY},
391 $self->{TITLE},
392 $self->{BOX_TYPE},
393 $self->{WIDTH} + $new_x,
394 $self->{HEIGHT} + $new_y,
395 $self->{RESIZABLE},
396 $self->{EDITABLE},
397 $self->{AUTO_SHRINK},
398 ) ;
399 }
400 else
401 {
402 if($new_end_x >= 0 && $new_end_y >= 0)
403 {
404 $self->setup
405 (
406 $self->{TEXT_ONLY},
407 $self->{TITLE},
408 $self->{BOX_TYPE},
409 $new_end_x + 1,
410 $new_end_y + 1,
411 $self->{RESIZABLE},
412 $self->{EDITABLE},
413 $self->{AUTO_SHRINK},
414 ) ;
415 }
416 }
417  
418 return(0, 0, $self->{WIDTH}, $self->{HEIGHT}) ;
419 }
420  
421 #-----------------------------------------------------------------------------
422  
423 sub get_text { my ($self) = @_ ; return($self->{TITLE}, $self->{TEXT_ONLY}) ; }
424  
425 #-----------------------------------------------------------------------------
426  
427 sub set_text
428 {
429 my ($self, $title, $text) = @_ ;
430  
431 my @displayed_elements = grep { $_->[$DISPLAY] } @{$self->{BOX_TYPE}} ;
432 $text = 'edit_me' if($text eq '' && @displayed_elements == 0) ;
433  
434 $self->setup
435 (
436 $text,
437 $title,
438 $self->{BOX_TYPE},
439 $self->{WIDTH},
440 $self->{HEIGHT},
441 $self->{RESIZABLE},
442 $self->{EDITABLE},
443 $self->{AUTO_SHRINK},
444 ) ;
445 }
446  
447 #-----------------------------------------------------------------------------
448  
449 sub get_box_type { my ($self) = @_ ; return($self->{BOX_TYPE}) ; }
450  
451 #-----------------------------------------------------------------------------
452  
453 sub set_box_type
454 {
455 my ($self, $box_type) = @_ ;
456 $self->setup
457 (
458 $self->{TEXT_ONLY},
459 $self->{TITLE},
460 $box_type,
461 $self->{WIDTH},
462 $self->{HEIGHT},
463 $self->{RESIZABLE},
464 $self->{EDITABLE},
465 $self->{AUTO_SHRINK},
466 ) ;
467 }
468  
469 #-----------------------------------------------------------------------------
470  
471 sub edit
472 {
473 my ($self, $asciio) = @_ ;
474  
475 return unless $self->{EDITABLE} ;
476  
477 my $text = $self->{TEXT_ONLY} ;
478 $text = make_vertical_text($text) if $self->{VERTICAL_TEXT} ;
479  
480 ($text, my $title) = $self->display_box_edit_dialog($self->{TITLE}, $text, $asciio, $self->{X}, $self->{Y}, $self->{TEXT_BEGIN_X}, $self->{TEXT_BEGIN_Y}, $self->{TITLE_SEPARATOR_EXIST}) ;
481  
482 my $tab_as_space = $asciio->{TAB_AS_SPACES} ;
483  
484 $text =~ s/\t/$tab_as_space/g ;
485 $title=~ s/\t/$tab_as_space/g ;
486  
487 $text = make_vertical_text($text) if $self->{VERTICAL_TEXT} ;
488  
489 $self->set_text($title, $text) ;
490 }
491  
492 #-----------------------------------------------------------------------------
493  
494 sub rotate_text
495 {
496 my ($self) = @_ ;
497  
498 my $text = make_vertical_text($self->{TEXT_ONLY}) ;
499  
500 $self->set_text($self->{TITLE}, $text) ;
501 $self->shrink() ;
502  
503 $self->{VERTICAL_TEXT} ^= 1 ;
504 }
505  
506 #-----------------------------------------------------------------------------
507  
508 sub shrink
509 {
510 my ($self) = @_ ;
511 $self->setup
512 (
513 $self->{TEXT_ONLY},
514 $self->{TITLE},
515 $self->{BOX_TYPE},
516 -5,
517 -5,
518 $self->{RESIZABLE},
519 $self->{EDITABLE},
520 $self->{AUTO_SHRINK},
521 ) ;
522 }
523  
524 #-----------------------------------------------------------------------------
525  
526 1 ;