KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > api > button > Button


1 /*
2  * $Id: Button.java,v 1.3 2002/03/31 19:47:26 skavish Exp $
3  *
4  * ===========================================================================
5  *
6  * The JGenerator Software License, Version 1.0
7  *
8  * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by Dmitry Skavish
24  * (skavish@usa.net, http://www.flashgap.com/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The name "The JGenerator" must not be used to endorse or promote
29  * products derived from this software without prior written permission.
30  * For written permission, please contact skavish@usa.net.
31  *
32  * 5. Products derived from this software may not be called "The JGenerator"
33  * nor may "The JGenerator" appear in their names without prior written
34  * permission of Dmitry Skavish.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
40  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  */

50
51 package org.openlaszlo.iv.flash.api.button;
52
53 import java.io.PrintStream JavaDoc;
54 import java.awt.geom.*;
55
56 import org.openlaszlo.iv.flash.util.*;
57 import org.openlaszlo.iv.flash.parser.*;
58 import org.openlaszlo.iv.flash.api.*;
59 import org.openlaszlo.iv.flash.api.image.*;
60 import org.openlaszlo.iv.flash.api.action.*;
61 import org.openlaszlo.iv.flash.context.Context;
62
63 /**
64  * Class represents Flash Button
65  */

66 public class Button extends FlashDef {
67
68     protected IVVector buttonRecords = new IVVector();
69     protected IVVector conditions = new IVVector(4);
70     protected ButtonCXForm buttonCXForm;
71     protected ButtonSound buttonSound;
72     protected boolean isProcessed;
73
74     public Button() {}
75
76     public int getTag() {
77         return Tag.DEFINEBUTTON;
78     }
79
80     public void setButtonCXForm( ButtonCXForm bcxf ) {
81         this.buttonCXForm = bcxf;
82     }
83
84     public ButtonCXForm getButtonCXForm() {
85         return this.buttonCXForm;
86     }
87
88     public void setButtonSound( ButtonSound bsnd ) {
89         this.buttonSound = bsnd;
90     }
91
92     public ButtonSound getButtonSound() {
93         return this.buttonSound;
94     }
95
96     public void addButtonRecord( ButtonRecord br ) {
97         buttonRecords.addElement(br);
98     }
99
100     public IVVector getButtonRecords() {
101         return buttonRecords;
102     }
103
104     public void setButtonRecords( IVVector buttonRecords ) {
105         this.buttonRecords = buttonRecords;
106     }
107
108     public void addActionCondition( ActionCondition ac ) {
109         conditions.addElement(ac);
110     }
111
112     public IVVector getActionConditions() {
113         return conditions;
114     }
115
116     public void setActionConditions( IVVector conditions ) {
117         this.conditions = conditions;
118     }
119
120     public static Button parse( Parser p ) {
121         Button o = new Button();
122 // o.tagCode = p.getTagCode();
123
o.setID( p.getUWord() );
124         o.parseButtonRecords(p,false);
125         o.addActionCondition( new ActionCondition(0,new Program(p.getBuf(), p.getPos(), p.getTagEndPos())) );
126         return o;
127     }
128
129     protected void parseButtonRecords( Parser p, boolean withAlpha ) {
130         for(;;) {
131             int flags = p.getUByte();
132             if( flags == 0 ) break;
133             ButtonRecord br = new ButtonRecord();
134             br.setStates( flags );
135             br.setDef( p.getDef( p.getUWord() ) );
136             br.setLayer( p.getUWord() );
137             br.setMatrix( p.getMatrix() );
138             br.setCXForm( CXForm.parse(p, withAlpha) );
139             addButtonRecord(br);
140         }
141     }
142
143     protected void writeButtonRecords( FlashOutput fob ) {
144         for( int i=0; i<buttonRecords.size(); i++ ) {
145             ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i);
146             t.write(fob);
147         }
148         fob.writeByte(0);
149     }
150
151     public void collectDeps( DepsCollector dc ) {
152         //System.out.println( "Button collectDeps: ID="+getID() );
153
for( int i=0; i<buttonRecords.size(); i++ ) {
154             ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i);
155             //System.out.println( " collected: ID="+t.getDef().getID() );
156
dc.addDep( t.getDef() );
157         }
158         if( buttonSound != null ) buttonSound.collectDeps(dc);
159     }
160
161     public void collectFonts( FontsCollector fc ) {
162         for( int i=0; i<buttonRecords.size(); i++ ) {
163             ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i);
164             t.getDef().collectFonts(fc);
165         }
166     }
167
168     /**
169      * Returns this button's bounds
170      *
171      * @return bounds of this button
172      */

