Skip to content

Commit

Permalink
CHANGED: scripting interface
Browse files Browse the repository at this point in the history
added: interface for wirl-arrows

changed: -s runs script in asciio

ADDED: ZBuffer stacked neighbor functions
  • Loading branch information
nkh committed Nov 16, 2023
1 parent 2b0171d commit f2b982e
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 79 deletions.
18 changes: 9 additions & 9 deletions documentation/mdbook_asciio/src/for_developers/scripting.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,10 @@ You'll also need to use the right module; in fact you have access to everything
use strict; use warnings;

use App::Asciio::Scripting ;
use App::Asciio::stripes::section_wirl_arrow;

#-----------------------------------------------------------------------------

add 'multi_wirl',
new App::Asciio::stripes::section_wirl_arrow
({
POINTS => [[5, 5, 'downright'], [10, 7, 'downright'], [7, 14, 'downleft'], ],
DIRECTION => '',
}),
5, 5 ;
add 'multi_wirl', new_wirl_arrow([5, 5, 'downright'], [10, 7, 'downright'], [7, 14, 'downleft']), 5, 5 ;

ascii_out ;
```
Expand All @@ -137,17 +130,19 @@ You can find more examples in the *documentation/scripting/* library of the proj
use strict; use warnings;

use App::Asciio::Scripting ;
use App::Asciio::stripes::process_box ;

#-----------------------------------------------------------------------------

add 'text1', new_text(TEXT_ONLY =>'text'), 22, 20 ;

add 'box1', new_box(TEXT_ONLY =>'box1'), 0, 2 ;
add 'box2', new_box(TEXT_ONLY =>'box2'), 20, 10 ;
add 'box3', new_box(TEXT_ONLY =>'box3'), 40, 5 ;

connect_elements 'box1', 'box2', 'down' ;
connect_elements 'box2', 'box3' ;
connect_elements 'box3', 'box1', 'up' ;
connect_elements 'box2', 'text1' ;

my $process = add_type 'process', 'Asciio/Boxes/process', 5, 15 ;
$process->set_text("line 1\nline 2\nline 3") ;
Expand Down Expand Up @@ -175,4 +170,9 @@ perl my_asciio_script.pl

Pick the file you want to execute.

Or pass it on the command line

```
asciio -s full_path_to_script
```

5 changes: 3 additions & 2 deletions documentation/scripting/connected_boxes.pl
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
use strict; use warnings;

use App::Asciio::Scripting ;
use App::Asciio::stripes::process_box ;

#-----------------------------------------------------------------------------

add 'box1', new_text(TEXT_ONLY =>'text'), 0, 20 ;
add 'text1', new_text(TEXT_ONLY =>'text'), 22, 20 ;

add 'box1', new_box(TEXT_ONLY =>'box1'), 0, 2 ;
add 'box2', new_box(TEXT_ONLY =>'box2'), 20, 10 ;
add 'box3', new_box(TEXT_ONLY =>'box3'), 40, 5 ;

connect_elements 'box1', 'box2', 'down' ;
connect_elements 'box2', 'box3' ;
connect_elements 'box3', 'box1', 'up' ;
connect_elements 'box2', 'text1' ;

my $process = add_type 'process', 'Asciio/Boxes/process', 5, 15 ;
$process->set_text("line 1\nline 2\nline 3") ;
Expand Down
11 changes: 2 additions & 9 deletions documentation/scripting/multi_wirl.pl
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@
use strict; use warnings;

use App::Asciio::Scripting ;
use App::Asciio::stripes::section_wirl_arrow;

#-----------------------------------------------------------------------------

add 'multi_wirl',
new App::Asciio::stripes::section_wirl_arrow
({
POINTS => [[5, 5, 'downright'], [10, 7, 'downright'], [7, 14, 'downleft'], ],
DIRECTION => '',
}),
5, 5 ;
add 'multi_wirl', new_wirl_arrow([5, 5, 'downright'], [10, 7, 'downright'], [7, 14, 'downleft']), 5, 5 ;

print to_ascii() ;
ascii_out ;

21 changes: 20 additions & 1 deletion lib/App/Asciio/GTK/Asciio.pm
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,26 @@ $gc->set_line_width(1);
my ($character_width, $character_height) = $self->get_character_size() ;
my ($widget_width, $widget_height) = ($widget->get_allocated_width(), $widget->get_allocated_height()) ;

# draw background
# my $zbuffer = App::Asciio::ZBuffer->new($self->{ELEMENTS}->@*) ;

# while( my($coordinate, $elements) = each $zbuffer->{intersecting_elements}->%*)
# {
# use App::Asciio::ZBuffer ;
# use Data::TreeDumper ;

# my $neighbors = $zbuffer->get_neighbors($coordinate) ;
# print DumpTree { stack => $elements, neighbors => $neighbors }, $coordinate ;

# my $neighbors = $zbuffer->get_neighbors_stack($coordinate) ;
# print DumpTree { stack => $elements, neighbors => $neighbors }, $coordinate ;

# my $neighbors = $zbuffer->get_cardinal_neighbors($coordinate) ;
# print DumpTree { stack => $elements, neighbors => $neighbors }, $coordinate ;

# my $neighbors = $zbuffer->get_cardinal_neighbors_stack($coordinate) ;
# print DumpTree { stack => $elements, neighbors => $neighbors }, $coordinate ;
# }

my $grid_rendering = $self->{CACHE}{GRID} ;

unless (defined $grid_rendering)
Expand Down
6 changes: 1 addition & 5 deletions lib/App/Asciio/Options.pm
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ my @flags_and_help =
'',

's|script=s' => \$asciio_config->{SCRIPT},
'script to be run at AsciiO start.',
'',

'h|help' => \$asciio_config->{HELP},
'Displays some help.',
'script to be run at Asciio start.',
'',
) ;

Expand Down
39 changes: 24 additions & 15 deletions lib/App/Asciio/Scripting.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,30 @@ require Exporter ;
@ISA = qw(Exporter) ;
@EXPORT = qw(
new_script
add
add_type
connect_elements
set_connection
optimize
save_to
to_ascii
ascii_out
new_box
new_text
new_wirl_arrow
select_all_elements
deselect_all_elements
connect_elements
set_connection
add_connection
move_named_connector
optimize_connections
get_canonizer
optimize
save_to
to_ascii
ascii_out
run_external_script
select_all_elements
deselect_all_elements
) ;

use strict ; use warnings ;
Expand All @@ -44,8 +50,8 @@ use App::Asciio::stripes::section_wirl_arrow ;
use App::Asciio::stripes::stripes ;
use App::Asciio::stripes::wirl_arrow ;

# use Data::TreeDumper ;
# sub ddt { print DumpTree @_ ; }
use Data::TreeDumper ;
sub ddt { print DumpTree @_ ; }

#--------------------------------------------------------------------------------------------

Expand All @@ -56,9 +62,9 @@ my %name_to_element ;

sub run_external_script
{
my ($asciio) = @_ ;
my ($asciio, $script) = @_ ;

my $script = $asciio->get_file_name() ;
$script //= $asciio->get_file_name() ;

if(defined $script)
{
Expand All @@ -72,7 +78,7 @@ if(defined $script)

sub new_script_asciio
{
$script_asciio = new App::Asciio() ;
$script_asciio = App::Asciio->new() ;

my ($command_line_switch_parse_ok, $command_line_parse_message, $asciio_config)
= $script_asciio->ParseSwitches([@ARGV], 0) ;
Expand Down Expand Up @@ -194,14 +200,17 @@ new App::Asciio::stripes::editable_box2

sub new_wirl_arrow
{
my @points = @_ ;

@points = ([5, 5, 'downright']) unless @points ;

new App::Asciio::stripes::section_wirl_arrow
({
POINTS => [[5, 5, 'downright']],
POINTS => [@points],
DIRECTION => '',
ALLOW_DIAGONAL_LINES => 0,
EDITABLE => 1,
RESIZABLE => 1,
@_,
}) ;
}

Expand Down
14 changes: 2 additions & 12 deletions lib/App/Asciio/Setup.pm
Original file line number Diff line number Diff line change
Expand Up @@ -507,19 +507,9 @@ my($self, $script) = @_ ;

if(defined $script)
{
my $context = new Eval::Context() ;

$context->eval
(
PRE_CODE => "use strict;\nuse warnings;\n",
CODE_FROM_FILE => $script,
INSTALL_VARIABLES =>
[
[ '$self' => $self => $Eval::Context::SHARED ],
] ,
) ;
require App::Asciio::Scripting ;

die "Asciio: can't load setup file '$script': $! $@\n" if $@ ;
App::Asciio::Scripting::run_external_script($self, $script) ;
}
}

Expand Down
104 changes: 78 additions & 26 deletions lib/App/Asciio/ZBuffer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ for my $element (@elements)
}
}

my $t1 = Time::HiRes::gettimeofday();
printf "add time: %0.4f sec.\n", $t1 - $t0 ;
# my $t1 = Time::HiRes::gettimeofday();
# printf "add time: %0.4f sec.\n", $t1 - $t0 ;
}

# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -134,14 +134,16 @@ sub get_neighbors
my ($self, $coordinate) = @_ ;
my ($x, $y) = split ';', $coordinate ;

# order: 315, up, 45, right, 135, down, 225, left

return
{
map
{
exists $self->{coordinates}{$_}
? (
$self->{coordinates}{$_} ne ' '
? ($_ => $self->{coordinates}{$_})
? ( $self->{coordinates}{$_} ne ' ' ? ($_ => $self->{coordinates}{$_}) : ())
: ()
)
: () }
Expand All @@ -150,34 +152,84 @@ return
($x-1) .';'. ($y+1), $x .';'. ($y+1), ($x+1) .';'. ($y+1)
}
}

# ------------------------------------------------------------------------------

# sub get_cardinal_neighbors
# {
# my ($self, $coordinate) = @_ ;
# my ($x, $y) = split ';', $coordinate ;
sub get_neighbors_stack
{
my ($self, $coordinate) = @_ ;
my ($x, $y) = split ';', $coordinate ;

# return
# {
# map
# {
# exists $self->{coordinates}{$_}
# ? ( $self->{coordinates}{$_} ne ' ' ? ($_ => $self->{coordinates}{$_}) : undef)
# : undef
# }
# $x .';'. ($y-1),
# $x .';'. ($y+1),
# ($x+1) .';'. $y,
# ($x-1) .';'. $y,
# ($x+1) .';'. ($y-1),
# ($x+1) .';'. ($y+1),
# ($x-1) .';'. ($y+1),
# ($x-1) .';'. ($y-1) ;
# }
# }
# order: 315, up, 45, right, 135, down, 225, left

return
{
map
{
exists $self->{coordinates}{$_}
? (
$self->{coordinates}{$_} ne ' '
? (exists $self->{intersecting_elements}{$_} ? ($_ => $self->{intersecting_elements}{$_}) : ($_ => [$self->{coordinates}{$_}]))
: ()
)
: () }
($x-1) .';'. ($y-1), $x .';'. ($y-1), ($x+1) .';'. ($y-1),
($x-1) .';'. $y, ($x+1) .';'. $y,
($x-1) .';'. ($y+1), $x .';'. ($y+1), ($x+1) .';'. ($y+1)
}
}

# ------------------------------------------------------------------------------

sub get_cardinal_neighbors
{
# returns undef for non existing neighbors

my ($self, $coordinate) = @_ ;
my ($x, $y) = split ';', $coordinate ;

# order: 315, up, 45, right, 135, down, 225, left

# my ($up, $down, $left, $right, $char_45, $char_135, $char_225, $char_315) = $zbuffer->get_cardinal_neighbors() ;
return
{
map
{
$_ => $self->{coordinates}{$_}
# exists $self->{coordinates}{$_}
# ? ( $self->{coordinates}{$_} ne ' ' ? ($_ => $self->{coordinates}{$_}) : ($_ => undef) )
# : undef
}
($x-1) .';'. ($y-1), $x .';'. ($y-1), ($x+1) .';'. ($y-1),
($x-1) .';'. $y, ($x+1) .';'. $y,
($x-1) .';'. ($y+1), $x .';'. ($y+1), ($x+1) .';'. ($y+1)
}

}

# ------------------------------------------------------------------------------

sub get_cardinal_neighbors_stack
{
# returns undef for non existing neighbors

my ($self, $coordinate) = @_ ;
my ($x, $y) = split ';', $coordinate ;

# order: 315, up, 45, right, 135, down, 225, left

return
{
map
{
exists $self->{coordinates}{$_}
? (exists $self->{intersecting_elements}{$_} ? ($_ => $self->{intersecting_elements}{$_}) : ($_ => [$self->{coordinates}{$_}]))
: ($_ => undef )
}
($x-1) .';'. ($y-1), $x .';'. ($y-1), ($x+1) .';'. ($y-1),
($x-1) .';'. $y, ($x+1) .';'. $y,
($x-1) .';'. ($y+1), $x .';'. ($y+1), ($x+1) .';'. ($y+1)
}
}

# ------------------------------------------------------------------------------

Expand Down

0 comments on commit f2b982e

Please sign in to comment.