Package com.flagstone.transform

Transform SWF is an API for reading and writing Flash files.

See:
          Description

Interface Summary
DefineTag The DefineTag interface is used to identify object that are used to add definitions for shapes, images, sounds ,etc.
MovieTag MovieTag defines a type that is used to identify all the objects that can be added to a Movie.
 

Class Summary
Background Background sets the background colour displayed in every frame in the movie.
Constants SWF contains definitions of constants used throughout the framework.
DefineData DefineData is used to embed binary data in a Flash file.
DoABC DoABC is used to define scripts containing Actionscript 3.0 byte-codes.
DoAction DoAction is used to add a set of actions to a frame in a movie.
EnableDebugger Enables a movie to be debugged when played using the Flash authoring tool, allowing the variables defined in the arrays of actions specified in object to be inspected.
EnableDebugger2 EnableDebugger2 is an updated version or the EnableDebugger instruction which enables a movie to be debugged.
EventHandler EventHandler is used to define the actions that a movie clip or button will execute in response to a particular event.
Export Export is used to export one or more shapes and other objects so they can be used in another Flash file.
FrameLabel FrameLabel defines a name for the current frame in a movie or movie clip.
Free Free deletes the object with the specified identifier, freeing up resources in the Flash Player.
Import Import is used to import shapes and other objects from another Flash file referenced by a URL.
Import2 Import2 is used to import shapes and other objects from another Flash file referenced by a URL.
LimitScript The LimitScript is used to define the execution environment of the Flash Player, limiting the resources available when executing actions and improving performance.
Movie Movie is a container class for the objects that represents the data structures in a Flash file.
MovieAttributes The MovieAttributes tag defines characteristics of a Movie.
MovieData MovieData is used to store one or more MovieTags which already have been encoded for writing to a Flash file.
MovieDecoder MovieDecoder is used to decode the different types of data structure encoded in a movie.
MovieHeader MovieHeader contains the attributes that make up the header fields of a Flash file.
MovieMetaData MetaData is used to add a user-defined information into a Flash file.
MovieObject MovieObject is used to represent any object decoded from a Movie that is not directly supported by Transform.
MovieTypes MovieTypes defines the constants that identify a MovieTag when it is encoded according to the Flash file format specification.
Place PlaceObject is used to add an object (shape, button, etc.) to the Flash Player's display list.
Place2 Place2 is used to add and manipulate objects (shape, button, etc.) on the Flash Player's display list.
Place3 Place3 is used to update the display list.
Protect Protect marks a file as not-readable, preventing the file from being loaded into an editor.
Remove RemoveObject removes an object from the Flash Player's Display List.
Remove2 RemoveObject2 removes an object from the display list, requiring only the layer number.
ScalingGrid ScalingGrid is used to define a 9-slice grid that can be used to control the way an object is scaled.
ScenesAndLabels ScenesAndLabels is used to list the scenes (main timeline only) and labelled frames for movies and movie clips.
SerialNumber SerialNumber is used to add a user-defined serial number into a Flash file.
ShowFrame ShowFrame is used to instruct the Flash Player to display a single frame in a movie or movie clip.
SymbolClass SymbolClass is used to export one or more Actionscript 3 classes so they can be used in another Flash file.
TabOrder The TabOrder class is used to set the tabbing order of text fields, movie clips and buttons visible on the display list.
 

Enum Summary
CharacterEncoding CharacterEncoding is used to identify the encoding used for characters in strings stored in the movie.
Event Event is used to represent all the different types of event that a button or movie clips responds to.
PlaceType Placement the different ways an object can be added to the display list by a Place2 or Place3 instruction.
 

Package com.flagstone.transform Description

Transform SWF is an API for reading and writing Flash files.

Flash is designed to display vector graphics across the Internet and displayed on host devices using the Macromedia Flash Player. Flash is designed to be compact, extensible and platform independent. Animations delivered using Flash will be displayed consistently and accurately across a wide range of devices.

A Flash file (movie) contain a sequence of tagged data blocks.

Each movie starts with header information followed by a sequence of tags that describe how the movie is displayed. All movies end with an End tag which marks the end of the file.

The header identifies the type and version of Flash contained in the file; the size of screen displayed by the Flash Player; the length of file; the rate at which frames are displayed and the number of frames in the movie.

Two categories of tag are provided: definition tags which are used to describe shapes, images, buttons, sounds, etc., and control tags that create instances of the shapes, etc. and display them on the Flash Player screen to create animations.

Flash also supports actions which control the behaviour of objects in response to events such as a button being clicked or when a particular frame is displayed. Actions are represented by byte-codes and the Flash Player supports a stack-based programming model which provides a rich programming environment that supports complex behaviours. This allows Flash movies to provide all the flexibility and sophistication of user interfaces found in desktop computers and personal devices.

