#!/usr/bin/perl -w

#
# Jacek Fedorynski <jfedor@jfedor.org>
# http://www.jfedor.org/
#
# A Gimp plugin that creates a lightsaber effect.
#
# Put it in $HOME/.gimp-1.2/plug-ins/ or whatever place you consider
# appropriate.
#
# 2002-06-04: version 0.1
#

use strict;

use Gimp ":auto";
use Gimp::Fu;
use Gimp::Util;

sub lightsaber {
	my ($img, $drawable, $color, $leave_layers, $round,
		$inner_glow_opacity,
		$outer_glow_opacity,
		$roundness,
		$scale,
		$core_feather,
		$inner_glow_grow,
		$inner_glow_feather,
		$outer_glow_grow,
		$outer_glow_feather,
	) = @_;

	map { $_ *= $scale } ( $core_feather, $inner_glow_grow, $inner_glow_feather, $outer_glow_grow, $outer_glow_feather );

	die "select the shape of the lightsaber!" if $img->selection_is_empty();

	$img->undo_push_group_start;

	my $layer_outer_glow = $img->add_new_layer(-1, TRANS_IMAGE_FILL, 1);
	my $layer_inner_glow = $img->add_new_layer(-1, TRANS_IMAGE_FILL, 1);
	my $layer_core = $img->add_new_layer(-1, TRANS_IMAGE_FILL, 1);

	$layer_outer_glow->set_name("outer glow");
	$layer_inner_glow->set_name("inner glow");
	$layer_core->set_name("core");

	perl_fu_round_sel($drawable, $roundness);

	gimp_selection_feather($img, $core_feather);

	gimp_palette_set_foreground([255, 255, 255]);

	gimp_edit_fill($layer_core, FG_IMAGE_FILL);

	gimp_selection_grow($img, $inner_glow_grow);
	gimp_selection_feather($img, $inner_glow_feather);

	gimp_palette_set_foreground($color);
	gimp_edit_fill($layer_inner_glow, FG_IMAGE_FILL);

	gimp_selection_grow($img, $outer_glow_grow);
	gimp_selection_feather($img, $outer_glow_feather);

	gimp_edit_fill($layer_outer_glow, FG_IMAGE_FILL);

	$layer_inner_glow->set_opacity($inner_glow_opacity);
	$layer_outer_glow->set_opacity($inner_glow_opacity);

	if (!$leave_layers) {
		(($layer_core->merge_down(EXPAND_AS_NECESSARY))->merge_down(EXPAND_AS_NECESSARY))->merge_down(EXPAND_AS_NECESSARY);
	}

	$img->undo_push_group_end;

	();
}

register
	"lightsaber",
	"Create a lightsaber effect",
	"Creates a lightsaber effect as seen in the Star Wars movies",
	"Jacek Fedorynski",
	"Copyright 2002 Jacek Fedorynski",
	"2002-05-23",
	"<Image>/Perl-Fu/Lightsaber",
	"*",
	[
	[PF_COLOR, "color", "The color of the blade - red for Sith sabers, etc.", [255,0,0]],
	[PF_TOGGLE, "leave_layers", "Leave the saber as 3 layers (don't flatten).", 1],
	[PF_TOGGLE, "round", "Round the selection first.", 1],
	[PF_FLOAT, "inner_glow_opacity", "Opacity of the inner glow of the blade.", 78],
	[PF_FLOAT, "outer_glow_opacity", "Opacity of the outer glow of the blade.", 48],
	[PF_INT32, "roundness", "How much to round, in pixels.", 16],
	[PF_FLOAT, "scale", "Scale value (everything below is multiplied by this). Use this if the lightsaber glow is too big/too small.", 1],
	[PF_FLOAT, "core_feather", "How \"fuzzy\" the core is.", 5],
	[PF_INT32, "inner_glow_grow", "How much bigger than the core the inner glow is.", 10],
	[PF_FLOAT, "inner_glow_feather", "How \"fuzzy\" the inner glow is.", 20],
	[PF_INT32, "outer_glow_grow", "How much bigger than the inner core the outer core is.", 40],
	[PF_FLOAT, "outer_glow_feather", "How \"fuzzy\" the outer core is.", 100],
	],
	\&lightsaber;

exit main();
