// Copyright (C) 1997-2006 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:
//      AEcameraTemplate
//
//  Description:
//      Creates the attribute editor controls for a camera.
//
//  Input Arguments:
//      nodeName 
//
//  Return Value:
//      None.
//


proc float AEcalculateFOV( string $focalStr, string $horStr )
{
	float $focal = `getAttr $focalStr`;
	float $aperture = `getAttr $horStr`;

	float $fov = (0.5 * $aperture) / ($focal * 0.03937);
	$fov = 2.0 * atan ($fov);
	$fov = 57.29578 * $fov; 

    return $fov;
}

global proc AEfovNew( string $focalStr, string $horStr )
{
	setUITemplate -pst attributeEditorTemplate;
	floatSliderGrp -label (uiRes("m_AEcameraTemplate.kAngleofView")) 
        	-precision 2 
        	-min 1.0 -max 165.0
        	fovGrp;
	setUITemplate -ppt;

    AEfovReplace($focalStr, $horStr);
}

global proc AEfovReplace( string $focalStr, string $horStr )
{
	floatSliderGrp -e
        -cc ("AEadjustFocal " + $focalStr + " " + $horStr)
        -value (AEcalculateFOV($focalStr, $horStr))
        fovGrp;

    scriptJob -parent fovGrp -replacePrevious
        -attributeChange $focalStr ("AEadjustFOV "+$focalStr+" "+$horStr);

    scriptJob -parent fovGrp
        -attributeChange $horStr ("AEadjustFOV "+$focalStr+" "+$horStr);
}

global proc AEadjustFOV( string $focalStr, string $horStr )
{
    float $fov = AEcalculateFOV($focalStr, $horStr);
    floatSliderGrp -e -value $fov fovGrp;
}

global proc AEadjustFocal( string $focalStr, string $horStr )
{
	float $fov = `floatSliderGrp -q -value fovGrp`;
	float $aperture = `getAttr $horStr`;
	float $focal = tan (0.00872665 * $fov);
	$focal = (0.5 * $aperture) / ($focal * 0.03937);
	
	setAttr $focalStr $focal;
}

global proc AEcameraClipNew( string $clippingPlanesPlug )
{
    checkBoxGrp
        -label ""
        -label1 (uiRes("m_AEcameraTemplate.kAutoRenderClipPlane")) 
        -numberOfCheckBoxes 1
        clip;

    AEcameraClipReplace( $clippingPlanesPlug );
}

global proc AEcameraClipReplace( string $clippingPlanesPlug )
{
    int $value = `getAttr $clippingPlanesPlug`;

    checkBoxGrp -e
        -value1 $value
        -changeCommand ("setAttr "+$clippingPlanesPlug+" #1")
        clip;
}

//
//  Procedure Name:
//      AEcameraFilmAspectNew
//
//  Description:
//      Procedure to create the film aspect control. This procedure is
//      called when the film aspect control is first created.
//
global proc AEcameraFilmAspectNew( string $hfa, string $vfa )
{
	setUITemplate -pst attributeEditorTemplate;
	floatSliderGrp -label (uiRes("m_AEcameraTemplate.kFilmAspectRatio")) 
        	-precision 2 
        	-min 0.01 -max 10.0
        	filmAspect;
	setUITemplate -ppt;

    AEcameraFilmAspectReplace($hfa, $vfa);
}

//
//  Procedure Name:
//      AEcameraFilmAspectReplace
//
//  Description:
//      Procedure to update the film aspect control. This procedure is
//      called when a new camera is swapped into the attribute editor.
//
global proc AEcameraFilmAspectReplace( string $hfa, string $vfa )
{
    float $value = `getAttr $hfa` / `getAttr $vfa`;

	floatSliderGrp -e
        -cc ("setAttr "+$hfa+" ((`getAttr "+$vfa+"`)*#1)")
        -value $value
        filmAspect;
}


global proc checkOrthographic ( string $nodeName ) 
{
	string $nodeAttr = $nodeName + ".orthographic";
	int $value = `getAttr $nodeAttr`;
	
	if ( $value == 1 ) {
		editorTemplate -dimControl $nodeName "orthographicWidth" false;
	} else {
		editorTemplate -dimControl $nodeName "orthographicWidth" true;
	}
}

