summaryrefslogtreecommitdiffstats
path: root/src/TextureByReference/AnimateTexturesBehavior.java
blob: adf9e60fd11f364aa3e0c342e4b0bba45ee484d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * $RCSfile$
 *
 * Copyright (c) 2004 Sun Microsystems, Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistribution of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any
 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
 * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
 * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
 * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
 * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
 * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
 * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that this software is not designed, licensed or
 * intended for use in the design, construction, operation or
 * maintenance of any nuclear facility.
 *
 * $Revision$
 * $Date$
 * $State$
 */

import javax.media.j3d.*;
import java.awt.image.BufferedImage;
import java.awt.*;
import com.sun.j3d.utils.image.TextureLoader;
import java.util.Enumeration;

public class AnimateTexturesBehavior extends Behavior {


  // what image are we on
  private int current;
  private int max;

  // the images
  private ImageComponent2D[] images;

  // the target
  private Texture2D texture;
  private Appearance appearance;

  // the wakeup criterion
  private WakeupCriterion wakeupC;

  // are the images flipped?
  private boolean flip;

  // need the current type because by copy changes all images
  // to TYPE_INT_ARGB
  private int currentType;

  // for custom types
  public static final int TYPE_CUSTOM_RGBA = 0x01;
  public static final int TYPE_CUSTOM_RGB = 0x02;

  private int customType;

  // create a new AnimateTextureBehavior
  // initialize the images
  public AnimateTexturesBehavior(Texture2D texP, 
     				 java.net.URL[] fnames,
				 Appearance appP,
				 TextureByReference applet) {
    int size = fnames.length;
    images = new ImageComponent2D[size];
    BufferedImage bImage;
    TextureLoader loader;
    for (int i = 0; i < size; i++) {
      loader = new TextureLoader(fnames[i],
 				 TextureLoader.BY_REFERENCE |
 				 TextureLoader.Y_UP, applet);
      images[i] = loader.getImage();
      bImage = images[i].getImage();
      
      // convert the image to TYPE_4BYTE_ABGR
      currentType = BufferedImage.TYPE_4BYTE_ABGR;
      bImage = ImageOps.convertImage(bImage, currentType);
      // flip the image
      flip = true;
      ImageOps.flipImage(bImage);
      
      // set the image on the ImageComponent to the new one
      images[i].set(bImage);

      images[i].setCapability(ImageComponent.ALLOW_IMAGE_READ);
      images[i].setCapability(ImageComponent.ALLOW_FORMAT_READ);
    }
    texture = texP;
    current = 0;
    max = size;
    wakeupC = new WakeupOnElapsedFrames(20);
    appearance = appP;
  }

  // initialize to the first image
  public void initialize() {
    texture.setImage(0, images[current]);
    if (current < max-1) current++;
    else current = 0;
    wakeupOn(wakeupC);
  }

  // procesStimulus changes the ImageComponent of the texture
  public void processStimulus(Enumeration criteria) {
    //    ImageOps.printType(images[current].getImage());
    texture.setImage(0, images[current]);
    appearance.setTexture(texture);
    if (current < max-1) current++; 
    else current = 0;
    wakeupOn(wakeupC);
  }

  // flip the image -- useful depending on yUp
  public void setFlipImages(boolean b) {
    // double check that flipping is necessary
    if (b != flip) {
      BufferedImage bImage;

      // these are the same for all images so get info once
      int format = images[0].getFormat();
      boolean byRef = images[0].isByReference();
      boolean yUp = images[0].isYUp();
      
      // flip all the images
      // have to new ImageComponents because can't set the image at runtime
      for (int i = 0; i < images.length; i++) {
	bImage = images[i].getImage();
	ImageOps.flipImage(bImage);
	// if we are byRef and the bImage type does not match currentType
	// we need to convert it.  If we are not byRef we will 
	// save converting until it is changed to byRef
	if (byRef && bImage.getType() != currentType) {
	  if (currentType != BufferedImage.TYPE_CUSTOM) {
	    bImage = ImageOps.convertImage(bImage, currentType);
	  }
	  else if (customType == this.TYPE_CUSTOM_RGBA) {
	    bImage = ImageOps.convertToCustomRGBA(bImage);
	  }
	  else {
	    bImage = ImageOps.convertToCustomRGB(bImage);
	  }
	}
	images[i] = new ImageComponent2D(format, bImage, byRef, yUp);
	images[i].setCapability(ImageComponent.ALLOW_IMAGE_READ);
	images[i].setCapability(ImageComponent.ALLOW_FORMAT_READ);
      }
      
      // set flip to new value
      flip = b;
    }
  }

