KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > compiler > SWFFile


1 /*****************************************************************************
2  * SWFFile.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.compiler;
11
12 import org.openlaszlo.sc.ScriptCompiler;
13 import org.openlaszlo.utils.ChainedException;
14
15 import org.openlaszlo.iv.flash.util.*;
16 import org.openlaszlo.iv.flash.api.*;
17 import org.openlaszlo.iv.flash.api.button.*;
18 import org.openlaszlo.iv.flash.api.action.*;
19 import org.openlaszlo.iv.flash.api.image.*;
20 import org.openlaszlo.iv.flash.api.sound.*;
21 import org.openlaszlo.iv.flash.api.text.*;
22 import org.openlaszlo.iv.flash.api.shape.*;
23
24 import java.awt.geom.Rectangle2D JavaDoc;
25 import java.awt.geom.AffineTransform JavaDoc;
26
27 import java.io.*;
28
29 import java.util.Properties JavaDoc;
30
31 import org.apache.log4j.*;
32
33 /**
34  * Extension of JGenerator FlashFile api used to
35  * constructs the Base SWF Library for an LZX output SWF.
36  *
37  * @author <a HREF="mailto:bloch@laszlosystems.com">Eric Bloch</a>
38  */

39 class SWFFile extends FlashFile {
40
41     public static final int TWIP = 20;
42     public static final int KEYCODE_ENTER = 13;
43     public static final int KEYCODE_TAB = 18;
44
45     private Properties JavaDoc mProperties = null;
46
47     /**
48      * Return a ActionBlock from the given text.
49      */

50     protected DoAction actionBlock(String JavaDoc s) {
51         return new DoAction(program(s));
52     }
53
54     /**
55      * Return a program from the given text.
56      */

57     protected Program program(String JavaDoc s) {
58         byte[] action = ScriptCompiler.compileToByteArray(s, mProperties);
59         return new Program(action, 0, action.length);
60     }
61
62
63     /**
64      * Export the given def with the given name
65      * @param n name
66      * @param def def
67      */

68     protected void export(String JavaDoc n, FlashDef def) {
69         addDef(def);
70         ExportAssets ea = new ExportAssets();
71         ea.addAsset(n, def);
72         Frame frame = getMainScript().getFrameAt(0);
73         frame.addFlashObject(ea);
74     }
75
76     /**
77      * Make a 100x100 rectangle
78      */

79     private Shape rectangle() {
80         Shape shape = new Shape();
81         shape.setFillStyle1( FillStyle.newSolid( AlphaColor.white ) );
82         Rectangle2D JavaDoc r = new Rectangle2D.Double JavaDoc(0, 0, 100*TWIP, 100*TWIP);
83         shape.drawRectangle( r );
84         shape.setBounds( r );
85         return shape;
86     }
87
88     /**
89      * Create the LFC Base Library
90      * @param path to base LFC bytecode library
91      * @param props properties affecting construction
92      */

93     public SWFFile(String JavaDoc path, Properties JavaDoc props) {
94
95         mProperties = props;
96
97
98         try {
99
100             String JavaDoc s;
101             Script movieClip, empty;
102             ClipActions ca;
103             Instance inst;
104             DoAction block;
105             Frame f0, f1;
106             Program p;
107             Button2 but;
108             Shape shape;
109             AffineTransform JavaDoc at = new AffineTransform JavaDoc();
110             AffineTransform JavaDoc offScreen = new AffineTransform JavaDoc();
111             int states;
112
113             Script mainScript = new Script(1);
114             mainScript.setMain();
115             setMainScript(mainScript);
116             Frame frame = getMainScript().getFrameAt(0);
117             Shape rectShape = rectangle();
118
119             // 1. Button moved offscreen so that it's not vis. rectangle with actions
120
but = new Button2();
121             states = ButtonRecord.HitTest;
122             but.addButtonRecord(new ButtonRecord(states, rectShape, 1, at, new CXForm()));
123             // '18' is the key code for "tab", not sure why.
124
but.addActionCondition(
125                 new ActionCondition(KEYCODE_TAB<<9, program("_root.LzKeys.gotKeyDown(9, 'extra');"))
126                 );
127             offScreen.scale(.1, .1);
128             offScreen.translate(-200*TWIP, -200*TWIP);
129             // NOTE: mimic old laszlolibrary.swf; put at depth 9
130
frame.addInstance(but, 9, offScreen, null);
131
132             // 2. Movieclip called 'entercontrol' with 2 frames.
133
movieClip = new Script(2);
134             // First frame of this movieclip has a stop
135
movieClip.getFrameAt(0).addStopAction();
136             // Second frame of this movieclip has a button with an empty action
137
but = new Button2();
138             states = ButtonRecord.HitTest;
139             but.addButtonRecord(new ButtonRecord(states, rectShape, 1, at, new CXForm()));
140             but.addActionCondition(new ActionCondition(KEYCODE_ENTER<<9, new Program()));
141             movieClip.getFrameAt(1).addInstance(but, 1, null, null);
142             // NOTE: mimic old laszlolibrary.swf; put at depth 11
143
frame.addInstance(movieClip, 11, offScreen, null, "entercontrol");
144
145             // 3. movieclip called "frameupdate" which has two frames and some clip actions:
146
movieClip = new Script(2);
147             block = actionBlock("_root.LzIdle.onidle.sendEvent( getTimer() );");
148             movieClip.getFrameAt(0).addFlashObject(block);
149             movieClip.getFrameAt(1).addFlashObject(block);
150
151             // NOTE: depth=2 required in order for this to work in swf6 for some
152
// unknown reason!
153
inst = frame.addInstance(movieClip, 2, null, null, "frameupdate");
154             ca = new ClipActions();
155             ca.setMask(ClipAction.KEY_DOWN|ClipAction.KEY_UP|ClipAction.MOUSE_DOWN|ClipAction.MOUSE_UP);
156             s = "_root.LzKeys.gotKeyDown(Key.getCode())";
157             ca.addAction(new ClipAction(ClipAction.KEY_DOWN, program(s)));
158             s = "_root.LzKeys.gotKeyUp(Key.getCode())";
159             ca.addAction(new ClipAction(ClipAction.KEY_UP, program(s)));
160             s = "_root.LzModeManager.rawMouseEvent('onmousedown')";
161             ca.addAction(new ClipAction(ClipAction.MOUSE_DOWN, program(s)));
162             s = "_root.LzModeManager.rawMouseEvent('onmouseup')";
163             ca.addAction(new ClipAction(ClipAction.MOUSE_UP, program(s)));
164             inst.actions = ca;
165
166             // 4. A movieclip with the export identifier "empty" that has 1 frame with nothing in it
167
empty = movieClip = new Script(1);
168             export("empty", movieClip);
169              
170             // 5. A movieclip with the export identifier "LzMask" that has 1 frame and three layers.
171
// *First layer is a mask layer which has:
172
// A 100x100 square rectangle
173
// *Second layer is an empty movieclip which is named "mask_sub"
174
// *Third layer is a movieclip named "meas" which contains a 100x100 rectangle and these actions:
175
// onClipEvent( load ){
176
// this._visible = false;
177
// }
178
// NOTE: mimic depths from old laszlolibrary.swf
179
movieClip = new Script(1);
180             export("LzMask", movieClip);
181             f0 = movieClip.getFrameAt(0);
182
183             // 3rd layer: NOT NEEDED according to adam.
184
/*
185             movieClip = new Script(1);
186             movieClip.getFrameAt(0).addInstance(rectShape, 1, at, null);
187             inst = f0.addInstance(movieClip, 1, at, null, "meas");
188             ca = new ClipActions();
189             ca.setMask(ClipAction.LOAD);
190             ca.addAction(new ClipAction(ClipAction.LOAD, program("this._visible = false;")));
191             inst.actions = ca;
192             */

193
194             // 2nd layer
195
f0.addInstance(empty, 4, null, null, "mask_sub");
196
197             // 1st layer
198
inst = f0.addInstance(rectShape, 3, at, null);
199             inst.clip = 6; // NOTE: [2004-02-17 bloch] mimic depth from old laszlolibrary.swf
200

201
202             // 6. A movieclip with the export identifier "LzMouseEvents" that has a 100x100
203
// button with no visible region and several actions
204
movieClip = new Script(1);
205             export("LzMouseEvents", movieClip);
206             but = new Button2();
207             states = ButtonRecord.HitTest;
208             but.addButtonRecord(new ButtonRecord(states, rectShape, 1, at, new CXForm()));
209             but.addActionCondition(ActionCondition.onPress(program(
210                 "_root.LzModeManager.handleMouseButton( myView, 'onmousedown')")));
211             but.addActionCondition(ActionCondition.onRelease(program(
212                 "_root.LzModeManager.handleMouseButton( myView, 'onmouseup');" +
213                 "_root.LzModeManager.handleMouseEvent( myView, 'onclick')")));
214             but.addActionCondition(ActionCondition.onReleaseOutside(program(
215                 "_root.LzModeManager.handleMouseButton( myView, 'onmouseup');" +
216                 "_root.LzModeManager.handleMouseEvent( myView, 'onmouseupoutside')")));
217             but.addActionCondition(ActionCondition.onRollOver(program(
218                 "_root.LzModeManager.handleMouseEvent( myView, 'onmouseover')")));
219             but.addActionCondition(ActionCondition.onRollOut(program(
220                 "_root.LzModeManager.handleMouseEvent( myView, 'onmouseout')")));
221             but.addActionCondition(ActionCondition.onDragOut(program(
222                 "_root.LzModeManager.handleMouseEvent( myView, 'onmouseout');" +
223                 "_root.LzModeManager.handleMouseEvent( myView, 'onmousedragout')")));
224             but.addActionCondition(ActionCondition.onDragOver(program(
225                 "_root.LzModeManager.handleMouseEvent( myView, 'onmouseover');" +
226                 "_root.LzModeManager.handleMouseEvent( myView, 'onmousedragin')")));
227             movieClip.getFrameAt(0).addInstance(but, 1, at, null);
228
229             // 7. A movieclip with the export identifier "swatch" which has a white 100x100 rectangle.
230
movieClip = new Script(1);
231             export("swatch", movieClip);
232             f0 = movieClip.getFrameAt(0);
233             f0.addInstance(rectShape, 1, at, null);
234
235             // 8. A movieclip with the export identifier "LZkranker" which has 2 frames:
236
String JavaDoc krank = mProperties.getProperty(CompilationEnvironment.KRANK_PROPERTY);
237             if ("true".equals(krank)) {
238                 movieClip = new Script(2);
239                 export("__LZkranker", movieClip);
240                 f0 = movieClip.getFrameAt(0);
241                 f0.addFlashObject(actionBlock("_root.LzSerializer.procStack();"));
242                 f1 = movieClip.getFrameAt(1);
243                 f1.addFlashObject(actionBlock("_root.LzSerializer.procStack()"));
244             }
245
246             // 9. A movieclip with the export identifier "LZprofiler" which has 2 frames:
247
String JavaDoc profile = mProperties.getProperty(CompilationEnvironment.PROFILE_PROPERTY);
248             if ("true".equals(profile)) {
249                 movieClip = new Script(2);
250                 export("__LZprofiler", movieClip);
251                 block = actionBlock("_root.$lzprofiler.dump()");
252                 movieClip.getFrameAt(0).addFlashObject(block);
253                 movieClip.getFrameAt(1).addFlashObject(block);
254             }
255
256             // 9.1 A movieclip with the export identifier "LZdebugger" which has 2 frames:
257
String JavaDoc debug = mProperties.getProperty(CompilationEnvironment.DEBUG_PROPERTY);
258             if ("true".equals(debug)) {
259                 movieClip = new Script(2);
260                 export("__LZdebugger", movieClip);
261                 block = actionBlock("_root.__LzDebug.background()");
262                 movieClip.getFrameAt(0).addFlashObject(block);
263                 movieClip.getFrameAt(1).addFlashObject(block);
264             }
265
266             // 10. The LFC ActionScript block, the only def in the incoming file
267
FlashFile oldLibrary = FlashFile.parse(path);
268             IVVector objs = oldLibrary.getMainScript().getFrameAt(0);
269             block = (DoAction)objs.elementAt(0);
270             if (block == null) {
271                 throw new ChainedException("no DoAction block in " + path);
272             }
273             frame.addFlashObject(block);
274
275             // 11. Add in end-of-LFC marker for qa
276
block = actionBlock("global[ '$endOfLFCMarker' ];");
277             frame.addFlashObject(block);
278
279         } catch (IVException e) {
280             throw new ChainedException(e);
281         } catch (FileNotFoundException e) {
282             throw new ChainedException(e);
283         } catch (IOException e) {
284             throw new ChainedException(e);
285         }
286     }
287
288     /**
289      * Create a SWFBaseLibrary that has no assets or logic in it
290      */

291     public SWFFile(Properties JavaDoc props) {
292         mProperties = props;
293     }
294
295     /**
296      * Add the preloader frame to
297      */

298     void addPreloaderFrame(String JavaDoc preloadLibPath) {
299                 
300         try {
301             // Insert a blank frame for the preloader
302
Timeline tm = getMainScript().getTimeline();
303             tm.insertFrames(0, 1);
304     
305             // Make preloader movieclip
306
Script prelClip = new Script(1);
307             prelClip.setName("lzpreloader");
308             Frame frame = prelClip.getFrameAt(0);
309             // Get preload lib
310
FlashFile lib = FlashFile.parse(preloadLibPath);
311             IVVector objs = lib.getMainScript().getFrameAt(0);
312             DoAction block = (DoAction)objs.elementAt(0);
313             frame.addFlashObject(block);
314             
315             // Export me
316
export("lzpreloader", prelClip);
317
318             // Insert me
319
tm.getFrameAt(0).addInstance(prelClip, 1, null, null, "lzpreloader");
320     
321         } catch (IVException e) {
322             throw new ChainedException(e);
323         } catch (FileNotFoundException e) {
324             throw new ChainedException(e);
325         } catch (IOException e) {
326             throw new ChainedException(e);
327         }
328     }
329 }
330
Popular Tags