001: /*
002: * $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/plugins/DefaultLightingPlugin.java,v 1.1 2005/04/20 21:04:39 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 Java 3D(tm) Fly Through.
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.j3dfly.plugins;
019:
020: import javax.media.j3d.BranchGroup;
021: import javax.media.j3d.BoundingSphere;
022: import javax.media.j3d.AmbientLight;
023: import javax.media.j3d.DirectionalLight;
024: import javax.media.j3d.Light;
025: import javax.vecmath.Vector3f;
026: import javax.vecmath.Color3f;
027: import javax.vecmath.Point3d;
028: import org.jdesktop.j3dfly.J3dFlyContext;
029: import org.jdesktop.j3dfly.utils.help.Help;
030: import org.jdesktop.j3dfly.utils.gui.SplashWindow;
031: import org.jdesktop.j3dfly.event.FlyEventListener;
032: import org.jdesktop.j3dfly.event.EventProcessor;
033: import org.jdesktop.j3dfly.event.DefaultLightingEnableEvent;
034: import org.jdesktop.j3dfly.event.DefaultLightingRequiredEvent;
035:
036: import javax.swing.JOptionPane;
037:
038: /**
039: *
040: * @author Paul Byrne
041: * @version $Id: DefaultLightingPlugin.java,v 1.1 2005/04/20 21:04:39 paulby Exp $
042: */
043: public class DefaultLightingPlugin extends J3dFlyPlugin implements
044: FlyEventListener {
045:
046: private BranchGroup lightBG = null;
047: private java.lang.ref.SoftReference panelReference = null;
048: private Light[] lights = new Light[3];
049:
050: /**
051: * Ask the user before adding default lights
052: */
053: static boolean askUser = false;
054:
055: /** Creates new form J3dFlyMenu */
056: public DefaultLightingPlugin() {
057: super ();
058: javax.swing.JPopupMenu.setDefaultLightWeightPopupEnabled(false);
059: initComponents();
060: menuBar.remove(defaultLightingMenu);
061: menuBar = null;
062: }
063:
064: public DefaultLightingPlugin(boolean askUser) {
065: this ();
066:
067: this .askUser = askUser;
068: }
069:
070: /**
071: * Returns the class of the plugin preference.
072: *
073: * Plugins that require more preference information should provide a
074: * subclass of PluginPrefernece that contains all the extra preference
075: * data. This class must be Serializable.
076: */
077: public Class getPluginPreferenceClass() {
078: return DefaultLightingPluginPreference.class;
079: }
080:
081: public void installPlugin(PluginPreference pluginPref,
082: J3dFlyContext j3dflyContext) {
083: // Only one DefaultLightingPlugin should be installed in the system, so
084: // remove any duplicates. Must uninstall Duplicates before we call super.installPlugin
085: j3dflyContext.getPluginPreferenceControl()
086: .uninstallDuplicatePlugins(pluginPref);
087:
088: super .installPlugin(pluginPref, j3dflyContext);
089:
090: if (pluginPref.isInstallInMenu())
091: getMenu("Plugins").add(defaultLightingMenu);
092:
093: j3dflyContext.getEventProcessor().addListener(this ,
094: DefaultLightingEnableEvent.class);
095: j3dflyContext.getEventProcessor().addListener(this ,
096: DefaultLightingRequiredEvent.class);
097: }
098:
099: public void uninstallPlugin() {
100: super .uninstallPlugin();
101:
102: if (pluginPref.isInstallInMenu())
103: getMenu("Plugins").remove(defaultLightingMenu);
104:
105: j3dflyContext.getEventProcessor().removeListener(this ,
106: DefaultLightingEnableEvent.class);
107: j3dflyContext.getEventProcessor().removeListener(this ,
108: DefaultLightingRequiredEvent.class);
109: }
110:
111: /**
112: * If true the plugin will ask the user if they
113: * want to add default lights to a scene, when
114: * that scene requires lights
115: */
116: public void setAskUser(boolean askUser) {
117: this .askUser = askUser;
118: }
119:
120: /** This method is called from within the constructor to
121: * initialize the form.
122: * WARNING: Do NOT modify this code. The content of this method is
123: * always regenerated by the FormEditor.
124: */
125: private void initComponents() {//GEN-BEGIN:initComponents
126: menuBar = new javax.swing.JMenuBar();
127: defaultLightingMenu = new javax.swing.JMenu();
128: enableCBMI = new javax.swing.JCheckBoxMenuItem();
129: configureMI = new javax.swing.JMenuItem();
130:
131: defaultLightingMenu.setText("Default Lighting");
132: enableCBMI.setText("Enable");
133: enableCBMI
134: .addActionListener(new java.awt.event.ActionListener() {
135: public void actionPerformed(
136: java.awt.event.ActionEvent evt) {
137: enableCBMIActionPerformed(evt);
138: }
139: });
140:
141: defaultLightingMenu.add(enableCBMI);
142: configureMI.setText("Configure...");
143: configureMI
144: .addActionListener(new java.awt.event.ActionListener() {
145: public void actionPerformed(
146: java.awt.event.ActionEvent evt) {
147: configureMIActionPerformed(evt);
148: }
149: });
150:
151: defaultLightingMenu.add(configureMI);
152: menuBar.add(defaultLightingMenu);
153:
154: }//GEN-END:initComponents
155:
156: private void configureMIActionPerformed(
157: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_configureMIActionPerformed
158: // Add your handling code here:
159: showControls();
160: }//GEN-LAST:event_configureMIActionPerformed
161:
162: private void enableCBMIActionPerformed(
163: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_enableCBMIActionPerformed
164: // Add your handling code here:
165: enableLights(enableCBMI.isSelected());
166: }//GEN-LAST:event_enableCBMIActionPerformed
167:
168: private void createDefaultLights() {
169: // Set up the directional lights
170: lightBG = new BranchGroup();
171: lightBG.setCapability(BranchGroup.ALLOW_DETACH);
172: BoundingSphere bounds = new BoundingSphere(new Point3d(),
173: Double.POSITIVE_INFINITY);
174: Color3f light1Color = new Color3f(0.7f, 0.7f, 0.7f);
175: Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
176: light1Direction.normalize();
177: Color3f light2Color = new Color3f(0.5f, 0.5f, 0.45f);
178: Vector3f light2Direction = new Vector3f(-4.0f, 7.0f, 12.0f);
179: light2Direction.normalize();
180:
181: Color3f ambientColor = new Color3f(0.3f, 0.3f, 0.3f);
182:
183: DirectionalLight light1 = new DirectionalLight(light1Color,
184: light1Direction);
185: light1.setInfluencingBounds(bounds);
186: lightBG.addChild(light1);
187: light1.setCapability(Light.ALLOW_STATE_READ);
188: light1.setCapability(Light.ALLOW_STATE_WRITE);
189: light1.clearCapabilityIsFrequent(Light.ALLOW_STATE_READ);
190: light1.clearCapabilityIsFrequent(Light.ALLOW_STATE_WRITE);
191: lights[0] = light1;
192:
193: DirectionalLight light2 = new DirectionalLight(light2Color,
194: light2Direction);
195: light2.setInfluencingBounds(bounds);
196: lightBG.addChild(light2);
197: light2.setCapability(Light.ALLOW_STATE_READ);
198: light2.setCapability(Light.ALLOW_STATE_WRITE);
199: light2.clearCapabilityIsFrequent(Light.ALLOW_STATE_READ);
200: light2.clearCapabilityIsFrequent(Light.ALLOW_STATE_WRITE);
201: lights[1] = light2;
202:
203: AmbientLight light3 = new AmbientLight(ambientColor);
204: light3.setInfluencingBounds(bounds);
205: lightBG.addChild(light3);
206: light3.setCapability(Light.ALLOW_STATE_READ);
207: light3.setCapability(Light.ALLOW_STATE_WRITE);
208: light3.clearCapabilityIsFrequent(Light.ALLOW_STATE_READ);
209: light3.clearCapabilityIsFrequent(Light.ALLOW_STATE_WRITE);
210: lights[2] = light3;
211: }
212:
213: private void enableLights(boolean enable) {
214: if (enable) {
215: if (lightBG == null)
216: createDefaultLights();
217: j3dflyContext.getJ3dFly().getController().addBranchGraph(
218: lightBG);
219: if (panelReference != null
220: && (DefaultLightingPanel) panelReference.get() != null)
221: ((javax.swing.JPanel) (DefaultLightingPanel) panelReference
222: .get()).setEnabled(true);
223: } else {
224: boolean wasLive = j3dflyContext.getJ3dFly()
225: .getCurrentContext().getLocale().getLive();
226: if (wasLive)
227: j3dflyContext.getJ3dFly().getCurrentContext()
228: .getLocale().setLive(false);
229: j3dflyContext.getJ3dFly().getController()
230: .removeBranchGraph(lightBG);
231: if (wasLive)
232: j3dflyContext.getJ3dFly().getCurrentContext()
233: .getLocale().setLive(true);
234: if (panelReference != null
235: && (DefaultLightingPanel) panelReference.get() != null)
236: ((javax.swing.JPanel) (DefaultLightingPanel) panelReference
237: .get()).setEnabled(false);
238: }
239:
240: if (!enableCBMI.isShowing())
241: enableCBMI.setSelected(enable);
242: }
243:
244: /**
245: * We've had a DefaultLightingRequiredEvent, take the appropriate action
246: */
247: private void lightsRequired() {
248: int answer = JOptionPane.YES_OPTION;
249:
250: if (askUser)
251: answer = JOptionPane
252: .showConfirmDialog(
253: null,
254: "SceneGraph does not contain any Lights\nBut some geometry is expecting Lighting\n\nWould you like to add some ?",
255: "Add Lights ?", JOptionPane.YES_NO_OPTION);
256:
257: if (answer == JOptionPane.YES_OPTION) {
258: enableLights(true);
259: System.out.println("Default lights enabled");
260: }
261: }
262:
263: /**
264: * Return the lights controlled by this plugin
265: */
266: Light[] getLights() {
267: return lights;
268: }
269:
270: /**
271: * Returns the control panel for this plugin, or null if there
272: * is no control panel
273: */
274: public javax.swing.JPanel getControlPanel() {
275: if (panelReference == null || panelReference.get() == null)
276: panelReference = new java.lang.ref.SoftReference(
277: new DefaultLightingPanel(this ));
278:
279: return (DefaultLightingPanel) panelReference.get();
280: }
281:
282: /**
283: * Get the title of the plugin
284: */
285: public String getPluginTitle() {
286: return "Default Lights";
287: }
288:
289: /**
290: * Called by EventProcessor when an event occurs for which this
291: * listener has registered interest
292: */
293: public void processFlyEvent(
294: final org.jdesktop.j3dfly.event.FlyEvent evt) {
295:
296: System.out.println("***** " + evt);
297:
298: if (evt instanceof DefaultLightingEnableEvent) {
299: enableLights(((DefaultLightingEnableEvent) evt).isEnabled());
300: } else if (evt instanceof DefaultLightingRequiredEvent)
301: lightsRequired();
302: else
303: throw new RuntimeException("Unexpected event " + evt);
304: }
305:
306: public static class DefaultLightingPluginPreference extends
307: PluginPreference {
308:
309: public DefaultLightingPluginPreference() {
310: super ();
311: }
312:
313: public DefaultLightingPluginPreference(boolean enabled,
314: boolean installed) {
315: super (enabled, installed);
316: }
317:
318: public J3dFlyPlugin instantiatePlugin() {
319: return new DefaultLightingPlugin();
320: }
321:
322: /** Getter for property askUser.
323: * @return Value of property askUser.
324: */
325: public boolean isAskUser() {
326: return askUser;
327: }
328:
329: /** Setter for property askUser.
330: * @param askUser New value of property askUser.
331: */
332: public void setAskUser(boolean askUserBeforeAdd) {
333: askUser = askUserBeforeAdd;
334: }
335:
336: /**
337: * Return a description of this plugin
338: */
339: public String getDescription() {
340: return "Controls for Default Lighting";
341: }
342:
343: /**
344: * Return the name of the Plugin for this prefernece.
345: * This is the name that will appear in the list of plugins
346: */
347: public String getName() {
348: return "Default Lighting";
349: }
350:
351: }
352:
353: /** Exit the Application */
354: // Variables declaration - do not modify//GEN-BEGIN:variables
355: private javax.swing.JCheckBoxMenuItem enableCBMI;
356: private javax.swing.JMenuBar menuBar;
357: private javax.swing.JMenu defaultLightingMenu;
358: private javax.swing.JMenuItem configureMI;
359: // End of variables declaration//GEN-END:variables
360:
361: }
|