//
//  Procedure Name:
//      AEcameraFilmbackModified
//
//  Description:
//      The user has modified the film aperture or squeeze ratio, the
//      film aspect ratio and option menu are updated.
//
global proc AEcameraFilmbackModified( string $nodeName )
{
	float $hfa = `getAttr ($nodeName+".horizontalFilmAperture")`;
	float $vfa = `getAttr ($nodeName+".verticalFilmAperture")`;
    floatSliderGrp -e -value ($hfa / $vfa) filmAspect;

    AEresetFilmbackMenu( $nodeName );
}

//
//  Procedure Name:
//      AEdofModified
//
//	Description:
//		If dof turned off, turn off focus distance + fstop too.
//
global proc AEdofModified( string $nodeName )
{
	if( `getAttr ($nodeName + ".depthOfField")` == 0 ) {
		editorTemplate -dimControl $nodeName "focusDistance" true;
		editorTemplate -dimControl $nodeName "fStop" true;
        editorTemplate -dimControl $nodeName "focusRegionScale" true;
	} else {
		editorTemplate -dimControl $nodeName "focusDistance" false;
		editorTemplate -dimControl $nodeName "fStop" false;
        editorTemplate -dimControl $nodeName "focusRegionScale" false;
	}
}

//
//  Procedure Name:
//      AEdepthTypeModified
//
//	Description:
//		If depthType is closest visible depth (0), then turn on
//			transparencyBasedDepth, 
//			threshold,
//
global proc AEdepthTypeModified( string $nodeName )
{
		string $nodeAttr = $nodeName + ".depthType";
		int $value = `getAttr $nodeAttr`;
	
		if ( $value == 0 ) {
			//
			// closestVisibleDepth
			editorTemplate -dimControl $nodeName "transparencyBasedDepth" false;
			AEtransBasedDepthModified $nodeName;
		} else {
			editorTemplate -dimControl $nodeName "transparencyBasedDepth" true;
			editorTemplate -dimControl $nodeName "threshold" true;
		}
}

//
//  Procedure Name:
//      AEtransBasedDepthModified
//
//	Description:
//		If transparencyBasedDepth turned off, turn off 
//			threshold,
//
global proc AEtransBasedDepthModified( string $nodeName )
{
	if (`getAttr ($nodeName + ".transparencyBasedDepth")` == 0 ) {
		editorTemplate -dimControl $nodeName "threshold" true;
	} else {
		editorTemplate -dimControl $nodeName "threshold" false;
	}
}

global proc AEcameraNodeCountNew( string $plug ) 
//
// Description:
//	
//	
{
	string $buffer[];
	tokenize( $plug, ".", $buffer );
	string $camera = $buffer[0];

	optionMenuGrp -label (uiRes("m_AEcameraTemplate.kControls"))  aeCameraNodeCount;

	menuItem -label (uiRes("m_AEcameraTemplate.kCamera"))  cameraNodeCount1;
	menuItem -label (uiRes("m_AEcameraTemplate.kCameraandAim"))  cameraNodeCount2;
	menuItem -label (uiRes("m_AEcameraTemplate.kCameraAimandUp"))  ;

	AEcameraNodeCountReplace( $plug );
}

global proc AEcameraNodeCountReplace( string $plug )
//
// Description:
//	
//	
{
	string $buffer[];
	tokenize( $plug, ".", $buffer );
	string $camera = $buffer[0];

    string $lookAt = getCameraNode( "lookAt", $camera );
    string $up = getCameraNode( "up", $camera );

	optionMenuGrp -e -cc ("AEchangeCameraNodeCount " + $camera ) 
		aeCameraNodeCount;

	int $nodeCount = 1;
	if( $lookAt != "" ) $nodeCount++;
	if( $up != "" ) $nodeCount++;

	optionMenuGrp -e -sl $nodeCount aeCameraNodeCount;
}

global proc AEchangeCameraNodeCount( string $camera ) 
//
// Description:
//	
//	
{
	string $reselectThis = $camera;

	// cameraMakeNode doesn't work properly if you call it with
	// a camera shape and not the transform above the camera shape.
	//
	if( `nodeType $camera` == "camera" ) {
		string $parents[] = `listRelatives -parent $camera`;
		$camera = $parents[0];
	}

	cameraMakeNode `optionMenuGrp -q -sl aeCameraNodeCount` $camera;

	// cameraMakeNode destroys the current selection list, replacing
	// it with only the camera and its associated lookAt and up, if
	// applicable.
	// 
	// Since this make the attrEd refocus and redraw, we'll put
	// just the camera back on the selection list.
	// 
	select -r $reselectThis;
}

