// 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.


proc buildPsdConvertSolidTxWindowMenu ()
//
//  Procedure Name:
//      buildPsdConvertSolidTxWindowMenu
//
//  Description:
//      Creates the menus for the ConvertSolidTx Window
//
//  Input Arguments:
//		None.
//
//  Return Value:
//      None.
//
{

	global string 	$psdConvSolidTxWindow;
	
	menu -l "Edit" 
		-tearOff true
		-postMenuCommandOnce true;
		menuItem -l "Reset Settings" 
			-command ("psdConvertSolidTxResetSettingsCmd");
		menuItem -l "Save Settings" 
			-command ("psdConvertSolidTxSaveSettingsCmd");

	setParent -menu ..;

}

global proc psdConvertSolidTxResetSettingsCmd()
{
	global string $psdChannelControl;
	psdConvSolidTxOptions -e -resetAll;
	
	psdChannelSelectCmd();
}

global proc psdConvertSolidTxSaveSettingsCmd()
{
}

global proc psdControlChangeCallback(string $controlName)
{
	global string	$psdconvertSolidTxAntiAlias, $psdConvertSolidBckMode, $psdConvertSolidTxFillTextureSeams,
						$psdConvertSolidTxSamplePlane, $psdConvertSolidBckColor, $psdConvertSolidTxBakeAlpha, 
						$psdChannelControl;
	string $selectedItem[] = `psdChannelOutliner -q -selectItem $psdChannelControl`;
	int $value = 0, $index;
	for($index = 0; $index < size($selectedItem); $index++)
	{
		switch($controlName)
		{
			case "psdconvertSolidTxAntiAlias":
					$value = `checkBoxGrp -q -value1  $psdconvertSolidTxAntiAlias`;
					psdConvSolidTxOptions -e  -channelString $selectedItem[$index] -antiAlias $value;
					break;
					
			case "psdConvertSolidTxFillTextureSeams":
					$value = `checkBoxGrp -q -value1  $psdConvertSolidTxFillTextureSeams`;
					psdConvSolidTxOptions -e  -channelString $selectedItem[$index] -fillTextureSeams $value;
					break;
					
			case "psdConvertSolidTxSamplePlane":
					$value = `checkBoxGrp -q -value1  $psdConvertSolidTxSamplePlane`;
					psdConvSolidTxOptions -e  -channelString $selectedItem[$index] -bakeUsingVirtualPlane $value;
					break;
					
			case "psdConvertSolidTxBakeAlpha":
					$value = `checkBoxGrp -q -value1  $psdConvertSolidTxBakeAlpha`;
					psdConvSolidTxOptions -e  -channelString $selectedItem[$index] -bakeTransparency $value;
					break;
					
			case "psdConvertSolidBckMode":
					string $bgMode = `optionMenuGrp -q -value  $psdConvertSolidBckMode`;
					performPsdConvertSolidBackModeCB();
					psdConvSolidTxOptions -e  -channelString $selectedItem[$index] -backgroundMode $bgMode;
					break;
					
			case "psdConvertSolidBckColor":
					float $bgColor[] = `colorSliderGrp -q  -rgbValue  $psdConvertSolidBckColor`;
					psdConvSolidTxOptions -e  -channelString $selectedItem[$index] -backgroundColor $bgColor[0] $bgColor[1] $bgColor[2];
					break;
		}
		
	}
	psdConvSolidTxOptions -e -saveAll;
}

proc psdInitializeConvSolidTxValues(string $channelAttribute)
{
	global string	$psdconvertSolidTxAntiAlias, $psdConvertSolidBckMode, $psdConvertSolidTxFillTextureSeams,
						$psdConvertSolidTxSamplePlane, $psdConvertSolidBckColor, $psdConvertSolidTxBakeAlpha; 
						
	psdConvSolidTxOptions -e 
									-channelString $channelAttribute
									-antiAlias false
									-backgroundMode "Shader default"
									-backgroundColor 0 0 0
									-fillTextureSeams true
									-bakeUsingVirtualPlane true
									-bakeTransparency false;
									
	checkBoxGrp -e	 -value1 off $psdconvertSolidTxAntiAlias;				
	optionMenuGrp  -e -value "Shader default" $psdConvertSolidBckMode;
	colorSliderGrp -e  -rgb 0 0 0 $psdConvertSolidBckColor;
	checkBoxGrp -e -value1 on $psdConvertSolidTxFillTextureSeams;
	checkBoxGrp -e -value1 off $psdConvertSolidTxSamplePlane;
	checkBoxGrp -e -value1 off $psdConvertSolidTxBakeAlpha;									
}

