//---------------------------------------------------------------------------
//  nnSetCustomShaderAttribute.mel
//
//  Copyright (C) 2002 - 2009 SEGA Corporation CS R&D Support Department
//  All Rights Reserved.
//
//  Module  : nnSetCustomShaderAttribute
//  File    : nnSetCustomShaderAttribute.mel
//  Create  : 2004/12/06
//  Modify  : 
//  Version : 1.20.00
//  Note    : nnSetCustomShaderAttribute
//
//---------------------------------------------------------------------------

global string $nnSetCustomShaderAttributeTitle		= "nnSetCustomShaderAttribute";
global string $nnSetCustomShaderAttributeVersion	= "0.80.00";
global string $nnCustomShaderListFileName			= "nnCustomShaderList.ini";

global string $nnCustomShaderNameList[];
global string $nnCustomShaderFXFileNameList[];
global string $nnCustomShaderTechniqueNameList[];

proc nnCustomShaderAttributeFileRead()
{
	global string $nnCustomShaderListFileName;
	global string $nnCustomShaderNameList[];
	global string $nnCustomShaderFXFileNameList[];
	global string $nnCustomShaderTechniqueNameList[];
	
	int $cnt = 0;

	string $userPrefDir = `internalVar -userPrefDir`;

	string $shaderListFile = $userPrefDir;
	$shaderListFile += $nnCustomShaderListFileName;

print($shaderListFile+"\n");

	int $fileId=`fopen $shaderListFile "rt"`;

	string $lineBuf = `fgetline $fileId`;

	while ( size( $lineBuf ) > 0 ) {
		if(`substring $lineBuf 1 1` != "#") {
			string $buffer1[];
			string $buffer2[];
			string $token1;
			string $token2;

			// 改行コード削除
			$lineBuf = substituteAllString($lineBuf, "\n", "");

			// TAB,スペースでトークン切り出し
		 	$numTokens = `tokenize $lineBuf "\t" $buffer1`;
		 	if( $numTokens < 3) {
				string	$tokenList[];
				int		$tokenCnt = 0;
			 	for( $token1 in $buffer1 ) {
				 	$numTokens = `tokenize $token1 " " $buffer2`;
				 	for( $token2 in $buffer2 ) {
						$tokenList[$tokenCnt] = $token2;
						$tokenCnt ++;
					}
				}
				
				if(size($tokenList) == 3 ) {
					$nnCustomShaderNameList[$cnt]			= $tokenList[0];
					$nnCustomShaderFXFileNameList[$cnt]		= $tokenList[1];
					$nnCustomShaderTechniqueNameList[$cnt]	= $tokenList[2];
					$cnt ++;
				}
			}
			else {
				$nnCustomShaderNameList[$cnt]			= $buffer1[0];
				$nnCustomShaderFXFileNameList[$cnt]		= $buffer1[1];
				$nnCustomShaderTechniqueNameList[$cnt]	= $buffer1[2];
				$cnt ++;
			}
		}
		$lineBuf = `fgetline $fileId`;
		
	};

	fclose $fileId;
}

proc nnSetCustomShaderAttributeToMat( int $shaderId, string $matList[] )
{
	string	$material;

	print("shaderId "+$shaderId+"\n");

	global string $nnCustomShaderNameList[];
	global string $nnCustomShaderFXFileNameList[];
	global string $nnCustomShaderTechniqueNameList[];

	for( $material in $matList ) {
		string $shaderName		= $material;
		string $shaderFXName	= $material;
		string $techniqueName	= $material;

		string $nodeType = nodeType( $material );
		if( $nodeType == "particleCloud" )	continue;

		$shaderName		+= ".nnCusShaderName";
		$shaderFXName	+= ".nnShaderFXName";
		$techniqueName	+= ".nnTechniqueName";

		if( `objExists $shaderName` == 0 ) {
			addAttr -ln "nnCusShaderName" -dt "string" $material;
		}
		setAttr $shaderName -type "string" $nnCustomShaderNameList[$shaderId];
		if( `objExists $shaderFXName` == 0 ) {
			addAttr -ln "nnShaderFXName" -dt "string" $material;
		}
		setAttr $shaderFXName -type "string" $nnCustomShaderFXFileNameList[$shaderId];
		if( `objExists $techniqueName` == 0 ) {
			addAttr -ln "nnTechniqueName" -dt "string" $material;
		}
		setAttr $techniqueName -type "string" $nnCustomShaderTechniqueNameList[$shaderId];
	}
}