global proc AEcameraBackgroundColorNew( string $plug ) 
//
// Description:
//	Creates the control which represents the camera background color.
//	The control does not have a map button because mapping a texture to the
//	background color is not supported.
//	
{
	attrColorSliderGrp
		-label (uiRes("m_AEcameraTemplate.kBackgroundColor")) 
		-showButton false
		cameraBackgroundColorCtrl;

	AEcameraBackgroundColorReplace( $plug );
}

global proc AEcameraBackgroundColorReplace( string $plug )
//
// Description:
//	Hooks up the camera background color attribute to the control which
//	represents it.
//	
{
	attrColorSliderGrp
		-edit
		-attribute $plug
		cameraBackgroundColorCtrl;
}


global proc dimShake( string $nodeName )
// if the shakeEnabled attribute is off, then
// dim the controls for it.  If it is on, then 
// enable the controls for it.
{
	int $shakeEnabled = `getAttr( $nodeName + ".shakeEnabled")`;
	if(0 == $shakeEnabled)
	{
		editorTemplate -dimControl $nodeName "shake" 1;		
		//editorTemplate -dimControl $nodeName "horizontalShake" 1;
		//editorTemplate -dimControl $nodeName "verticalShake" 1;		
	}else{
		editorTemplate -dimControl $nodeName "shake" 0;		
		//editorTemplate -dimControl $nodeName "horizontalShake" 0;
		//editorTemplate -dimControl $nodeName "verticalShake" 0;		
	}
}

global proc dimShakeOverscan( string $nodeName )
// if the shakeOverscanEnabled attribute is off, then
// dim the controls for it.  If it is on, then 
// enable the controls for it.
{
	int $shakeOverscanEnabled = `getAttr( $nodeName + ".shakeOverscanEnabled")`;
	if(0 == $shakeOverscanEnabled)
	{
		editorTemplate -dimControl $nodeName "shakeOverscan" 1;
	}else{
		editorTemplate -dimControl $nodeName "shakeOverscan" 0;
	}
}

