- Clean up AssetDataObject / SpatialAssetDataObject / AssetData, add javadoc - Add AssetDataPropertyChangeListener interface and functionality - Improve storage of ORIGINAL_XXX data - Add SpatialUtil git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10303 75d07b2b-3a1a-0410-a2c5-0572b91ccdca3.0
parent
9a9e9a8d5a
commit
940e5315cf
@ -0,0 +1,43 @@ |
||||
/* |
||||
* Copyright (c) 2003-2012 jMonkeyEngine |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* * Redistributions 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 'jMonkeyEngine' nor the names of its contributors |
||||
* may be used to endorse or promote products derived from this software |
||||
* without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
package com.jme3.gde.core.assets; |
||||
|
||||
/** |
||||
* Allows listening for property changes in AssetData objects. The call is not |
||||
* necessarily coming from the main AWT thread but from the import thread. |
||||
* |
||||
* @author normenhansen |
||||
*/ |
||||
public interface AssetDataPropertyChangeListener { |
||||
|
||||
public void assetDataPropertyChanged(String property, String before, String after); |
||||
} |
@ -0,0 +1,216 @@ |
||||
/* |
||||
* Copyright (c) 2003-2012 jMonkeyEngine |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* * Redistributions 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 'jMonkeyEngine' nor the names of its contributors |
||||
* may be used to endorse or promote products derived from this software |
||||
* without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
package com.jme3.gde.core.util; |
||||
|
||||
import com.jme3.gde.core.scene.ApplicationLogHandler.LogLevel; |
||||
import com.jme3.scene.Geometry; |
||||
import com.jme3.scene.SceneGraphVisitor; |
||||
import com.jme3.scene.SceneGraphVisitorAdapter; |
||||
import com.jme3.scene.Spatial; |
||||
import java.util.ArrayList; |
||||
import java.util.logging.Level; |
||||
import java.util.logging.Logger; |
||||
|
||||
/** |
||||
* Various utilities, mostly for operating on Spatials recursively. |
||||
* |
||||
* @author normenhansen |
||||
*/ |
||||
public class SpatialUtil { |
||||
|
||||
private static final Logger logger = Logger.getLogger(SpatialUtil.class.getName()); |
||||
//TODO: use these variables
|
||||
public static final String ORIGINAL_NAME = "ORIGINAL_NAME"; |
||||
public static final String ORIGINAL_PATH = "ORIGINAL_PATH"; |
||||
|
||||
/** |
||||
* Gets a "pathname" for the given Spatial, combines the Spatials and |
||||
* parents names to make a long name. This "path" is stored in geometry |
||||
* after the first import for example. |
||||
* |
||||
* @param spat |
||||
* @return |
||||
*/ |
||||
public static String getSpatialPath(Spatial spat) { |
||||
StringBuilder geometryIdentifier = new StringBuilder(); |
||||
while (spat != null) { |
||||
String name = spat.getName(); |
||||
if (name == null) { |
||||
logger.log(Level.WARNING, "Null spatial name!"); |
||||
name = "null"; |
||||
} |
||||
geometryIdentifier.insert(0, name); |
||||
geometryIdentifier.insert(0, '/'); |
||||
spat = spat.getParent(); |
||||
} |
||||
String id = geometryIdentifier.toString(); |
||||
return id; |
||||
} |
||||
|
||||
/** |
||||
* Stores ORIGINAL_NAME and ORIGINAL_PATH UserData to all Geometry in loaded |
||||
* spatial |
||||
* |
||||
* @param spat |
||||
*/ |
||||
public static void storeOriginalPathUserData(Spatial spat) { |
||||
//TODO: only stores for geometry atm
|
||||
final ArrayList<String> geomMap = new ArrayList<String>(); |
||||
if (spat != null) { |
||||
spat.depthFirstTraversal(new SceneGraphVisitorAdapter() { |
||||
@Override |
||||
public void visit(Geometry geom) { |
||||
Spatial curSpat = geom; |
||||
String geomName = curSpat.getName(); |
||||
if (geomName == null) { |
||||
logger.log(Level.WARNING, "Null geometry name!"); |
||||
geomName = "null"; |
||||
} |
||||
geom.setUserData("ORIGINAL_NAME", geomName); |
||||
logger.log(Level.FINE, "Set ORIGINAL_NAME for {0}", geomName); |
||||
String id = SpatialUtil.getSpatialPath(curSpat); |
||||
if (geomMap.contains(id)) { |
||||
logger.log(Level.WARNING, "Cannot create unique name for Geometry {0}: {1}", new Object[]{geom, id}); |
||||
} |
||||
geomMap.add(id); |
||||
geom.setUserData("ORIGINAL_PATH", id); |
||||
logger.log(Level.FINE, "Set ORIGINAL_PATH for {0}", id); |
||||
super.visit(geom); |
||||
} |
||||
}); |
||||
} else { |
||||
logger.log(Level.SEVERE, "No geometry available when trying to scan initial geometry configuration"); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Finds a previously marked spatial in the supplied root Spatial, creates |
||||
* the name and path to be looked for from the given needle Spatial. |
||||
* |
||||
* @param root |
||||
* @param needle |
||||
* @return |
||||
*/ |
||||
public static Spatial findTaggedSpatial(final Spatial root, final Spatial needle) { |
||||
if (needle == null) { |
||||
logger.log(Level.WARNING, "Trying to find null needle for {0}", root); |
||||
return null; |
||||
} |
||||
final String name = needle.getName(); |
||||
final String path = getSpatialPath(needle); |
||||
if (name == null || path == null) { |
||||
logger.log(Level.INFO, "Trying to find tagged spatial with null name spatial for {0}.", root); |
||||
} |
||||
final Class clazz = needle.getClass(); |
||||
String rootName = root.getUserData("ORIGINAL_NAME"); |
||||
String rootPath = root.getUserData("ORIGINAL_PATH"); |
||||
if (name.equals(rootName) && path.equals(rootPath)) { |
||||
return root; |
||||
} |
||||
final SpatialHolder holder = new SpatialHolder(); |
||||
root.depthFirstTraversal(new SceneGraphVisitor() { |
||||
public void visit(Spatial spatial) { |
||||
String spName = spatial.getUserData("ORIGINAL_NAME"); |
||||
String spPath = spatial.getUserData("ORIGINAL_PATH"); |
||||
if (name.equals(spName) && path.equals(spPath) && clazz.isInstance(spatial)) { |
||||
if (holder.spatial == null) { |
||||
holder.spatial = spatial; |
||||
} else { |
||||
logger.log(Level.WARNING, "Found spatial {0} twice in {1}", new Object[]{path, root}); |
||||
} |
||||
} |
||||
} |
||||
}); |
||||
return holder.spatial; |
||||
} |
||||
|
||||
/** |
||||
* Finds a spatial in the given Spatial tree with the specified name and |
||||
* path, the path and name are constructed from the given (sub-)spatial(s) |
||||
* and is not read from the UserData of the objects. This is mainly used to |
||||
* check if the original spatial still exists in the original file. |
||||
* |
||||
* @param root |
||||
* @param name |
||||
* @param path |
||||
*/ |
||||
public static Spatial findSpatial(final Spatial root, final String name, final String path) { |
||||
if (name.equals(root.getName()) && getSpatialPath(root).equals(path)) { |
||||
return root; |
||||
} |
||||
final SpatialHolder holder = new SpatialHolder(); |
||||
root.depthFirstTraversal(new SceneGraphVisitor() { |
||||
public void visit(Spatial spatial) { |
||||
if (name.equals(spatial.getName()) && getSpatialPath(spatial).equals(path)) { |
||||
if (holder.spatial == null) { |
||||
holder.spatial = spatial; |
||||
} else { |
||||
logger.log(Level.WARNING, "Found spatial {0} twice in {1}", new Object[]{path, root}); |
||||
} |
||||
} |
||||
} |
||||
}); |
||||
return holder.spatial; |
||||
} |
||||
|
||||
public static void updateOriginalMeshData(final Spatial root, final Spatial original) { |
||||
original.depthFirstTraversal(new SceneGraphVisitorAdapter() { |
||||
@Override |
||||
public void visit(Geometry geom) { |
||||
//will always return same class type, so casting is safe
|
||||
Geometry spat = (Geometry) findTaggedSpatial(root, geom); |
||||
if (spat != null) { |
||||
spat.setMesh(geom.getMesh().deepClone()); |
||||
logger.log(LogLevel.USERINFO, "Updated mesh for geometry {0}", geom.getName()); |
||||
} else { |
||||
// addNewOriginal()
|
||||
} |
||||
} |
||||
}); |
||||
return; |
||||
} |
||||
|
||||
private void addNewOriginalGeometry(final Spatial root, final Geometry original) { |
||||
|
||||
return; |
||||
} |
||||
|
||||
public void clearRemovedOriginals(final Spatial root, final Spatial original) { |
||||
|
||||
return; |
||||
} |
||||
|
||||
private static class SpatialHolder { |
||||
|
||||
Spatial spatial; |
||||
} |
||||
} |
Loading…
Reference in new issue