  // create new ImageComponents with yUp set to the parameter.  yUp on
  // an ImageComponent cannot be changed at runtim
  public void setYUp(boolean b) {
    // double check that changing yUp is necessary
    if (b != images[0].isYUp()) {

      // these are the same for all images so get info once
      int format = images[0].getFormat();
      boolean byRef = images[0].isByReference();
      
      // reset yUp on all the images -- have to new ImageComponents because
      // cannot change the value at runtime
      for (int i = 0; i < images.length; i++) {
	// if we are byRef and the bImage type does not match currentType
	// we need to convert it.  If we are not byRef we will 
	// save converting until it is changed to byRef
	BufferedImage bImage = images[i].getImage();
	if (byRef && bImage.getType() != currentType) {
	  //	  bImage = ImageOps.convertImage(bImage, currentType);
	  if (currentType != BufferedImage.TYPE_CUSTOM) {
	    bImage = ImageOps.convertImage(bImage, currentType);
	  }
	  else if (customType == this.TYPE_CUSTOM_RGBA) {
	    bImage = ImageOps.convertToCustomRGBA(bImage);
	  }
	  else {
	    bImage = ImageOps.convertToCustomRGB(bImage);
	  }
	}
	images[i] = new ImageComponent2D(format, bImage, 
					 byRef, b);
	images[i].setCapability(ImageComponent.ALLOW_IMAGE_READ);
	images[i].setCapability(ImageComponent.ALLOW_FORMAT_READ);
      }
    }
  }

  // create new ImageComponents with ByReference set by parameter.  
  // by reference cannot be changed on an image component at runtime 
  public void setByReference(boolean b) {
    // double check that changing is necessary
    if (b != images[0].isByReference()) {

      // these are the same for all images so get info once
      int format = images[0].getFormat();
      boolean yUp = images[0].isYUp();

      // reset yUp on all the images
      // have to new ImageComponents because cannot set value
      for (int i = 0; i < images.length; i++) {
	// if the bImage type does not match currentType and we are setting
	// to byRef we need to convert it
	BufferedImage bImage = images[i].getImage();
	if (bImage.getType() != currentType && b) {
	  //	  bImage = ImageOps.convertImage(bImage, currentType);
	  if (currentType != BufferedImage.TYPE_CUSTOM) {
	    bImage = ImageOps.convertImage(bImage, currentType);
	  }
	  else if (customType == this.TYPE_CUSTOM_RGBA) {
	    bImage = ImageOps.convertToCustomRGBA(bImage);
	  }
	  else {
	    bImage = ImageOps.convertToCustomRGB(bImage);
	  }
	}
	images[i] = new ImageComponent2D(format, bImage, b, yUp);
	images[i].setCapability(ImageComponent.ALLOW_IMAGE_READ);
	images[i].setCapability(ImageComponent.ALLOW_FORMAT_READ);
      }
    }
  }

  // make a new wakeup criterion object based on the new delay time
  public void setFrameDelay(int delay) {
    wakeupC = new WakeupOnElapsedFrames(delay);
  }

  //change the type of image
  public void setImageType(int newType) {
    currentType = newType;

    // only need to change the images if we are byRef otherwise will change
    // them when we chnage to byRef
    if (images[0].isByReference() == true) {
      // this information is the same for all
      int format = images[0].getFormat();
      boolean yUp = images[0].isYUp();
      boolean byRef = true;
      for (int i = 0; i < images.length; i++) {
	BufferedImage bImage = images[i].getImage();
	bImage = ImageOps.convertImage(bImage, currentType);
	images[i] = new ImageComponent2D(format, bImage, byRef, yUp);
	images[i].setCapability(ImageComponent.ALLOW_IMAGE_READ);
	images[i].setCapability(ImageComponent.ALLOW_FORMAT_READ);
      }
    }
  }

  public void setImageTypeCustomRGBA() {
    currentType = BufferedImage.TYPE_CUSTOM;
    customType = this.TYPE_CUSTOM_RGBA;

    // only need to change images if we are byRef otherwise will change
    // them when we change to byRef
    if (images[0].isByReference()) {
      // this information is the same for all
      int format = images[0].getFormat();
      boolean yUp = images[0].isYUp();
      boolean byRef = true;
      for (int i = 0; i < images.length; i++) {
	BufferedImage bImage = images[i].getImage();
	bImage = ImageOps.convertToCustomRGBA(bImage);
	images[i] = new ImageComponent2D(format, bImage, byRef, yUp);
	images[i].setCapability(ImageComponent.ALLOW_IMAGE_READ);
	images[i].setCapability(ImageComponent.ALLOW_FORMAT_READ);
      }
    }
  }

  public void setImageTypeCustomRGB() {
    currentType = BufferedImage.TYPE_CUSTOM;
    customType = this.TYPE_CUSTOM_RGB;

    // only need to change images if we are byRef otherwise will change
    // them when we change to byRef
    if (images[0].isByReference()) {
      // this information is the same for all
      int format = images[0].getFormat();
      boolean yUp = images[0].isYUp();
      boolean byRef = true;
      for (int i = 0; i < images.length; i++) {
	BufferedImage bImage = images[i].getImage();
	bImage = ImageOps.convertToCustomRGB(bImage);
	images[i] = new ImageComponent2D(format, bImage, byRef, yUp);
	images[i].setCapability(ImageComponent.ALLOW_IMAGE_READ);
	images[i].setCapability(ImageComponent.ALLOW_FORMAT_READ);
      }
    }
  }
}