PassNode and PassNodeState Savable patch

heres the patch for PassNode and PassNodeState to utilize Savable interface and support binary import and export functionalities.



Index: E:/Project Utility/Source Code/jME2.0/src/com/jme/scene/PassNode.java
===================================================================
--- E:/Project Utility/Source Code/jME2.0/src/com/jme/scene/PassNode.java   (revision 3891)
+++ E:/Project Utility/Source Code/jME2.0/src/com/jme/scene/PassNode.java   (working copy)
@@ -32,82 +32,112 @@
 
 package com.jme.scene;
 
+import java.io.IOException;
 import java.util.ArrayList;
 
 import com.jme.renderer.RenderContext;
 import com.jme.renderer.Renderer;
 import com.jme.system.DisplaySystem;
+import com.jme.util.export.InputCapsule;
+import com.jme.util.export.JMEExporter;
+import com.jme.util.export.JMEImporter;
+import com.jme.util.export.OutputCapsule;
 
-/** PassNode Creator: rikard.herlitz, 2007-maj-10 */
+/**
+ * PassNode Creator: rikard.herlitz, 2007-maj-10
+ * @version Modified date: 07-07-2008 12:30 EST
+ *          Yi Wang (Neakor) added in binary import export support.
+ */
 public class PassNode extends Node {
-    private static final long serialVersionUID = 1L;
+   private static final long serialVersionUID = 1L;
 
-    private ArrayList<PassNodeState> passNodeStates =
-            new ArrayList<PassNodeState>();
+   private ArrayList<PassNodeState> passNodeStates =
+      new ArrayList<PassNodeState>();
 
-    public PassNode(String name) {
-        super(name);
-    }
+   public PassNode(String name) {
+      super(name);
+   }
 
-    public PassNode() {
-        super();
-    }
+   public PassNode() {
+      super();
+   }
 
-    @Override
-    public void draw(Renderer r) {
-        if (children == null) {
-            return;
-        }
+   @Override
+   public void draw(Renderer r) {
+      if (children == null) {
+         return;
+      }
 
-        RenderContext context =
-                DisplaySystem.getDisplaySystem().getCurrentContext();
-        DisplaySystem.getDisplaySystem().getRenderer().getQueue().swapBuckets();
-        for (PassNodeState pass : passNodeStates) {
-            if (!pass.isEnabled()) {
-                continue;
-            }
+      RenderContext context =
+         DisplaySystem.getDisplaySystem().getCurrentContext();
+      DisplaySystem.getDisplaySystem().getRenderer().getQueue().swapBuckets();
+      for (PassNodeState pass : passNodeStates) {
+         if (!pass.isEnabled()) {
+            continue;
+         }
 
-            pass.applyPassNodeState(r, context);
+         pass.applyPassNodeState(r, context);
 
-            Spatial child;
-            for (int i = 0, cSize = children.size(); i < cSize; i++) {
-                child = children.get(i);
-                if (child != null) {
-                    child.onDraw(r);
-                }
-            }
-            r.renderQueue();
+         Spatial child;
+         for (int i = 0, cSize = children.size(); i < cSize; i++) {
+            child = children.get(i);
+            if (child != null) {
+               child.onDraw(r);
+            }
+         }
+         r.renderQueue();
 
-            pass.resetPassNodeStates(r, context);
-        }
-        DisplaySystem.getDisplaySystem().getRenderer().getQueue().swapBuckets();
-    }
+         pass.resetPassNodeStates(r, context);
+      }
+      DisplaySystem.getDisplaySystem().getRenderer().getQueue().swapBuckets();
+   }
 
-    public void addPass(PassNodeState toAdd) {
-        passNodeStates.add(toAdd);
-    }
+   public void addPass(PassNodeState toAdd) {
+      passNodeStates.add(toAdd);
+   }
 
-    public void insertPass(PassNodeState toAdd, int index) {
-        passNodeStates.add(index, toAdd);
-    }
+   public void insertPass(PassNodeState toAdd, int index) {
+      passNodeStates.add(index, toAdd);
+   }
 
-    public boolean containsPass(PassNodeState s) {
-        return passNodeStates.contains(s);
-    }
+   public boolean containsPass(PassNodeState s) {
+      return passNodeStates.contains(s);
+   }
 
-    public boolean removePass(PassNodeState toRemove) {
-        return passNodeStates.remove(toRemove);
-    }
+   public boolean removePass(PassNodeState toRemove) {
+      return passNodeStates.remove(toRemove);
+   }
 
-    public PassNodeState getPass(int index) {
-        return passNodeStates.get(index);
-    }
+   public PassNodeState getPass(int index) {
+      return passNodeStates.get(index);
+   }
 
-    public int nrPasses() {
-        return passNodeStates.size();
-    }
+   public int nrPasses() {
+      return passNodeStates.size();
+   }
 
-    public void clearAll() {
-        passNodeStates.clear();
-    }
+   public void clearAll() {
+      passNodeStates.clear();
+   }
+
+   @Override
+   @SuppressWarnings("unchecked")
+   public Class getClassTag() {
+      return PassNode.class;
+   }
+
+   @Override
+   public void write(JMEExporter e) throws IOException {
+      super.write(e);
+      OutputCapsule oc = e.getCapsule(this);
+      oc.writeSavableArrayList(this.passNodeStates, "PassNodeStates", null);
+   }
+
+   @Override
+   @SuppressWarnings("unchecked")
+   public void read(JMEImporter e) throws IOException {
+      super.read(e);
+      InputCapsule ic = e.getCapsule(this);
+      this.passNodeStates = ic.readSavableArrayList("PassNodeStates", null);
+   }
 }
