// __autor__ = Juan Carlos De Abreu R.
// __date__ = 03/13/2008
// __version__ = 1.0
//
// This file provide some functions to build the instruments
// and to manipulate it for the exercise "cutThreads".
//

include("Util.snf");

// Path for the each part of the instrument.
bodyInstrumentPath = "../Models/tijera.obj";
headInstrumentPath = "../Models/hojilla.obj";

// Load each geometry part of the instrument.
bodyInstrumentGeometry = Model(bodyInstrumentPath);
leftHeadInstrumentOpenGeometry = Model(headInstrumentPath);
rightHeadInstrumentOpenGeometry = Model(headInstrumentPath);
leftHeadInstrumentCloseGeometry = Model(headInstrumentPath);
rightHeadInstrumentCloseGeometry = Model(headInstrumentPath);

// Defs for instrument render types.
openInstrument = 1; closeInstrument = 2;

// Set the initial instrument render type.
displayInstrument(closeInstrument);
//displayInstrument(1);

// Actual instrument position and orientation.
instrumentPosition = (0, 2, 0);
instrumentOrientation = (100, -30, 180);
moveStep = 0.1;

// Function to create the instrument SceneNode.
instrumentNode = Group(
					translate(0, 2, 0),
					rotate(100, 1, 0, 0),
					rotate(-30, 0, 1, 0),
					rotate(180, 0, 0, 1),
					scale(2, 2, 2),
					
					Group(
					  translate(0, 0, -5.8),
					  scale(11,11,11),
					  bodyInstrumentGeometry
					),
					
					Group(
					  translate(0.012, 0.05, 0),
					  rightHeadInstrumentCloseGeometry
					),
					Group(
					  translate(-0.012, -0.05, 0),
					  rotate(180, 0, 0, 1),
					  leftHeadInstrumentCloseGeometry
					),
					
					Group(
						translate(0, 0.1, 0),
						rotate(-15, 1, 0, 0),
						translate(0.012, 0.05, 0),
						//translate(0.005, 0.05, 0),
						rightHeadInstrumentOpenGeometry
					),
					Group(
						translate(0, -0.1, 0),
						rotate(15, 1, 0, 0),
						translate(-0.012, -0.05, 0),
						//translate(-0.005, -0.05, 0),
						rotate(180, 0, 0, 1),
						leftHeadInstrumentOpenGeometry
					)
				);

// Function to set the instrument render type.
def displayInstrument(mode) {
  if (mode == openInstrument) {
	hide(leftHeadInstrumentCloseGeometry);
	hide(rightHeadInstrumentCloseGeometry);
	show(leftHeadInstrumentOpenGeometry);
	show(rightHeadInstrumentOpenGeometry);
  }
  else if (mode == closeInstrument) {
    hide(leftHeadInstrumentOpenGeometry);
	hide(rightHeadInstrumentOpenGeometry);
	show(leftHeadInstrumentCloseGeometry);
	show(rightHeadInstrumentCloseGeometry);
  }
}

// Function to update the instrument position and orientation.
def updateInstrument() {
  setXYZ(instrumentNode.get(0), instrumentPosition);
  instrumentNode.get(1).setAngle(instrumentOrientation.get(0));
  instrumentNode.get(2).setAngle(instrumentOrientation.get(1));
  instrumentNode.get(3).setAngle(instrumentOrientation.get(2));
}

// Set the keys for move the instrument.
setKeyFunc("I", moveInstrumentUp());
setKeyFunc("K", moveInstrumentDown());
setKeyFunc("J", moveInstrumentLeft());
setKeyFunc("L", moveInstrumentRight());
setKeyFunc("U", moveInstrumentFordward());
setKeyFunc("O", moveInstrumentBackward());


def moveInstrumentLeft() {
  instrumentPosition.set(0, instrumentPosition.get(0) - moveStep);
  updateInstrument();
}

def moveInstrumentRight() {
  instrumentPosition.set(0, instrumentPosition.get(0) + moveStep);
  updateInstrument();
}

def moveInstrumentFordward() {
  instrumentPosition.set(1, instrumentPosition.get(1) - moveStep);
  updateInstrument();
}

def moveInstrumentBackward() {
  instrumentPosition.set(1, instrumentPosition.get(1) + moveStep);
  updateInstrument();
}

def moveInstrumentUp() {
  instrumentPosition.set(2, instrumentPosition.get(2) - moveStep);
  updateInstrument();
}

def moveInstrumentDown() {
  instrumentPosition.set(2, instrumentPosition.get(2) + moveStep);
  updateInstrument();
}
