package com.graphicsengine.scene;

import com.graphicsengine.map.PlayfieldNode;
import com.nucleus.camera.ViewFrustum;
import com.nucleus.component.ComponentException;
import com.nucleus.geometry.MeshFactory;
import com.nucleus.renderer.NucleusRenderer;
import com.nucleus.scene.ComponentNode;
import com.nucleus.scene.DefaultNodeFactory;
import com.nucleus.scene.Node;
import com.nucleus.scene.NodeException;
import com.nucleus.scene.NodeFactory;
import com.nucleus.scene.RootNode;

/**
 * Implementation of {@link NodeFactory}.
 * This factory shall handle all graphics-engine specific nodes.
 * 
 * @author Richard Sahlin
 *
 */
public class GraphicsEngineNodeFactory extends DefaultNodeFactory implements NodeFactory {

    private static final String NOT_IMPLEMENTED = "Not implemented: ";

    @Override
    public Node create(NucleusRenderer renderer, MeshFactory meshFactory, Node source,
            RootNode root) throws NodeException {
        if (source.getType() == null) {
            throw new NodeException("Type not set in source node - was it created programatically?");
        }
        GraphicsEngineNodeType type = null;
        try {
            type = GraphicsEngineNodeType.valueOf(source.getType());
        } catch (IllegalArgumentException e) {
            return super.create(renderer, meshFactory, source, root);
        }
        Node created = internalCreateNode(renderer, root, source, meshFactory);
        switch (type) {
        case playfieldNode:
            ((PlayfieldNode) created).createMap();
            break;
        case spriteComponentNode:
        case sharedMeshNode:
        case quadNode:
        case element:
            break;
        default:
            throw new IllegalArgumentException(NOT_IMPLEMENTED + type);
        }
        return created;
    }

    @Override
    protected Node internalCreateNode(NucleusRenderer renderer, RootNode root, Node source, MeshFactory meshFactory)
            throws NodeException {
        Node node = super.internalCreateNode(renderer, root, source, meshFactory);
        try {
            if (node instanceof ComponentNode) {
                internalCreateComponents(renderer, (ComponentNode) node, meshFactory);
            }
            return node;
        } catch (ComponentException e) {
            throw new NodeException(e);
        }
    }

    /**
     * Creates the components in the node, this could for instance mean creating the mesh that is
     * needed by component.
     * 
     * @param renderer
     * @param node
     * @param meshFactory
     * @throws ComponentException
     */
    protected void internalCreateComponents(NucleusRenderer renderer, ComponentNode node, MeshFactory meshFactory)
            throws ComponentException {
        node.createComponents(renderer);
    }

    /**
     * Checks if the node data has viewfrustum data, if it has it is set in the node.
     * 
     * @param source The source node containing the viewfrustum
     * @param node Node to check, or null
     */
    @Override
    protected void setViewFrustum(Node source, Node node) {
        if (node == null) {
            return;
        }
        ViewFrustum projection = source.getViewFrustum();
        if (projection == null) {
            return;
        }
        node.setViewFrustum(new ViewFrustum(projection));
    }

}

@@ -80,48 +80,8 @@
 e;%0D%0A
-import com.nucleus.camera.ViewFrustum;%0D%0A
 impo
@@ -118,32 +118,32 @@
 nentException;%0D%0A
+
 import com.nucle
@@ -1453,24 +1453,28 @@
  %7B%0D%0A        
+    
 case playfie
@@ -1474,32 +1474,36 @@
 playfieldNode:%0D%0A
+    
             ((Pl
@@ -1546,32 +1546,36 @@
             
+  
+  
 break;%0D%0A
         case
@@ -1554,32 +1554,36 @@
         break;%0D%0A
+    
         case spr
@@ -1597,24 +1597,28 @@
 onentNode:%0D%0A
+    
         case
@@ -1639,24 +1639,28 @@
 e:%0D%0A        
+    
 case quadNod
@@ -1667,24 +1667,28 @@
 e:%0D%0A        
+ 
+   
 case element
@@ -1698,32 +1698,36 @@
             
+  
+  
 break;%0D%0A
         defa
@@ -1706,32 +1706,36 @@
         break;%0D%0A
+    
         default:
@@ -1732,24 +1732,28 @@
   default:%0D%0A
+    
             
@@ -2818,32 +2818,32 @@
 entException %7B%0D%0A
+
         node.cre
@@ -2881,560 +2881,7 @@
 %0D%0A%0D%0A
-    /**%0D%0A     * Checks if the node data has viewfrustum data, if it has it is set in the node.%0D%0A     * %0D%0A     * @param source The source node containing the viewfrustum%0D%0A     * @param node Node to check, or null%0D%0A     */%0D%0A    @Override%0D%0A    protected void setViewFrustum(Node source, Node node) %7B%0D%0A        if (node == null) %7B%0D%0A            return;%0D%0A        %7D%0D%0A        ViewFrustum projection = source.getViewFrustum();%0D%0A        if (projection == null) %7B%0D%0A            return;%0D%0A        %7D%0D%0A        node.setViewFrustum(new ViewFrustum(projection));%0D%0A    %7D%0D%0A%0D%0A
 %7D%0D%0A

Remove method to set viewfrustum, handled in node