//
//  Procedure Name:
//      AEcameraTemplate
//
global proc AEcameraTemplate ( string $nodeName )
{
	AEswatchDisplay $nodeName;

	editorTemplate -beginScrollLayout;
	editorTemplate -beginLayout (uiRes("m_AEcameraTemplate.kCameraAttributes"))  -collapse 0;

		editorTemplate -callCustom 	AEcameraNodeCountNew
									AEcameraNodeCountReplace $nodeName;  

		editorTemplate -callCustom "AEfovNew" "AEfovReplace" 
					"focalLength" 
					"horizontalFilmAperture";
		editorTemplate -addControl "focalLength";
		editorTemplate -addControl "cameraScale";
		editorTemplate -callCustom "AEcameraClipNew" "AEcameraClipReplace" 
					"bestFitClippingPlanes";
		editorTemplate -suppress   "clippingPlanes";
		editorTemplate -addControl "nearClipPlane";
		editorTemplate -addControl "farClipPlane";
	editorTemplate -endLayout;

	editorTemplate -beginLayout (uiRes("m_AEcameraTemplate.kFilmBack"))  -collapse 0;
		editorTemplate -callCustom "AEcameraFilmbackNew" "AEcameraFilmbackReplace"
					"horizontalFilmAperture" 
					"verticalFilmAperture"
					"lensSqueezeRatio";
		editorTemplate -addControl "cameraAperture" "AEcameraFilmbackModified";
        editorTemplate -callCustom
            "AEcameraFilmAspectNew" "AEcameraFilmAspectReplace"
            "horizontalFilmAperture" "verticalFilmAperture";
		editorTemplate -addControl "lensSqueezeRatio" "AEcameraFilmbackModified";
		editorTemplate -addSeparator;

        editorTemplate -label (uiRes("m_AEcameraTemplate.kFitResolutionGate"))  -addControl "filmFit";
		editorTemplate -addControl "filmFitOffset";
		editorTemplate -addControl "filmOffset";

//		See bug 272228: camera shake attributes need to be suppressed for Maya 2008		
//
//		editorTemplate -addSeparator; 
//		
//		editorTemplate -addControl "shakeEnabled" "dimShake"; 
//		editorTemplate -addControl "shake"; 			
//		editorTemplate -addControl "shakeOverscanEnabled" "dimShakeOverscan"; 
//		editorTemplate -addControl "shakeOverscan";
		editorTemplate -suppress "shakeEnabled"; 
		editorTemplate -suppress "shake"; 			
		editorTemplate -suppress "shakeOverscanEnabled"; 
		editorTemplate -suppress "shakeOverscan";

		editorTemplate -suppress "stereoHorizontalImageTranslateEnabled";	
		editorTemplate -suppress "stereoHorizontalImageTranslate";

		editorTemplate -addSeparator; 

		editorTemplate -addControl "preScale";

		editorTemplate -addControl "filmTranslate";
				
		editorTemplate -addControl "filmRollPivot"; 
		editorTemplate -addControl "filmRollValue"; 
		editorTemplate -addControl "filmRollOrder"; 
		editorTemplate -suppress   "filmRollControl"; 

		editorTemplate -addControl "postScale"; 
		editorTemplate -suppress   "postProjection"; 

	editorTemplate -endLayout;

	AEmentalrayCamera $nodeName;

    editorTemplate -beginLayout (uiRes("m_AEcameraTemplate.kDepthofField")) ;
        editorTemplate -addControl "depthOfField" "AEdofModified";
        editorTemplate -addControl "focusDistance";
        editorTemplate -addControl "fStop";
        editorTemplate -addControl "focusRegionScale";
    editorTemplate -endLayout;

	editorTemplate -beginLayout (uiRes("m_AEcameraTemplate.kOutputSettings")) ;
		editorTemplate -addControl "renderable";
		editorTemplate -addControl "image";
		editorTemplate -addControl "mask";
		editorTemplate -addControl "depth";
        editorTemplate -addControl "depthType" "AEdepthTypeModified";
		editorTemplate -addControl "transparencyBasedDepth" "AEdepthTypeModified";
		editorTemplate -addControl "threshold";
		editorTemplate -suppress "imageName";
	editorTemplate -endLayout;

	editorTemplate -beginLayout (uiRes("m_AEcameraTemplate.kEnvironment")) ;
		editorTemplate 
			-callCustom 
				"AEcameraBackgroundColorNew" 
				"AEcameraBackgroundColorReplace"
				"backgroundColor";
		editorTemplate -callCustom "AEcameraImagePlaneNew" "AEcameraImagePlaneReplace"
					"imagePlane" 
					"horizontalFilmAperture" 
					"verticalFilmAperture";
	editorTemplate -endLayout;

	editorTemplate -beginLayout (uiRes("m_AEcameraTemplate.kSpecialEffects")) ;
        editorTemplate -addControl "shutterAngle";
		editorTemplate -suppress "useExploreDepthFormat";
	editorTemplate -endLayout;

	editorTemplate -beginLayout (uiRes("m_AEcameraTemplate.kDisplayOptions")) ;
		editorTemplate -beginNoOptimize;
		editorTemplate -addControl "displayFilmGate";
		editorTemplate -addControl "displayResolution";
		editorTemplate -addControl "displayFieldChart";
		editorTemplate -addControl "displaySafeAction";
		editorTemplate -addControl "displaySafeTitle";
		editorTemplate -addControl "displayFilmPivot";
		editorTemplate -addControl "displayFilmOrigin";
		editorTemplate -addControl "overscan";
		editorTemplate -endNoOptimize;
	editorTemplate -endLayout;

	editorTemplate -beginLayout (uiRes("m_AEcameraTemplate.kMovementOptions")) ;
		editorTemplate -label (uiRes("m_AEcameraTemplate.kUndoableMovements"))  -addControl "journalCommand";
		editorTemplate -addControl "centerOfInterest";
		editorTemplate -addControl "tumblePivot";
		editorTemplate -addControl "usePivotAsLocalSpace";
		editorTemplate -addControl "bookmarks";
	editorTemplate -endLayout;

	editorTemplate -beginLayout (uiRes("m_AEcameraTemplate.kOrthographicViews")) ;
		editorTemplate -addControl "orthographic" "checkOrthographic";
		editorTemplate -addControl "orthographicWidth";
	editorTemplate -endLayout;

	AEshapeTemplate $nodeName;

	ccAttributeDispatcher( $nodeName );

	//suppressed Attributes
	editorTemplate -suppress "motionBlur";
	editorTemplate -suppress "homeCommand";

editorTemplate -addExtraControls;
editorTemplate -endScrollLayout;

}
