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

use SWF qw(:ALL);

SWF::setScale(1.0);
SWF::useSWFVersion(4);

my $m = new SWF::Movie();
$m->setRate(36.0);
$m->setDimension(1200, 800);
$m->setBackground(0, 0, 0);

# mouse tracking sprite - empty, but follows mouse so we can
#  get its x and y coordinates 

my $i = $m->add(new SWF::Sprite());
$i->setName('mouse');

$m->add(new SWF::Action("
    startDrag('/mouse', 1);  // '1' means lock sprite to the mouse 
  "));

# might as well turn off antialiasing, since these are just squares. 

$m->add(new SWF::Action("
    this.quality = 0;
  "));

# morphing box
my $r = new SWF::Morph();
my $s1 = $r->getShape1();

$s1->setLeftFill($s1->addFill(0xff, 0xff, 0xff));
$s1->movePenTo(-40, -40);
$s1->drawLine(80, 0);
$s1->drawLine(0, 80);
$s1->drawLine(-80, 0);
$s1->drawLine(0, -80);

my $s2 = $r->getShape2();

$s2->setLeftFill($s2->addFill(0x00, 0x00, 0x00));
$s2->movePenTo(-1, -1);
$s2->drawLine(2, 0);
$s2->drawLine(0, 2);
$s2->drawLine(-2, 0);
$s2->drawLine(0, -2);


# sprite container for morphing box -
#  this is just a timeline w/ the box morphing

my $box = new SWF::Sprite();
$box->add(new SWF::Action("
    stop();
  "));
$i = $box->add($r);

for(my $n=0; $n<=20; ++$n)
{
  $i->setRatio($n/20);
  $box->nextFrame();
}


# this container sprite allows us to use the same action code many times */

my $cell = new SWF::Sprite();
$i = $cell->add($box);
$i->setName('box');

$cell->add(new SWF::Action("

    setTarget('box');

    /* ...x means the x coordinate of the parent, i.e. (..).x */
    dx = (/mouse.x + random(6)-3 - ...x)/5;
    dy = (/mouse.y + random(6)-3 - ...y)/5;
    gotoFrame(int(dx*dx + dy*dy));

  "));

$cell->nextFrame();
$cell->add(new SWF::Action("

    gotoFrame(0);
    play();

  "));

$cell->nextFrame();


# finally, add a bunch of the cells to the movie */

for(my $x=0; $x<12; ++$x)
{
  for(my $y=0; $y<8; ++$y)
  {
    $i = $m->add($cell);
    $i->moveTo(100*$x+50, 100*$y+50);
  }
}

$m->nextFrame();

$m->add(new SWF::Action("

    gotoFrame(1);
    play();

  "));

$m->save("test.swf");
