📄 fsmovie.java
字号:
/*
* FSMovie.java
* Transform
*
* Copyright (c) 2001-2006 Flagstone Software Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Flagstone Software Ltd. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.flagstone.transform;
import java.util.*;
import java.util.zip.*;
import java.io.*;
/**
FSMovie is a container class for the objects that represents the data structures in a Flash file.
<p>FSMovie is the core class of the Transform package. It is used to parse and generate Flash files, translating the binary format of the Flash file into an array objects that can be inspected and updated.</p>
<p>An FSMovie object also contains the attributes that make up the header information of the Flash file, identifying the version support, size of the Flash Player screen, etc.:</p>
<table class="datasheet">
<tr><th align="left" colspan="2">Attributes</th></tr>
<tr><td><a name="FSMovie_0">signature</a></td>
<td>The signature is a three character code that identifies that the file contains Flash encoded data. For Flash versions 1 to 5 this was 'F', 'W', 'S' ("SWF" reversed). In Flash 6, zlib compressed files were introduced to reduce file sizes. Compressed files are identified by the signature, 'C', 'W', 'S'.</td>
</tr>
<tr><td><a name="FSMovie_1">version</a></td>
<td>The version of Flash that is contained in the file.</td>
</tr>
<tr><td><a name="FSMovie_2">frameSize</a></td>
<td>An FSBounds object that define the size of the Flash Player's screen when playing the movie.</td>
</tr>
<tr><td><a name="FSMovie_3">frameRate</a></td>
<td>The number of frames per second that the movie will be played at.</td>
</tr>
<tr><td><a name="FSMovie_4">objects</a></td>
<td>An array of FSMovieObjects that define the shapes, buttons, images etc to be displayed along with the commands that control how the movie is animated.</td>
</tr>
</table>
<p>The header information defined in the Macromedia Flash (SWF) File Format Specification also identifies the length of the (uncompressed) movie and the number of frames. These attributes are derived when the FSMovie object is encoded to a file.</p>
<h1 class="datasheet">Generating a Flash File</h1>
<p>Flash files can be built from scratch by simply constructing instances of objects that represent
the respective Flash data structure and adding them to an FSMovie object in the order they will
be executed by the Flash player.</p>
<pre>
FSMovie movie = new FSMovie();
// Define a shape to be displayed
FSDefineShape rectangle = new FSDefineShape(movie.newIdentifier(), ....);
// Set the size of the Flash Player screen 400x400 and the number of
// frames displayed per second.
movie.setFrameSize(new FSBounds(0, 0, 400, 400));
movie.setFrameRate(1.0);
// Set the background colour for the movie
movie.add(new FSSetBackgroundColor(FSColor.lightblue()));
// Add the shape to the movie. All objects must be defined before
// they can be used.
movie.add(rectangle);
// Place the shape in the centre of the screen (200,200) on the first layer
// in the Flash Player's display list.
movie.add(new FSPlaceObject2(FSPlaceObject2.New, rectangle.getIdentifier(), 1, 200, 200));
// Display the frame
movie.add(new FSShowFrame());
</pre>
<p>Note that when the shape was defined a call was made to the newIdentifier() method on the movie. Each object that defines either a shape, button, sound etc., is assigned a unique identifier to allow the object to be referenced in objects such as FSPlaceObject which places the shape on the Flash Player's display list. The identifier is unique only within a movie so the FSMovie class maintains a counter that is used to generate values for the identifiers that are assigned to objects derived from the FSDefineObject class.</p>
<p>To generate a Flash file use the <code>encodeToFile(String fileName)</code> method:</p>
<pre>
try {
aMovie.encodeToFile(filename);
}
catch (Exception e) {
... Process Exception ...
}
</pre>
<h1 class="datasheet">Parsing a Flash File</h1>
<p>To parse a Flash file simply create an instance using the <code>FSMovie(String fileName)</code>
constructor:</p>
<pre>
FSMovie aMovie = null;
String fileName = "aFlashFile.swf";
try {
aMovie = new FSMovie(filename);
}
catch (Exception e) {
... Process Exception ...
}
</pre>
<p>The objects array of the movie will contain an instance of the respective package class for
each data structure decoded from the file. The objects may be inspected and their attributes
changed accordingly.</p>
<p>The FSMovie object keeps track of the identifiers assigned to objects that define the shapes,
buttons, sounds etc. that make up a Flash file. If a new object is added to the movie then
the call to newIdentifier() is guaranteed to return a value that is unique within the movie.</p>
<p><b>Multiple Language Support</b><br>Formal support for Unicode strings was added in
Flash 6, providing a unified mechanism for supporting different languages. Previously
Macromedia released different versions of the Flash authoring tool that supported a
character set unique to a given language, for example SJIS for the Japanese language
edition. The FSMovie class includes the <em>encoding</em> attribute that identifies
the character set used to encode the strings found in objects such as FSFrameLabel
and FSDefineTextField. Note this does not include the characters in font definitions.
The default value of UTF8 is the same encoding scheme used by Macromedia so files
containing Flash 6 or later may be decoded from any source. For earlier version of
Flash the encoding attribute may be set to match the character set of the application
used to encoded the Flash file.</p>';
*/
public class FSMovie implements Cloneable
{
static void encodeObjects(FSCoder coder, ArrayList array)
{
for (Iterator i = array.iterator(); i.hasNext();)
{
FSMovieObject object = (FSMovieObject)i.next();
int objStart = coder.getPointer();
int objLength = object.getLength();
int start = coder.getPointer() + ((object.getExtendLength() || objLength >= 63) ? 48 : 16);
int next = start + (objLength << 3);
object.encode(coder);
coder.setPointer(next);
int delta = (coder.getPointer() - next) >> 3;
if (delta != 0)
{
coder.context[FSCoder.CodingError] = 1;
coder.context[FSCoder.TypeInError] = object.getType();
coder.context[FSCoder.StartOfError] = objStart >>> 3;
coder.context[FSCoder.ExpectedLength] = (next-objStart)>>>3;
coder.context[FSCoder.Delta] = delta;
}
}
}
static FSMovieObject decodeObject(FSCoder coder)
{
FSMovieObject currentObject = null;
int objStart = coder.getPointer();
int type = coder.scanWord(2, false) >> 6;
int length = coder.scanWord(2, false) & 0x3F;
int next = coder.getPointer() + 16 + (length << 3);
int identifier = 0;
if (type == 0)
{
/*
* Skip over the End object so it does not generate an event.
*/
coder.setPointer(next);
return currentObject;
}
if (length == 0x3F)
{
coder.adjustPointer(16);
length = coder.scanWord(4, false);
next = coder.getPointer() + 32 + (length << 3);
coder.adjustPointer(-16);
}
switch (type)
{
case FSMovieObject.ShowFrame:
currentObject = FSShowFrame.getInstance();
coder.setPointer(next);
break;
case FSMovieObject.DefineShape:
currentObject = new FSDefineShape(coder);
identifier = ((FSDefineObject)currentObject).getIdentifier();
break;
case FSMovieObject.PlaceObject:
currentObject = new FSPlaceObject(coder);
break;
case FSMovieObject.RemoveObject:
currentObject = new FSRemoveObject(coder);
break;
case FSMovieObject.DefineJPEGImage:
currentObject = new FSDefineJPEGImage(coder);
identifier = ((FSDefineObject)currentObject).getIdentifier();
break;
case FSMovieObject.DefineButton:
currentObject = new FSDefineButton(coder);
identifier = ((FSDefineObject)currentObject).getIdentifier();
break;
case FSMovieObject.JPEGTables:
currentObject = new FSJPEGEncodingTable(coder);
break;
case FSMovieObject.SetBackgroundColor:
currentObject = new FSSetBackgroundColor(coder);
break;
case FSMovieObject.DefineFont:
currentObject = new FSDefineFont(coder);
identifier = ((FSDefineObject)currentObject).getIdentifier();
break;
case FSMovieObject.DefineText:
currentObject = new FSDefineText(coder);
identifier = ((FSDefineObject)currentObject).getIdentifier();
break;
case FSMovieObject.DoAction:
currentObject = new FSDoAction(coder);
break;
case FSMovieObject.FontInfo:
currentObject = new FSFontInfo(coder);
break;
case FSMovieObject.DefineSound:
currentObject = new FSDefineSound(coder);
identifier = ((FSDefineObject)currentObject).getIdentifier();
break;
case FSMovieObject.StartSound:
currentObject = new FSStartSound(coder);
break;
case FSMovieObject.SoundStreamHead:
currentObject = new FSSoundStreamHead(coder);
break;
case FSMovieObject.SoundStreamBlock:
currentObject = new FSSoundStreamBlock(coder);
break;
// Flash 2
case FSMovieObject.ButtonSound:
currentObject = new FSButtonSound(coder);
break;
case FSMovieObject.DefineImage:
currentObject = new FSDefineImage(coder);
identifier = ((FSDefineObject)currentObject).getIdentifier();
break;
case FSMovieObject.DefineJPEGImage2:
currentObject = new FSDefineJPEGImage2(coder);
identifier = ((FSDefineObject)currentObject).getIdentifier();
break;
case FSMovieObject.DefineShape2:
currentObject = new FSDefineShape2(coder);
identifier = ((FSDefineObject)currentObject).getIdentifier();
break;
case FSMovieObject.ButtonColorTransform:
currentObject = new FSButtonColorTransform(coder);
break;
case FSMovieObject.Protect:
currentObject = new FSProtect(coder);
break;
// Flash 3
case FSMovieObject.Free:
currentObject = new FSFree(coder);
break;
case FSMovieObject.PlaceObject2:
currentObject = new FSPlaceObject2(coder);
break;
case FSMovieObject.RemoveObject2:
currentObject = new FSRemoveObject2(coder);
break;
case FSMovieObject.DefineShape3:
currentObject = new FSDefineShape3(coder);
identifier = ((FSDefineObject)currentObject).getIdentifier();
break;
case FSMovieObject.DefineText2:
currentObject = new FSDefineText2(coder);
identifier = ((FSDefineObject)currentObject).getIdentifier();
break;
case FSMovieObject.DefineButton2:
currentObject = new FSDefineButton2(coder);
identifier = ((FSDefineObject)currentObject).getIdentifier();
break;
case FSMovieObject.DefineJPEGImage3:
currentObject = new FSDefineJPEGImage3(coder);
identifier = ((FSDefineObject)currentObject).getIdentifier();
break;
case FSMovieObject.DefineImage2:
currentObject = new FSDefineImage2(coder);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -