// 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.

//
//	Alias|Wavefront Script File
//
//	Creation Date:	May 29, 1997
//
//	Procedure Name:
//	AEmeshTemplate
//
//	Description Name;
//	Creates the attribute editor controls for the mesh node
//
//	Input Value:
//	nodeName
//
//	Output Value:
//	None
//

global proc checkMeshDisplayBorder ( string $nodeName )
{
	string $nodeAttr = $nodeName + ".displayBorders";
	int $value = `getAttr $nodeAttr`;

	if ( $value ) {
		editorTemplate -dimControl $nodeName "borderWidth" false;
	} else {
		editorTemplate -dimControl $nodeName "borderWidth" true;
	}
}

global proc checkMeshUseMaxSubd ( string $nodeName )
{
  string $nodeAttr = $nodeName + ".useMaxSubdivisions";
  int $value = `getAttr $nodeAttr`;

	if ( $value ) {
	  editorTemplate -dimControl $nodeName "maxSubd" false;
	} else {
	  editorTemplate -dimControl $nodeName "maxSubd" true;
	}
}

global proc checkMeshUseMinScreen ( string $nodeName )
{
	string $nodeAttr = $nodeName + ".useMinScreen";
	int $value = `getAttr $nodeAttr`;

	if ( $value ) {
		editorTemplate -dimControl $nodeName "minScreen" false;
	} else {
		editorTemplate -dimControl $nodeName "minScreen" true;
	}
}

global proc checkMeshUseMaxEdgeLength ( string $nodeName )
{
	string $nodeAttr = $nodeName + ".useMaxEdgeLength";
	int $value = `getAttr $nodeAttr`;

	if ( $value ) {
		editorTemplate -dimControl $nodeName "maxEdgeLength" false;
	} else {
		editorTemplate -dimControl $nodeName "maxEdgeLength" true;
	}
}

global proc checkMeshUseMaxUv ( string $nodeName )
{
	string $nodeAttr = $nodeName + ".useMaxUV";
	int $value = `getAttr $nodeAttr`;

	if ( $value ) {
		editorTemplate -dimControl $nodeName "maxUv" false;
	} else {
		editorTemplate -dimControl $nodeName "maxUv" true;
	}
}



global proc checkMeshUseMinEdgeLength ( string $nodeName )
{
	string $nodeAttr = $nodeName + ".useMinEdgeLength";
	int $value = `getAttr $nodeAttr`;

	if ( $value ) {
		editorTemplate -dimControl $nodeName "minEdgeLength" false;
	} else {
		editorTemplate -dimControl $nodeName "minEdgeLength" true;
	}
}

global proc checkMeshDisplayNormals ( string $nodeName )
{
	string $nodeAttr = $nodeName + ".displayNormal";
	int $value = `getAttr $nodeAttr`;

	if ( $value ) {
		editorTemplate -dimControl $nodeName "normalSize" false;
		editorTemplate -dimControl $nodeName "normalType" false;
	} else {
		editorTemplate -dimControl $nodeName "normalSize" true;
		editorTemplate -dimControl $nodeName "normalType" true;
	}
}

global proc checkMeshDisplayVertices ( string $nodeName )
{
	string $nodeAttr = $nodeName + ".displayVertices";
	int $value = `getAttr $nodeAttr`;

	if ( $value ) {
		editorTemplate -dimControl $nodeName "vertexBackfaceCulling" false;
	} else {
		editorTemplate -dimControl $nodeName "vertexBackfaceCulling" true;
	}
}

proc setDisplayColorChannelOptionMenu( string $attrName )
{
	string $channel = `getAttr $attrName`;
	int $option = 4; // Diffuse
	if ( $channel == "color" || $channel == "Diffuse" ) $option = 4;
	else if ( $channel == "None" ) $option = 1;
	else if ( $channel == "Ambient" ) $option = 2;
	else if ( $channel == "Ambient+Diffuse" ) $option = 3;
	else if ( $channel == "Specular" ) $option = 5;
	else if ( $channel == "Emission" ) $option = 6;

	if ( `optionMenuGrp -q -exists displayColorChannelOptionMenu` )
	{
		optionMenuGrp -e
			-select $option
			displayColorChannelOptionMenu;
	}
}

