⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tagwriter.java

📁 java和flash混合编程
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/****************************************************************
 * Copyright (c) 2001, David N. Main, All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the 
 * following conditions are met:
 *
 * 1. Redistributions of source code must retain the above 
 * copyright notice, this list of conditions and the following 
 * disclaimer. 
 * 
 * 2. 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.
 * 
 * 3. The name of the author may not be used to endorse or 
 * promote products derived from this software without specific 
 * prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 
 * AUTHOR 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.anotherbigidea.flash.writers;

import java.io.*;
import java.util.*;
import java.util.zip.*;
import com.anotherbigidea.io.*;
import com.anotherbigidea.flash.*;
import com.anotherbigidea.flash.structs.*;
import com.anotherbigidea.flash.interfaces.*;

/**
 * A writer that implements the SWFTagTypes interface and writes
 * to a SWFTags interface
 */
public class TagWriter implements SWFTagTypes, SWFConstants, SWFFileSignature 
{
    protected SWFTags mTags;
        
    protected OutStream out;
    protected ByteArrayOutputStream bytes;
    protected int     tagType;
    protected boolean longTag;
    protected int version;
    protected String mStringEncoding = SWFConstants.STRING_ENCODING_MX;
    
    public TagWriter( SWFTags tags )
    {
        mTags = tags;
    }
    
    protected OutStream getOutStream() { return out; }
    
    protected SWFActions factorySWFActions()
    {
        return new ActionWriter( this, version );
    }
    
    protected SWFShape factorySWFShape( boolean hasAlpha, boolean hasStyle )
    {
        return new SWFShapeImpl( this, hasAlpha, hasStyle );
    }
    
	/**
	 * @see SWFFileSignature#signature(String)
	 */
	public void signature( String sig ) {
		if( mTags instanceof SWFFileSignature ) {
			((SWFFileSignature) mTags).signature( sig );
		}
	}    
    
    /**
     * Start a new tag context
     */
    protected void startTag( int tagType, boolean longTag )
    {
        this.tagType = tagType;
        this.longTag = longTag;
        
        bytes = new ByteArrayOutputStream( 10000 );
        out   = new OutStream( bytes );
    }
    
    /**
     * Start a new definition tag context
     */
    protected void startTag( int tagType, int id, boolean longTag )
        throws IOException 
    {
        startTag( tagType, longTag );        
        out.writeUI16( id );
    }
        
    /**
     * Finish the tag context and write the tag
     */
    protected void completeTag() throws IOException 
    {
        out.flush();
        byte[] contents = bytes.toByteArray();
        
        out = null;
        bytes = null;
        
        mTags.tag( tagType, longTag, contents );        
    }
    
    /**
     * SWFTags interface
     */    
    public void tag( int tagType, boolean longTag, byte[] contents ) 
        throws IOException
    {
        mTags.tag( tagType, longTag, contents );
    }
    
    /**
     * SWFHeader interface.
     * Sets movie length to -1 to force a recalculation since the length
     * cannot be guaranteed to be the same as the original.
     */
    public void header( int version, long length,
                        int twipsWidth, int twipsHeight,
                        int frameRate, int frameCount ) throws IOException
    {
        this.version = version;
        mTags.header( version, -1, twipsWidth, twipsHeight, frameRate, frameCount );
        
        //set encoding to Ascii if pre-MX
        if( version < SWFConstants.FLASH_MX_VERSION ) {
        	mStringEncoding = SWFConstants.STRING_ENCODING_PRE_MX; 
        } 
    }
    
    /**
     * SWFTagTypes interface
     */
    public void tagEnd() throws IOException
    {
        mTags.tag( TAG_END, false, null );
    }

    /**
     * SWFTagTypes interface
     */
    public void tagShowFrame() throws IOException
    {
        mTags.tag( TAG_SHOWFRAME, false, null );
    }
    
    /**
     * SWFTagTypes interface
     */
    public void tagDefineSound( int id, int format, int frequency,
                                boolean bits16, boolean stereo,
                                int sampleCount, byte[] soundData ) 
        throws IOException
    {
        startTag( TAG_DEFINESOUND, id, true );
        out.writeUBits( 4, format );
        out.writeUBits( 2, frequency );
        out.writeUBits( 1, bits16 ? 1 : 0 );
        out.writeUBits( 1, stereo ? 1 : 0 );
        out.writeUI32 ( sampleCount );
        out.write( soundData );
        completeTag();
    }
    
