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

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