package com.graphicsengine.scene;

import com.google.gson.annotations.SerializedName;
import com.graphicsengine.spritemesh.SpriteMesh;
import com.nucleus.scene.Node;
import com.nucleus.scene.RootNode;
import com.nucleus.texturing.Texture2D;
import com.nucleus.texturing.TextureType;
import com.nucleus.vecmath.Rectangle;

/**
 * A Quad child that has to be appended to QuadNode in order to be rendered.
 * This node will share the mesh from the parent QuadNode.
 * 
 * @author Richard Sahlin
 *
 */
public class SharedMeshQuad extends Node {

    /**
     * The framenumber for this quad, from the texture in the referenced mesh.
     */
    private int frame;
    /**
     * The index of this shared mesh quad node with it's parent.
     */
    transient private int childIndex;
    /**
     * The rectangle defining the sprites, all sprites will have same size
     * 4 values = x1,y1 + width and height
     */
    @SerializedName(Rectangle.RECT)
    private Rectangle rectangle;

    /**
     * Used by GSON and {@link #createInstance(RootNode)} method - do NOT call directly
     */
    @Deprecated
    protected SharedMeshQuad() {
    }

    protected SharedMeshQuad(RootNode root) {
        super(root, GraphicsEngineNodeType.sharedMeshNode);
    }

    /**
     * Called when the parent node is created - remember that shared mesh quad does not
     * use it's own mesh
     * TODO Need to provide size and color from scene definition.
     * 
     * @param mesh The source mesh
     * @param index
     */
    public void onCreated(SpriteMesh mesh, int index) {
        this.childIndex = index;
        if (rectangle == null && mesh.getTexture(Texture2D.TEXTURE_0).getTextureType() == TextureType.Untextured) {
            // Must have size
            throw new IllegalArgumentException("Node does not define RECT and texture is untextured");
        }
        Rectangle quadRect = rectangle != null ? rectangle
                : mesh.getTexture(Texture2D.TEXTURE_0).calculateWindowRectangle();
        mesh.buildQuad(index, mesh.getMaterial().getProgram(), quadRect);
        initBounds(quadRect);
        if (transform == null) {
            mesh.setScale(index, 1, 1);
        } else {
            mesh.setTransform(index, transform);
        }
        mesh.setFrame(index, frame);
        if (mesh.getTexture(Texture2D.TEXTURE_0).textureType == TextureType.Untextured) {
            mesh.setColor(index, mesh.getMaterial().getAmbient());
        }
    }

    /**
     * Sets the index of this child with the parent, this is normally done by the parent.
     * 
     * @param index
     */
    public void setChildIndex(int index) {
        this.childIndex = index;
    }


    @Override
    public Node createInstance(RootNode root) {
        SharedMeshQuad copy = new SharedMeshQuad(root);
        copy.set(this);
        return copy;

    }

    /**
     * Sets the values from the source into this node
     * 
     * @param source
     */
    public void set(SharedMeshQuad source) {
        super.set(source);
        this.frame = source.frame;
        if (source.rectangle != null) {
            setQuadRectangle(source.rectangle);
        } else {
            rectangle = null;
        }
    }

    /**
     * Internal method, sets the rectangle defining each sprite
     * This will only set the size parameter, createMesh must be called to actually create the mesh
     * 
     * param rectangle values defining sprite, X1, Y1, width, height.
     */
    private void setQuadRectangle(Rectangle rectangle) {
        this.rectangle = new Rectangle(rectangle);
    }

}

@@ -1609,32 +1609,27 @@
     
-if (rectangle == null &&
+Texture2D texture =
  mes
@@ -1665,51 +1665,166 @@
 E_0)
-.getTextureType() == TextureType.Untextured
+;%0A        if (rectangle == null && (texture.getTextureType() == TextureType.Untextured %7C%7C %0A                texture.getWidth() == 0 %7C%7C texture.getHeight()== 0)
 ) %7B%0A
@@ -1952,16 +1952,32 @@
 textured
+ or size is zero
 %22);%0A    
@@ -2063,44 +2063,15 @@
   : 
-mesh.getTexture(Texture2D.TEXTURE_0)
+texture
 .cal

Check the quad node has texture to calculate size if needed