// Copyright (C) 1997-2004 Alias Systems Corp.
// 
// The information in this file is provided for the exclusive use of the
// licensees of Alias.  Such users have the right to use, modify,
// and incorporate this code into other products for purposes authorized
// by the Alias license agreement, without fee.
// 
// ALIAS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
// INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
// EVENT SHALL ALIAS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
// DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

//
//  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 -l "Angle of View"
        	-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 "Auto Render Clip Plane"
        -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 -l "Film Aspect Ratio"
        	-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 "Controls" aeCameraNodeCount;

	menuItem -label "Camera" cameraNodeCount1;
	menuItem -label "Camera and Aim" cameraNodeCount2;
	menuItem -label "Camera, Aim, and Up" ;

	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 "Background Color"
		-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;
}



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

	editorTemplate -beginScrollLayout;
	editorTemplate -beginLayout "Camera Attributes" -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   "bestFitClippingPlanes";
		editorTemplate -suppress   "clippingPlanes";
		editorTemplate -addControl "nearClipPlane";
		editorTemplate -addControl "farClipPlane";
	editorTemplate -endLayout;

	editorTemplate -beginLayout "Film Back" -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 "Fit Resolution Gate" -addControl "filmFit";
		editorTemplate -addControl "filmFitOffset";
		editorTemplate -addControl "filmOffset";

		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 "Depth of Field";
        editorTemplate -addControl "depthOfField" "AEdofModified";
        editorTemplate -addControl "focusDistance";
        editorTemplate -addControl "fStop";
        editorTemplate -addControl "focusRegionScale";
    editorTemplate -endLayout;

	editorTemplate -beginLayout "Output Settings";
		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 "Environment";
		editorTemplate 
			-callCustom 
				"AEcameraBackgroundColorNew" 
				"AEcameraBackgroundColorReplace"
				"backgroundColor";
		editorTemplate -callCustom "AEcameraImagePlaneNew" "AEcameraImagePlaneReplace"
					"imagePlane" 
					"horizontalFilmAperture" 
					"verticalFilmAperture";
	editorTemplate -endLayout;

	editorTemplate -beginLayout "Special Effects";
        editorTemplate -addControl "shutterAngle";
		editorTemplate -suppress "useExploreDepthFormat";
	editorTemplate -endLayout;

	editorTemplate -beginLayout "Display Options";
		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 "Movement Options";
		editorTemplate -label "Undoable Movements" -addControl "journalCommand";
		editorTemplate -addControl "centerOfInterest";
		editorTemplate -addControl "tumblePivot";
		editorTemplate -addControl "bookmarks";
	editorTemplate -endLayout;

	editorTemplate -beginLayout "Orthographic Views";
		editorTemplate -addControl "orthographic" "checkOrthographic";
		editorTemplate -addControl "orthographicWidth";
	editorTemplate -endLayout;

	AEshapeTemplate $nodeName;

	ccAttributeDispatcher( $nodeName );
	
	//suppressed Attributes
	editorTemplate -suppress "motionBlur";
	editorTemplate -suppress "homeCommand";
	editorTemplate -suppress "imagePlane";
	editorTemplate -suppress "backgroundColor";

editorTemplate -addExtraControls;
editorTemplate -endScrollLayout;

}
