Search in sources :

Example 11 with Savable

use of com.jme3.export.Savable in project jmonkeyengine by jMonkeyEngine.

the class DOMInputCapsule method readStringSavableMap.

public Map<String, ? extends Savable> readStringSavableMap(String name, Map<String, ? extends Savable> defVal) throws IOException {
    Map<String, Savable> ret = null;
    Element tempEl;
    if (name != null) {
        tempEl = findChildElement(currentElem, name);
    } else {
        tempEl = currentElem;
    }
    if (tempEl != null) {
        ret = new HashMap<String, Savable>();
        NodeList nodes = tempEl.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node n = nodes.item(i);
            if (n instanceof Element && n.getNodeName().equals("MapEntry")) {
                Element elem = (Element) n;
                currentElem = elem;
                String key = currentElem.getAttribute("key");
                Savable val = readSavable("Savable", null);
                ret.put(key, val);
            }
        }
    } else {
        return defVal;
    }
    currentElem = (Element) tempEl.getParentNode();
    return ret;
}
Also used : Savable(com.jme3.export.Savable)

Example 12 with Savable

use of com.jme3.export.Savable in project jmonkeyengine by jMonkeyEngine.

the class SavableSerializer method writeObject.

@Override
public void writeObject(ByteBuffer buffer, Object object) throws IOException {
    Savable s = (Savable) object;
    BufferOutputStream out = new BufferOutputStream(buffer);
    exporter.save(s, out);
    out.close();
}
Also used : Savable(com.jme3.export.Savable)

Example 13 with Savable

use of com.jme3.export.Savable in project jmonkeyengine by jMonkeyEngine.

the class SavableSerializer method readObject.

@Override
@SuppressWarnings("unchecked")
public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException {
    BufferInputStream in = new BufferInputStream(data);
    Savable s = importer.load(in);
    in.close();
    return (T) s;
}
Also used : Savable(com.jme3.export.Savable)

Example 14 with Savable

use of com.jme3.export.Savable in project jmonkeyengine by jMonkeyEngine.

the class Spatial method read.

public void read(JmeImporter im) throws IOException {
    InputCapsule ic = im.getCapsule(this);
    name = ic.readString("name", null);
    worldBound = (BoundingVolume) ic.readSavable("world_bound", null);
    cullHint = ic.readEnum("cull_mode", CullHint.class, CullHint.Inherit);
    batchHint = ic.readEnum("batch_hint", BatchHint.class, BatchHint.Inherit);
    queueBucket = ic.readEnum("queue", RenderQueue.Bucket.class, RenderQueue.Bucket.Inherit);
    shadowMode = ic.readEnum("shadow_mode", ShadowMode.class, ShadowMode.Inherit);
    localTransform = (Transform) ic.readSavable("transform", Transform.IDENTITY);
    localLights = (LightList) ic.readSavable("lights", null);
    localLights.setOwner(this);
    ArrayList<MatParamOverride> localOverridesList = ic.readSavableArrayList("overrides", null);
    if (localOverridesList == null) {
        localOverrides = new SafeArrayList<>(MatParamOverride.class);
    } else {
        localOverrides = new SafeArrayList(MatParamOverride.class, localOverridesList);
    }
    worldOverrides = new SafeArrayList<>(MatParamOverride.class);
    //changed for backward compatibility with j3o files generated before the AnimControl/SkeletonControl split
    //the AnimControl creates the SkeletonControl for old files and add it to the spatial.
    //The SkeletonControl must be the last in the stack so we add the list of all other control before it.
    //When backward compatibility won't be needed anymore this can be replaced by :
    //controls = ic.readSavableArrayList("controlsList", null));
    controls.addAll(0, ic.readSavableArrayList("controlsList", null));
    userData = (HashMap<String, Savable>) ic.readStringSavableMap("user_data", null);
}
Also used : Bucket(com.jme3.renderer.queue.RenderQueue.Bucket) SafeArrayList(com.jme3.util.SafeArrayList) ShadowMode(com.jme3.renderer.queue.RenderQueue.ShadowMode) MatParamOverride(com.jme3.material.MatParamOverride)

Example 15 with Savable

use of com.jme3.export.Savable in project jmonkeyengine by jMonkeyEngine.

the class TestSaveGame method simpleInitApp.

public void simpleInitApp() {
    //node that is used to store player data
    Node myPlayer = new Node();
    myPlayer.setName("PlayerNode");
    myPlayer.setUserData("name", "Mario");
    myPlayer.setUserData("health", 100.0f);
    myPlayer.setUserData("points", 0);
    //the actual model would be attached to this node
    Spatial model = (Spatial) assetManager.loadModel("Models/Oto/Oto.mesh.xml");
    myPlayer.attachChild(model);
    //before saving the game, the model should be detached so its not saved along with the node
    myPlayer.detachAllChildren();
    SaveGame.saveGame("mycompany/mygame", "savegame_001", myPlayer);
    //later the game is loaded again
    Node player = (Node) SaveGame.loadGame("mycompany/mygame", "savegame_001");
    player.attachChild(model);
    rootNode.attachChild(player);
    //and the data is available
    System.out.println("Name: " + player.getUserData("name"));
    System.out.println("Health: " + player.getUserData("health"));
    System.out.println("Points: " + player.getUserData("points"));
    AmbientLight al = new AmbientLight();
    rootNode.addLight(al);
//note you can also implement your own classes that implement the Savable interface.
}
Also used : Spatial(com.jme3.scene.Spatial) Node(com.jme3.scene.Node) AmbientLight(com.jme3.light.AmbientLight)

Aggregations

Savable (com.jme3.export.Savable)22 IOException (java.io.IOException)8 Element (org.w3c.dom.Element)6 MatParamOverride (com.jme3.material.MatParamOverride)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 InputCapsule (com.jme3.export.InputCapsule)1 BinaryExporter (com.jme3.export.binary.BinaryExporter)1 BinaryImporter (com.jme3.export.binary.BinaryImporter)1 AmbientLight (com.jme3.light.AmbientLight)1 Material (com.jme3.material.Material)1 Bucket (com.jme3.renderer.queue.RenderQueue.Bucket)1 ShadowMode (com.jme3.renderer.queue.RenderQueue.ShadowMode)1 Node (com.jme3.scene.Node)1 Spatial (com.jme3.scene.Spatial)1 Control (com.jme3.scene.control.Control)1 SafeArrayList (com.jme3.util.SafeArrayList)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1