# Started in 2009, they are known to work in modo 302.
#

use strict;


my $gVariables;
my $gPrefix;

sub lxSetVariables($$)
{
    ($gVariables,$gPrefix) = (@_);
}

#
=item lxInfo(message)

Show an info dialog

=cut

sub lxInfo($)
{
    my ($msg) = (@_);
    lx("dialog.setup info");

    $msg =~ s/[\"]//gm;
    lx("dialog.msg \"$msg\"");
    lx("dialog.open");
    my $r = lxq("dialog.result ?");
    return $r;
}

=item int l xGetItemCount()

Get the number of items in the scene.

=cut

sub lxGetItemCount()
{
    my $k = lxq("query sceneservice item.N ? current");
    return $k;
}

=item lxGetItemAttr(index,attribute)

For an indexed scene item, get one attribute, like "name", "type" or "id"

=cut

sub lxGetItemAttr($$)
{
    my ($ix,$prop) = (@_);
    $ix = $ix * 1;
    my $value = lxq("query sceneservice item.$prop ? $ix");
    return $value;
}

=item lxGetItemName(index)
Get an item's name.
=cut
sub lxGetItemName($)
{
    my ($ix) = (@_);
    return lxGetItemAttr($ix,"name");
}
=item lxGetItemType(index)
Get an item's type
=cut
sub lxGetItemType($)
{
    my ($ix) = (@_);
    return lxGetItemAttr($ix,"type");
}
=item lxGetItemId(index)
Get an item's id
=cut
sub lxGetItemId($)
{
    my ($ix) = (@_);
    return lxGetItemAttr($ix,"id");
}
=item lxSetLayerVisible(idOrIndex,visible)
Set item's little eyeball by index or name
=cut
sub lxSetLayerVisible($$)
{
    my ($id,$vis) = (@_);
    if($id > 0)
    {
        # oops, got an item index instead of a magic layer id
        # no problem, fix it.
        $id = lxGetItemId($id);
    }
    $vis = ($vis > 0) ? 1 : -1;
    lx("layer.setVisibility $id $vis");
}
=item lxSetIndirectIllum($)
Set the render property indirect illumination
=cut
sub lxSetIndirectIllum($)
{
    my ($v) = (@_);
    if($v eq "true" or $v eq "yes" or $v eq "on")
    {
        $v = 1;
    }
    else
    {
        $v = $v * 1;
    }
    $v = $v ? "true" : "false";

    lx("item.channel polyRender\$globEnable $v");
}

=item lxGetConfigValue(name,type)

First, use the global variable prefix $gPrefix that
you set from your main program.

Second, if the variable is not already available, make
it become available, with the type specified.

TODO: allow initializing, range, choices, &c
=cut
sub lxGetConfigValue($$)
{
    my ($name,$type) = (@_);
    if(defined($gPrefix))
    {
        $name = $gPrefix . "." . $name;
    }

    if (lxq("query scriptsysservice userValue.isdefined ? $name") == 0)
	{
		lx( "user.defNew $name $type");
	}
    my $result = lxq("user.value $name ?");
    return $result;
}


sub lxGetVariables(@)
{
    my (@argv) = (@_);

    # first, get the current values from the config, so if they're set from
    # a form or other Modo means, they're there. Then, let any arguments to
    # the script like name=value override them.

    my %values;

    if(defined($gVariables))
    {
        foreach my $name (keys(%$gVariables))
        {
            my $type = $$gVariables{$name};
            my $value = lxGetConfigValue($name,$type);
            $values{$name} = $value;
        }
    }



    foreach my $a (@argv)
    {
        if($a =~ /^(.*?)=(.*)$/)
        {
            my $name = $1;
            my $value = $2;
            $values{$name} = $value;
        }
    }

    return \%values;
}

# library success:
return 1;


# end of file.