    /**
     * SWFTagTypes interface
     */
    public void tagDefineButtonSound( int buttonId,
                    int rollOverSoundId, SoundInfo rollOverSoundInfo,
                    int rollOutSoundId,  SoundInfo rollOutSoundInfo,
                    int pressSoundId,    SoundInfo pressSoundInfo,
                    int releaseSoundId,  SoundInfo releaseSoundInfo )
        throws IOException
    {
        startTag( TAG_DEFINEBUTTONSOUND, buttonId, true );
        
        out.writeUI16( rollOverSoundId );
        if( rollOverSoundId != 0 ) rollOverSoundInfo.write( out );

        out.writeUI16( rollOutSoundId );
        if( rollOutSoundId != 0 ) rollOutSoundInfo.write( out );
        
        out.writeUI16( pressSoundId );
        if( pressSoundId != 0 ) pressSoundInfo.write( out );
        out.writeUI16( releaseSoundId );
        if( releaseSoundId != 0 ) releaseSoundInfo.write( out );
        
        completeTag();
    }
    
    /**
     * SWFTagTypes interface
     */
    public void tagStartSound( int soundId, SoundInfo info ) throws IOException
    {
        startTag( TAG_STARTSOUND, soundId, false );
        info.write( out );
        completeTag();
    }
    
    /**
     * SWFTagTypes interface
     */
    public void tagSoundStreamHead( 
        int playbackFrequency, boolean playback16bit, boolean playbackStereo,
        int streamFormat, int streamFrequency, boolean stream16bit, boolean streamStereo,
        int averageSampleCount ) throws IOException
    {
        writeSoundStreamHead( TAG_SOUNDSTREAMHEAD,
            playbackFrequency, playback16bit, playbackStereo,
            streamFormat, streamFrequency, stream16bit, streamStereo,
            averageSampleCount );
    }
    
    /**
     * SWFTagTypes interface
     */
    public void tagSoundStreamHead2( 
        int playbackFrequency, boolean playback16bit, boolean playbackStereo,
        int streamFormat, int streamFrequency, boolean stream16bit, boolean streamStereo,
        int averageSampleCount ) throws IOException
    {
        writeSoundStreamHead( TAG_SOUNDSTREAMHEAD2,
            playbackFrequency, playback16bit, playbackStereo,
            streamFormat, streamFrequency, stream16bit, streamStereo,
            averageSampleCount );
    }
    
    public void writeSoundStreamHead( int tag,
        int playbackFrequency, boolean playback16bits, boolean playbackStereo,
        int streamFormat, int streamFrequency, boolean stream16bits, boolean streamStereo,
        int averageSampleCount ) throws IOException    
    {
        startTag( tag, false );
        
        out.writeUBits(4,0);
        out.writeUBits(2,playbackFrequency);
        out.writeUBits(1, playback16bits ? 1 : 0 );
        out.writeUBits(1, playbackStereo ? 1 : 0 );
        
        out.writeUBits(4,streamFormat);
        out.writeUBits(2,streamFrequency);
        out.writeUBits(1, stream16bits ? 1 : 0 );
        out.writeUBits(1, streamStereo ? 1 : 0 );        
        out.writeUI16 (averageSampleCount);
        
        if( streamFormat == SWFConstants.SOUND_FORMAT_MP3 )
        {
            out.writeUI16 (0); //unknown
        }
        
        completeTag();
    }
    
    /**
     * SWFTagTypes interface
     */
    public void tagSoundStreamBlock( byte[] soundData ) throws IOException
    {
        startTag( TAG_SOUNDSTREAMBLOCK, true );
        out.write( soundData );
        completeTag();
    }
    
    /**
     * SWFTagTypes interface
     */
    public void tagSerialNumber( String serialNumber ) throws IOException
    {
        startTag( TAG_SERIALNUMBER, false );
        out.writeString( serialNumber, mStringEncoding );
        completeTag();
    }
    
    /**
     * SWFTagTypes interface
     */
    public void tagGenerator( byte[] data ) throws IOException
    {
        startTag( TAG_FLASHGENERATOR, false );
        out.write( data );
        completeTag();
    }
    