The Transform SWF library is a collection of classes for each of the tags and data structures that make up the Flash File Format. The Movie class is the principal class in the library. Movie objects are used to represent a complete Flash movie, containing instances of the classes that represent each type of tag.

Movie is designed to simplify the creation and manipulation of Flash files. Creating an instance of the class:

 Movie aMovie(filename);
 

parses the Flash file, filename and creates objects for each different tags and data structure decoded from the file. The objects may be inspected and their attributes changed accordingly. Once the objects have been updated:

 aMovie.writeToFile(filename);
 

encodes each object and generates the file according to the Macromedia Flash (SWF) File Format Specification.

1.1. Flash Programming Model

Flash is, in effect, a programming language for the Flash Player. A movie contains definitions of objects which are created, displayed and updated to create an animation. In order to understand how the classes provided in Transform are used to create Flash movies it is essential to understand the programming model provided by the Flash Player.

The Dictionary is a table that contains the definitions of the objects (buttons, images, sounds) that will be displayed in the movie. Each definition tag (represented in Transform by classes derived from Definition) contains a unique identifier which allows the definition to be referenced when creating instances of a particular shape, button, etc., to display on the screen. Entries are added to the dictionary whenever a definition tag is decoded from the Flash movie. Dictionary entries may be deleted using the Free class once a definition is no longer needed.

The Display List controls the order in which objects are displayed on the screen. The display list is divided into layers. To display an object on the screen it is placed on a display list layer (using the PlaceObject or PlaceObject2 classes). The layer number controls the stacking order of object when the movie is displayed. Objects placed on a higher layer are displayed in front on an object on a lower layer. Only one object can be placed on a given layer at a time so like the unique identifier used to create the object from its definition in the Dictionary the layer number can be used to refer to an object once it is on the display list. When placed on the Display List an object is always visible (unless hidden by another or moved off the visible area of the screen) until it is explicitly removed (using RemoveObject or RemoveObject2). Once an object is removed it is "destroyed" so another instance must be re-created from its definition if the object must be displayed again.

The Movie List is used to manage the movie and any movie clips (also known as sprites). The Movie List contains a number of virtual levels, analogous to the layers in the Display List, which are used to keep track of which movie clips are currently loaded. The main movie is placed automatically on _level0. Movie clips that are displayed within the main movie may be loaded to _level1, _level2, etc. Note that movie clips must still be placed on the Display List for them to become visible on the Screen. Loading a movie clip (using the GetUrl2 class) to a level replaces any clip already loaded at that level. Movies loaded to _level0 replace the main movie. This allows several movies sequences to be chained together into a single movie.

The Screen is a bit-mapped display where the Flash Player renders the graphics objects to create the animation. Each time a ShowFrame tag is decoded the objects added to the Display List are rendered on the Screen. All graphics and fonts are anti-aliased. The size of the Screen is defined by the frameSize attribute of the Movie object. Coordinates in Flash are specified in twips where 1 twip equals 1/1440th of an inch or 1/20th of a point. For most practical purposes this can be simplified to 1 twip = 1/20th of a pixel.

It is important to note that in Flash the positive x and y axes extend from the top left corner of the screen (not from the lower left as in the Cartesian coordinate system). The coordinate range may also be specified in the attributes of a Flash movie so that the top left corner is set to an arbitrary pair of coordinates. For example specifying the corners at the points (-100,-100) and (100,100) defines a screen 200 twips by 200 twips with the point (0,0) located in the centre. Specifying the points (0,0) and (200,200) defines a screen with the same size however the centre is now located at (100,100).

The Flash Player supports three other physical devices (through the host operating system) to allow a user to interact with the animation being played. Users can enter data and keystrokes representing commands through a keyboard. A mouse or other pointing device is supported to allow users to click buttons and generate events for movie clips. Finally the Flash Player supports CD-quality sound in mono or stereo with between three and eight sound channels playing simultaneously, depending on the capabilities of the host platform. Sounds can be played in response to specific events, such as a mouse being clicked or streamed with the movie to provide as sound-track as it is displayed. With the introduction of Flash 5, access to the keyboard and mouse are provided through the Key and Mouse objects and the methods they support, see Object-Oriented Programming below.