//---------------------------------------------------------------------------
//  関数名 : nnCustomShaderAttributeDialogTemplate
//  機　能 : nnCustomShaderAttributeDialogTemplateを作成します。
//  書　式 : proc nnCustomShaderAttributeDialogTemplate()
//  引　数 : なし
//
//  戻り値 : なし
//  説　明 : ccAutoSelectFaceTool　GUIで使用するレイアウトのテンプレートを定義します。
//---------------------------------------------------------------------------
proc nnCustomShaderAttributeDialogTemplate()
{

	// nnCustomShaderAttributeDialogTemplateがすでに存在している場合は一度削除する。
	if ( `uiTemplate -exists nnCustomShaderAttributeDialogTemplate` ) {
		deleteUI -uiTemplate nnCustomShaderAttributeDialogTemplate;
	}
	// DialogTemplateを作成
	uiTemplate nnCustomShaderAttributeDialogTemplate;

	// columnLayout
	columnLayout
		-defineTemplate		nnCustomShaderAttributeDialogTemplate
		-adjustableColumn	true
		-manage 			true
		;

	// frameLayout
	frameLayout
		-defineTemplate 	nnCustomShaderAttributeDialogTemplate
		-visible			true
		-collapsable		false
		-collapse			false
		-borderVisible		true
		-borderStyle		"etchedOut"
		-label				""
		-labelVisible		true
		-labelAlign			"center"
		-labelIndent		5
		-marginWidth		5
		-marginHeight		5
		;

	// rowLayout
	rowLayout
		-defineTemplate		nnCustomShaderAttributeDialogTemplate

		-columnAttach1		both
		-columnOffset1		0
		-columnAlign1		left

		-columnAttach2		left left
		-columnOffset2		0 0
		-columnAlign2		left left

		-columnAttach3		both both both
		-columnOffset3		0 0 0
		-columnAlign3		left left left

		-columnAttach4		both both both both
		-columnOffset4		0 0 0 0
		-columnAlign4		center center center center
		;


	// scrollLayout
	scrollLayout
		-defineTemplate 	nnCustomShaderAttributeDialogTemplate
		-visible			true
		-childResizable		true
		;

	// textFieldButtonGrp
	textFieldButtonGrp
		-defineTemplate		nnCustomShaderAttributeDialogTemplate

		-rowAttach			1 bottom 10
		-label				""
		-text				""
		-buttonLabel		""
		-columnAttach		1 both 0
		-columnAttach		2 both 0
		-columnAttach		3 both 10
		-columnAlign		1 right
		-columnAlign		2 left
		-columnAlign		3 center
		;

	// optionMenuGrp
	optionMenuGrp
		-defineTemplate		nnCustomShaderAttributeDialogTemplate
		-label			""

		-columnAttach2		both both
		-columnOffset2		0 5
		-columnAlign2		right left
		-rowAttach			1 top 5
		;

	// checkBoxGrp
	checkBoxGrp
		-defineTemplate		nnCustomShaderAttributeDialogTemplate
		-label 				""

		-columnAttach2		both both
		-columnOffset2		0 5
		-columnAlign2		right left

		-columnAttach4		both both both both
		-columnOffset4		0 0 0 0
		-columnAlign4		right left left left

		-columnAttach5		both both both both both
		-columnOffset5		0 0 0 0 0
		-columnAlign5		right left left left left
		;

	// radioButtonGrp
	radioButtonGrp
		-defineTemplate		nnCustomShaderAttributeDialogTemplate
		-label				""

		-columnAttach3		both both both
		-columnOffset3		0 5 0
		-columnAlign3		right left left

		-columnAttach4		both both both both
		-columnOffset4		0 0 0 0
		-columnAlign4		right left left left
		;

	// button
	button
		-defineTemplate		nnCustomShaderAttributeDialogTemplate

		-label				""
		-align				center
		;

	// floatField
	floatField
		-defineTemplate		nnCustomShaderAttributeDialogTemplate
		-precision			6
		;

	// floatSliderGrp
	floatSliderGrp
		-defineTemplate 	nnCustomShaderAttributeDialogTemplate
		-label				""
		-field				true
		-precision			6
		-columnAttach2		both both
		-columnOffset2		0 5
		-columnAlign2		right left
		-columnAttach3		both both both
		-columnOffset3		0 5 0
		-columnAlign3		right left left
		;

	// intFieldGrp
	intFieldGrp
		-defineTemplate		nnCustomShaderAttributeDialogTemplate
		-columnAttach2		both both
		-columnOffset2		0 5
		-columnAlign2		right left
		;

	// intSliderGrp
	intSliderGrp
		-defineTemplate 	nnCustomShaderAttributeDialogTemplate
		-label				""
		-field				true
		-columnOffset2		0 5
		-columnAttach2		both both
		-columnAttach3		both both both
		-columnOffset3		0 5 0
		-columnAlign3		right left left
		;
}


