Skip to content

Commit

Permalink
ADDED: elements can store user data
Browse files Browse the repository at this point in the history
$element->set_user_data($key, $value) ;
$element->get_user_data($key) ;
  • Loading branch information
nkh committed Dec 6, 2023
1 parent f40eda1 commit b48ff92
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 22 deletions.
2 changes: 1 addition & 1 deletion documentation/scripting/connected_boxes.pl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

optimize ;

save_to "from_script.asciio" ;
# save_to "from_script.asciio" ;

ascii_out ;

Expand Down
8 changes: 8 additions & 0 deletions lib/App/Asciio.pm
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,14 @@ return($self) ;

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

sub get_elements()
{
my ($self) = @_;
$self->{ELEMENTS}->@* ;
}

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

sub event_options_changed
{
my ($self) = @_;
Expand Down
71 changes: 50 additions & 21 deletions lib/App/Asciio/Scripting.pm
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ use App::Asciio::Actions::Colors ;
{

my $script_asciio ; # make script non OO
my %name_to_element ;

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

Expand Down Expand Up @@ -174,45 +173,50 @@ sub add
{
my ($name, $element, $x, $y) = @_ ;

$name_to_element{$name} = $element ;
$element->set_user_data(NAME => $name) ;
$element->set_user_data(SCRIPT_OBJECT => 1) ;
$script_asciio->add_element_at($element, $x, $y) ;
}

sub move
{
my ($name, $x, $y) = @_ ;

my $element = $name_to_element{$name} ;

@$element{'X', 'Y'} = ($x, $y) if defined $element && defined $x && defined $y ;
for my $element (grep { $_->get_user_data('NAME') eq $name } $script_asciio->get_elements())
{
@$element{'X', 'Y'} = ($x, $y) if defined $x && defined $y ;
}
}

sub offset
{
my ($name, $x_offset, $y_offset) = @_ ;

my $element = $name_to_element{$name} ;

@$element{'X', 'Y'} = ($element->{X} + $x_offset, $element->{Y} + $y_offset)
if defined $element && defined $x_offset && defined $y_offset ;
for my $element (grep { $_->get_user_data('NAME') eq $name } $script_asciio->get_elements())
{
@$element{'X', 'Y'} = ($element->{X} + $x_offset, $element->{Y} + $y_offset)
if defined $x_offset && defined $y_offset ;
}
}

sub select_by_name
{
my ($name) = @_ ;

my $element = $name_to_element{$name} ;

$script_asciio->select_elements(1, $element) ;
for my $element (grep { $_->get_user_data('NAME') eq $name } $script_asciio->get_elements())
{
$script_asciio->select_elements(1, $element) ;
}
}

sub delete_by_name
{
my ($name) = @_ ;

my $element = $name_to_element{$name} ;

$script_asciio->delete_elements($element) ;
for my $element (grep { $_->get_user_data('NAME') eq $name } $script_asciio->get_elements())
{
$script_asciio->delete_elements($element) ;
}
}

sub delete_selected_elements
Expand All @@ -233,27 +237,52 @@ sub add_type
{
my ($name, $type, $x, $y) = @_ ;

$name_to_element{$name} = $script_asciio->add_new_element_named('Asciio/Boxes/process', $x, $y) ;
my $element = $script_asciio->add_new_element_named($type, $x, $y) ;
$element->set_user_data(NAME => $name) ;

return $element ;
}

sub connect_elements
{
my ($element1_name, $element2_name, @args) = @_ ;

add_connection($script_asciio, @name_to_element{$element1_name, $element2_name}, @args) ;
my ($element1) = grep { ($_->get_user_data('NAME') // '') eq $element1_name } $script_asciio->get_elements() ;
my ($element2) = grep { ($_->get_user_data('NAME') // '') eq $element2_name } $script_asciio->get_elements() ;

add_connection($script_asciio, $element1, $element2, @args)
if defined $element1 && defined $element2 ;
}

sub select_elements
{
my ($state, @elements) = @_ ;
$script_asciio->select_elements($state, @name_to_element{@elements}) ;
my ($state, @elements_names) = @_ ;

my %named_elements = map { $_ => 1 } @elements_names ;

for my $element (grep { $_->get_user_data('SCRIPT_OBJECT') } $script_asciio->get_elements())
{
$script_asciio->select_elements($state, $element) ;
}
}

sub select_all_elements { $script_asciio->select_all_elements() ; }
sub select_all_script_elements { $script_asciio->select_elements(1, values %name_to_element) ; }
sub select_all_script_elements
{
for my $element (grep { $_->get_user_data('SCRIPT_OBJECT') } $script_asciio->get_elements())
{
$script_asciio->select_elements(1, $element) ;
}
}

sub deselect_all_elements { $script_asciio->deselect_all_elements() ; }
sub deselect_all_script_elements { $script_asciio->select_elements(0, values %name_to_element) ; }
sub deselect_all_script_elements
{
for my $element (grep { $_->get_user_data('SCRIPT_OBJECT') } $script_asciio->get_elements())
{
$script_asciio->select_elements(0, $element) ;
}
}

sub delete_all_ruler_lines { delete $script_asciio->{RULER_LINES} ; } ;

Expand Down
18 changes: 18 additions & 0 deletions lib/App/Asciio/stripes/stripes.pm
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,22 @@ delete $self->{CACHE};

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

sub set_user_data
{
my ($self, $key, $value) = @_ ;

$self->{USER_DATA}{$key} = $value ;
}

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

sub get_user_data
{
my ($self, $key) = @_ ;

return $self->{USER_DATA}{$key} ;
}

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

1 ;

0 comments on commit b48ff92

Please sign in to comment.