Index: E:/Project Utility/Source Code/jME2.0/src/com/jme/scene/PassNodeState.java
===================================================================
--- E:/Project Utility/Source Code/jME2.0/src/com/jme/scene/PassNodeState.java   (revision 3891)
+++ E:/Project Utility/Source Code/jME2.0/src/com/jme/scene/PassNodeState.java   (working copy)
@@ -32,157 +32,215 @@
 
 package com.jme.scene;
 
+import java.io.IOException;
+import java.io.Serializable;
+
 import com.jme.renderer.RenderContext;
 import com.jme.renderer.Renderer;
 import com.jme.scene.state.RenderState;
+import com.jme.util.export.InputCapsule;
+import com.jme.util.export.JMEExporter;
+import com.jme.util.export.JMEImporter;
+import com.jme.util.export.OutputCapsule;
+import com.jme.util.export.Savable;
 
-/** PassNodeState Creator: rikard.herlitz, 2007-maj-10 */
-public class PassNodeState {
+/**
+ * PassNodeState Creator: rikard.herlitz, 2007-maj-10
+ * @version Modified date: 07-07-2008 12:30 EST
+ *          Yi Wang (Neakor) added in binary import export support.
+ */
+public class PassNodeState implements Serializable, Savable {
+   /**
+    * Serial version.
+    */
+   private static final long serialVersionUID = -1228020075840075748L;
 
-    /** if false, pass will not be updated or rendered. */
-    protected boolean enabled = true;
+   /** if false, pass will not be updated or rendered. */
+   protected boolean enabled;
 
-    /**
-     * offset params to use to differentiate multiple passes of the same scene
-     * in the zbuffer.
-     */
-    protected float zFactor;
-    protected float zOffset;
+   /**
+    * offset params to use to differentiate multiple passes of the same scene
+    * in the zbuffer.
+    */
+   protected float zFactor;
+   protected float zOffset;
 
-    /**
-     * RenderStates registered with this pass - if a given state is not null it
-     * overrides the corresponding state set during rendering.
-     */
-    protected RenderState[] passStates =
-            new RenderState[RenderState.RS_MAX_STATE];
+   /**
+    * RenderStates registered with this pass - if a given state is not null it
+    * overrides the corresponding state set during rendering.
+    */
+   protected RenderState[] passStates;
+   ;
 
-    /**
-     * a place to internally save previous states setup before rendering this
-     * pass
-     */
-    protected RenderState[] savedStates =
-            new RenderState[RenderState.RS_MAX_STATE];
+   /**
+    * a place to internally save previous states setup before rendering this
+    * pass
+    */
+   protected RenderState[] savedStates;
 
-    /**
-     * Applies all currently set renderstates and z offset parameters to the
-     * supplied context
-     *
-     * @param r
-     * @param context
-     */
-    public void applyPassNodeState(Renderer r, RenderContext context) {
-        applyPassStates(context);
-        r.setPolygonOffset(zFactor, zOffset);
-    }
+   /**
+    * Constructor of <code>PassNodeState</code>.
+    */
+   public PassNodeState() {
+      this.enabled = true;
+      this.passStates = new RenderState[RenderState.RS_MAX_STATE];
+      this.savedStates = new RenderState[RenderState.RS_MAX_STATE];
+   }
 
-    /**
-     * Resets currently set renderstates and z offset parameters on the supplied
-     * context
-     *
-     * @param r
-     * @param context
-     */
-    public void resetPassNodeStates(Renderer r, RenderContext context) {
-        r.clearPolygonOffset();
-        resetOldStates(context);
-    }
+   /**
+    * Applies all currently set renderstates and z offset parameters to the
+    * supplied context
+    *
+    * @param r
+    * @param context
+    */
+   public void applyPassNodeState(Renderer r, RenderContext context) {
+      applyPassStates(context);
+      r.setPolygonOffset(zFactor, zOffset);
+   }
 
-    /**
-     * Enforce a particular state. In other words, the given state will override
-     * any state of the same type set on a scene object. Remember to clear the
-     * state when done enforcing. Very useful for multipass techniques where
-     * multiple sets of states need to be applied to a scenegraph drawn multiple
-     * times.
-     *
-     * @param state state to enforce
-     */
-    public void setPassState(RenderState state) {
-        passStates[state.getType()] = state;
-    }
+   /**
+    * Resets currently set renderstates and z offset parameters on the supplied
+    * context
+    *
+    * @param r
+    * @param context
+    */
+   public void resetPassNodeStates(Renderer r, RenderContext context) {
+      r.clearPolygonOffset();
+      resetOldStates(context);
+   }
 
-    /**
-     * Clears an enforced render state index by setting it to null. This allows
-     * object specific states to be used.
-     *
-     * @param renderStateType The type of RenderState to clear enforcement on.
-     */
-    public void clearPassState(int renderStateType) {
-        passStates[renderStateType] = null;
-    }
+   /**
+    * Enforce a particular state. In other words, the given state will override
+    * any state of the same type set on a scene object. Remember to clear the
+    * state when done enforcing. Very useful for multipass techniques where
+    * multiple sets of states need to be applied to a scenegraph drawn multiple
+    * times.
+    *
+    * @param state state to enforce
+    */
+   public void setPassState(RenderState state) {
+      passStates[state.getType()] = state;
+   }
 
-    /**
-     * sets all enforced states to null.
-     *
-     * @see RenderContext#clearEnforcedState(int)
-     */
-    public void clearPassStates() {
-        for (int i = 0; i < passStates.length; i++) {
-            passStates[i] = null;
-        }
-    }
+   /**
+    * Clears an enforced render state index by setting it to null. This allows
+    * object specific states to be used.
+    *
+    * @param renderStateType The type of RenderState to clear enforcement on.
+    */
+   public void clearPassState(int renderStateType) {
+      passStates[renderStateType] = null;
+   }
 
-    /**
-     * Applies all currently set renderstates to the supplied context
-     *
-     * @param context
-     */
-    protected void applyPassStates(RenderContext context) {
-        for (int x = RenderState.RS_MAX_STATE; --x >= 0;) {
-            if (passStates[x] != null) {
-                savedStates[x] = context.enforcedStateList[x];
-                context.enforcedStateList[x] = passStates[x];
-            }
-        }
-    }
+   /**
+    * sets all enforced states to null.
+    *
+    * @see RenderContext#clearEnforcedState(int)
+    */
+   public void clearPassStates() {
+      for (int i = 0; i < passStates.length; i++) {
+         passStates[i] = null;
+      }
+   }
 
-    /**
-     * Resets all renderstates on the supplied context
-     *
-     * @param context
-     */
-    protected void resetOldStates(RenderContext context) {
-        for (int x = RenderState.RS_MAX_STATE; --x >= 0;) {
-            if (passStates[x] != null) {
-                context.enforcedStateList[x] = savedStates[x];
-            }
-        }
-    }
+   /**
+    * Applies all currently set renderstates to the supplied context
+    *
+    * @param context
+    */
+   protected void applyPassStates(RenderContext context) {
+      for (int x = RenderState.RS_MAX_STATE; --x >= 0;) {
+         if (passStates[x] != null) {
+            savedStates[x] = context.enforcedStateList[x];
+            context.enforcedStateList[x] = passStates[x];
+         }
+      }
+   }
 
-    /** @return Returns the enabled. */
-    public boolean isEnabled() {
-        return enabled;
-    }
+   /**
+    * Resets all renderstates on the supplied context
+    *
+    * @param context
+    */
+   protected void resetOldStates(RenderContext context) {
+      for (int x = RenderState.RS_MAX_STATE; --x >= 0;) {
+         if (passStates[x] != null) {
+            context.enforcedStateList[x] = savedStates[x];
+         }
+      }
+   }
 
-    /** @param enabled The enabled to set. */
-    public void setEnabled(boolean enabled) {
-        this.enabled = enabled;
-    }
+   /** @return Returns the enabled. */
+   public boolean isEnabled() {
+      return enabled;
+   }
 
-    /** @return Returns the zFactor. */
-    public float getZFactor() {
-        return zFactor;
-    }
+   /** @param enabled The enabled to set. */
+   public void setEnabled(boolean enabled) {
+      this.enabled = enabled;
+   }
 
-    /**
-     * Sets the polygon offset param - factor - for this Pass.
-     *
-     * @param factor The zFactor to set.
-     */
-    public void setZFactor(float factor) {
-        zFactor = factor;
-    }
+   /** @return Returns the zFactor. */
+   public float getZFactor() {
+      return zFactor;
+   }
 
-    /** @return Returns the zOffset. */
-    public float getZOffset() {
-        return zOffset;
-    }
+   /**
+    * Sets the polygon offset param - factor - for this Pass.
+    *
+    * @param factor The zFactor to set.
+    */
+   public void setZFactor(float factor) {
+      zFactor = factor;
+   }
 
-    /**
-     * Sets the polygon offset param - offset - for this Pass.
-     *
-     * @param offset The zOffset to set.
-     */
-    public void setZOffset(float offset) {
-        zOffset = offset;
-    }
+   /** @return Returns the zOffset. */
+   public float getZOffset() {
+      return zOffset;
+   }
+
+   /**
+    * Sets the polygon offset param - offset - for this Pass.
+    *
+    * @param offset The zOffset to set.
+    */
+   public void setZOffset(float offset) {
+      zOffset = offset;
+   }
+
+   @Override
+   @SuppressWarnings("unchecked")
+   public Class getClassTag() {
+      return PassNodeState.class;
+   }
+
+   @Override
+   public void write(JMEExporter e) throws IOException {
+      OutputCapsule oc = e.getCapsule(this);
+      oc.write(this.enabled, "Enabled", true);
+      oc.write(this.zFactor, "ZFactor", 0);
+      oc.write(this.zOffset, "ZOffset", 0);
+      oc.write(this.passStates, "PassStates", null);
+      oc.write(this.savedStates, "SavedStates", null);
+   }
+
+   @Override
+   public void read(JMEImporter e) throws IOException {
+      InputCapsule ic = e.getCapsule(this);
+      this.enabled = ic.readBoolean("Enabled", true);
+      this.zFactor = ic.readFloat("ZFactor", 0);
+      this.zOffset = ic.readFloat("ZOffset", 0);
+      Savable[] temp = ic.readSavableArray("PassStates", null);
+      this.passStates = new RenderState[temp.length];
+      for(int i = 0; i < temp.length; i++) {
+         this.passStates[i] = (RenderState)temp[i];
+      }
+      temp = ic.readSavableArray("SavedStates", null);
+      this.savedStates = new RenderState[temp.length];
+      for(int i = 0; i < temp.length; i++) {
+         this.savedStates[i] = (RenderState)temp[i];
+      }
+   }
 }

any news on this patch?

Gone and done the savable add by hand as the patch…  see svn.  Modified to follow same code and string formatting as other parts of the core.

renanse said:

Gone and done the savable add by hand as the patch...  see svn.  Modified to follow same code and string formatting as other parts of the core.


great thx~