#!/usr/bin/perl -w
use strict;

# ming-0.3cvs(07.04.2003)
use lib("/aodv/bin/lib/site_perl");

# ming-0.2a
#use lib("/aodv/bin/lib2/site_perl");

use SWF qw(:ALL);
use SWF::Constants qw(:Button);


#SWF::setVersion(4);
SWF::setVersion(5);

print "Mingversion: $SWF::VERSION\n";
my $fontdir="/aodv/fonts";
my $fontfile = "$fontdir/_sans.fdb";

my $moviex=400;
my $moviey=300;
my $winx=100;

my $movie= new SWF::Movie;
$movie->setDimension(400,300);
my $closeShape=&makeClose(0xdd, 0xdd, 0xdd, 0xff, 16, 16, 0,0,0,0);
my $closeShape2=&makeClose(0xff, 0xcc, 0x33, 0xff, 16, 16, 0,0,0,0);
my $font = new SWF::Font("$fontfile");
my $s=makerect(0xff, 0xff, 0xcc, 0xff, $winx, 100, 1);


my $mc1= new SWF::MovieClip;
my $mc2= new SWF::MovieClip;
my $mc3= new SWF::MovieClip;

my $s_i1=$mc1->add($s);
my $s_i2=$mc2->add($s);
my $s_i3=$mc3->add($s);

# F4 style
#my $dragbar1=&makedragbar($winx,"Drag All","../../");
#my $dragbar2=&makedragbar($winx,"Drag Me","../");
#my $dragbar3=&makedragbar($winx,"Drag Other","_level0/window1");

# F5 style not working, drags only the dragbar
my $dragbar1=&makedragbar($winx,"Drag All","_parent._parent");
my $dragbar2=&makedragbar($winx,"Drag Me","_parent");
my $dragbar3=&makedragbar($winx,"Drag Other","_root.window1");

my $dragbar_i1=$mc1->add($dragbar1);
my $dragbar_i2=$mc2->add($dragbar2);
my $dragbar_i3=$mc3->add($dragbar3);

$mc1->nextFrame;
$mc2->nextFrame;
$mc3->nextFrame;

my $mc_i1=$movie->add($mc1);
my $mc_i2=$movie->add($mc2);
my $mc_i3=$movie->add($mc3);

$mc_i1->setName("window1");
$mc_i2->setName("window2");
$mc_i3->setName("window3");

$mc_i1->move(10,40);
$mc_i2->move(120,40);
$mc_i3->move(230,40);

$movie->nextFrame;
$movie->save("$0.swf");


sub makedragbar {
	my ($w,$text,$target) =@_;

	my $t=new SWF::Text;
	$t->setFont($font);
	$t->setHeight(16);
	$t->setColor(0xff,0xff,0xff);
	$t->addString($text);

	my $drag = new SWF::Button;
	my $s =makerect(0xff, 0x99, 0x00, 0xff, $w, 20, 1);
	my $s2 =makerect(0xff, 0xcc, 0x33, 0xff, $w, 20, 1);
	
	$drag->addShape($s, SWFBUTTON_HIT | SWFBUTTON_UP);
	$drag->addShape($s2, SWFBUTTON_DOWN | SWFBUTTON_OVER);
	
  # F5
	$drag->addAction(new SWF::Action("startDrag('$target', 0,-$w+40,$moviex-20,10,$moviey);"), SWFBUTTON_MOUSEDOWN);
	#$drag->addAction(new SWF::Action("startDrag('$target', 0);"), SWFBUTTON_MOUSEDOWN);
	$drag->addAction(new SWF::Action("stopDrag();"), SWFBUTTON_MOUSEUPOUTSIDE | SWFBUTTON_MOUSEUP);

	my $close = new SWF::Button;
	$close->addShape($closeShape, SWFBUTTON_HIT | SWFBUTTON_UP);
	$close->addShape($closeShape2, SWFBUTTON_DOWN | SWFBUTTON_OVER);
	#$close->addAction(new SWF::Action("$target._visible=0;"), SWFBUTTON_MOUSEUP);
	
	# F5
	my $as=$target.'._visible=0;';
	$close->addAction(new SWF::Action("$as"), SWFBUTTON_MOUSEUP);
		
	my $mc = new SWF::MovieClip;
	my $drag_i=$mc->add($drag);
	$drag_i->move(0,-20);
	
	my $t_i = $mc->add($t);
	$t_i->move(5,-4);
	
	my $close_i = $mc->add($close);
	$close_i->move($w-18,-18);
	
	$mc->nextFrame;

	return $mc;
}

# make a closeshape for a close button
sub makeClose{ 
	# red,green,blue,alpha,width,heigth,linewidth,linered,linegreen,lineblue  
	my($r,$g,$b,$a,$x,$y,$lw,$lr,$lg,$lb) = @_;
	
	my $s = new SWF::Shape;
	
	# more precision
	SWF::setScale(1);
	$s->setLine($lw, 0, 0, 0);
	$s-> movePenTo($x*0.25*20, $y*0.25*20);
	$s->drawLineTo($x*0.75*20, $y*0.75*20);
	$s-> movePenTo($x*0.75*20, $y*0.25*20);
	$s->drawLineTo($x*0.25*20, $y*0.75*20);
	
	$s->setLine($lw, 0, 0, 0);
	$s->setRightFill($r,$g,$b,$a);
	$s->movePenTo(0, 0);
	$s->drawLineTo($x*20,0);
	$s->drawLineTo($x*20,$y*20);
	$s->drawLineTo(0,$y*20);
	$s->drawLineTo(0,0);
	
	# change back to default value ..
	SWF::setScale(20);
	return $s;
}

# a simple rectangle
sub makerect{   
	my($r,$g,$b,$a,$x,$y,$lw,$lr,$lg,$lb) = @_;
	my $s = new SWF::Shape;
	$s->setLine($lw,0,0,0);
	$s->setRightFill($r,$g,$b,$a);
	$s->drawLine($x,0);
	$s->drawLine(0,$y);
	$s->drawLine(-$x,0);
	$s->drawLineTo(0,0);
	return $s;
}