Actions executed by the Flash Player are loaded into the Instruction Buffer. Actions may be specified as part of a button definition to be executed when the button is clicked. Flash also allows actions to be executed whenever each frame is displayed (using the DoAction class). Flash does not support the concept of a program as such. Rather the complete movie can be regarded as the program executed by the Flash Player. Sequences of actions defined in DefineButton, DoAction objects are best viewed as procedures or sub-routines that are executed whenever a particular event occurs. A given set of actions are executed each and every time the corresponding event occurs. The Instruction Buffer is byte-addressable. Control flow within the Instruction Buffer is controlled by the Instruction Pointer which contains the address of the next instruction to be executed. Conditional and unconditional jumps are supported through the If and Jump classes respectively. Each action contains a 16-bit offset which is added to the address in the Instruction Pointer to obtain the address of the next instruction in the Instruction Buffer.

Calculations are performed using the Stack. The Push class is used to push values onto the stack. When actions are executed any required arguments are popped from the Stack and any results are pushed back on the stack then the action finishes executing. Values pushed onto the Stack are primarily integers or strings. When values are used repeatedly in a calculation the Flash Player provides the String Table and a set of 256 internal Registers (increased from 4 in Flash 6) to improve efficiency. Rather than repeatedly push a string value onto the Stack the Table class allows an array of strings to be defined in a look-up table, using the Table class. A string is referenced from the table by pushing an TableIndex value onto the Stack. A table index only occupies 1 or 2 bytes in the Instruction Buffer so the space required by a sequence of actions can be reduced considerably using indices rather than explicitly pushing strings onto the stack. The Flash Player also has a set of 256 internal registers for temporarily storing values. Values are copied (not popped) from the top of the Stack using the RegisterCopy action. The contents of the register is referenced by pushing an RegisterIndex onto the Stack. This simplifies use of the Stack is complex calculations and again reduced the space required to represent a given set of actions in the Instruction Buffer.

Although calculations are performed using the Stack, Flash also supports the concept of named variables. Variables are stored in Memory which is a scratch-pad memory accessible only by the Flash Player. It cannot be accessed using any of the actions provided by Flash. Variables are fetched and set from Memory using the GetVariable and SetVariable actions (byte-code actions are supported using the Action class). The name of the variable is pushed onto the Stack and a GetVariable action is executed. The value of the variable is fetched form Memory and pushed onto the Stack by the Flash Player. Similarly to set a variable the value followed by the variable name are pushed onto the Stack and a SetVariable action is executed to update the variable in Memory.

1.2. A Quick Tour of Flash

Flash provides a rich environment for drawing object and creating animations. This is a brief description of the main aspects of Flash and is intended to introduce the main classes available in the Transform SWF library. Examples showing how to use the individual classes to create Flash movies are presented on Flagstone's web site.

Drawing Shapes
Shapes are drawn using a combination Line and Curve which specify the line or curve to draw relative to the current drawing point. The line style is defined using SolidLine class - only solid lines are supported. Shapes can be filled with solid colour (SolidFill), an image (BitmapFill) or a colour gradient (GradientFill). The ShapeStyle class is used to select a particular line and fill style while drawing a shape. It can also change the current drawing point and the set of line and fill styles used for a particular shape. Shapes are defined using DefineShape, DefineShape2 or DefineShape3. DefineShape is the most commonly used class. DefineShape2 extends DefineShape to support a large number (greater than 65535) of line and fill styles while DefineShape3 supports transparent shapes.

Flash supports shape morphing using the DefineMorphShape class which defines the appearance of the start and end shapes. The interpolation between the start and end shapes is performed by the Flash Player. Displaying a morphing shape is controlled by the PlaceObject2 class which identifies the point in the morphing process and updates the display list with the morphed shape.

Buttons
Buttons are created by defining a shape for when button is up, the mouse is over the button or the button is clicked.DefineButton displays a button that executes a set of actions when it is clicked. DefineButton2 provides are more flexible class allowing actions to be executed for any of the states a button can occupy. Sounds can also be played, using the ButtonSound class, when an event occurs, such as the button being clicked.

Images
Flash supports JPEG and lossless image formats such as GIF. Several classes are provided which display "regular" and transparent images. Images cannot be displayed directly. They can only be displayed inside a shape that contains a bitmap fill style. Creating such "image shapes" is simple however.

Fonts and Text
Flash ensures consistency and quality when display text by defining the glyphs that represent each of the text characters displayed in a movie. Fonts are defined using DefineFont and DefineFont2 - the latter combines the DefineFont and FontInfo classes into a more convenient and flexible class. A string is defined, using DefineText and Text, by specifying the index of the glyph, from the font definition, that represents a given character. Using the index of a glyph allows only the glyphs actually displayed to be used, greatly reducing the size of a Flash file. The relative placement of glyphs is also specified in the text string giving a high degree of control over how a piece of text is rendered.