//---------------------------------------------------------------------------
//  関数名 : nnSelectionCustomShaderLayout
//  機　能 : Selection Shader Listの構築
//  書　式 : proc nnSelectionCustomShaderLayout()
//  引　数 : なし
//
//  戻り値 : なし
//  説　明 : Selection Shader Listの構築をする
//---------------------------------------------------------------------------
global proc nnSelectionCustomShaderLayout(string $parent)
{
	global string $nnCustomShaderNameList[];
	int		$i;
	string	$cmd = "textScrollList -e";

	for( $i = 0; $i <size($nnCustomShaderNameList); $i++ ) {
		$cmd += " -append \"";
		$cmd += $nnCustomShaderNameList[$i];
		$cmd += "\"";
	}
	$cmd += " nnSelectionCustomShaderTextScrollList";

	textScrollList -edit -removeAll nnSelectionCustomShaderTextScrollList;
	eval( $cmd );
	textScrollList -edit -selectIndexedItem 1 nnSelectionCustomShaderTextScrollList;

	return;
}

//---------------------------------------------------------------------------
//  関数名 : nnSelectionCustomShaderWinClose
//  機　能 : nnSetCustomShaderAttributeWin Windowを閉じます。
//  書　式 : proc nnSelectionCustomShaderWinClose()
//  引　数 : なし
//
//  戻り値 : なし
//  説　明 :
//---------------------------------------------------------------------------
global proc nnSelectionCustomShaderWinClose()
{
	if ( `window -exists nnSetCustomShaderAttributeWin` == true ) {
		deleteUI -window nnSetCustomShaderAttributeWin;
	}
	return;
}

