blib/lib/App/Asciio/Scripting.pm untested

filter: Statementstmt 0.0% 0 / 130 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 196 CC 6 SCAR 15.4
CC
6
Cov
0%
move_named_connector
20.0
new_script_asciio
12.0
add
2.0
CRAP 4.7
1  
2 package App::Asciio::Scripting ;
3  
4 require Exporter ;
5 @ISA = qw(Exporter) ;
6 @EXPORT = qw(
7 new_script
8 add
9 add_type
10 connect_elements
11 set_connection
12 optimize
13 save_to
14 to_ascii
15 ascii_out
16 new_box
17 new_wirl_arrow
18 add_connection
19 move_named_connector
20 optimize_connections
21 get_canonizer
22 ) ;
23  
24 use strict ; use warnings ;
25 use utf8 ;
26  
27 use Module::Util qw(find_installed) ;
28 use File::Basename ;
29 use File::Slurp ;
30  
31 use App::Asciio ;
32 use App::Asciio::Connections ;
33 use App::Asciio::Elements ;
34 use App::Asciio::Io ;
35 use App::Asciio::Options ;
36  
37 use App::Asciio::stripes::angled_arrow ;
38 use App::Asciio::stripes::angled_arrow ;
39 use App::Asciio::stripes::section_wirl_arrow ;
40 use App::Asciio::stripes::stripes ;
41 use App::Asciio::stripes::wirl_arrow ;
42  
43 # use Data::TreeDumper ;
44 # sub ddt { print DumpTree @_ ; }
45  
46 #--------------------------------------------------------------------------------------------
47  
48 {
49  
50 my $script_asciio ; # make script non OO
51 my %name_to_element ;
52  
53 sub new_script_asciio
54 {
55 $script_asciio = new App::Asciio() ;
56  
57 my ($command_line_switch_parse_ok, $command_line_parse_message, $asciio_config)
58 = $script_asciio->ParseSwitches([@ARGV], 0) ;
59  
60 die "Error: '$command_line_parse_message'!" unless $command_line_switch_parse_ok ;
61  
62 if(@{$asciio_config->{SETUP_PATHS}})
63 {
64 $script_asciio->setup($asciio_config->{SETUP_PATHS}) ;
65 }
66 else
67 {
68 my ($basename, $path, $ext) = File::Basename::fileparse(find_installed('App::Asciio'), ('\..*')) ;
69 $script_asciio->setup([$path . $basename . '/setup/setup.ini']) ;
70 }
71 }
72  
73 # initialize object when module is loaded
74 new_script_asciio() ;
75  
76 #--------------------------------------------------------------------------------------------
77  
78 sub add
79 {
80 my ($name, $element, $x, $y) = @_ ;
81  
82 $name_to_element{$name} = $element ;
83 $script_asciio->add_element_at($element, $x, $y) ;
84 }
85  
86 sub add_type
87 {
88 my ($name, $type, $x, $y) = @_ ;
89  
90 $name_to_element{$name} = $script_asciio->add_new_element_named('Asciio/Boxes/process', $x, $y) ;
91 }
92  
93 sub connect_elements
94 {
95 my ($element1_name, $element2_name, @args) = @_ ;
96  
97 add_connection($script_asciio, @name_to_element{$element1_name, $element2_name}, @args) ;
98 }
99  
100 sub save_to { $script_asciio->save_with_type(undef, 'asciio', $_[0]) ; }
101 sub to_ascii { $script_asciio->transform_elements_to_ascii_buffer() ; }
102 sub ascii_out { print $script_asciio->transform_elements_to_ascii_buffer() ; }
103 sub optimize { $script_asciio->call_hook('CANONIZE_CONNECTIONS', $script_asciio->{CONNECTIONS}) ; }
104 sub set_connection { $script_asciio->add_connections(@_) ; }
105  
106 }
107  
108 #--------------------------------------------------------------------------------------------
109  
110 sub new_box
111 {
112 my (@arguments_to_constructor) = @_ ;
113  
114 use App::Asciio::stripes::editable_box2 ;
115  
116 new App::Asciio::stripes::editable_box2
117 ({
118 TEXT_ONLY => 'box',
119 TITLE => '',
120 EDITABLE => 1,
121 RESIZABLE => 1,
122 @arguments_to_constructor,
123 }) ;
124 }
125  
126 #-----------------------------------------------------------------------------------------------------------
127  
128 sub new_wirl_arrow
129 {
130 new App::Asciio::stripes::section_wirl_arrow
131 ({
132 POINTS => [[5, 5, 'downright']],
133 DIRECTION => '',
134 ALLOW_DIAGONAL_LINES => 0,
135 EDITABLE => 1,
136 RESIZABLE => 1,
137 @_,
138 }) ;
139 }
140  
141 #--------------------------------------------------------------------------------------------
142  
143 sub add_connection
144 {
145 my ($self, $source_element, $destination_element, $hint, @arguments_to_constructor) = @_ ;
146  
147 $hint ||= 'right-down' ;
148  
149 my @destination_connections = grep {$_->{NAME} ne 'resize'} $destination_element->get_connection_points() ;
150 my $destination_connection = $destination_connections[0] ;
151  
152 my @source_connections = grep {$_->{NAME} ne 'resize'} $source_element->get_connection_points() ;
153 my $source_connection = $source_connections[0] ;
154  
155 my $new_element = new App::Asciio::stripes::section_wirl_arrow
156 ({
157 POINTS =>
158 [
159 [
160 ($destination_element->{X} + $destination_connection->{X})
161 - ($source_element->{X} + $source_connection->{X}) ,
162
163 ($destination_element->{Y} + $destination_connection->{Y})
164 - ($source_element->{Y} + $source_connection->{Y}) ,
165
166 $hint,
167 ]
168 ],
169
170 DIRECTION => $hint,
171 ALLOW_DIAGONAL_LINES => 0,
172 EDITABLE => 1,
173 RESIZABLE => 1,
174 @arguments_to_constructor,
175 }) ;
176  
177 @$new_element{'X', 'Y'} = ($source_element->{X} + $source_connection->{X}, $source_element->{Y} + $source_connection->{Y}) ;
178  
179 $self->add_elements($new_element) ;
180 }
181  
182 #--------------------------------------------------------------------------------------------
183  
184 sub move_named_connector
185 {
186 my ($connected, $connector_name, $connectee, $connection_name) = @_ ;
187  
188 do { die "Invalid argument to 'move_named_connector'!\n" unless defined $_} for (@_) ;
189 die "Invalid number of arguments to 'move_named_connector'!\n" unless @_ == 4 ;
190
191 my $connector = $connected->get_named_connection($connector_name) ;
192 my $connection = $connectee->get_named_connection($connection_name) ;
193  
194 if(defined $connector && defined $connection)
195 {
196 my $connector_x = $connected->{X} + $connector->{X} ;
197 my $connector_y = $connected->{Y} + $connector->{Y} ;
198
199 my $connection_x = $connectee->{X} + $connection->{X} ;
200 my $connection_y = $connectee->{Y} + $connection->{Y} ;
201
202 my $connector_x_offset = $connection_x - $connector_x ;
203 my $connector_y_offset = $connection_y - $connector_y ;
204
205 my ($x_offset, $y_offset, $width, $height, $new_connector) =
206 $connected->move_connector
207 (
208 $connector_name,
209 $connector_x_offset, $connector_y_offset
210 ) ;
211
212 $connected->{X} += $x_offset ;
213 $connected->{Y} += $y_offset ;
214
215 return
216 {
217 CONNECTED => $connected,
218 CONNECTOR =>$new_connector,
219 CONNECTEE => $connectee,
220 CONNECTION => $connection,
221 } ;
222 }
223 else
224 {
225 return ;
226 }
227 }
228  
229 #-----------------------------------------------------------------------------------------------------------
230  
231 sub optimize_connections
232 {
233 my ($self) = @_;
234 $self->call_hook('CANONIZE_CONNECTIONS', $self->{CONNECTIONS}) ;
235 }
236  
237 #--------------------------------------------------------------------------------------------
238  
239 sub get_canonizer
240 {
241 my ($basename, $path, $ext) = File::Basename::fileparse(find_installed('App::Asciio'), ('\..*')) ;
242 my $setup = $path . $basename . "/setup" ;
243  
244 my $context = new Eval::Context() ;
245  
246 $context->eval
247 (
248 REMOVE_PACKAGE_AFTER_EVAL => 0, # VERY IMPORTANT as we return code references that will cease to exist otherwise
249 PRE_CODE => <<'EOC' ,
250 use strict;
251 use warnings;
252  
253 sub register_hooks
254 {
255 return \&canonize_connections ;
256 }
257  
258 EOC
259 CODE_FROM_FILE => $setup . '/hooks/canonize_connections.pl' ,
260 ) ;
261 }
262 # ~/nadim/devel/repositories/perl_modules/P5-App-Asciio/setup/hooks/canonize_connections.pl"
263  
264 #--------------------------------------------------------------------------------------------
265  
266 1 ;