global proc setDisplayColorChannelAttribute( string $attrName )
{
	string $channel = "Diffuse";
	int $option = `optionMenuGrp -q -select displayColorChannelOptionMenu`;

	if ( $option == 4 ) $channel = "Diffuse";
	else if ( $option == 1 ) $channel = "None";
	else if ( $option == 2 ) $channel = "Ambient";
	else if ( $option == 3 ) $channel = "Ambient+Diffuse";
	else if ( $option == 5 ) $channel = "Specular";
	else if ( $option == 6 ) $channel = "Emission";

	setAttr -type "string" $attrName $channel;
}

global proc AEdisplayColorChannelNew ( string $attrName )
{
	setUITemplate -pushTemplate attributeEditorTemplate;
	string $parent = `setParent -q`;

	optionMenuGrp 
		-label "Display Color Channel"
		displayColorChannelOptionMenu;
	menuItem -label "None";
	menuItem -label "Ambient";
	menuItem -label "Ambient+Diffuse";
	menuItem -label "Diffuse";
	menuItem -label "Specular";
	menuItem -label "Emission";
	setDisplayColorChannelOptionMenu( $attrName );

	optionMenuGrp -e
		-changeCommand ( "setDisplayColorChannelAttribute " + $attrName )
		displayColorChannelOptionMenu;

	setUITemplate -popTemplate;
}

global proc AEdisplayColorChannelReplace( string $attrName )
{
	optionMenuGrp -e
		-changeCommand ""
		displayColorChannelOptionMenu;

	setDisplayColorChannelOptionMenu( $attrName );

	optionMenuGrp -e
		-changeCommand ( "setDisplayColorChannelAttribute " + $attrName )
		displayColorChannelOptionMenu;
}

global proc AEmeshSmoothControls ( string $nodeName ) 
{
	string $nodeAttr = $nodeName + ".outSmoothMesh";
	int $state = true;
	if (size(`listConnections $nodeAttr`)) {
		// SmoothMesh is not being used.  Disable the controls.
		$state = false;
	}
	editorTemplate -dimControl $nodeName "smoothLevel" $state;
	editorTemplate -dimControl $nodeName "continuity" $state;
	editorTemplate -dimControl $nodeName "smoothUVs" $state;
	editorTemplate -dimControl $nodeName "keepBorder" $state;
	editorTemplate -dimControl $nodeName "keepHardEdge" $state;
	editorTemplate -dimControl $nodeName "keepMapBorders" $state;
	editorTemplate -dimControl $nodeName "smoothOffset" $state;
}

global proc AEbuildSmoothControls(string $nodeName)
{
	// Set this to 1, and restart Maya to get access to these controls
	// on the Attribute Editor
	global int $gEnableSmoothMeshControls = 0;
	if ($gEnableSmoothMeshControls) {
		editorTemplate -beginLayout "Smooth Mesh Controls";	
			editorTemplate -addControl "smoothLevel" "AEmeshSmoothControls";
			editorTemplate -addControl "continuity" "AEmeshSmoothControls";
			editorTemplate -addControl "smoothUVs" "AEmeshSmoothControls";
			editorTemplate -addControl "keepBorder" "AEmeshSmoothControls";
			editorTemplate -addControl "keepHardEdge" "AEmeshSmoothControls";
			editorTemplate -addControl "keepMapBorders" "AEmeshSmoothControls";	
			editorTemplate -addControl "smoothOffset" "AEmeshSmoothControls";
		editorTemplate -endLayout;
	} else {
		editorTemplate -suppress "smoothLevel";
		editorTemplate -suppress "continuity";
		editorTemplate -suppress "smoothUVs";
		editorTemplate -suppress "keepBorder";
		editorTemplate -suppress "keepHardEdge";
		editorTemplate -suppress "keepMapBorders";
		editorTemplate -suppress "smoothOffset";
	}
}