//---------------------------------------------------------------------------
//  関数名 : nnMaterialShaderListLayout
//  機　能 : nnMaterialShaderListHeadRowLayoutの構築
//  書　式 : proc nnMaterialShaderListLayout()
//  引　数 : なし
//
//  戻り値 : なし
//  説　明 : nnMaterialShaderListHeadRowLayoutの構築をする
//---------------------------------------------------------------------------
proc nnMaterialShaderListLayout(string $parent)
{
	global string $nnCustomShaderNameList[];
	global string $nnCustomShaderFXFileNameList[];
	global string $nnCustomShaderTechniqueNameList[];

	string	$materialList[] = `ls -materials`;
	string	$material;

	// Material Shader Listの再構築
	if( `scrollLayout -exists nnMaterialShaderListScrollLayout` ) {
		deleteUI nnMaterialShaderListScrollLayout;
	}

	setParent $parent;

	scrollLayout
		-width			400
		-height			200
		nnMaterialShaderListScrollLayout;
		rowColumnLayout
//			-height				200
			-numberOfColumns 	2
			-columnWidth		1 150
			-columnWidth		2 250
/*************
			-columnWidth		3 100
			-columnWidth		4 100
*************/
			-columnAlign		1 "left"
			-columnAlign		2 "left"
/*************
			-columnAlign		3 "left"
			-columnAlign		4 "left"
*************/
			nnMaterialShaderListColumnLayout;

			for($material in $materialList) {
				string $nodeType = nodeType( $material );
				if( $nodeType == "particleCloud" )	continue;

				string $shaderName		= $material;
				string $shaderFXName	= $material;
				string $techniqueName	= $material;

				$shaderName		+= ".nnCusShaderName";
				$shaderFXName	+= ".nnShaderFXName";
				$techniqueName	+= ".nnTechniqueName";

				text -label $material;
				if( `objExists $shaderName` ) {
					text -label `getAttr $shaderName`;
				}
				else {
					text -label " ";
				}
/*************
				if( `objExists $shaderFXName` ) {
					text -label `getAttr $shaderFXName`;
				}
				else {
					text -label " ";
				}
				if( `objExists $techniqueName` ) {
					text -label `getAttr $techniqueName`;
				}
				else {
					text -label " ";
				}
*************/
			}
			text -height 50 " ";
			text -height 50 " ";

		// ]] rowLayout
		setParent ..;
	// ]] scrollLayout
	setParent ..;
}

//---------------------------------------------------------------------------
//  関数名 : nnApplyCustomShedar
//  機　能 : 現在のシェーダーを選択状態のマテリアルへアサイン
//  書　式 : proc nnMaterialShaderListLayout()
//  引　数 : なし
//
//  戻り値 : なし
//  説　明 : 現在のシェーダーを選択状態のマテリアルへアサインする
//---------------------------------------------------------------------------
global proc nnApplyCustomShedar()
{
	string	$materialList[] = `ls -selection -materials`;
	string	$material;
	int $index[] = `textScrollList -query -selectIndexedItem nnSelectionCustomShaderTextScrollList`;

	// シェーダー属性の設定
	nnSetCustomShaderAttributeToMat( $index[0]-1, $materialList );

	// Material Shader Listの作成
	nnMaterialShaderListLayout("nnMaterialShaderListHeadRowLayout");

	return;
}