proc psdDisplaySwatchImage(string $connectedAttribute)
{
	global string $psdSwatchPort, $psdTextureName;
	
	string $tokens[];
	
	string $tokens[] ;
	tokenize $connectedAttribute "." $tokens;
	
	// Replace bump with normalCamera for getting the connections.
	
	string $connections[] ;
	if($tokens[size($tokens)-1] == "bump"){
		$connectedAttribute = $tokens[0] + "." + "normalCamera";
		$connections = `listConnections $connectedAttribute`;
		$connectedAttribute = $connections[0] + ".bumpValue";
		
	}
	
	if ($tokens[size($tokens)-1] == "displacement"){
		$connectedChannel = $tokens[0] + ".outColor" ;
		$connections = `listConnections -destination on -source off $connectedChannel`;
		
		// The default lambert material's outColor is connected to both initialShadingGroup and 
		// initialParticleSE. We need to take the displacement attribute of initialShadingGroup
		// only.
		
		if(size($connections) > 1){
			int $index;
			for($index = 0; $index < size($connections); $index++)
			{
				 if(`nodeType $connections[$index]` == "shadingEngine"){
						$connections[0] = $connections[$index];
				 }
			}
		}
		$connectedAttribute = $connections[0] + ".displacementShader" ;
	}
	
	string $connectedTextures = `connectionInfo -sourceFromDestination $connectedAttribute`;
	if(size($connectedTextures) > 0)
	{
		tokenize $connectedTextures "." $tokens;
		swatchDisplayPort -e  -shadingNode $tokens[0] $psdSwatchPort;
		text -e -label $tokens[0] $psdTextureName;
		
	}
}

proc psdAddChannelAttributes(string $channelAttributes[])
{
	global string $psdChannelControl, $psdSwatchPort ;

	int $index, $numTokens;
	
	string $tokens[];
	
	psdChannelOutliner -e -removeAll $psdChannelControl ;
	psdConvSolidTxOptions -e -deleteAll;
	
	string $imageFileName;
	for($index < 0; $index < size($channelAttributes); $index++)
	{
		$numTokens = `tokenize $channelAttributes[$index] "."  $tokens`;
		$imageFileName = psdCheckConnectionsForChannel($channelAttributes[$index]);
		psdInitializeConvSolidTxValues($channelAttributes[$index]);
		// Add the items for the channelOutliner control.
		psdChannelOutliner -e 
							-psdParent $tokens[0]
							-addChild $tokens[$numTokens -1] $imageFileName
							$psdChannelControl ;
	}
	// Keep the first one selected and assgn the attributes to them.
	psdChannelOutliner -e -select $channelAttributes[0] $psdChannelControl;
	psdDisplaySwatchImage($channelAttributes[0]);
}

global proc performPsdConvertSolidBackModeCB()
{
	int $isBackColor = `optionMenuGrp -q -sl psdConvertSolidBckMode`;
	disable -v ($isBackColor != 2) psdConvertSolidBckColor;
}

