#!/usr/bin/perl

#  This is slider, a Gimp-perl plugin by Hugh Denman, based on basic_logo by Dov Grobgeld
#  This program is under the Gnu Public License
#  The code comes with no warranty whatsoever; see the GPL for further details
#  Hugh Denman, November 29, 1999

use Gimp qw/:auto/;
use Gimp::Fu;


sub slider {
    my($font, $border, $text, $bgcolor, $fgcolor, $aa, $frames, $step, $delay, $trans) = @_;

    my ($textlayers, $bglayer, $i);

    # Create a new image of an arbitrary size
    $img = gimp_image_new(100, 100, RGB);

    if (! $trans)
      {
	# Create a new layer for the background and add it to the image
	my $background = gimp_layer_new($img, 100, 100,
					RGB, "Background", 100,
					NORMAL_MODE);
	gimp_image_add_layer($background, 1);
      }
    
    # Set color for text
    gimp_palette_set_foreground($fgcolor);

    # Create the text layer. Using -1 as the drawable creates a new layer.
    my $text_layer = gimp_text_fontname($img, -1, 0, 0, $text,
					$border, $aa, 
					xlfd_size($font), $font);

    # Get size of the text drawable and resize the image and the
    # background layer to this size.
    my($width, $height) = ($text_layer->width, $text_layer->height);
    gimp_image_resize($img, $width, $height, 0, 0);
    
    # Sort out background unless transparent
    if (! $trans)
      {
	gimp_layer_resize($background, $width, $height, 0, 0);
	gimp_palette_set_background($bgcolor);
	gimp_edit_fill($background);
	# Merge background and text
	$background = $img->merge_visible_layers(1);
      }
    else {$background=$text_layer;}

    # Set final delay. In gimp animations, you set the delay of a frame 
    # using the name of the corresponding layer.
    # For example, a layer called 'bground (1000ms)' will have a delay of 1 sec

    $background->set_name("bground (${delay}ms)");

    # Protect this layer from further merging
    $background->set_visible(0);

    # Create the subsequent frames
    for ($i=1; $i<$frames; $i++)
      {
	# the order in which layers are created here matters
	# the background has to be created first, so's it's below
	# the two text layers. Otherwise it obscurs them.

	# background layer for this frame (none if transparent)
	if (!$trans)
	  {
	    $bglayer = gimp_layer_new ($img,$width,$height,
				       RGB_IMAGE,"bg",100,NORMAL_MODE);
	    gimp_drawable_fill ($bglayer,BG_IMAGE_FILL);
	    gimp_image_add_layer($img, $bglayer, 0);
	  }

	# left hand side text
	$textlayers[$i] = gimp_text_fontname($img, -1, -($step*$i), 0, $text,
					     $border, $aa, 
					     xlfd_size($font), $font);
	
	# right hand side text
	$textlayers[$i+1] = gimp_text_fontname($img, -1, $step*$i, 0, $text,
					       $border, $aa, 
					       xlfd_size($font), $font);

	#merge both text layers, & frame background if there is one
	$textlayers[$i] = $img->merge_visible_layers(1);

	$img->lower_layer_to_bottom($textlayers[$i]);

	# protect from further merging
	$textlayers[$i]->set_visible(0);
      }
    

    # make all layers visible. There is a nicer way to do this
    # but it doesn't work for me :(

    for ($i=1;$i<$frames;$i++) {$textlayers[$i]->set_visible(1);}
    $background->set_visible(1);

    # some of the layers extend outside the image; crop 'em
    gimp_crop($img,$width,$height,0,0);

    return $img;
}

# register the script
register "slider", "sliding text", "sliding text",
    "Hugh Denman", "Hugh Denman",
    "1999-29-11",
    "<Toolbox>/Xtns/Perl-Fu/Slider", 
    "*",
    [
     [PF_FONT,   "font",       "font",   "-*-desdemona-*-*-*-*-*-190-*-*-*-*-*-*"],
     [PF_INT,    "border",     "border", "10"],
     [PF_STRING, "text",       "text", "Hello world!"],
     [PF_COLOR,  "bg_color",   "Background color", [0,0,0]],
     [PF_COLOR,  "fg_color",   "Foreground color", [255,255,255]],
     [PF_INT,    "antialias",  "0=Don't antialias, 1= do", "1"],
     [PF_INT,    "frames",     "Number of frames", "20"],
     [PF_INT,    "stepsize",   "Pixels per frame", "5"],
     [PF_INT,    "delay",      "Wait on last frame", "1000"],
     [PF_INT,    "transparent","Tranparent background (0=no, 1=yes)", "0"],
    ],
    \&slider;

# Handle over control to gimp
exit main();
