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

filter: Statementstmt 0.0% 0 / 140 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 249 CC 16 SCAR 28.3
CC
16
Cov
0%
match_connector
42.0
get_named_connection
30.0
setup
20.0
CRAP 16.9
1  
2 package App::Asciio::stripes::process_box ;
3 use base App::Asciio::stripes::stripes ;
4  
5 use strict;
6 use warnings;
7  
8 use List::Util qw(min max) ;
9 use Readonly ;
10  
11 use App::Asciio::String ;
12  
13 #-----------------------------------------------------------------------------
14  
15 sub new
16 {
17 my ($class, $element_definition) = @_ ;
18  
19 my $self = bless {}, __PACKAGE__ ;
20  
21 $self->setup
22 (
23 $element_definition->{TEXT_ONLY},
24 $element_definition->{WIDTH} || 1,
25 $element_definition->{HEIGHT} || 1,
26 $element_definition->{EDITABLE},
27 $element_definition->{RESIZABLE},
28 ) ;
29  
30 return $self ;
31 }
32  
33 #-----------------------------------------------------------------------------
34  
35 sub setup
36 {
37 my ($self, $text_only, $end_x, $end_y, $editable, $resizable) = @_ ;
38  
39 Readonly my $side_glyphs_size => 4 ;
40  
41 $text_only = '' unless defined $text_only ;
42  
43 my @lines = split("\n", $text_only) ;
44 @lines = ('') unless @lines;
45  
46 my $number_of_lines = scalar(@lines) ;
47 my $text_lines = $number_of_lines ;
48  
49 if($end_y - 3 > $number_of_lines)
50 {
51 my $lines_to_add = ($end_y - 3) - $number_of_lines ;
52 $lines_to_add += $lines_to_add % 2 ; # number of lines is always even
53
54 unshift @lines, map {''} (1 .. $lines_to_add / 2) ;
55 push @lines, map {''} (1 .. $lines_to_add / 2) ;
56
57 $number_of_lines += $lines_to_add ;
58 }
59  
60 my $half_the_lines = int($number_of_lines / 2) ;
61 my $element_width = 0 ;
62  
63 my $current_half_the_lines = $half_the_lines ;
64 my (@lines_width_plus_offset) ;
65 for my $line (@lines)
66 {
67 push @lines_width_plus_offset, unicode_length($line) + abs($current_half_the_lines) ;
68 $current_half_the_lines-- ;
69 }
70  
71 my $text_width_plus_offset = max(@lines_width_plus_offset, $end_x) ;
72  
73 my @top_lines = (splice @lines, 0, $number_of_lines / 2) ;
74  
75 my $center_line = shift @lines || '' ;
76  
77 my @bottom_lines = @lines ;
78 push @bottom_lines, '' for (1 .. scalar(@top_lines) - scalar(@bottom_lines)) ;
79  
80 my (@stripes, $strip_text, $x_offset, $y_offset) ;
81  
82 $strip_text = '_' x (($text_width_plus_offset - 1) + $side_glyphs_size) . "\n\\" . ' ' x (($text_width_plus_offset - 2) + $side_glyphs_size) . "\\" ;
83  
84 push @stripes,
85 {
86 'HEIGHT' => 2,
87 'TEXT' => $strip_text,
88 'WIDTH' => $text_width_plus_offset + $side_glyphs_size,
89 'X_OFFSET' => 0,
90 'Y_OFFSET' =>0,
91 } ;
92  
93 $x_offset = 1 ;
94 $y_offset = 2 ;
95  
96 $current_half_the_lines = $half_the_lines ;
97 for my $line (@top_lines)
98 {
99 my $front_padding = ' ' x $current_half_the_lines ;
100 my $padding = ' ' x ($text_width_plus_offset - (unicode_length($line) + $current_half_the_lines)) ;
101 my $strip_text = "\\ $front_padding$line$padding \\" ;
102
103 push @stripes,
104 {
105 'HEIGHT' => 1,
106 'TEXT' => $strip_text,
107 'WIDTH' => unicode_length($strip_text),
108 'X_OFFSET' => $x_offset,
109 'Y_OFFSET' => $y_offset ,
110 } ;
111
112 $x_offset++ ;
113 $y_offset++ ;
114 $current_half_the_lines-- ;
115 }
116  
117 my $padding = ' ' x ($text_width_plus_offset - unicode_length($center_line)) ;
118 $strip_text = ') ' . $center_line . $padding . ' )' ;
119 $element_width = unicode_length($strip_text) + $y_offset - 1 ; # first stripe is two lines high, compensate offset by substracting one
120 my $left_center_x = $y_offset - 2 ; # compensate as above and shft left
121  
122 push @stripes,
123 {
124 'HEIGHT' => 1,
125 'TEXT' => $strip_text,
126 'WIDTH' => unicode_length($strip_text),
127 'X_OFFSET' => $x_offset,
128 'Y_OFFSET' => $y_offset,
129 };
130  
131 $y_offset++ ;
132 $x_offset-- ;
133 $current_half_the_lines = 1;
134  
135 for my $line (@bottom_lines)
136 {
137 my $front_padding = ' ' x $current_half_the_lines ;
138 my $padding = ' ' x ($text_width_plus_offset - (unicode_length($line) + $current_half_the_lines)) ;
139
140 my $strip_text = "/ $front_padding$line$padding /" ;
141
142 push @stripes,
143 {
144 'HEIGHT' => 1,
145 'TEXT' => $strip_text,
146 'WIDTH' => unicode_length($strip_text),
147 'X_OFFSET' => $x_offset,
148 'Y_OFFSET' => $y_offset ,
149 } ;
150 $x_offset-- ;
151 $y_offset++ ;
152 $current_half_the_lines++;
153 }
154  
155 $strip_text = '/' . '_' x (($text_width_plus_offset - 2) + $side_glyphs_size ) . '/' ;
156 push @stripes,
157 {
158 'HEIGHT' => 1,
159 'TEXT' => $strip_text,
160 'WIDTH' => $text_width_plus_offset + $side_glyphs_size,
161 'X_OFFSET' => 0,
162 'Y_OFFSET' => $y_offset,
163 };
164  
165 $self->set
166 (
167 STRIPES => \@stripes,
168 WIDTH => $element_width,
169 HEIGHT => $y_offset + 1,
170 LEFT_CENTER_X => $left_center_x,
171 RESIZE_POINT_X => $text_width_plus_offset + $side_glyphs_size - 1,
172 TEXT_ONLY => $text_only,
173 TEXT_BEGIN_X => int(($y_offset/2)+2),
174 TEXT_BEGIN_Y => int(($y_offset-$text_lines+2)/2),
175 EDITABLE => $editable,
176 RESIZABLE => $resizable,
177 EXTENTS => [0, 0, $element_width, $y_offset + 1],
178 ) ;
179 }
180  
181 #-----------------------------------------------------------------------------
182  
183 sub get_selection_action
184 {
185 my ($self, $x, $y) = @_ ;
186  
187 ($x == $self->{RESIZE_POINT_X} && $y == $self->{HEIGHT} - 1)
188 ? 'resize'
189 : 'move' ;
190 }
191  
192 #-----------------------------------------------------------------------------
193  
194 sub match_connector
195 {
196 my ($self, $x, $y) = @_ ;
197  
198 my $middle_width = int($self->{WIDTH} / 2) ;
199 my $middle_height = int($self->{HEIGHT} / 2) ;
200  
201 if($x == $middle_width && $y == -1)
202 {
203 return {X => $x, Y => $y, NAME => 'top_center'} ;
204 }
205 elsif($x == $middle_width && $y == $self->{HEIGHT})
206 {
207 return {X => $x, Y => $y, NAME => 'bottom_center'} ;
208 }
209 if($x == $self->{LEFT_CENTER_X} && $y == $middle_height)
210 {
211 return {X => $x, Y => $y, NAME => 'left_center'} ;
212 }
213 elsif($x == $self->{WIDTH} && $y == $middle_height)
214 {
215 return {X => $x, Y => $y, NAME => 'right_center'} ;
216 }
217 elsif($x >= 0 && $x < $self->{WIDTH} && $y >= 0 && $y < $self->{HEIGHT})
218 {
219 return {X => $middle_width, Y => -1, NAME => 'to_be_optimized'} ;
220 }
221 else
222 {
223 return ;
224 }
225 }
226  
227 #-----------------------------------------------------------------------------
228  
229 sub get_connection_points
230 {
231 my ($self) = @_ ;
232 my $middle_width = int($self->{WIDTH} / 2) ;
233 my $middle_height = int($self->{HEIGHT} / 2) ;
234  
235 return
236 (
237 {X => $middle_width, Y => -1, NAME => 'top_center'},
238 {X => $middle_width, Y => $self->{HEIGHT}, NAME => 'bottom_center'},
239 {X => $self->{LEFT_CENTER_X}, Y => $middle_height, NAME => 'left_center'},
240 {X => $self->{WIDTH}, Y => $middle_height, NAME => 'right_center'},
241 ) ;
242 }
243  
244 #-----------------------------------------------------------------------------
245  
246 sub get_extra_points
247 {
248 my ($self) = @_ ;
249 return ( {X => $self->{RESIZE_POINT_X}, Y => $self->{HEIGHT} - 1 , NAME => 'resize'} ) ;
250 }
251  
252 #-----------------------------------------------------------------------------
253  
254 sub get_named_connection
255 {
256 my ($self, $name) = @_ ;
257 my $middle_width = int($self->{WIDTH} / 2) ;
258 my $middle_height = int($self->{HEIGHT} / 2) ;
259  
260 if($name eq 'top_center')
261 {
262 return {X => $middle_width, Y => -1, NAME => 'top_center'} ;
263 }
264 elsif($name eq 'bottom_center')
265 {
266 return {X => $middle_width, Y => $self->{HEIGHT}, NAME => 'bottom_center'} ;
267 }
268 elsif($name eq 'left_center')
269 {
270 return {X => $self->{LEFT_CENTER_X}, Y => $middle_height, NAME => 'left_center'},
271 }
272 elsif($name eq 'right_center')
273 {
274 return {X => $self->{WIDTH}, Y => $middle_height, NAME => 'right_center'},
275 }
276 else
277 {
278 return ;
279 }
280 }
281  
282 #-----------------------------------------------------------------------------
283  
284 sub resize
285 {
286 my ($self, $reference_x, $reference_y, $new_x, $new_y) = @_ ;
287  
288 my $new_end_x = $new_x ;
289 my $new_end_y = $new_y ;
290  
291 if($new_end_x >= 0 && $new_end_y >= 0)
292 {
293 $self->setup
294 (
295 $self->{TEXT_ONLY},
296 $new_end_x + 1 - ($self->{WIDTH} - $self->{RESIZE_POINT_X}), # compensate for resize point X not equal to width
297 $new_end_y + 1,
298 $self->{EDITABLE}, $self->{RESIZABLE}
299 ) ;
300 }
301  
302 return(0, 0, $self->{WIDTH}, $self->{HEIGHT}) ;
303 }
304  
305 #-----------------------------------------------------------------------------
306  
307 sub get_text
308 {
309 my ($self) = @_ ;
310 return($self->{TEXT_ONLY}) ;
311 }
312  
313 #-----------------------------------------------------------------------------
314  
315 sub set_text
316 {
317 my ($self, $text) = @_ ;
318 $self->setup
319 (
320 $text,
321 $self->{RESIZE_POINT_X} - 3, # magic number are ugly
322 $self->{HEIGHT} - 1,
323 $self->{EDITABLE}, $self->{RESIZABLE}
324 ) ;
325 }
326  
327 #-----------------------------------------------------------------------------
328  
329 sub edit
330 {
331 my ($self, $asciio) = @_ ;
332  
333 return unless $self->{EDITABLE} ;
334  
335 my ($text_only) = $asciio->display_edit_dialog('process object', $self->{TEXT_ONLY}, $asciio, $self->{X}, $self->{Y}, $self->{TEXT_BEGIN_X}, $self->{TEXT_BEGIN_Y}) ;
336  
337 my $tab_as_space = $asciio->{TAB_AS_SPACES} ;
338 $text_only =~ s/\t/$tab_as_space/g ;
339  
340 $self->set_text($text_only) ;
341 }
342  
343 #-----------------------------------------------------------------------------
344  
345 1 ;