global proc checkMeshReuseTrianglesAE( string $nodeName )
{
	int $val = `getAttr ($nodeName+".reuseTriangles")`;	
	polyOptions -reuseTriangles $val $nodeName;
}

global proc AEmeshTemplate( string $nodeName )
{
	editorTemplate -beginScrollLayout;


		editorTemplate -beginLayout "Tessellation Attributes";

			editorTemplate -addControl "maxTriangles";

			editorTemplate -addControl "useMaxSubdivisions" 
						"checkMeshUseMaxSubd";
			editorTemplate -addControl "maxSubd";

			editorTemplate -addControl "useMinScreen"
						"checkMeshUseMinScreen";
			editorTemplate -addControl "minScreen";

			editorTemplate -addControl "useMaxUV"
						"checkMeshUseMaxUv";
			editorTemplate -addControl "maxUv";

			editorTemplate -addControl "useMinEdgeLength"
						"checkMeshUseMinEdgeLength";
			editorTemplate -addControl "minEdgeLength";

			editorTemplate -addControl "useMaxEdgeLength"
						"checkMeshUseMaxEdgeLength";
			editorTemplate -addControl "maxEdgeLength";

		editorTemplate -endLayout;

		editorTemplate -beginLayout "Mesh Component Display";
			editorTemplate -beginNoOptimize;
			editorTemplate -addControl "displayImmediate";
			editorTemplate -addControl "displayVertices";
						//"checkMeshDisplayVertices";
			editorTemplate -addControl "vertexBackfaceCulling";
			editorTemplate -addControl "backfaceCulling";

			editorTemplate -endNoOptimize;
	
			editorTemplate -addSeparator;
			editorTemplate -addControl "displayEdges";	
			editorTemplate -addControl "displayBorders" 
				"checkMeshDisplayBorder";
			editorTemplate -addControl "borderWidth";
	
			editorTemplate -addSeparator;
			editorTemplate -beginNoOptimize;
			editorTemplate -suppress "displayItemNumbers";			
			editorTemplate -addControl "displayCenter";
			editorTemplate -addControl "displayTriangles";
			editorTemplate -addControl "displayUVs";
			editorTemplate -addControl "displayNonPlanar";
			
			editorTemplate -addSeparator;
					
			editorTemplate -addControl "displayColors";
			editorTemplate -callCustom 
				"AEdisplayColorChannelNew"
				"AEdisplayColorChannelReplace"
				"displayColorChannel";
				
			editorTemplate -addControl "materialBlend";				
			
			editorTemplate -endNoOptimize;
	
			editorTemplate -addSeparator;

			editorTemplate -addControl "displayNormal" 
						"checkMeshDisplayNormals";
			editorTemplate -addControl "normalSize";
			editorTemplate -addControl "normalType";

		editorTemplate -endLayout;

		editorTemplate -beginLayout "Mesh Controls";
			editorTemplate -addControl "currentUVSet";
			editorTemplate -addControl "allowTopologyMod";
			editorTemplate -addSeparator;
			editorTemplate -addControl "reuseTriangles" "checkMeshReuseTrianglesAE";
		editorTemplate -endLayout;
		
		AEbuildSmoothControls($nodeName);
		
		// include/call base class/node attributes
		AEsurfaceShapeTemplate $nodeName;

		// supressed attributes
		editorTemplate -suppress "inMesh";
		editorTemplate -suppress "outMesh";
		editorTemplate -suppress "outSmoothMesh";
		editorTemplate -suppress "cachedInMesh";
		editorTemplate -suppress "useNumTriangles";
		editorTemplate -suppress "numTriangles";
		editorTemplate -suppress "pnts";
		editorTemplate -suppress "colors";
		editorTemplate -suppress "colorPerVertex";
		editorTemplate -suppress "normalPerVertex";
		editorTemplate -suppress "uvSet";
		editorTemplate -suppress "normals";
		editorTemplate -suppress "displayFacesWithGroupId";
		editorTemplate -suppress "userTrg";

		ccAttributeDispatcher( $nodeName );

	editorTemplate -addExtraControls;
	editorTemplate -endScrollLayout;

}