    /**
     * SWFTagTypes interface
     */
    public void tagGeneratorText( byte[] data ) throws IOException
    {
        startTag( TAG_GENERATOR_TEXT, false );
        out.write( data );
        completeTag();
    }
    
    /**
     * SWFTagTypes interface
     */
    public void tagGeneratorCommand( byte[] data ) throws IOException
    {
        startTag( TAG_TEMPLATECOMMAND, false );
        out.write( data );
        completeTag();
    }
    
    /**
     * SWFTagTypes interface
     */
    public void tagGeneratorFont( byte[] data ) throws IOException
    {
        startTag( TAG_GEN_EXTERNAL_FONT, false );
        out.write( data );
        completeTag();
    }
    
    /**
     * SWFTagTypes interface
     */
    public void tagNameCharacter( byte[] data ) throws IOException
    {
        startTag( TAG_NAMECHARACTER, false );
        out.write( data );
        completeTag();
    }
    
    /**
     * SWFTagTypes interface
     */
    public void tagDefineBits( int id, byte[] imageData ) throws IOException
    {
        startTag( TAG_DEFINEBITS, id, true );
        out.write( imageData );
        completeTag();
    }  
    
    /**
     * SWFTagTypes interface
     */
    public void tagJPEGTables( byte[] jpegEncodingData ) throws IOException
    {
        startTag( TAG_JPEGTABLES, true );
        out.write( jpegEncodingData );
        completeTag();
    }

    /**
     * SWFTagTypes interface
     */
    public void tagDefineBitsJPEG3( int id, byte[] imageData, byte[] alphaData ) throws IOException
    {
        startTag( TAG_DEFINEBITSJPEG3, id, true );
        out.writeUI32( imageData.length );
        out.write( imageData );
        out.write( alphaData );
        completeTag();        
    }        
    
    /**
     * SWFTagTypes interface
     */
    public SWFActions tagDoAction() throws IOException
    {
        startTag( TAG_DOACTION, true );
        return factorySWFActions();
    }
    
	/**
	 * SWFTagTypes interface
	 */
	public SWFActions tagDoInitAction( int spriteId ) throws IOException {
		startTag( TAG_DOINITACTION, spriteId, true );
		return factorySWFActions();		    
	}
    
    /**
     * SWFTagTypes interface
     */
    public SWFShape tagDefineShape( int id, Rect outline ) throws IOException
    {
        startShape( TAG_DEFINESHAPE, id, outline );
        return factorySWFShape( false, true );
    }
    
    /**
     * SWFTagTypes interface
     */
    public SWFShape tagDefineShape2( int id, Rect outline ) throws IOException
    {
        startShape( TAG_DEFINESHAPE2, id, outline );
        return factorySWFShape( false, true );
    }
    
    /**
     * SWFTagTypes interface
     */
    public SWFShape tagDefineShape3( int id, Rect outline ) throws IOException
    {
        startShape( TAG_DEFINESHAPE3, id, outline );
        return factorySWFShape( true, true );        
    }    
    
    /**
     * SWFTagTypes interface
     */    
    public void tagFreeCharacter( int charId ) throws IOException 
    {
        startTag( TAG_FREECHARACTER, false );
        out.writeUI16( charId );
        completeTag();
    }
    
    /**
     * SWFTagTypes interface
     */    
    public void tagPlaceObject( int charId, int depth, 
                                Matrix matrix, AlphaTransform cxform ) 
        throws IOException
    {
        startTag( TAG_PLACEOBJECT, false );
        out.writeUI16( charId );        
        out.writeUI16( depth );
        matrix.write ( out );
        if( cxform != null ) cxform.write( out );
        completeTag();
    }
    
    /**
     * SWFTagTypes interface
     */    
    public SWFActions tagPlaceObject2( boolean isMove,
                                       int clipDepth,
                                       int depth,
                                       int charId,
                                       Matrix matrix,
                                       AlphaTransform cxform,
                                       int ratio,
                                       String name,
                                       int clipActionFlags )  
        throws IOException    
    {
        boolean hasClipActions = ( clipActionFlags != 0 );
        
        startTag( TAG_PLACEOBJECT2, false );
        

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -