From 3bb3277f3ae37e705c131e029c6fa6a4865e70cc Mon Sep 17 00:00:00 2001 From: "rem..om" Date: Sat, 6 Aug 2011 16:30:19 +0000 Subject: [PATCH] sdk : - Material live update in platform core git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7984 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../sceneexplorer/MaterialChangeListener.java | 18 ++++ .../sceneexplorer/MaterialChangeProvider.java | 25 +++++ .../SceneExplorerTopComponent.java | 92 ++++++++++++++++++- .../core/sceneexplorer/nodes/JmeGeometry.java | 84 ++++++++++++++++- .../nodes/JmeParticleEmitter.java | 1 + .../sceneviewer/SceneViewerTopComponent.java | 13 ++- 6 files changed, 223 insertions(+), 10 deletions(-) create mode 100644 sdk/jme3-core/src/com/jme3/gde/core/sceneexplorer/MaterialChangeListener.java create mode 100644 sdk/jme3-core/src/com/jme3/gde/core/sceneexplorer/MaterialChangeProvider.java diff --git a/sdk/jme3-core/src/com/jme3/gde/core/sceneexplorer/MaterialChangeListener.java b/sdk/jme3-core/src/com/jme3/gde/core/sceneexplorer/MaterialChangeListener.java new file mode 100644 index 000000000..461392f13 --- /dev/null +++ b/sdk/jme3-core/src/com/jme3/gde/core/sceneexplorer/MaterialChangeListener.java @@ -0,0 +1,18 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.jme3.gde.core.sceneexplorer; + +import com.jme3.material.Material; + +/** + * + * @author Nehon + */ +public interface MaterialChangeListener { + + public String getKey(); + + public void setMaterial(Material material); +} diff --git a/sdk/jme3-core/src/com/jme3/gde/core/sceneexplorer/MaterialChangeProvider.java b/sdk/jme3-core/src/com/jme3/gde/core/sceneexplorer/MaterialChangeProvider.java new file mode 100644 index 000000000..ab1aaf871 --- /dev/null +++ b/sdk/jme3-core/src/com/jme3/gde/core/sceneexplorer/MaterialChangeProvider.java @@ -0,0 +1,25 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.jme3.gde.core.sceneexplorer; + +import com.jme3.gde.core.sceneexplorer.MaterialChangeListener; +import java.util.List; + +/** + * + * @author Nehon + */ +public interface MaterialChangeProvider { + + public String getKey(); + + public void addMaterialChangeListener(MaterialChangeListener listener); + + public void removeMaterialChangeListener(MaterialChangeListener listener); + + public void addAllMaterialChangeListener(List listeners); + + public void clearMaterialChangeListeners(); +} diff --git a/sdk/jme3-core/src/com/jme3/gde/core/sceneexplorer/SceneExplorerTopComponent.java b/sdk/jme3-core/src/com/jme3/gde/core/sceneexplorer/SceneExplorerTopComponent.java index afb1613d9..64f42144d 100644 --- a/sdk/jme3-core/src/com/jme3/gde/core/sceneexplorer/SceneExplorerTopComponent.java +++ b/sdk/jme3-core/src/com/jme3/gde/core/sceneexplorer/SceneExplorerTopComponent.java @@ -37,8 +37,13 @@ import com.jme3.gde.core.scene.SceneListener; import com.jme3.gde.core.scene.SceneRequest; import com.jme3.gde.core.sceneexplorer.nodes.AbstractSceneExplorerNode; import com.jme3.gde.core.sceneexplorer.nodes.JmeNode; +import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.logging.Level; import java.util.logging.Logger; import org.openide.util.Lookup.Result; import org.openide.util.NbBundle; @@ -77,6 +82,8 @@ public final class SceneExplorerTopComponent extends TopComponent implements Exp private final Result nodeSelectionResult; private AbstractSceneExplorerNode selectedSpatial; private AbstractSceneExplorerNode lastSelected; + private Map materialChangeProviders = new HashMap(); + private Map> materialChangeListeners = new HashMap>(); public SceneExplorerTopComponent() { initComponents(); @@ -95,7 +102,7 @@ public final class SceneExplorerTopComponent extends TopComponent implements Exp CopyAction copy = SystemAction.get(CopyAction.class); getActionMap().put(copy.getActionMapKey(), ExplorerUtils.actionCopy(explorerManager)); PasteAction paste = SystemAction.get(PasteAction.class); - getActionMap().put(paste.getActionMapKey(), ExplorerUtils.actionPaste(explorerManager)); + getActionMap().put(paste.getActionMapKey(), ExplorerUtils.actionPaste(explorerManager)); DeleteAction delete = SystemAction.get(DeleteAction.class); getActionMap().put(delete.getActionMapKey(), ExplorerUtils.actionDelete(explorerManager, true)); } @@ -238,7 +245,6 @@ public final class SceneExplorerTopComponent extends TopComponent implements Exp public UndoRedo getUndoRedo() { return Lookup.getDefault().lookup(UndoRedo.class); } - private transient ExplorerManager explorerManager = new ExplorerManager(); public ExplorerManager getExplorerManager() { @@ -261,6 +267,10 @@ public final class SceneExplorerTopComponent extends TopComponent implements Exp public void sceneRequested(SceneRequest request) { this.request = request; JmeNode node = request.getJmeNode(); + for (Iterator it = materialChangeProviders.values().iterator(); it.hasNext();) { + MaterialChangeProvider provider = (MaterialChangeProvider) it.next(); + provider.clearMaterialChangeListeners(); + } if (node != null) { explorerManager.setRootContext(node); explorerManager.getRootContext().setDisplayName(node.getName()); @@ -282,4 +292,82 @@ public final class SceneExplorerTopComponent extends TopComponent implements Exp public AbstractSceneExplorerNode getLastSelected() { return lastSelected; } + + public void addMaterialChangeProvider(MaterialChangeProvider provider) { + Logger.getLogger(SceneExplorerTopComponent.class.getName()).log(Level.INFO, "New materail provider registered for: {0}", provider.getKey()); + materialChangeProviders.put(provider.getKey(), provider); + List listeners = materialChangeListeners.get(provider.getKey()); + if (listeners == null) { + return; + } + provider.addAllMaterialChangeListener(listeners); + } + + public void removeMaterialChangeProvider(MaterialChangeProvider provider) { + Logger.getLogger(SceneExplorerTopComponent.class.getName()).log(Level.INFO, "Removing material provider for : {0}", provider.getKey()); + System.out.println("Removing provider : " + provider.getKey()); + materialChangeProviders.remove(provider.getKey()); + } + + public void addMaterialChangeListener(MaterialChangeListener listener) { + + if (listener.getKey() != null) { + Logger.getLogger(SceneExplorerTopComponent.class.getName()).log(Level.INFO, "New material listener for : {0}", listener.getKey()); + List listeners = materialChangeListeners.get(listener.getKey()); + if (listeners == null) { + listeners = new ArrayList(); + materialChangeListeners.put(listener.getKey(), listeners); + } + listeners.add(listener); + + MaterialChangeProvider provider = materialChangeProviders.get(listener.getKey()); + if (provider != null) { + provider.addMaterialChangeListener(listener); + } + } + } + + public void removeMaterialChangeListener(MaterialChangeListener listener) { + Logger.getLogger(SceneExplorerTopComponent.class.getName()).log(Level.INFO, "Removing material listener for: {0}", listener.getKey()); + List listeners = materialChangeListeners.get(listener.getKey()); + if (listeners == null) { + return; + } + listeners.remove(listener); + + MaterialChangeProvider provider = materialChangeProviders.get(listener.getKey()); + if (provider != null) { + provider.removeMaterialChangeListener(listener); + } + } + + public void swapMaterialChangeListener(MaterialChangeListener listener, String oldKey, String newKey) { + Logger.getLogger(SceneExplorerTopComponent.class.getName()).log(Level.INFO, "Swaping material listeners : {0} -> {1}", new Object[]{oldKey, newKey}); + if (oldKey != null) { + List listeners = materialChangeListeners.get(oldKey); + if (listeners != null) { + listeners.remove(listener); + } + + MaterialChangeProvider provider = materialChangeProviders.get(oldKey); + if (provider != null) { + provider.removeMaterialChangeListener(listener); + } + } + + if (newKey != null) { + // assert newKey.equals(listener.getKey()); + List listeners = materialChangeListeners.get(newKey); + if (listeners == null) { + listeners = new ArrayList(); + materialChangeListeners.put(newKey, listeners); + } + listeners.add(listener); + + MaterialChangeProvider provider = materialChangeProviders.get(newKey); + if (provider != null) { + provider.addMaterialChangeListener(listener); + } + } + } } diff --git a/sdk/jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/JmeGeometry.java b/sdk/jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/JmeGeometry.java index 6429b068c..a022ed7a0 100644 --- a/sdk/jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/JmeGeometry.java +++ b/sdk/jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/JmeGeometry.java @@ -31,11 +31,16 @@ */ package com.jme3.gde.core.sceneexplorer.nodes; +import com.jme3.gde.core.scene.SceneApplication; +import com.jme3.gde.core.sceneexplorer.SceneExplorerTopComponent; import com.jme3.gde.core.sceneexplorer.nodes.SceneExplorerNode; +import com.jme3.gde.core.sceneexplorer.MaterialChangeListener; import com.jme3.material.Material; import com.jme3.scene.Geometry; import com.jme3.scene.Mesh; import java.awt.Image; +import java.io.IOException; +import java.util.concurrent.Callable; import org.openide.loaders.DataObject; import org.openide.nodes.Sheet; import org.openide.util.ImageUtilities; @@ -44,8 +49,8 @@ import org.openide.util.ImageUtilities; * * @author normenhansen */ -@org.openide.util.lookup.ServiceProvider(service=SceneExplorerNode.class) -public class JmeGeometry extends JmeSpatial { +@org.openide.util.lookup.ServiceProvider(service = SceneExplorerNode.class) +public class JmeGeometry extends JmeSpatial implements MaterialChangeListener { private static Image smallImage = ImageUtilities.loadImage("com/jme3/gde/core/sceneexplorer/nodes/icons/geometry.gif"); @@ -58,6 +63,18 @@ public class JmeGeometry extends JmeSpatial { super(spatial, children); getLookupContents().add(spatial); this.geom = spatial; + java.awt.EventQueue.invokeLater(new Runnable() { + + public void run() { + try { + SceneExplorerTopComponent.findInstance().addMaterialChangeListener(JmeGeometry.this); + + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + setName(spatial.getName()); } @@ -71,6 +88,23 @@ public class JmeGeometry extends JmeSpatial { return smallImage; } + @Override + public void destroy() throws IOException { + super.destroy(); + java.awt.EventQueue.invokeLater(new Runnable() { + + public void run() { + try { + SceneExplorerTopComponent.findInstance().removeMaterialChangeListener(JmeGeometry.this); + + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + + } + @Override protected Sheet createSheet() { //TODO: multithreading.. @@ -92,18 +126,62 @@ public class JmeGeometry extends JmeSpatial { } + @Override public Class getExplorerObjectClass() { return Geometry.class; } + @Override public Class getExplorerNodeClass() { return JmeGeometry.class; } + @Override public org.openide.nodes.Node[] createNodes(Object key, DataObject key2, boolean cookie) { - JmeSpatialChildren children=new JmeSpatialChildren((com.jme3.scene.Spatial)key); + JmeSpatialChildren children = new JmeSpatialChildren((com.jme3.scene.Spatial) key); children.setReadOnly(cookie); children.setDataObject(key2); return new org.openide.nodes.Node[]{new JmeGeometry((Geometry) key, children).setReadOnly(cookie)}; } + + @Override + public void propertyChange(String name, final Object before, final Object after) { + super.propertyChange(name, before, after); + System.out.println(name); + if (name.equals("Material")) { + System.out.println(before.toString()+" "+after.toString()); + java.awt.EventQueue.invokeLater(new Runnable() { + + public void run() { + try { + SceneExplorerTopComponent.findInstance().swapMaterialChangeListener(JmeGeometry.this, ((Material) before).getAssetName(), ((Material) after).getAssetName()); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + + + } + } + + public void setMaterial(final Material material) { + if (material.getAssetName().equals(getKey())) { + SceneApplication.getApplication().enqueue(new Callable() { + + public Object call() throws Exception { + geom.setMaterial(material); + return null; + } + }); + } + } + + public Geometry getGeometry() { + return geom; + } + + public String getKey() { + return geom.getMaterial().getAssetName(); + } } diff --git a/sdk/jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/JmeParticleEmitter.java b/sdk/jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/JmeParticleEmitter.java index 84135d737..98c1f61c3 100644 --- a/sdk/jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/JmeParticleEmitter.java +++ b/sdk/jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/JmeParticleEmitter.java @@ -119,6 +119,7 @@ public class JmeParticleEmitter extends JmeGeometry { @Override public void propertyChange(String name, Object before, Object after) { + super.propertyChange(name, before, after); if (!name.equals("Emit all particles")) { fireSave(true); firePropertyChange(name, before, after); diff --git a/sdk/jme3-core/src/com/jme3/gde/core/sceneviewer/SceneViewerTopComponent.java b/sdk/jme3-core/src/com/jme3/gde/core/sceneviewer/SceneViewerTopComponent.java index 3556e791f..4bec7e891 100644 --- a/sdk/jme3-core/src/com/jme3/gde/core/sceneviewer/SceneViewerTopComponent.java +++ b/sdk/jme3-core/src/com/jme3/gde/core/sceneviewer/SceneViewerTopComponent.java @@ -102,22 +102,25 @@ public final class SceneViewerTopComponent extends TopComponent { }); addKeyListener(new KeyListener() { - public void keyTyped(KeyEvent evt) { + public void keyTyped(KeyEvent evt) { } public void keyPressed(KeyEvent evt) { int code = AwtKeyInput.convertAwtKey(evt.getKeyCode()); KeyInputEvent keyEvent = new KeyInputEvent(code, evt.getKeyChar(), true, false); - keyEvent.setTime(evt.getWhen()); - app.getActiveCameraController().onKeyEvent(keyEvent); + keyEvent.setTime(evt.getWhen()); + if (app.getActiveCameraController() != null) { + app.getActiveCameraController().onKeyEvent(keyEvent); + } } public void keyReleased(KeyEvent evt) { int code = AwtKeyInput.convertAwtKey(evt.getKeyCode()); KeyInputEvent keyEvent = new KeyInputEvent(code, evt.getKeyChar(), false, false); keyEvent.setTime(evt.getWhen()); - - app.getActiveCameraController().onKeyEvent(keyEvent); + if (app.getActiveCameraController() != null) { + app.getActiveCameraController().onKeyEvent(keyEvent); + } } });