Source code: com/anotherbigidea/flash/movie/Placement.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.*;
39 import com.anotherbigidea.flash.interfaces.*;
40 import com.anotherbigidea.flash.writers.*;
41 import com.anotherbigidea.flash.readers.*;
42 import com.anotherbigidea.flash.structs.*;
43
44 /**
45 * A Placement holds the transformation and other values relating to the
46 * "placement" of a Symbol Instance within a particular frame.
47 *
48 * They can also denote an "UndefineSymbol" operation.
49 */
50 public class Placement
51 {
52 protected boolean isAlteration;
53 protected boolean isReplacement;
54 protected int frameNumber;
55 protected Instance instance;
56 protected Transform matrix;
57 protected AlphaTransform cxform;
58 protected String name;
59 protected int ratio = -1;
60 protected int clipDepth = -1;
61 protected boolean isRemove = false;
62 protected Actions[] clipActions;
63
64 protected Symbol symbolToFree;
65
66 /**
67 * Return true if the placement represents an UndefineSymbol operation
68 */
69 public boolean isUndefineSymbol() { return symbolToFree != null; }
70
71 /**
72 * Return true if the placement is replacing the symbol at a given
73 * depth with a new symbol
74 */
75 public boolean isReplacement() { return isReplacement; }
76
77 /**
78 * Return true if this placement is an alteration to an Instance that
79 * was placed in a previous frame.
80 */
81 public boolean isAlteration() { return isAlteration; }
82
83 /**
84 * Get the number of the frame within which this placement takes place
85 */
86 public int getFrameNumber() { return frameNumber; }
87
88 /**
89 * Get the Symbol Instance represented by this Placement
90 */
91 public Instance getInstance() { return instance; }
92
93 /**
94 * The transform may be null
95 */
96 public Transform getTransform() { return matrix; }
97
98 /**
99 * The color transform may be null
100 */
101 public AlphaTransform getColorTransform() { return cxform; }
102
103 /**
104 * The name only relates to MovieClip instances and may be null.
105 * The name is only present on the first Placement of an Instance.
106 */
107 public String getName() { return name; }
108
109 /**
110 * The ratio only relates to Morph Shapes and will be -1 otherwise.
111 * The ratio is from zero to 65535 and denotes the degree of the morph
112 * from the initial shape to the final shape.
113 */
114 public int getRatio() { return ratio; }
115
116 /**
117 * The clip depth defines the range of depths which will be clipped by this
118 * symbol. All symbols placed at depths from depth+1 to clipDepth (inclusive)
119 * will be clipped. If this symbol is not a clipping symbol then the clip
120 * depth will be -1.
121 *
122 * The clip depth is only present on the first Placement of an Instance.
123 */
124 public int getClipDepth() { return clipDepth; }
125
126 /**
127 * If true then this Placement denotes the removal of the Instance from
128 * the stage.
129 */
130 public boolean isRemove() { return isRemove; }
131
132 /**
133 * Get the actions for a movie clip
134 */
135 public Actions[] getClipActions() { return clipActions; }
136
137 /**
138 * Set the actions for a movie clip
139 */
140 public void setClipActions( Actions[] clipActions ) { this.clipActions = clipActions; }
141
142 /**
143 * An UndefineSymbol operation
144 */
145 protected Placement( Symbol symbol )
146 {
147 this.symbolToFree = symbol;
148 }
149
150 protected Placement( Instance instance, int frameNumber )
151 {
152 this.instance = instance;
153 this.frameNumber = frameNumber;
154 this.isRemove = true;
155 }
156
157 protected Placement( Instance instance, Transform matrix, AlphaTransform cxform,
158 String name, int ratio, int clipDepth, int frameNumber,
159 boolean alteration, boolean replacement, Actions[] clipActions )
160 {
161 this.instance = instance;
162 this.frameNumber = frameNumber;
163 this.matrix = matrix;
164 this.cxform = cxform;
165 this.name = name;
166 this.ratio = ratio;
167 this.clipDepth = clipDepth;
168 this.isRemove = false;
169 this.isAlteration = alteration;
170 this.isReplacement = replacement;
171 this.clipActions = clipActions;
172 }
173
174 protected void flushDefinitions( Movie movie,
175 SWFTagTypes timelineWriter,
176 SWFTagTypes definitionWriter )
177 throws IOException
178 {
179 if( symbolToFree != null ) return;
180
181 if( (! isAlteration) && ! isRemove )
182 {
183 //--Make sure that the symbol is defined
184 Symbol symbol = instance.getSymbol();
185
186 symbol.define( movie, timelineWriter, definitionWriter );
187 }
188 }
189
190 protected void write( Movie movie,
191 SWFTagTypes movieTagWriter,
192 SWFTagTypes timelineTagWriter )
193 throws IOException
194 {
195 if( symbolToFree != null )
196 {
197 int id = symbolToFree.getId();
198 timelineTagWriter.tagFreeCharacter( id );
199 return;
200 }
201
202 int depth = instance.getDepth();
203 if( depth < 0 ) return;
204
205 if( isRemove )
206 {
207 //--Remove the instance
208 timelineTagWriter.tagRemoveObject2( depth );
209 return;
210 }
211
212 //--Check whether the Instance has been placed
213 if( ! isAlteration )
214 {
215 //--Make sure that the symbol is defined
216 Symbol symbol = instance.getSymbol();
217 int id = symbol.define( movie, timelineTagWriter, movieTagWriter );
218
219 int flags = 0;
220
221 if( clipActions != null && clipActions.length > 0 )
222 {
223 for( int i = 0; i < clipActions.length; i++ )
224 {
225 flags |= clipActions[i].getConditions();
226 }
227 }
228
229 SWFActions acts = timelineTagWriter.tagPlaceObject2(
230 isReplacement, clipDepth, depth, id,
231 matrix, cxform, ratio, name, flags );
232
233 if( clipActions != null && clipActions.length > 0 )
234 {
235 for( int i = 0; i < clipActions.length; i++ )
236 {
237 acts.start( clipActions[i].getConditions() );
238 acts.blob( clipActions[i].bytes );
239 }
240
241 acts.done();
242 }
243 }
244 else
245 {
246 //--Instance is placed - this is just a move
247 timelineTagWriter.tagPlaceObject2( true, clipDepth, depth, -1,
248 matrix, cxform, ratio, null, 0 );
249 }
250 }
251 }