159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/* 259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Copyright (c) 2009-2010 jMonkeyEngine 359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * All rights reserved. 459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Redistribution and use in source and binary forms, with or without 659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * modification, are permitted provided that the following conditions are 759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * met: 859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions of source code must retain the above copyright 1059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * notice, this list of conditions and the following disclaimer. 1159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 1259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions in binary form must reproduce the above copyright 1359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * notice, this list of conditions and the following disclaimer in the 1459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * documentation and/or other materials provided with the distribution. 1559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 1659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 1759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * may be used to endorse or promote products derived from this software 1859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * without specific prior written permission. 1959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 2059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 2459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 2559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 2759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 2859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 2959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 3059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 3259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapackage com.jme3.scene; 3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.asset.AssetInfo; 3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.asset.AssetManager; 3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.asset.ModelKey; 3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.export.InputCapsule; 3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.export.JmeExporter; 4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.export.JmeImporter; 4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.export.OutputCapsule; 4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.export.binary.BinaryImporter; 4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.util.SafeArrayList; 4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.io.IOException; 4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.Map.Entry; 4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.*; 4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.logging.Level; 4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.logging.Logger; 4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/** 5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * The AssetLinkNode does not store its children when exported to file. 5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Instead, you can add a list of AssetKeys that will be loaded and attached 5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * when the AssetLinkNode is restored. 5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @author normenhansen 5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic class AssetLinkNode extends Node { 5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta protected ArrayList<ModelKey> assetLoaderKeys = new ArrayList<ModelKey>(); 6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta protected Map<ModelKey, Spatial> assetChildren = new HashMap<ModelKey, Spatial>(); 6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public AssetLinkNode() { 6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public AssetLinkNode(ModelKey key) { 6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta this(key.getName(), key); 6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public AssetLinkNode(String name, ModelKey key) { 7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta super(name); 7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta assetLoaderKeys.add(key); 7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Add a "linked" child. These are loaded from the assetManager when the 7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * AssetLinkNode is loaded from a binary file. 7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param key 7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void addLinkedChild(ModelKey key) { 8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (assetLoaderKeys.contains(key)) { 8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return; 8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta assetLoaderKeys.add(key); 8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void removeLinkedChild(ModelKey key) { 8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta assetLoaderKeys.remove(key); 8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public ArrayList<ModelKey> getAssetLoaderKeys() { 9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return assetLoaderKeys; 9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void attachLinkedChild(AssetManager manager, ModelKey key) { 9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta addLinkedChild(key); 9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Spatial child = manager.loadAsset(key); 9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta assetChildren.put(key, child); 9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta attachChild(child); 9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void attachLinkedChild(Spatial spat, ModelKey key) { 10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta addLinkedChild(key); 10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta assetChildren.put(key, spat); 10459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta attachChild(spat); 10559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 10659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 10759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void detachLinkedChild(ModelKey key) { 10859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Spatial spatial = assetChildren.get(key); 10959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (spatial != null) { 11059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta detachChild(spatial); 11159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 11259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta removeLinkedChild(key); 11359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta assetChildren.remove(key); 11459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 11559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 11659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void detachLinkedChild(Spatial child, ModelKey key) { 11759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta removeLinkedChild(key); 11859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta assetChildren.remove(key); 11959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta detachChild(child); 12059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 12159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 12259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 12359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Loads the linked children AssetKeys from the AssetManager and attaches them to the Node<br> 12459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * If they are already attached, they will be reloaded. 12559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param manager 12659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 12759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void attachLinkedChildren(AssetManager manager) { 12859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta detachLinkedChildren(); 12959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for (Iterator<ModelKey> it = assetLoaderKeys.iterator(); it.hasNext();) { 13059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta ModelKey assetKey = it.next(); 13159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Spatial curChild = assetChildren.get(assetKey); 13259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (curChild != null) { 13359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta curChild.removeFromParent(); 13459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 13559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Spatial child = manager.loadAsset(assetKey); 13659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta attachChild(child); 13759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta assetChildren.put(assetKey, child); 13859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 13959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 14059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 14159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void detachLinkedChildren() { 14259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Set<Entry<ModelKey, Spatial>> set = assetChildren.entrySet(); 14359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for (Iterator<Entry<ModelKey, Spatial>> it = set.iterator(); it.hasNext();) { 14459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Entry<ModelKey, Spatial> entry = it.next(); 14559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta entry.getValue().removeFromParent(); 14659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta it.remove(); 14759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 14859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 14959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 15059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta @Override 15159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void read(JmeImporter e) throws IOException { 15259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta super.read(e); 15359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta InputCapsule capsule = e.getCapsule(this); 15459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta BinaryImporter importer = BinaryImporter.getInstance(); 15559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta AssetManager loaderManager = e.getAssetManager(); 15659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 15759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta assetLoaderKeys = (ArrayList<ModelKey>) capsule.readSavableArrayList("assetLoaderKeyList", new ArrayList<ModelKey>()); 15859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for (Iterator<ModelKey> it = assetLoaderKeys.iterator(); it.hasNext();) { 15959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta ModelKey modelKey = it.next(); 16059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta AssetInfo info = loaderManager.locateAsset(modelKey); 16159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Spatial child = null; 16259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (info != null) { 16359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta child = (Spatial) importer.load(info); 16459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 16559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (child != null) { 16659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta child.parent = this; 16759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta children.add(child); 16859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta assetChildren.put(modelKey, child); 16959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } else { 17059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot locate {0} for asset link node {1}", 17159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta new Object[]{ modelKey, key }); 17259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 17359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 17459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 17559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 17659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta @Override 17759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void write(JmeExporter e) throws IOException { 17859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta SafeArrayList<Spatial> childs = children; 17959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta children = new SafeArrayList<Spatial>(Spatial.class); 18059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta super.write(e); 18159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta OutputCapsule capsule = e.getCapsule(this); 18259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta capsule.writeSavableArrayList(assetLoaderKeys, "assetLoaderKeyList", null); 18359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta children = childs; 18459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 18559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta} 186