001: /*
002: * $Id: Button.java,v 1.3 2002/03/31 19:47:26 skavish Exp $
003: *
004: * ===========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash.api.button;
052:
053: import java.io.PrintStream;
054: import java.awt.geom.*;
055:
056: import org.openlaszlo.iv.flash.util.*;
057: import org.openlaszlo.iv.flash.parser.*;
058: import org.openlaszlo.iv.flash.api.*;
059: import org.openlaszlo.iv.flash.api.image.*;
060: import org.openlaszlo.iv.flash.api.action.*;
061: import org.openlaszlo.iv.flash.context.Context;
062:
063: /**
064: * Class represents Flash Button
065: */
066: public class Button extends FlashDef {
067:
068: protected IVVector buttonRecords = new IVVector();
069: protected IVVector conditions = new IVVector(4);
070: protected ButtonCXForm buttonCXForm;
071: protected ButtonSound buttonSound;
072: protected boolean isProcessed;
073:
074: public Button() {
075: }
076:
077: public int getTag() {
078: return Tag.DEFINEBUTTON;
079: }
080:
081: public void setButtonCXForm(ButtonCXForm bcxf) {
082: this .buttonCXForm = bcxf;
083: }
084:
085: public ButtonCXForm getButtonCXForm() {
086: return this .buttonCXForm;
087: }
088:
089: public void setButtonSound(ButtonSound bsnd) {
090: this .buttonSound = bsnd;
091: }
092:
093: public ButtonSound getButtonSound() {
094: return this .buttonSound;
095: }
096:
097: public void addButtonRecord(ButtonRecord br) {
098: buttonRecords.addElement(br);
099: }
100:
101: public IVVector getButtonRecords() {
102: return buttonRecords;
103: }
104:
105: public void setButtonRecords(IVVector buttonRecords) {
106: this .buttonRecords = buttonRecords;
107: }
108:
109: public void addActionCondition(ActionCondition ac) {
110: conditions.addElement(ac);
111: }
112:
113: public IVVector getActionConditions() {
114: return conditions;
115: }
116:
117: public void setActionConditions(IVVector conditions) {
118: this .conditions = conditions;
119: }
120:
121: public static Button parse(Parser p) {
122: Button o = new Button();
123: // o.tagCode = p.getTagCode();
124: o.setID(p.getUWord());
125: o.parseButtonRecords(p, false);
126: o.addActionCondition(new ActionCondition(0, new Program(p
127: .getBuf(), p.getPos(), p.getTagEndPos())));
128: return o;
129: }
130:
131: protected void parseButtonRecords(Parser p, boolean withAlpha) {
132: for (;;) {
133: int flags = p.getUByte();
134: if (flags == 0)
135: break;
136: ButtonRecord br = new ButtonRecord();
137: br.setStates(flags);
138: br.setDef(p.getDef(p.getUWord()));
139: br.setLayer(p.getUWord());
140: br.setMatrix(p.getMatrix());
141: br.setCXForm(CXForm.parse(p, withAlpha));
142: addButtonRecord(br);
143: }
144: }
145:
146: protected void writeButtonRecords(FlashOutput fob) {
147: for (int i = 0; i < buttonRecords.size(); i++) {
148: ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i);
149: t.write(fob);
150: }
151: fob.writeByte(0);
152: }
153:
154: public void collectDeps(DepsCollector dc) {
155: //System.out.println( "Button collectDeps: ID="+getID() );
156: for (int i = 0; i < buttonRecords.size(); i++) {
157: ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i);
158: //System.out.println( " collected: ID="+t.getDef().getID() );
159: dc.addDep(t.getDef());
160: }
161: if (buttonSound != null)
162: buttonSound.collectDeps(dc);
163: }
164:
165: public void collectFonts(FontsCollector fc) {
166: for (int i = 0; i < buttonRecords.size(); i++) {
167: ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i);
168: t.getDef().collectFonts(fc);
169: }
170: }
171:
172: /**
173: * Returns this button's bounds
174: *
175: * @return bounds of this button
176: */
177: public Rectangle2D getBounds() {
178: Rectangle2D rect = null;
179: Rectangle2D bounds = GeomHelper.newRectangle();
180: for (int i = 0; i < buttonRecords.size(); i++) {
181: ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i);
182: // uncomment this if only UP and Hit shapes should be considered
183: //if( (t.getStates()&(ButtonRecord.Up|ButtonRecord.HitTest)) == 0 ) continue;
184: GeomHelper.calcBounds(t.getMatrix(),
185: t.getDef().getBounds(), bounds);
186: rect = GeomHelper.add(rect, bounds);
187: }
188: return rect;
189: }
190:
191: public void write(FlashOutput fob) {
192: int pos = fob.getPos();
193: fob.skip(6);
194: fob.writeDefID(this );
195: writeButtonRecords(fob);
196: ((ActionCondition) conditions.elementAt(0)).getProgram().write(
197: fob);
198: fob.writeLongTagAt(getTag(), fob.getPos() - pos - 6, pos);
199: writeExternals(fob);
200: }
201:
202: protected void writeExternals(FlashOutput fob) {
203: if (buttonCXForm != null) {
204: buttonCXForm.setButton(this );
205: buttonCXForm.write(fob);
206: }
207: if (buttonSound != null) {
208: buttonSound.setButton(this );
209: buttonSound.write(fob);
210: }
211: }
212:
213: public void printContent(PrintStream out, String indent) {
214: out.println(indent + "Button(" + Tag.tagNames[getTag()]
215: + "): id=" + getID());
216: for (int i = 0; i < buttonRecords.size(); i++) {
217: ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i);
218: t.printContent(out, indent);
219: }
220: for (int i = 0; i < conditions.size(); i++) {
221: ActionCondition ac = (ActionCondition) conditions
222: .elementAt(i);
223: ac.printContent(out, indent);
224: }
225: }
226:
227: protected boolean _isConstant() {
228: for (int i = 0; i < buttonRecords.size(); i++) {
229: ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i);
230: if (!t.getDef().isConstant())
231: return false;
232: }
233: for (int i = 0; i < conditions.size(); i++) {
234: ActionCondition ac = (ActionCondition) conditions
235: .elementAt(i);
236: if (!ac.getProgram().isConstant())
237: return false;
238: }
239: return true;
240: }
241:
242: public void process(FlashFile file, Context context)
243: throws IVException {
244: for (int i = 0; i < buttonRecords.size(); i++) {
245: ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i);
246: FlashDef def = t.getDef();
247: if (!isProcessed()) {
248: file.processObject(def, context);
249: }
250: }
251: }
252:
253: public boolean isProcessed() {
254: return isProcessed;
255: }
256:
257: public void setProcessed() {
258: this .isProcessed = true;
259: }
260:
261: public void apply(Context context) {
262: super .apply(context);
263: for (int i = 0; i < buttonRecords.size(); i++) {
264: ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i);
265: t.getDef().apply(context);
266: }
267: for (int i = 0; i < conditions.size(); i++) {
268: ActionCondition ac = (ActionCondition) conditions
269: .elementAt(i);
270: ac.getProgram().apply(context);
271: }
272: }
273:
274: protected FlashItem copyInto(FlashItem item, ScriptCopier copier) {
275: super .copyInto(item, copier);
276: ((Button) item).buttonRecords = buttonRecords.getCopy(copier);
277: ((Button) item).conditions = conditions.getCopy(copier);
278: ((Button) item).buttonCXForm = buttonCXForm != null ? (ButtonCXForm) buttonCXForm
279: .getCopy(copier)
280: : null;
281: ((Button) item).buttonSound = buttonSound != null ? (ButtonSound) buttonSound
282: .getCopy(copier)
283: : null;
284: ((Button) item).isProcessed = isProcessed;
285: return item;
286: }
287:
288: public FlashItem getCopy(ScriptCopier copier) {
289: return copyInto(new Button(), copier);
290: }
291:
292: }
|