Creating fonts and text directly using the Transform classes can be very cumbersome. Flagstone provides the Transform Utilities library that allows fonts and text to be created from existing Flash or Java font definitions greatly simplifying the process.

Text fields can be created using the DefineTextField class. This class is very flexible and can be used to create editable text fields that allow users to submit information to a server in the same way as HTML forms.

Sound
Flash can supports sounds that are played when an event occurs such as a button being clicked (DefineSound) or streaming sound that allows a soundtrack to be played while displaying a movie (SoundStreamHead, SoundStreamBlock). Flash can play sounds that are compressed using ADPCM or MP3. A high degree of control over the sound is available - an envelope can be created that controls how a sound fades in and fades out (Sound).

Movie Clips
Movie Clips (sprites) contain "pre-packaged" animation sequences that liven up a movie. Movie clip are defined using the DefineMovie class. A Flash file can contain all the required movie clips or they can be loaded on demand using the GetUrl and GetUrl2 classes.

Display List
Shapes, buttons, images and text are placed on the display list using the PlaceObject and PlaceObject2 classes. Objects can be removed from the display list using RemoveObject and RemoveObject2.

Timelines
A Flash file is composed of a series of frames. Each frame contains definitions of objects to be displayed and the sequence of commands used to update the display list. Frames in a movie are displayed using the ShowFrame class. This instructs the Flash Player to display all the objects currently in the display list. Successive frames in a movie or movie clip are delimited by successive ShowFrame objects. The timeline is controlled by GotoFrame, GotoFrame2 and GotoLabel actions which instruct the Flash Player to play the movie or clip starting at a particular frame.

Actions
Actions are executed when a frame is displayed, a button is clicked or an event occurs in a movie clip. The DoAction class is used to define the list of actions for a given frame. The ButtonEvent class defines actions for a button and the ClipEvent class in conjunction with the PlaceObject2 class is used to define the events for a movie clip.

Push is used to push integers, strings, double precision floating point numbers onto the Stack. The byte-codes that represent stack-based actions are supported using the Action class. The type assigned to instances of this class identify the type of action to be executed.

Control flow is performed using If and Jump.

GetUrl and GetUrl2 are used to load web pages, movies and movie clips. GetUrl2 also allows variables to be loaded and submitted to and from a remote server. XML formatted data can also be exchanged using the XMLSocket class allowing direct communication with a server process.

1.3. Object-Oriented Programming

With the introduction of Flash 5 the concept of classes and objects (in the programming sense) was introduced to Flash. The Flash Player supports several pre-defined classes for different data types such as Strings, Integers and XML formatted data structures. Classes are also used access to system resources such as the mouse or other pointing device supported by the host device. The Flash Player also supports Socket objects which allow XML formatted data to be exchanged between the Flash Player and a remote server.

Objects are instantiated and assigned to variables in Memory. Instances of pre-defined classes (String, Integer, etc.) are created by pushing any arguments followed by the name of the class onto the Stack then executing a NamedObject action. Methods are executed by pushing the arguments, if any, followed by a reference to the object (usually through a variable) and a string containing the name of a method. The ExecuteMethod pops the items from the stack and executes the function associated with the method definition, returning any results to the stack.

User defined classes and objects can also be defined and created. User-defined classes are created by pushing pairs of strings and default values onto the Stack for each member variable then executing the NewObject action. A reference to the object in memory is pushed on the Stack. Assigning this reference to a variable allows the object to be referenced in future calculations. Methods can be defined by using the NewFunction class to define a function and then assigning it to a member variable of the class using the SetAttribute action. Methods can be executed using the ExecuteMethod action using the same sequence of operations described previously.

The introduction of classes and objects is significant. Future releases of Flash and the Flash Player can have new functionality added using the general purpose mechanism describe above for creating objects and executing methods without adding new tags to the file format specification.

1.4. ActionScript

ActionScript is a high-level language provided by Macromedia to control and manipulate objects using the Flash authoring program. ActionScript is compiled into the byte-codes and actions supported by Transform when a movie is encoded to the Flash file format. Programming directly using the actions defined in Transform is analogous to assembly-language programming for microprocessors. Creating programs at this low level is relatively cumbersome, prone to error and difficult to debug. To address this, Flagstone developed Translate, a Java based program that compiles ActionScript into the objects supported in the Transform SWF Library. This greatly simplifies the creation of programs that can be used in Flash files. Although Translate is current only available for Java, compiled scripts can easily be added to programs developed in C++ by compiling the ActionScript and encoding the objects to a Flash file. The file can then be parsed using the Movie class and the compiled actions extracted and added to the relevant objects in the current movie.



Copyright © 2002-2010 Flagstone Software Ltd.. All Rights Reserved.