//---------------------------------------------------------------------------
//  関数名 : nnSetCustomShaderAttributeGUI
//  機　能 : nnSetCustomShaderAttribute GUIメイン処理。
//  書　式 : proc nnSetCustomShaderAttributeGUI()
//  引　数 : なし
//
//  戻り値 : なし
//  説　明 : nnSetCustomShaderAttribute GUIメイン処理。
//---------------------------------------------------------------------------
proc nnSetCustomShaderAttributeGUI()
{
	global string $nnCustomShaderNameList[];
	global string $nnCustomShaderFXFileNameList[];
	global string $nnCustomShaderTechniqueNameList[];
	global string $nnSetCustomShaderAttributeTitle;
	global string $nnSetCustomShaderAttributeVersion;

	// GUI用テンプレート作成
	nnCustomShaderAttributeDialogTemplate();

	// nnSetCustomShaderAttributeWinウインドを再構築の為に一度削除
	if( `window -exists nnSetCustomShaderAttributeWin` == true ) {
		deleteUI nnSetCustomShaderAttributeWin;
	}

	string $titleName;
	$titleName	= $nnSetCustomShaderAttributeTitle;
	$titleName	+= " ";
	$titleName	+= $nnSetCustomShaderAttributeVersion;

	// nnSetCustomShaderAttributeWin windowの設定
	window
		-title			$titleName
		-width			400
		-menuBar		true
		-sizeable		true
		-resizeToFitChildren true
		-iconName 		$titleName
		nnSetCustomShaderAttributeWin;

		// GUI用テンプレートをセット
		setUITemplate -pushTemplate nnCustomShaderAttributeDialogTemplate;

		// columnLayout [[
		columnLayout
			-width				400
			-height				450
			-adjustableColumn	true
			nnCustomShaderAttributeWinColumnLayout;

			frameLayout
				-width			400
				-height			450
				-borderStyle	"etchedIn"
				nnCustomShaderAttributeFrameLayout;

				// columnLayout
				columnLayout
					-width			400
					-height			400
					-adjustableColumn	true
					nnCustomShaderAttributeColumnLayout;

					// Titleの作成
					rowLayout
						-columnWidth		1 150
						-adjustableColumn1	true
						-numberOfColumns 	1;
						text
							-width			250
							-height			15
							-font			"boldLabelFont"
							-align			"right"
							-label			"Selection Custom Shader";
					// ]] rowLayout
					setParent ..;

					rowLayout
						-numberOfColumns	1
						-height				400
						-columnWidth1		200
						-adjustableColumn1	true
						nnSelectionCustomShaderRowLayout;

						textScrollList
							-width					400
							-height					200
							-numberOfRows			10
							-allowMultiSelection	false
							-font					"boldLabelFont"
							nnSelectionCustomShaderTextScrollList;

						// Selection Shader Listの作成
						nnSelectionCustomShaderLayout("nnSelectionCustomShaderRowLayout");
					// ]] rowLayout
					setParent ..;

					text -height 10 -label " ";

					rowLayout
						-numberOfColumns	1
						-height				15
						-columnWidth1		400
						-columnAttach1		"left"
						-columnAlign1		"center"
						-columnOffset1		120
						-adjustableColumn1	true
						nnSelectionCustomShaderApplyButtonRowLayout;
						// Apply
						button
							-label		"Apply"
							-width		80
							-command	"nnApplyCustomShedar"
							nnSelectionCustomShaderApplyButton;
					// ]] rowLayout
					setParent ..;

					text -height 5 -label " ";
					separator -style "in";		//--------
					text -height 5 -label " ";

					text -height 10 -label " ";

					rowLayout
						-numberOfColumns	1
						-columnWidth1		200
						-height				200
						-adjustableColumn1	true
						nnMaterialShaderListHeadRowLayout;

						// Material Shader Listの作成
						nnMaterialShaderListLayout("nnMaterialShaderListHeadRowLayout");
					// ]] rowLayout
					setParent ..;

					text -height 5 -label " ";
					separator -style "in";		//--------
					text -height 5 -label " ";

					rowLayout
						-numberOfColumns	1
						-height				15
						-columnWidth1		400
						-columnAttach1		"left"
						-columnAlign1		"center"
						-columnOffset1		120
						-adjustableColumn1	true
						nnSelectionCustomShaderCloseButtonRowLayout;
						// Apply
						button
							-label		"Close"
							-width		80
							-command	"nnSelectionCustomShaderWinClose"
							nnSelectionCustomShaderCloseButton;
					// ]] rowLayout
					setParent ..;

				// ]] columnLayout
				setParent ..;
			// ]] frameLayout
			setParent ..;
		// ]] columnLayout
		setParent ..;

		// テンプレート使用の終了
		setUITemplate -popTemplate DefaultTemplate;

	// nnSetCustomShaderAttributeWin window表示
	showWindow nnSetCustomShaderAttributeWin;

}

global proc nnSetCustomShaderAttribute()
{
	global string $nnCustomShaderNameList[];
	global string $nnCustomShaderFXFileNameList[];
	global string $nnCustomShaderTechniqueNameList[];

	string $selectMatList[];
	int $shaderId = 1;

	// Custom Shader Attribute
	nnCustomShaderAttributeFileRead();

	// 設定ウィンドウの起動
	nnSetCustomShaderAttributeGUI();

	string	$token;
	print("Shader Name\n");
	for( $token in $nnCustomShaderNameList ) {
		print("\t"+$token+"\n");
	}
	print("Shader FX Name\n");
	for( $token in $nnCustomShaderFXFileNameList ) {
		print("\t"+$token+"\n");
	}
	print("Technique Name\n");
	for( $token in $nnCustomShaderTechniqueNameList ) {
		print("\t"+$token+"\n");
	}
}
// End of file
