001: /*
002: * $Id: MorphFillStyle.java,v 1.1 2002/02/15 23:46: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.shape;
052:
053: import java.awt.geom.*;
054: import java.io.PrintStream;
055: import org.openlaszlo.iv.flash.util.*;
056: import org.openlaszlo.iv.flash.api.*;
057: import org.openlaszlo.iv.flash.parser.*;
058: import org.openlaszlo.iv.flash.api.image.*;
059:
060: /**
061: * FillStyle of MorphShape.
062: *
063: * @author Dmitry Skavish
064: * @see FillStyle
065: */
066: public final class MorphFillStyle extends FlashItem {
067:
068: private int type; // type of this fillstyle
069: private Color color_start; // start color of this fillstyle
070: private Color color_end; // end color of this fillstyle
071: private AffineTransform matrix_start; // start matrix of this fillstyle
072: private AffineTransform matrix_end; // end matrix of this fillstyle
073: private MorphGradient gradient; // gradient of this fillstyle
074: private Bitmap bitmap; // bitmap of this fillstyle
075:
076: public MorphFillStyle() {
077: }
078:
079: public int getType() {
080: return type;
081: }
082:
083: public void setType(int type) {
084: this .type = type;
085: }
086:
087: public Color getColorStart() {
088: return color_start;
089: }
090:
091: public void setColorStart(Color color) {
092: this .color_start = color;
093: }
094:
095: public Color getColorEnd() {
096: return color_end;
097: }
098:
099: public void setColorEnd(Color color) {
100: this .color_end = color;
101: }
102:
103: public AffineTransform getMatrixStart() {
104: return matrix_start;
105: }
106:
107: public void setMatrixStart(AffineTransform matrix) {
108: this .matrix_start = matrix;
109: }
110:
111: public AffineTransform getMatrixEnd() {
112: return matrix_end;
113: }
114:
115: public void setMatrixEnd(AffineTransform matrix) {
116: this .matrix_end = matrix;
117: }
118:
119: public MorphGradient getGraduent() {
120: return gradient;
121: }
122:
123: public void setGradient(MorphGradient gradient) {
124: this .gradient = gradient;
125: }
126:
127: public Bitmap getBitmap() {
128: return bitmap;
129: }
130:
131: public void setBitmap(Bitmap bitmap) {
132: this .bitmap = bitmap;
133: }
134:
135: /**
136: * Creates new solid fill.
137: *
138: * @param color_start start color
139: * @param color_end end color
140: * @return solid fillstyle
141: */
142: public static MorphFillStyle newSolid(Color color_start,
143: Color color_end) {
144: MorphFillStyle fs = new MorphFillStyle();
145: fs.setColorStart(color_start);
146: fs.setColorEnd(color_end);
147: fs.setType(FillStyle.SOLID);
148: return fs;
149: }
150:
151: /**
152: * Creates new linear gradient fill.
153: *
154: * @param gradient gradient of gradient fill
155: * @param matrix_start start matrix
156: * @param matrix_end end matrix
157: * @return linear gradient fill
158: */
159: public static MorphFillStyle newLinearGradient(
160: MorphGradient gradient, AffineTransform matrix_start,
161: AffineTransform matrix_end) {
162: MorphFillStyle fs = new MorphFillStyle();
163: fs.setGradient(gradient);
164: fs.setMatrixStart(matrix_start);
165: fs.setMatrixEnd(matrix_end);
166: fs.setType(FillStyle.LINEAR_GRADIENT);
167: return fs;
168: }
169:
170: /**
171: * Creates new radial gradient fill.
172: *
173: * @param gradient gradient of gradient fill
174: * @param matrix_start start matrix
175: * @param matrix_end end matrix
176: * @return radial gradient fill
177: */
178: public static MorphFillStyle newRadialGradient(
179: MorphGradient gradient, AffineTransform matrix_start,
180: AffineTransform matrix_end) {
181: MorphFillStyle fs = new MorphFillStyle();
182: fs.setGradient(gradient);
183: fs.setMatrixStart(matrix_start);
184: fs.setMatrixEnd(matrix_end);
185: fs.setType(FillStyle.RADIAL_GRADIENT);
186: return fs;
187: }
188:
189: /**
190: * Creates new tiled bitmap fill.
191: *
192: * @param bitmap bitmap of bitmap fill
193: * @param matrix_start start matrix
194: * @param matrix_end end matrix
195: * @return tiled bitmap fill
196: */
197: public static MorphFillStyle newTiledBitmap(Bitmap bitmap,
198: AffineTransform matrix_start, AffineTransform matrix_end) {
199: MorphFillStyle fs = new MorphFillStyle();
200: fs.setBitmap(bitmap);
201: fs.setMatrixStart(matrix_start);
202: fs.setMatrixEnd(matrix_end);
203: fs.setType(FillStyle.TILED_BITMAP);
204: return fs;
205: }
206:
207: /**
208: * Creates new clipped bitmap fill.
209: *
210: * @param bitmap bitmap of bitmap fill
211: * @param matrix_start start matrix
212: * @param matrix_end end matrix
213: * @return clipped bitmap fill
214: */
215: public static MorphFillStyle newClippedBitmap(Bitmap bitmap,
216: AffineTransform matrix_start, AffineTransform matrix_end) {
217: MorphFillStyle fs = new MorphFillStyle();
218: fs.setBitmap(bitmap);
219: fs.setMatrixStart(matrix_start);
220: fs.setMatrixEnd(matrix_end);
221: fs.setType(FillStyle.CLIPPED_BITMAP);
222: return fs;
223: }
224:
225: /**
226: * Parses morph fill style
227: *
228: * @param p parser
229: * @return parsed fillstyle
230: */
231: public static MorphFillStyle parse(Parser p) {
232: MorphFillStyle fs = new MorphFillStyle();
233: int type = fs.type = p.getUByte();
234:
235: if ((type & 0x10) != 0) { // gradient
236:
237: // Get the gradient matrix.
238: fs.matrix_start = p.getMatrix();
239: fs.matrix_end = p.getMatrix();
240: // get gradient itself
241: fs.gradient = MorphGradient.parse(p);
242:
243: } else if ((type & 0x40) != 0) { // bitmap
244:
245: int id = p.getUWord(); // id may be equal to 0xffff, I don't know what it means
246: fs.bitmap = (Bitmap) p.getDef(id);
247: fs.matrix_start = p.getMatrix();
248: fs.matrix_end = p.getMatrix();
249:
250: } else { // A solid color
251:
252: fs.color_start = AlphaColor.parse(p);
253: fs.color_end = AlphaColor.parse(p);
254: }
255:
256: return fs;
257: }
258:
259: /**
260: * Writes fillstyle to flash buffer
261: *
262: * @param fob buffer to write
263: */
264: public void write(FlashOutput fob) {
265: fob.writeByte(type);
266: switch (type) {
267: case FillStyle.SOLID:
268: color_start.writeRGBA(fob);
269: color_end.writeRGBA(fob);
270: break;
271: case FillStyle.LINEAR_GRADIENT:
272: case FillStyle.RADIAL_GRADIENT:
273: fob.write(matrix_start);
274: fob.write(matrix_end);
275: gradient.write(fob);
276: break;
277: case FillStyle.TILED_BITMAP:
278: case FillStyle.CLIPPED_BITMAP:
279: if (bitmap == null) {
280: fob.writeWord(0xffff);
281: } else {
282: fob.writeDefID(bitmap);
283: }
284: fob.write(matrix_start);
285: fob.write(matrix_end);
286: break;
287: }
288: }
289:
290: public void printContent(PrintStream out, String indent) {
291: switch (type) {
292: case FillStyle.SOLID:
293: out.println(indent + "MorphFillStyle (SOLID):");
294: color_start.printContent(out, indent + " ");
295: color_end.printContent(out, indent + " ");
296: break;
297: case FillStyle.LINEAR_GRADIENT:
298: out.println(indent + "MorphFillStyle (LINEAR_GRADIENT):");
299: out.println(indent + " " + matrix_start);
300: out.println(indent + " " + matrix_end);
301: gradient.printContent(out, indent + " ");
302: break;
303: case FillStyle.RADIAL_GRADIENT:
304: out.println(indent + "FillStyle (RADIAL_GRADIENT):");
305: out.println(indent + " " + matrix_start);
306: out.println(indent + " " + matrix_end);
307: gradient.printContent(out, indent + " ");
308: break;
309: case FillStyle.TILED_BITMAP:
310: out.println(indent + "FillStyle (TILED_BITMAP):");
311: out.println(indent + " " + matrix_start);
312: out.println(indent + " " + matrix_end);
313: out.println(indent + " bitmapID=" + bitmap.getID());
314: break;
315: case FillStyle.CLIPPED_BITMAP:
316: out.println(indent + "FillStyle (CLIPPED_BITMAP):");
317: out.println(indent + " " + matrix_start);
318: out.println(indent + " " + matrix_end);
319: out.println(indent + " bitmapID=" + bitmap.getID());
320: break;
321: }
322: }
323:
324: protected FlashItem copyInto(FlashItem item, ScriptCopier copier) {
325: ((MorphFillStyle) item).type = type;
326: switch (type) {
327: case FillStyle.SOLID:
328: ((MorphFillStyle) item).color_start = (Color) color_start
329: .getCopy(copier);
330: ((MorphFillStyle) item).color_end = (Color) color_end
331: .getCopy(copier);
332: break;
333: case FillStyle.LINEAR_GRADIENT:
334: case FillStyle.RADIAL_GRADIENT:
335: ((MorphFillStyle) item).gradient = (MorphGradient) gradient
336: .getCopy(copier);
337: ((MorphFillStyle) item).matrix_start = (AffineTransform) matrix_start
338: .clone();
339: ((MorphFillStyle) item).matrix_end = (AffineTransform) matrix_end
340: .clone();
341: break;
342: case FillStyle.TILED_BITMAP:
343: case FillStyle.CLIPPED_BITMAP:
344: ((MorphFillStyle) item).bitmap = (Bitmap) copier
345: .copy(bitmap);
346: ((MorphFillStyle) item).matrix_start = (AffineTransform) matrix_start
347: .clone();
348: ((MorphFillStyle) item).matrix_end = (AffineTransform) matrix_end
349: .clone();
350: break;
351: }
352: return item;
353: }
354:
355: public FlashItem getCopy(ScriptCopier copier) {
356: return copyInto(new MorphFillStyle(), copier);
357: }
358: }
|