global proc psdChannelSelectCmd()
{
	global string $psdChannelControl;
	global string	$psdconvertSolidTxAntiAlias, $psdConvertSolidBckMode, $psdConvertSolidTxFillTextureSeams,
						$psdConvertSolidTxSamplePlane, $psdConvertSolidBckColor, $psdConvertSolidTxBakeAlpha; 
	
	string $selectedItem[] = `psdChannelOutliner -q -selectItem $psdChannelControl`;
	
	int $antiAliasFlag = `psdConvSolidTxOptions  -channelString $selectedItem[0] -q -antiAlias`;
	int $fillTxFlag = `psdConvSolidTxOptions  -channelString $selectedItem[0] -q -fillTextureSeams`;
	int $bakeVirtualPlane = `psdConvSolidTxOptions  -channelString $selectedItem[0] -q -bakeUsingVirtualPlane`;
	int $transFlag = `psdConvSolidTxOptions  -channelString $selectedItem[0] -q -bakeTransparency`;
	string $bgMode = `psdConvSolidTxOptions  -channelString $selectedItem[0] -q -backgroundMode`;
	float  $bgColor[] =  `psdConvSolidTxOptions  -channelString $selectedItem[0] -q -backgroundColor`;
	
	checkBoxGrp -e	 -value1 $antiAliasFlag $psdconvertSolidTxAntiAlias;				
	optionMenuGrp  -e -value $bgMode $psdConvertSolidBckMode;
	colorSliderGrp -e -rgb $bgColor[0] $bgColor[1] $bgColor[2]  $psdConvertSolidBckColor;
	checkBoxGrp -e -value1 $fillTxFlag $psdConvertSolidTxFillTextureSeams;
	checkBoxGrp -e -value1 $bakeVirtualPlane $psdConvertSolidTxSamplePlane;
	checkBoxGrp -e -value1 $transFlag $psdConvertSolidTxBakeAlpha;
	
	psdDisplaySwatchImage( $selectedItem[0]);
}

global proc psdConvertSolidTxCloseCmd()
{
	global string $psdConvSolidTxWindow ;
	window -e -visible false $psdConvSolidTxWindow;
}
global proc psdConvertSolidTxApplyCmd()
{
	global string $psdConvSolidTxWindow ;
	psdConvSolidTxOptions -e -saveAll;
	window -e -visible false $psdConvSolidTxWindow;
}

