// Copyright (C) 1997-2010 Autodesk, Inc., and/or its licensors.
// All rights reserved.
//
// The coded instructions, statements, computer programs, and/or related
// material (collectively the "Data") in these files contain unpublished
// information proprietary to Autodesk, Inc. ("Autodesk") and/or its licensors,
// which is protected by U.S. and Canadian federal copyright law and by
// international treaties.
//
// The Data is provided for use exclusively by You. You have the right to use,
// modify, and incorporate this Data into other products for purposes authorized 
// by the Autodesk software license agreement, without fee.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND. AUTODESK
// DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTIES
// INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF NON-INFRINGEMENT,
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, OR ARISING FROM A COURSE 
// OF DEALING, USAGE, OR TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS
// LICENSORS BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL,
// DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK AND/OR ITS
// LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY OR PROBABILITY OF SUCH DAMAGES.

//
//  Procedure Name:
//	AEorientConstraintTemplate
//
//  Description Name;
//	Creates the attribute editor controls for the orientConstraint Node
//
//  Input Value:
//	nodeName 
//
//  Output Value:
//	None
//
global proc AEorientCheckInterpType(string $nodeName)
{
	int $interpType = `getAttr ($nodeName+".interpType")`;

	string $cacheExists[] = `listConnections ($nodeName+".interpCache")`;
	int $enableDeleteCache = (size($cacheExists) > 0);
	button -e -en $enableDeleteCache deleteCacheButton;

	// interpType is an enum, and interpType == 4 is "cache"
	//
	int $enableCreateCache = (($interpType == 4) && (size($cacheExists) == 0) ||
							  ($interpType == 0));
	button -e -en $enableCreateCache createCacheButton;	
}

global proc AEorientCacheReplace(string $plug)
{
	string $buff[];
	tokenize($plug,".",$buff);
	string $nodeName = $buff[0];

	float $rangeStart = `playbackOptions -q -min`;
	float $rangeEnd   = `playbackOptions -q -max`;

	button -e -c ("evalEcho \"orientConstraint -e -dc "+$nodeName+"\"; "+
				  "button -e -en false deleteCacheButton; " +
				  "button -e -en true createCacheButton") deleteCacheButton;

	button -e -c ("evalEcho \"orientConstraint -e -cc "+
				  $rangeStart+" "+$rangeEnd+" "+$nodeName+"\"; "+
				  "button -e -en true deleteCacheButton") createCacheButton;
}

global proc AEorientCacheNew(string $plug)
{
	setUITemplate -pst attributeEditorTemplate;
		
 		columnLayout cachectlColumn;
 		rowLayout -nc 5 deleteCacheLayout;
 		text -label (uiRes("m_AEorientConstraintTemplate.kCache")) ;
		button -label (uiRes("m_AEorientConstraintTemplate.kCreate"))  createCacheButton;
 		text -l "";
		button -label (uiRes("m_AEorientConstraintTemplate.kDelete"))  deleteCacheButton;		
 		text -l "";		
 		setParent ..;

	AEorientCacheReplace($plug);
	setUITemplate -ppt;
}

global proc AEenableRestOrientCallback( string $nodeName )
{
	int $enable = `getAttr ($nodeName+".enableRestPosition")`;
	editorTemplate -dimControl $nodeName "restRotate" (! $enable);
}

global proc AEorientConstraintTemplate ( string $nodeName )
{
	editorTemplate -beginScrollLayout;
		
		// include/call base class/node attributes
		AEtransformMain $nodeName;
	
		editorTemplate -beginLayout (uiRes("m_AEorientConstraintTemplate.kOrientConstraintAttributes"))  -collapse 0;

		    editorTemplate -addControl "lockOutput";
			editorTemplate -addControl "offset";
			editorTemplate -addControl "interpType" "AEorientCheckInterpType";
			editorTemplate -callCustom "AEorientCacheNew" "AEorientCacheReplace" "interpType";

			editorTemplate -addSeparator;
			editorTemplate -addControl "constraintRotate";
			editorTemplate -addControl "enableRestPosition" "AEenableRestOrientCallback";			
			editorTemplate -addControl "restRotate";			
		editorTemplate -endLayout;

		ccAttributeDispatcher( $nodeName );

	//suppressed attributes
	editorTemplate -suppress "constraintParentInverseMatrix";
	editorTemplate -suppress "target";
	editorTemplate -suppress "constraintRotateOrder";
	editorTemplate -suppress "constraintJointOrient";
	editorTemplate -suppress "interpCache";
	editorTemplate -suppress "lastTargetRotate";

		// include/call base class/node attributes
		AEtransformNoScroll $nodeName;

	editorTemplate -addExtraControls;
	editorTemplate -endScrollLayout;
}