173     public Rectangle2D getBounds() {
174         Rectangle2D rect = null;
175         Rectangle2D bounds = GeomHelper.newRectangle();
176         for( int i=0; i<buttonRecords.size(); i++ ) {
177             ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i);
178             // uncomment this if only UP and Hit shapes should be considered
179
//if( (t.getStates()&(ButtonRecord.Up|ButtonRecord.HitTest)) == 0 ) continue;
180
GeomHelper.calcBounds(t.getMatrix(), t.getDef().getBounds(), bounds);
181             rect = GeomHelper.add( rect, bounds );
182         }
183         return rect;
184     }
185
186     public void write( FlashOutput fob ) {
187         int pos = fob.getPos();
188         fob.skip(6);
189         fob.writeDefID( this );
190         writeButtonRecords(fob);
191         ((ActionCondition)conditions.elementAt(0)).getProgram().write(fob);
192         fob.writeLongTagAt(getTag(), fob.getPos()-pos-6, pos);
193         writeExternals( fob );
194     }
195
196     protected void writeExternals( FlashOutput fob ) {
197         if( buttonCXForm != null ) {
198             buttonCXForm.setButton( this );
199             buttonCXForm.write(fob);
200         }
201         if( buttonSound != null ) {
202             buttonSound.setButton( this );
203             buttonSound.write(fob);
204         }
205     }
206
207     public void printContent( PrintStream JavaDoc out, String JavaDoc indent ) {
208         out.println( indent+"Button("+Tag.tagNames[getTag()]+"): id="+getID() );
209         for( int i=0; i<buttonRecords.size(); i++ ) {
210             ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i);
211             t.printContent(out, indent);
212         }
213         for( int i=0; i<conditions.size(); i++ ) {
214             ActionCondition ac = (ActionCondition) conditions.elementAt(i);
215             ac.printContent(out, indent);
216         }
217     }
218
219     protected boolean _isConstant() {
220         for( int i=0; i<buttonRecords.size(); i++ ) {
221             ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i);
222             if( !t.getDef().isConstant() ) return false;
223         }
224         for( int i=0; i<conditions.size(); i++ ) {
225             ActionCondition ac = (ActionCondition) conditions.elementAt(i);
226             if( !ac.getProgram().isConstant() ) return false;
227         }
228         return true;
229     }
230
231     public void process( FlashFile file, Context context ) throws IVException {
232         for( int i=0; i<buttonRecords.size(); i++ ) {
233             ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i);
234             FlashDef def = t.getDef();
235             if( !isProcessed() ) {
236                 file.processObject(def, context);
237             }
238         }
239     }
240
241     public boolean isProcessed() {
242         return isProcessed;
243     }
244
245     public void setProcessed() {
246         this.isProcessed = true;
247     }
248
249     public void apply( Context context ) {
250         super.apply( context );
251         for( int i=0; i<buttonRecords.size(); i++ ) {
252             ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i);
253             t.getDef().apply(context);
254         }
255         for( int i=0; i<conditions.size(); i++ ) {
256             ActionCondition ac = (ActionCondition) conditions.elementAt(i);
257             ac.getProgram().apply(context);
258         }
259     }
260
261     protected FlashItem copyInto( FlashItem item, ScriptCopier copier ) {
262         super.copyInto( item, copier );
263         ((Button)item).buttonRecords = buttonRecords.getCopy(copier);
264         ((Button)item).conditions = conditions.getCopy(copier);
265         ((Button)item).buttonCXForm = buttonCXForm != null? (ButtonCXForm)buttonCXForm.getCopy(copier): null;
266         ((Button)item).buttonSound = buttonSound != null? (ButtonSound)buttonSound.getCopy(copier): null;
267         ((Button)item).isProcessed = isProcessed;
268         return item;
269     }
270
271     public FlashItem getCopy( ScriptCopier copier ) {
272         return copyInto( new Button(), copier );
273     }
274
275 }
276
277
Popular Tags