Source code: com/anotherbigidea/flash/movie/MovieClip.java
1 /****************************************************************
2 * Copyright (c) 2001, David N. Main, All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the
6 * following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 *
17 * 3. The name of the author may not be used to endorse or
18 * promote products derived from this software without specific
19 * prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ****************************************************************/
34 package com.anotherbigidea.flash.movie;
35
36 import java.io.*;
37 import java.util.*;
38 import com.anotherbigidea.flash.interfaces.*;
39 import com.anotherbigidea.flash.writers.*;
40 import com.anotherbigidea.flash.readers.*;
41 import com.anotherbigidea.flash.structs.*;
42
43 /**
44 * A Movie Clip (aka Sprite) Symbol
45 */
46 public class MovieClip extends Symbol implements TimeLine
47 {
48 protected SortedMap frames = new TreeMap();
49 protected int frameCount = 0;
50
51 protected int depth = 1; //the next available depth
52
53 public MovieClip()
54 {
55 }
56
57 /**
58 * Get the current number of frames in the timeline.
59 */
60 public int getFrameCount()
61 {
62 return frameCount;
63 }
64
65 /**
66 * Get the Frame object for the given frame number - or create one if
67 * none exists. If the frame number is larger than the current frame count
68 * then the frame count is increased.
69 *
70 * @param frameNumber must be 1 or larger
71 */
72 public Frame getFrame( int frameNumber )
73 {
74 if( frameNumber < 1 ) return null;
75
76 Integer num = new Integer( frameNumber );
77 Frame frame = (Frame)frames.get( num );
78
79 if( frame == null )
80 {
81 frame = new Frame( frameNumber, this );
82 frames.put( num, frame );
83 if( frameNumber > frameCount ) frameCount = frameNumber;
84 }
85
86 return frame;
87 }
88
89 /**
90 * Append a frame to the end of the timeline
91 */
92 public Frame appendFrame()
93 {
94 frameCount++;
95 Frame frame = new Frame( frameCount, this );
96 frames.put( new Integer(frameCount), frame );
97 return frame;
98 }
99
100 public Frame appendFrame( Frame frame )
101 {
102 frameCount++;
103 frame.timeline = this;
104 frame.frameNumber = frameCount;
105 frames.put( new Integer(frameCount), frame );
106 return frame;
107 }
108
109 /**
110 * Get the next available depth in the timeline
111 */
112 public int getAvailableDepth()
113 {
114 return depth;
115 }
116
117 /**
118 * Set the next available depth in the timeline
119 * @param depth must be >= 1
120 */
121 public void setAvailableDepth( int depth )
122 {
123 if( depth < 1 ) return;
124 this.depth = depth;
125 }
126
127 protected int defineSymbol( Movie movie,
128 SWFTagTypes timelineWriter,
129 SWFTagTypes definitionWriter )
130 throws IOException
131 {
132 //--flush all symbol definitions
133 for( Iterator iter = frames.values().iterator(); iter.hasNext(); )
134 {
135 Frame frame = (Frame)iter.next();
136 frame.flushDefinitions( movie, timelineWriter, definitionWriter );
137 }
138
139 int id = getNextId( movie );
140 SWFTagTypes spriteWriter = definitionWriter.tagDefineSprite( id );
141
142 int lastFrame = 0;
143 for( Iterator iter = frames.values().iterator(); iter.hasNext(); )
144 {
145 Frame frame = (Frame)iter.next();
146
147 int number = frame.getFrameNumber();
148
149 //write any intermediate empty frames
150 while( number > lastFrame + 1 )
151 {
152 spriteWriter.tagShowFrame();
153 lastFrame++;
154 }
155
156 frame.write( movie, definitionWriter, spriteWriter );
157
158 lastFrame = number;
159 }
160
161 //end of time line
162 spriteWriter.tagEnd();
163
164 return id;
165 }
166 }