global proc psdConvertSolidTxPanel(string $channelAttributes[])
{
	global string	$psdConvSolidTxWindow, $psdSwatchPort, $psdTextureName ;
	global string	$psdChannelControl,  $psdApplyButton,  $psdCloseButton ;
	global string	$psdconvertSolidTxAntiAlias, $psdConvertSolidBckMode, $psdConvertSolidTxFillTextureSeams,
						$psdConvertSolidTxSamplePlane, $psdConvertSolidBckColor, $psdConvertSolidTxBakeAlpha; 

	if(`window -exists "psdConvSolidTxWindow"`)
	{
		showWindow $psdConvSolidTxWindow;
		window -e -visible true $psdConvSolidTxWindow;
	}else{
		$psdConvSolidTxWindow = `window -ret  -h 300 -w 550
											-title "PSD Convert Solid Textures Options" 
											psdConvSolidTxWindow`;
		
		string $texMenubar = `menuBarLayout texMenubarLayout`;
		string $topForm = `formLayout  -p $texMenubar -numberOfDivisions 100 topForm`;
				
				$psdApplyButton = `button  -p $topForm -label  "Apply" -command "psdConvertSolidTxApplyCmd"  psdApplyButton`;
				$psdCloseButton = `button -p $topForm  -label  "Close" -command "psdConvertSolidTxCloseCmd" psdCloseButton`;
				
				string $leftForm = `formLayout  -numberOfDivisions 100 leftForm`;
					$psdChannelControlLabel = `text -label "Connected Attributes" psdChannelControlLabel`;
					$psdChannelControl = `psdChannelOutliner -height 125 
															-selectCommand "psdChannelSelectCmd"
        													psdChannelControl`;
        		
        		formLayout -e 
        					-attachForm $psdChannelControlLabel "left" 5
        					-attachForm $psdChannelControlLabel "right" 5
        					-attachForm $psdChannelControlLabel "top" 2
        					
        					-attachForm $psdChannelControl "left" 5
        					-attachForm $psdChannelControl "right" 5
        					-attachControl $psdChannelControl "top" 2 $psdChannelControlLabel 
        					-attachForm $psdChannelControl "bottom" 5
        					
        					$leftForm;
        		setParent .. ;
        		
        		string $swatchPortForm = `formLayout  -numberOfDivisions 100 swatchPortForm`;
        				text -label "Texture Sample" psdTextureSampleLabel;
        				$psdSwatchPort = `swatchDisplayPort -wh 64 64 swatchPort`;
        				$psdTextureName = `text -label "" -font "boldLabelFont" psdTextureName`;
        				separator -height 15 swatchSeparator;
        				
        				formLayout -e 
        						-attachForm psdTextureSampleLabel "left" 60
        						-attachForm psdTextureSampleLabel "top" 35
        						-attachForm psdTextureSampleLabel "bottom" 20
        						
        						-attachForm swatchPort "left" 140
        						-attachForm swatchPort "top" 10
        						-attachForm swatchPort "bottom" 20
        						
        						-attachControl psdTextureName "left" 5 swatchPort
        						-attachForm psdTextureName "top" 35
        						-attachForm psdTextureName "bottom" 40
        						
        						-attachForm swatchSeparator "left"  0
        						-attachForm swatchSeparator "right" 0
        						-attachControl swatchSeparator "top" 3 swatchPort
        						$swatchPortForm ;
        		setParent ..;
	        	
	     
				string $rightColumn =`columnLayout -adjustableColumn 1`;
				
					$psdconvertSolidTxAntiAlias = `checkBoxGrp
																				-label ""
																				-label1 "Anti-alias"
																				-numberOfCheckBoxes 1
																				-value1 off
																				-changeCommand ("psdControlChangeCallback  psdconvertSolidTxAntiAlias" )
																				psdconvertSolidTxAntiAlias`;
				
					$psdConvertSolidBckMode = `optionMenuGrp -l "Background Mode" -cal 1 "right"
																		-changeCommand ("psdControlChangeCallback   psdConvertSolidBckMode")
																		psdConvertSolidBckMode`;
						menuItem -l "Shader default";
						menuItem -l "Custom color";
						menuItem -l "Extend Edge Color";

					$psdConvertSolidBckColor = `colorSliderGrp -label "Background Color" -rgb 0 0 0
																-changeCommand ("psdControlChangeCallback   psdConvertSolidBckColor")
																psdConvertSolidBckColor`;
					
					disable -v true psdConvertSolidBckColor;

					separator -height 15;

					$psdConvertSolidTxFillTextureSeams = `checkBoxGrp -label ""
																						-label1 "Fill Texture Seams"
																						-numberOfCheckBoxes 1
																						-value1 on
																						-changeCommand ("psdControlChangeCallback  psdConvertSolidTxFillTextureSeams")
																						psdConvertSolidTxFillTextureSeams`;

					$psdConvertSolidTxSamplePlane = `checkBoxGrp -label "" 
																				-label1 "Bake Using Virtual Plane"
																				-cw  2 180
																				-numberOfCheckBoxes 1
																				-value1 off
																				-changeCommand ("psdControlChangeCallback  psdConvertSolidTxSamplePlane")
																				psdConvertSolidTxSamplePlane`;

					$psdConvertSolidTxBakeAlpha = `checkBoxGrp 
																				-label ""
																				-label1 "Bake Transparency"
																				-cw  2 120
																				-numberOfCheckBoxes 1
																				-value1 off
																				-changeCommand ("psdControlChangeCallback psdConvertSolidTxBakeAlpha")
																				psdConvertSolidTxBakeAlpha`;
				
				setParent .. ;

		
		formLayout -e 
					-attachForm $leftForm "left" 5
					-attachForm $leftForm "top" 5
					-attachPosition  $leftForm "right" 2 30
					-attachForm $leftForm "bottom" 34
					
					-attachForm $swatchPortForm "top" 0
					-attachForm $swatchPortForm "right" 0
					-attachControl $swatchPortForm "left" 0 $leftForm
					
					-attachForm $rightColumn "right" 0
					-attachControl $rightColumn "top" 0 $swatchPortForm
					-attachControl $rightColumn "left" 0 $leftForm
					
					-attachForm $psdApplyButton "left" 5
					-attachPosition $psdApplyButton "right" 2 50
					-attachForm $psdApplyButton "bottom" 5
					-attachControl $psdApplyButton "top" 2 $leftForm
					
					-attachControl $psdCloseButton "left" 2 $psdApplyButton
					-attachPosition $psdCloseButton "right" 2 99
					-attachForm $psdCloseButton "bottom" 5
					-attachControl $psdCloseButton "top" 2 $leftForm
					$topForm;
					
		setParent ..;
		
		buildPsdConvertSolidTxWindowMenu;
		
		showWindow $psdConvSolidTxWindow;
		window -e -visible true $psdConvSolidTxWindow;
	}
	psdAddChannelAttributes($channelAttributes);
}