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

filter: Statementstmt 0.0% 0 / 209 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 329 CC 24 SCAR 55.5
CC
24
Cov
0%
get_arrow
462.0
setup
12.0
get_selection_action
6.0
CRAP 257.2
1  
2 package App::Asciio::stripes::editable_arrow2 ;
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 use Clone ;
11  
12 #-----------------------------------------------------------------------------
13  
14 Readonly my $DEFAULT_ARROW_TYPE =>
15 [
16 ['Up', '|', '|', '^', 1],
17 ['45', '/', '/', '^', 1],
18 ['Right', '-', '-', '>', 1],
19 ['135', '\\', '\\', 'v', 1],
20 ['Down', '|', '|', 'v', 1],
21 ['225', '/', '/', 'v', 1],
22 ['Left', '-', '-', '<', 1],
23 ['315', '\\', '\\', '^', 1],
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->{ARROW_TYPE} || Clone::clone($DEFAULT_ARROW_TYPE),
35 $element_definition->{END_X}, $element_definition->{END_Y},
36 $element_definition->{EDITABLE},
37 ) ;
38  
39 return $self ;
40 }
41  
42 #-----------------------------------------------------------------------------
43  
44 sub setup
45 {
46 my ($self, $arrow_type, $end_x, $end_y, $editable) = @_ ;
47  
48 my ($stripes, $real_end_x, $real_end_y) = get_arrow($arrow_type, $end_x, $end_y) ;
49  
50 my ($ex1, $ey1, $ex2, $ey2) ;
51  
52 if ($real_end_x < 0)
53 {
54 $ex1 = $real_end_x ;
55 $ex2 = 1 ;
56 }
57 else
58 {
59 $ex1 = 0 ;
60 $ex2 = $real_end_x + 1 ;
61 }
62  
63 if ($real_end_y < 0)
64 {
65 $ey1 = $real_end_y ;
66 $ey2 = 1 ;
67 }
68 else
69 {
70 $ey1 = 0 ;
71 $ey2 = $real_end_y + 1 ;
72 }
73  
74 $self->set
75 (
76 STRIPES => $stripes,
77 END_X => $real_end_x,
78 END_Y => $real_end_y,
79 ARROW_TYPE => $arrow_type,
80 CACHE => undef,
81 EXTENTS => [$ex1, $ey1, $ex2, $ey2],
82 WIDTH => $ex2 - $ex1,
83 HEIGHT => $ey2 - $ey1,
84 ) ;
85 }
86  
87 #-----------------------------------------------------------------------------
88  
89 sub get_arrow
90 {
91 my ($arrow_type, $end_x, $end_y) = @_ ;
92 my ($stripes, $real_end_x, $real_end_y, $height, $width) = ([]) ;
93  
94 $end_y *= 2 ; # compensate for aspect ratio
95  
96 my $direction = $end_x >= 0
97 ? $end_y <= 0
98 ? -$end_y > $end_x
99 ? -$end_y / 4 > $end_x
100 ? 'up'
101 :'45'
102 : -$end_y > $end_x / 2
103 ? '45'
104 : 'right'
105 : $end_y < $end_x
106 ? $end_y < $end_x / 2
107 ? 'right'
108 :'135'
109 : $end_y / 4 < $end_x
110 ? '135'
111 : 'down'
112 : $end_y < 0
113 ? $end_y < $end_x
114 ? $end_y / 4 < $end_x
115 ? 'up'
116 : '315'
117 : $end_y < $end_x / 2
118 ? '315'
119 : 'left'
120 : $end_y > -$end_x
121 ? $end_y / 4 > -$end_x
122 ? 'down'
123 : '225'
124 : $end_y > -$end_x / 2
125 ? '225'
126 : 'left' ;
127  
128 $end_y /= 2 ; # done compensating for aspect ratio
129  
130 my $arrow ;
131  
132 for ($direction)
133 {
134 $_ eq 'up' and do
135 {
136 my ($start, $body, $end) = @{$arrow_type->[0]}[1 .. 3] ;
137
138 $height = -$end_y + 1 ;
139 $real_end_y = $end_y ;
140 $real_end_x = 0 ;
141
142 $arrow = $height == 2
143 ? $end . "\n" . $start
144 : $end . "\n" . ("$body\n" x ($height -2)) . $start ;
145
146 push @{$stripes},
147 {
148 'HEIGHT' => $height,
149 'TEXT' => $arrow,
150 'WIDTH' => 1,
151 'X_OFFSET' => 0,
152 'Y_OFFSET' => $end_y,
153 } ;
154
155 last ;
156 } ;
157
158 $_ eq '45' and do
159 {
160 my ($start, $body, $end) = @{$arrow_type->[1]}[1 .. 3] ;
161
162 $height = -$end_y + 1 ;
163 $real_end_y = $end_y ;
164
165 $width = $height ;
166 $real_end_x = - $real_end_y;
167
168 push @{$stripes},
169 {
170 'HEIGHT' => 1,
171 'TEXT' => $start,
172 'WIDTH' => 1,
173 'X_OFFSET' => 0,
174 'Y_OFFSET' => 0,
175 } ;
176
177 for(my $position = -$end_y - 1 ; $position > 0 ; $position--)
178 {
179 push @{$stripes},
180 {
181 'HEIGHT' => 1,
182 'TEXT' => $body,
183 'WIDTH' => 1,
184 'X_OFFSET' => $position,
185 'Y_OFFSET' => -$position,
186 } ;
187 }
188
189 push @{$stripes},
190 {
191 'HEIGHT' => 1,
192 'TEXT' => $end,
193 'WIDTH' => 1,
194 'X_OFFSET' => -$end_y ,
195 'Y_OFFSET' => $end_y ,
196 } ;
197
198 last ;
199 } ;
200
201 $_ eq 'right' and do
202 {
203 my ($start, $body, $end) = @{$arrow_type->[2]}[1 .. 3] ;
204
205 $width = $end_x + 1 ;
206 $real_end_x = $end_x ;
207 $real_end_y = 0 ;
208
209 $arrow = $width == 1
210 ? $end
211 : $width == 2
212 ? $start . $end
213 : $start . ($body x ($width -2)) . $end ;
214
215 push @{$stripes},
216 {
217 'HEIGHT' => 1,
218 'TEXT' => $arrow,
219 'WIDTH' => $width,
220 'X_OFFSET' => 0,
221 'Y_OFFSET' => 0,
222 } ;
223
224 last ;
225 } ;
226
227 $_ eq '135' and do
228 {
229 my ($start, $body, $end) = @{$arrow_type->[3]}[1 .. 3] ;
230
231 $height = $end_y + 1 ;
232 $real_end_y = $end_y ;
233
234 $width = $height ;
235 $real_end_x = $real_end_y ;
236
237 push @{$stripes},
238 {
239 'HEIGHT' => 1,
240 'TEXT' => $start,
241 'WIDTH' => 1,
242 'X_OFFSET' => 0 ,
243 'Y_OFFSET' => 0 ,
244 } ;
245
246 for(my $position = 1 ; $position < $end_y ; $position++)
247 {
248 push @{$stripes},
249 {
250 'HEIGHT' => 1,
251 'TEXT' => $body,
252 'WIDTH' => 1,
253 'X_OFFSET' => $position,
254 'Y_OFFSET' => $position,
255 } ;
256 }
257
258 push @{$stripes},
259 {
260 'HEIGHT' => 1,
261 'TEXT' => $end,
262 'WIDTH' => 1,
263 'X_OFFSET' => $end_y ,
264 'Y_OFFSET' => $end_y ,
265 } ;
266
267 last ;
268 } ;
269
270 $_ eq 'down' and do
271 {
272 my ($start, $body, $end) = @{$arrow_type->[4]}[1 .. 3] ;
273 $height = $end_y + 1 ;
274 $real_end_y = $end_y ;
275 $real_end_x = 0 ;
276
277 $arrow = $height == 2
278 ? $start . "\n" . $end
279 : $start . "\n" . ("$body\n" x ($height -2)) . $end ;
280
281 push @{$stripes},
282 {
283 'HEIGHT' => $height,
284 'TEXT' => $arrow,
285 'WIDTH' => 1,
286 'X_OFFSET' => 0,
287 'Y_OFFSET' => 0,
288 } ;
289
290 last ;
291 } ;
292
293 $_ eq '225' and do
294 {
295 my ($start, $body, $end) = @{$arrow_type->[5]}[1 .. 3] ;
296
297 $height = $end_y + 1 ;
298 $real_end_y = $end_y ;
299
300 $width = $height ;
301 $real_end_x = -$real_end_y ;
302
303 push @{$stripes},
304 {
305 'HEIGHT' => 1,
306 'TEXT' => $start,
307 'WIDTH' => 1,
308 'X_OFFSET' => 0,
309 'Y_OFFSET' => 0,
310 } ;
311
312 for(my $position = $end_y - 1 ; $position > 0 ; $position--)
313 {
314 push @{$stripes},
315 {
316 'HEIGHT' => 1,
317 'TEXT' => $body,
318 'WIDTH' => 1,
319 'X_OFFSET' => -$position,
320 'Y_OFFSET' => $position,
321 } ;
322 }
323
324 push @{$stripes},
325 {
326 'HEIGHT' => 1,
327 'TEXT' => $end,
328 'WIDTH' => 1,
329 'X_OFFSET' => -$end_y ,
330 'Y_OFFSET' => $end_y ,
331 } ;
332
333 last ;
334 } ;
335
336 $_ eq 'left' and do
337 {
338 my ($start, $body, $end) = @{$arrow_type->[6]}[1 .. 3] ;
339
340 $width = -$end_x + 1 ;
341
342 $real_end_y = 0 ;
343 $real_end_x = $end_x ;
344
345 $arrow = $width == 2
346 ? $end . $start
347 : $end . ($body x ($width -2)) . $start ;
348
349 push @{$stripes},
350 {
351 'HEIGHT' => 1,
352 'TEXT' => $arrow,
353 'WIDTH' => $width,
354 'X_OFFSET' => $end_x,
355 'Y_OFFSET' => 0,
356 } ;
357
358 last ;
359 } ;
360
361 $_ eq '315' and do
362 {
363 my ($start, $body, $end) = @{$arrow_type->[7]}[1 .. 3] ;
364
365 $height = -$end_y + 1 ;
366 $real_end_y = $end_y ;
367
368 $width = $height ;
369 $real_end_x = $real_end_y ;
370
371 push @{$stripes},
372 {
373 'HEIGHT' => 1,
374 'TEXT' => $start,
375 'WIDTH' => 1,
376 'X_OFFSET' => 0,
377 'Y_OFFSET' => 0,
378 } ;
379
380 for(my $position = 1 ; $position < -$end_y ; $position++)
381 {
382 push @{$stripes},
383 {
384 'HEIGHT' => 1,
385 'TEXT' => '\\',
386 'WIDTH' => 1,
387 'X_OFFSET' => -$position,
388 'Y_OFFSET' => -$position,
389 } ;
390 }
391
392 push @{$stripes},
393 {
394 'HEIGHT' => 1,
395 'TEXT' => $end,
396 'WIDTH' => 1,
397 'X_OFFSET' => $end_y,
398 'Y_OFFSET' => $end_y,
399 } ;
400
401 last ;
402 } ;
403 }
404  
405 return($stripes, $real_end_x, $real_end_y) ;
406 }
407  
408 #-----------------------------------------------------------------------------
409  
410 sub get_extra_points
411 {
412 my ($self) = @_ ;
413  
414 return ( {X => $self->{END_X}, Y => $self->{END_Y}, NAME => 'resize'},) ;
415 }
416  
417 #-----------------------------------------------------------------------------
418  
419 sub get_selection_action
420 {
421 my ($self, $x, $y) = @_ ;
422  
423 if ($x == $self->{END_X} && $y == $self->{END_Y})
424 {
425 'resize' ;
426 }
427 else
428 {
429 'move' ;
430 }
431 }
432  
433 #-----------------------------------------------------------------------------
434  
435 sub resize
436 {
437 my ($self, $reference_x, $reference_y, $new_x, $new_y) = @_ ;
438  
439 my $new_end_x = $new_x ;
440 my $new_end_y = $new_y ;
441  
442 $self->setup($self->{ARROW_TYPE}, $new_end_x, $new_end_y, $self->{EDITABLE}) ;
443  
444 return(0, 0, $self->{END_X} + 1, $self->{END_X} + 1) ;
445 }
446  
447 #-----------------------------------------------------------------------------
448  
449 sub get_text { my ($self) = @_ ; }
450  
451 #-----------------------------------------------------------------------------
452  
453 sub set_text { my ($self) = @_ ; }
454  
455 #-----------------------------------------------------------------------------
456  
457 sub edit
458 {
459 my ($self, $asciio) = @_ ;
460  
461 $self->display_box_edit_dialog() ;
462  
463 $self->setup($self->{ARROW_TYPE}, $self->{END_X}, $self->{END_Y}, $self->{EDITABLE}) ;
464 }
465  
466 #-----------------------------------------------------------------------------
467  
468 1 ;