001: /*
002: * $Header: /cvs/j3dfly/J3dEditor/src/org/jdesktop/j3dedit/scenegrapheditor/nodeeditors/panels/TextureAttributesPanel.java,v 1.1 2005/04/20 22:21:10 paulby Exp $
003: *
004: * Sun Public License Notice
005: *
006: * The contents of this file are subject to the Sun Public License Version
007: * 1.0 (the "License"). You may not use this file except in compliance with
008: * the License. A copy of the License is available at http://www.sun.com/
009: *
010: * The Original Code is the Java 3D(tm) Scene Graph Editor.
011: * The Initial Developer of the Original Code is Paul Byrne.
012: * Portions created by Paul Byrne are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Paul Byrne.
016: *
017: **/
018: package org.jdesktop.j3dedit.scenegrapheditor.nodeeditors.panels;
019:
020: import javax.media.j3d.TextureAttributes;
021: import javax.media.j3d.NodeComponent;
022: import javax.media.j3d.Appearance;
023: import javax.media.j3d.Texture;
024: import javax.media.j3d.Texture2D;
025: import javax.media.j3d.ImageComponent2D;
026: import java.awt.image.BufferedImage;
027: import javax.swing.JComboBox;
028: import org.jdesktop.j3dedit.scenegraph.SGNodeComponent;
029: import org.jdesktop.j3dedit.scenegraph.SGAppearance;
030: import org.jdesktop.j3dedit.scenegraph.SGTexture;
031: import org.jdesktop.j3dedit.scenegraph.SGTextureAttributes;
032: import org.jdesktop.j3dedit.scenegrapheditor.nodeeditors.NodeComponentEditorPanel;
033:
034: /**
035: * @author Paul Byrne
036: * @version $Id: TextureAttributesPanel.java,v 1.1 2005/04/20 22:21:10 paulby Exp $
037: */
038: public class TextureAttributesPanel extends NodeComponentEditorPanel {
039:
040: private int origTextureMode;
041: private int origPerspectiveCorrection;
042:
043: private java.awt.CardLayout cardLayout;
044:
045: private TexturePanel texturePanel;
046: private boolean doingSetControls = false;
047:
048: /** Creates new form t */
049: public TextureAttributesPanel() {
050: super ();
051: initComponents();
052: cardLayout = (java.awt.CardLayout) getLayout();
053: }
054:
055: protected void setTexturePanel(TexturePanel texturePanel) {
056: this .texturePanel = texturePanel;
057: }
058:
059: public void setReadOnly(boolean readOnly) {
060: this .readOnly = readOnly;
061:
062: createTextureAttributeB.setEnabled(!readOnly);
063: textureModeC.setEnabled(!readOnly);
064: perspectiveCorrectionModeC.setEnabled(!readOnly);
065: }
066:
067: /** This method is called from within the constructor to
068: * initialize the form.
069: * WARNING: Do NOT modify this code. The content of this method is
070: * always regenerated by the FormEditor.
071: */
072: private void initComponents() {//GEN-BEGIN:initComponents
073: java.awt.GridBagConstraints gridBagConstraints;
074:
075: normalPanel = new javax.swing.JPanel();
076: panel2 = new javax.swing.JPanel();
077: textureModeC = new JComboBox(new String[] { "BLEND", "DECAL",
078: "MODULATE", "REPLACE" });
079: perspectiveCorrectionModeC = new JComboBox(new String[] {
080: "FASTEST", "NICEST" });
081:
082: jLabel1 = new javax.swing.JLabel();
083: jLabel2 = new javax.swing.JLabel();
084: ncCommonP = new org.jdesktop.j3dedit.scenegrapheditor.nodeeditors.panels.CommonNodeComponentPanel();
085: nullPanel = new javax.swing.JPanel();
086: panel1 = new javax.swing.JPanel();
087: jLabel3 = new javax.swing.JLabel();
088: createTextureAttributeB = new javax.swing.JButton();
089:
090: setLayout(new java.awt.CardLayout());
091:
092: panel2.setLayout(new java.awt.GridBagLayout());
093:
094: gridBagConstraints = new java.awt.GridBagConstraints();
095: gridBagConstraints.gridx = 1;
096: gridBagConstraints.gridy = 1;
097: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
098: panel2.add(textureModeC, gridBagConstraints);
099:
100: gridBagConstraints = new java.awt.GridBagConstraints();
101: gridBagConstraints.gridx = 1;
102: gridBagConstraints.gridy = 2;
103: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
104: panel2.add(perspectiveCorrectionModeC, gridBagConstraints);
105:
106: jLabel1.setText("Texture Mode");
107: gridBagConstraints = new java.awt.GridBagConstraints();
108: gridBagConstraints.gridx = 0;
109: gridBagConstraints.gridy = 1;
110: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
111: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 4);
112: panel2.add(jLabel1, gridBagConstraints);
113:
114: jLabel2.setText("Perspective Correction");
115: gridBagConstraints = new java.awt.GridBagConstraints();
116: gridBagConstraints.gridx = 0;
117: gridBagConstraints.gridy = 2;
118: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
119: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 4);
120: panel2.add(jLabel2, gridBagConstraints);
121:
122: gridBagConstraints = new java.awt.GridBagConstraints();
123: gridBagConstraints.gridwidth = 2;
124: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
125: panel2.add(ncCommonP, gridBagConstraints);
126:
127: normalPanel.add(panel2);
128:
129: add(normalPanel, "card0");
130:
131: panel1.setLayout(new java.awt.GridBagLayout());
132:
133: jLabel3.setFont(new java.awt.Font("Dialog", 1, 14));
134: jLabel3.setText("Texture Attribute is Null");
135: panel1.add(jLabel3, new java.awt.GridBagConstraints());
136:
137: createTextureAttributeB.setText("Create Attributes");
138: createTextureAttributeB
139: .addActionListener(new java.awt.event.ActionListener() {
140: public void actionPerformed(
141: java.awt.event.ActionEvent evt) {
142: createTextureAttributeBActionPerformed(evt);
143: }
144: });
145:
146: panel1.add(createTextureAttributeB,
147: new java.awt.GridBagConstraints());
148:
149: nullPanel.add(panel1);
150:
151: add(nullPanel, "card1");
152:
153: }//GEN-END:initComponents
154:
155: private void createTextureAttributeBActionPerformed(
156: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createTextureAttributeBActionPerformed
157: // Add your handling code here:
158: setUpdateRequired(true);
159: node = new SGTextureAttributes(new TextureAttributes(),
160: nodeParent.getContext());
161: if (readOnly)
162: setReadCapabilityBits(node.getJ3dNode());
163: else
164: setReadWriteCapabilityBits(node.getJ3dNode());
165: ((Appearance) getParentJ3dNodeComponent())
166: .setTextureAttributes((TextureAttributes) node
167: .getJ3dNode());
168: ((SGAppearance) nodeParent)
169: .setSGTextureAttributes((SGTextureAttributes) node);
170:
171: if (((Appearance) getParentJ3dNodeComponent()).getTexture() == null) {
172: Texture texture = new Texture2D(Texture.BASE_LEVEL,
173: Texture.RGB, 2, 2);
174: texture.setImage(0, new ImageComponent2D(
175: ImageComponent2D.FORMAT_RGB, new BufferedImage(2,
176: 2, BufferedImage.TYPE_INT_RGB)));
177: texture.setEnable(false);
178: texturePanel.setEditorCapabilities(texture);
179: SGTexture sgTexture = new SGTexture(texture, nodeParent
180: .getContext());
181: texturePanel.startEdit(sgTexture, nodeParent, parentEditor);
182: texturePanel.setControls(); // Ensure we can call resetChanges
183: ((Appearance) getParentJ3dNodeComponent())
184: .setTexture(texture);
185: ((SGAppearance) nodeParent).setSGTexture(sgTexture);
186: }
187:
188: ((SGAppearance) node)
189: .setSGTextureAttributes((SGTextureAttributes) node);
190: ncCommonP.startEdit(node);
191: setControls();
192: }//GEN-LAST:event_createTextureAttributeBActionPerformed
193:
194: private void textureModeCActionPerformed(
195: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textureModeCActionPerformed
196: // Add your handling code here:
197: if (doingSetControls)
198: return;
199: setUpdateRequired(true);
200: TextureAttributes textureAttr = ((Appearance) node.getJ3dNode())
201: .getTextureAttributes();
202: textureAttr.setTextureMode(getTextureMode());
203: }//GEN-LAST:event_textureModeCActionPerformed
204:
205: private void perspectiveCorrectionCActionPerformed(
206: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_perspectiveCorrectionCActionPerformed
207: // Add your handling code here:
208: if (doingSetControls)
209: return;
210: setUpdateRequired(true);
211: TextureAttributes textureAttr = ((Appearance) node.getJ3dNode())
212: .getTextureAttributes();
213: textureAttr
214: .setPerspectiveCorrectionMode(getPerspectiveCorrectionMode());
215: }//GEN-LAST:event_perspectiveCorrectionCActionPerformed
216:
217: protected void setControls() {
218: if (node != null)
219: System.out.println(node.getJ3dNode());
220: if (node == null || node.getJ3dNode() == null) {
221: cardLayout.last(this );
222: } else {
223: cardLayout.first(this );
224: doingSetControls = true;
225:
226: TextureAttributes textureAttr = (TextureAttributes) node
227: .getJ3dNode();
228: origTextureMode = textureAttr.getTextureMode();
229: switch (origTextureMode) {
230: case TextureAttributes.BLEND:
231: textureModeC.setSelectedIndex(0);
232: break;
233: case TextureAttributes.DECAL:
234: textureModeC.setSelectedIndex(1);
235: break;
236: case TextureAttributes.MODULATE:
237: textureModeC.setSelectedIndex(2);
238: break;
239: case TextureAttributes.REPLACE:
240: textureModeC.setSelectedIndex(3);
241: break;
242: default:
243: throw new RuntimeException("Unknown Texture Mode");
244: }
245:
246: origPerspectiveCorrection = textureAttr
247: .getPerspectiveCorrectionMode();
248: switch (origPerspectiveCorrection) {
249: case TextureAttributes.FASTEST:
250: perspectiveCorrectionModeC.setSelectedIndex(0);
251: break;
252: case TextureAttributes.NICEST:
253: perspectiveCorrectionModeC.setSelectedIndex(1);
254: break;
255: default:
256: throw new RuntimeException(
257: "Unknown Perspective Correction Mode");
258: }
259: doingSetControls = false;
260: }
261: }
262:
263: public void applyChanges() {
264: setControls();
265: setUpdateRequired(false);
266: }
267:
268: public void resetChanges() {
269: if (node != null) {
270: ((TextureAttributes) node.getJ3dNode())
271: .setTextureMode(origTextureMode);
272: ((TextureAttributes) node.getJ3dNode())
273: .setPerspectiveCorrectionMode(origPerspectiveCorrection);
274: }
275: setControls();
276: setUpdateRequired(false);
277: }
278:
279: /**
280: * Set capability bits for read-only operations on this
281: * Nodes properties
282: */
283: protected void setReadCapabilityBits(NodeComponent node) {
284: if (node == null || node.isLive())
285: return;
286:
287: node.setCapability(TextureAttributes.ALLOW_MODE_READ);
288: }
289:
290: /**
291: * Set capability bits for read/write operations on this
292: * Nodes properties.
293: */
294: protected void setReadWriteCapabilityBits(NodeComponent node) {
295: if (node == null || node.isLive())
296: return;
297:
298: node.setCapability(TextureAttributes.ALLOW_MODE_READ);
299: node.setCapability(TextureAttributes.ALLOW_MODE_WRITE);
300:
301: }
302:
303: private int getTextureMode() {
304: switch (textureModeC.getSelectedIndex()) {
305: case 0:
306: return TextureAttributes.BLEND;
307: case 1:
308: return TextureAttributes.DECAL;
309: case 2:
310: return TextureAttributes.MODULATE;
311: case 3:
312: return TextureAttributes.REPLACE;
313: }
314:
315: throw new RuntimeException("Bad Texture Mode");
316: }
317:
318: private int getPerspectiveCorrectionMode() {
319: switch (perspectiveCorrectionModeC.getSelectedIndex()) {
320: case 0:
321: return TextureAttributes.FASTEST;
322: case 1:
323: return TextureAttributes.NICEST;
324: }
325:
326: throw new RuntimeException("Bad Perspective Correction Mode");
327: }
328:
329: /**
330: * Get the CommonNodeComponentPanel
331: */
332: protected org.jdesktop.j3dedit.scenegrapheditor.nodeeditors.panels.CommonNodeComponentPanel getCommonNCPanel() {
333: return ncCommonP;
334: }
335:
336: // Variables declaration - do not modify//GEN-BEGIN:variables
337: private javax.swing.JPanel nullPanel;
338: private javax.swing.JPanel panel2;
339: private javax.swing.JPanel panel1;
340: private javax.swing.JComboBox perspectiveCorrectionModeC;
341: private javax.swing.JComboBox textureModeC;
342: private javax.swing.JPanel normalPanel;
343: private org.jdesktop.j3dedit.scenegrapheditor.nodeeditors.panels.CommonNodeComponentPanel ncCommonP;
344: private javax.swing.JLabel jLabel3;
345: private javax.swing.JLabel jLabel2;
346: private javax.swing.JLabel jLabel1;
347: private javax.swing.JButton createTextureAttributeB;
348: // End of variables declaration//GEN-END:variables
349:
350: }
|