import processing.core.PApplet;
// A Cell object

public class Cell 
{

  // A cell object knows about its location in the grid as well as its size with the variables x, y, w, h.
  float x, y, z;   // x,y,z location
  float w, h, d;   // width, height, depth
  int border;   // stroke used for each cell 255=white to 0=black
  // Colors specifications for the cell
  int colorR, colorG, colorB;
  int colorA;   // Opacity
  // Colors for mouse selection
  int rON, gON, bON, aON;
  PApplet parent;
  
  
  // Cell Constructor
  Cell(float tempX, float tempY, float tempZ, 
       float tempW, float tempH, float tempD,
       int tempStroke,
       int tempR, int tempG, int tempB,
       int tempAlpha,
       PApplet p
       ) 
  {
	parent = p;  
    // Spatial attributes
    x = tempX;
    y = tempY;
    z = tempZ;
    w = tempW;
    h = tempH;
    d = tempD;
    // Color attributes
    border     = tempStroke;
    colorR     = tempR;
    colorG     = tempG;
    colorB     = tempB;
    colorA     = tempAlpha;
    rON        = 255;
    gON        = 255;
    bON        = 255;
    aON        = 95;
  }
  
  boolean display2D(float gridMinX, float gridMinY, 
		            float gridMaxX, float gridMaxY) 
  {	  
	  boolean retVal = false;
	  parent.stroke(border);
	  if(checkover(gridMinX, gridMinY, gridMaxX, gridMaxY))
	  {
		  parent.fill(colorR, colorG, colorB, aON);
		  retVal = true;
	  }
	  else
	  {
		  parent.fill(colorR, colorG, colorB, colorA);
	  }
	  parent.rect(x,y,w,h);
	  return retVal;
  }
   
  boolean display3D(int curAlpha) 
  {	  
	  boolean retVal = true;
	  parent.stroke(border, curAlpha);
	  parent.fill(colorR, colorG, colorB, curAlpha);
	  parent.pushMatrix();
	  parent.translate(0,0,z);
	  parent.rect(x,y,w,h);
	  parent.popMatrix();
	  return retVal;
  }  
  
  boolean displayAnim(int curAlpha, int curDepth) 
  {	 
	  //int curDepth = (int)d;
	  boolean retVal = true;
	  parent.stroke(border, curAlpha);
	  parent.fill(colorR, colorG, colorB, curAlpha);
	  drawBox(x, y, 0, w, h, curDepth);
	  return retVal;
  }
  
  void drawBox(float curX, float curY, float curZ, 
		       float curW, float curH, float curD)
  {
	  parent.pushMatrix();
	  parent.translate(curX+curW/2 ,curY+curH/2, curZ+curD/2);
	  parent.box(curW, curH, curD);
	  parent.popMatrix();
  }
   
  void updateColor(int R, int G, int B, int A) 
  {
    colorR = R; 
    colorG = G;
    colorB = B; 
    colorA = A;
  }
  
  // Returns true if mouse is currently over the cell
  boolean checkover(float gridMinX, float gridMinY, float gridMaxX, float gridMaxY)
  { 
    // Mouse is over (current row OR column) AND (within grid limits) 
    if(((parent.mouseX > x && parent.mouseX < x+w) ||
        (parent.mouseY > y && parent.mouseY < y+h)
       ) &&
       ((parent.mouseX > gridMinX && parent.mouseX < gridMaxX) &&
        (parent.mouseY > gridMinY && parent.mouseY < gridMaxY)
       )
      ) 
    {
      return true;
    }
    return false;
  }
  
  // Returns true if mouse is currently over the cell
  boolean isSelected(float gridMinX, float gridMinY, float gridMaxX, float gridMaxY)
  { 
    // Mouse is over (current row AND column)
    if((parent.mouseX > x && parent.mouseX < x+w) &&
       (parent.mouseY > y && parent.mouseY < y+h)
      ) 
    {
      return true;
    }
    return false;
  }
}
