# Flash::Object

package      Flash::Object;
use          Exporter;
@ISA       = qw(Exporter);
@EXPORT    = qw(new add_tag inc_frame set_frame num_frames current_frame write set_headers to_tag from_tag);
@EXPORT_OK = qw();


my %tagdef = (

    End                         => 0,
    ShowFrame                   => 1,
    DefineShape                 => 2,
    FreeCharacter               => 3,
    PlaceObject                 => 4,
    RemoveObject                => 5,
    DefineBits                  => 6,
    DefineButton                => 7,
    JPEGTables                  => 8,
    SetBackgroundColor          => 9,
    DefineFont                  => 10,
    DefineText                  => 11,
    DoAction                    => 12,
    DefineFontInfo              => 13,
    DefineSound                 => 14,  # Event sound tags.
    StartSound                  => 15,
    DefineButtonSound           => 17,
    SoundStreamHead             => 18,
    SoundStreamBlock            => 19,
    DefineBitsLossless          => 20,  # A bitmap using lossless zlib compression.
    DefineBitsJPEG2             => 21,  # A bitmap using an internal JPEG compression table.
    DefineShape2                => 22,
    DefineButtonCxform          => 23,
    Protect                     => 24,  # This file should not be importable for editing.

    # These are the new tags for Flash 3.

    PlaceObject2                => 26,  # The new style place w/ alpha color transform and name.
    RemoveObject2               => 28,  # A more compact remove object that omits the character tag (just depth).
    DefineShape3                => 32,  # A shape V3 includes alpha values.
    DefineText2                 => 33,  # A text V2 includes alpha values.
    DefineButton2               => 34,  # A button V2 includes color transform, alpha and multiple actions
    DefineBitsJPEG3             => 35,  # A JPEG bitmap with alpha info.
    DefineBitsLossless2         => 36,  # A lossless bitmap with alpha info.
    DefineSprite                => 39,  # Define a sequence of tags that describe the behavior of a sprite.
    NameCharacter               => 40,  # Name a character definition, character id and a string,
                                        #    (used for buttons, bitmaps, sprites and sounds).
    FrameLabel                  => 43,  # A string label for the current frame.
    SoundStreamHead2            => 45,  # For lossless streaming sound, should not have needed this...
    DefineMorphShape            => 46,  # A morph shape definition
    DefineFont2                 => 48,  # 


);

my %tagdict = reverse %tagdef;


sub new {
       
       my $class = shift;
       my $self  = {};
       bless  $self, $class;
       $self->{frame}   = 0;     # the pointer to which frame we're on
       $self->{frames}  = [[]];  # our list of frames
       $self->{objects} = [];  # our list of objects
       return $self;
}


# add a new tag to our list of tags
sub add_tag {
        
        my ($self, %tag) = @_;
                
        # if this tag is a new frame tag then inc our frame pointer
     
        
        push (@{$self->{frames}[$self->{frame}]}, \%tag);
        $self->inc_frame()  if ($tag{'type'}==1);

        # reur the curent frame pointer
        return $self->{frame};
}


sub inc_frame {
        
        my $self = shift;
      
        return $self->set_frame(++($self->{frame}));
}

sub set_frame {
        
        my ($self, $frame) = @_;
        
        #my @frames = $self->{frames};
        
        # check to see if tee *actually* tat many frames
        # if not, make it so ;)
        
      
        my @temp = @{$self->{frames}};
        for ($a=$#temp;$a<$frame;$a++)
        {
        	$temp[++$#temp]=[];
        }
        $self->{frames} = \@temp;
        $frame =0 if ($frame<0);
        $self->{frame}  = $frame;
        # and return it
        return $frame;
}



sub parse {
        
        my ($self, $parser, $filename) = @_;
        
        return "Error, no filename given" if ($filename eq "");
        # check to see if he paser actualy exists
        eval ("require Flash::"."$parser"."::Parser; Flash::"."$parser"."::Parser->parse(\$self, \$filename);");
        return "That import filter '$parser' does not exist" if $@;
        
        
        return undef;
}


sub write {
        
        my ($self, $writer) = @_;
        
        eval ("require Flash::"."$writer"."::Writer; Flash::"."$writer"."::Writer->write(\$self);");
        return "That export filter '$writer' does not exist" if $@;
        
        return undef; 

        
}
  
sub set_headers {

      my ($self,$ver,$len,$xmin,$xmax,$ymin,$ymax,$framec,$framer, $offset) = @_;
      
      $self->{version} = $ver;
      $self->{length}  = $len;
      $self->{xmin}    = $xmin;
      $self->{xmax}    = $xmax;
      $self->{ymin}    = $ymin;
      $self->{ymax}    = $ymax;
      $self->{frame_count}  = $framec;
      $self->{frame_rate}   = $framer;
      $self->{offset}   = $offset;
      

}

sub get_headers {

   my $self = shift;
   # at some point I want to dynamically generate the version and the filesize etc etc
   return ($self->{version}, $self->{length}, $self->{xmin}, $self->{xmax}, $self->{ymin}, $self->{ymax}, $self->{frame_count}, $self->{frame_rate}, $self->{offset});

}



sub from_tag {


   my ($self, $tag) = @_;

   return $tagdef{$tag};
}



sub to_tag {

   my ($self, $tag) = @_;

   return $tagdict{$tag};
}

sub num_frames {
	$self = shift;
	
	return scalar @{$self->{frames}}
}
	
sub current_frame {
	$self = shift;
	
	return $self->{frame};
}

1;