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

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