package modeler;


import net.java.games.jogl.*;

import javax.vecmath.*;

public class TranslateManip extends Manip {

	public TranslateManip(Transformation t) {

	  this.t = t;
  }

  Vector3f tempVec = new Vector3f();
  public void dragged(Vector2f mousePosition, Vector2f mouseDelta) {
	Vector3f translation = new Vector3f();
	if (c instanceof OrthoCamera) {
		c.convertMotion(mouseDelta, translation);
	} else {
		// Perform a corrected computation for the perspective camera
		PerspectiveCamera pc = (PerspectiveCamera)c;
		t.toWorld(new Vector3f(0,0,0), tempVec);
		Vector3f toManip = new Vector3f(tempVec);
	    toManip.sub(pc.getEye());
	    float dist = toManip.length();
	    
	    // use the perpendicular plane height at the manipulator, not at the eye
	    float heightAtManip = (float)(Math.tan(Math.toRadians(pc.fovy/2.0)) * dist);
	    
	    translation.scale(-mouseDelta.x * pc.aspect * heightAtManip, pc.right);
	    translation.scaleAdd(-mouseDelta.y * heightAtManip, pc.up, translation);
	}
    
	switch (axisMode) {
	    case PICK_X:
	  	translation.z = translation.y = 0;
	      break;
	    case PICK_Y:
	  	translation.z = translation.x = 0;
	      break;
	    case PICK_Z:
	  	translation.y = translation.x = 0;
	      break;
	    case PICK_OTHER:
	  	// don't modify
	      break;
    }
    t.T.sub(translation);
  }

  Vector3f skewX = new Vector3f();
  Vector3f skewY = new Vector3f();
  Vector3f skewZ = new Vector3f();
  public void glRender(GLDrawable d, double scale) {

    GL gl = d.getGL();
    gl.glPushAttrib(GL.GL_COLOR);

    gl.glPushAttrib(GL.GL_LIGHTING);
    gl.glDisable(GL.GL_LIGHTING);

    gl.glPushAttrib(GL.GL_DEPTH_TEST);
    gl.glDisable(GL.GL_DEPTH_TEST);

    gl.glPushMatrix();
    glDoLocation(gl, t);

    gl.glScaled(scale, scale, scale);

    normalizedSkewedAxes(gl, skewX, skewY, skewZ);
    
    gl.glPushMatrix();
	glRotateTo(gl,skewX);
    gl.glColor4d(0.8, 0, 0, 1);
    gl.glLoadName(PICK_X);
    glRenderArrow(gl);
    gl.glPopMatrix();
    
    gl.glPushMatrix();
    glRotateTo(gl,skewY);
    gl.glColor4d(0, 0.8, 0, 1);
    gl.glLoadName(PICK_Y);
    glRenderArrow(gl);
    gl.glPopMatrix();
    
    gl.glPushMatrix();
    glRotateTo(gl,skewZ);
    gl.glColor4d(0, 0, 0.8, 1);
    gl.glLoadName(PICK_Z);
    glRenderArrow(gl);
    gl.glPopMatrix();    

    gl.glLoadName(PICK_OTHER);
    gl.glColor4d(0.8, 0.8, 0, 1);
    glRenderBox(gl);

    gl.glPopMatrix();

    gl.glPopAttrib();
    gl.glPopAttrib();
    gl.glPopAttrib();
  }

}
