/******************************************************************************
	NINTENDO NITRO-System Maya Plugin

	File:         NNS_SetRenderPriority.mel
	Description:  set render priority
	Date:         2004/11/30
	Author:       T.Endo

	Copyright 2003-2006 Nintendo. All rights reserved.
******************************************************************************/

// UpdateWindow

/******************************************************************************
	variables
******************************************************************************/
global int $nnsSetRenderPriority_AttrJob_Use = -1;
global int $nnsSetRenderPriority_AttrJob_Value = -1;

/******************************************************************************
	get selected SG

		return SG size
******************************************************************************/
proc int GetSelectedSG(string $sgs[])
{
	$sgs = `ls -sl -typ shadingEngine`;
	if (size($sgs) == 0) {
		// get SG from selected material
		string $mats[] = `ls -sl -mat`;
		for ($mat in $mats) {
			string $connected_sgs[] = `listConnections -type shadingEngine $mat`;
			for ($sg in $connected_sgs) {
				if ($sg != "initialParticleSE" &&
					$sg != "swatchShadingGroup")
					$sgs[size($sgs)] = $sg;
			}
		}
	}
	return size($sgs);
}

/******************************************************************************
	get selection for set attr
******************************************************************************/
proc GetSelectionForSetAttr(string $sgs[])
{
	if (GetSelectedSG($sgs) == 0)
		error "No shadingEngine (material) node is selected";
}

/******************************************************************************
	add attribute use
******************************************************************************/
proc AddAttribute_Use(string $sg)
{
	addAttr -ln "nns_use_render_priority" -at "bool" -dv 0 $sg;
}

/******************************************************************************
	add attribute value
******************************************************************************/
proc AddAttribute_Value(string $sg)
{
	addAttr -ln "nns_render_priority" -at "short" -min 1 -max 255 -dv 1 $sg;
}

/******************************************************************************
	set attribute
******************************************************************************/
proc SetAttribute(int $use_priority, int $render_priority)
{

//-----------------------------------------------------------------------------
// get selection
	string $sgs[];
	GetSelectionForSetAttr($sgs);

//-----------------------------------------------------------------------------
// loop for sg
	for ($sg in $sgs) {
		if (!`attributeQuery -n $sg -ex "nns_use_render_priority"`)
			AddAttribute_Use($sg);
		setAttr ($sg + ".nns_use_render_priority") $use_priority;

		if (!`attributeQuery -n $sg -ex "nns_render_priority"`)
			AddAttribute_Value($sg);
		setAttr ($sg + ".nns_render_priority") $render_priority;
	}
}

/******************************************************************************
	delete attr change job
******************************************************************************/
proc DeleteAttrChangeJob()
{
	global int $nnsSetRenderPriority_AttrJob_Use;
	global int $nnsSetRenderPriority_AttrJob_Value;

	if (`window -ex nnsSetRenderPriority_Win`) {
		if ($nnsSetRenderPriority_AttrJob_Use != -1 &&
			`scriptJob -ex $nnsSetRenderPriority_AttrJob_Use`)
			scriptJob -k $nnsSetRenderPriority_AttrJob_Use;
		if ($nnsSetRenderPriority_AttrJob_Value != -1 &&
			`scriptJob -ex $nnsSetRenderPriority_AttrJob_Value`)
			scriptJob -k $nnsSetRenderPriority_AttrJob_Value;
	}

	$nnsSetRenderPriority_AttrJob_Use = -1;
	$nnsSetRenderPriority_AttrJob_Value = -1;
}

/******************************************************************************
	set attr change job
******************************************************************************/
proc SetAttrChangeJob(string $sg)
{
	global int $nnsSetRenderPriority_AttrJob_Use;
	global int $nnsSetRenderPriority_AttrJob_Value;

	DeleteAttrChangeJob();

	if (!`attributeQuery -n $sg -ex "nns_use_render_priority"`)
		AddAttribute_Use($sg);
	$nnsSetRenderPriority_AttrJob_Use = `scriptJob -kws -p nnsSetRenderPriority_Win
		-ac ($sg + ".nns_use_render_priority") "nnsSetRenderPriority_UpdateWindow(0)"`;

	if (!`attributeQuery -n $sg -ex "nns_render_priority"`)
		AddAttribute_Value($sg);
	$nnsSetRenderPriority_AttrJob_Value = `scriptJob -kws -p nnsSetRenderPriority_Win
		-ac ($sg + ".nns_render_priority") "nnsSetRenderPriority_UpdateWindow(0)"`;
}

/******************************************************************************
	render priority callback
******************************************************************************/
global proc nnsSetRenderPriority_RenderPriorityCB()
{

//-----------------------------------------------------------------------------
// get use priority
	int $use_priority =
		(`radioButtonGrp -q -sl nnsSetRenderPriority_RenderPriorityUse` == 1);

//-----------------------------------------------------------------------------
// clamp render priority in [1, 255]
	int $render_priority = `intField -q -v nnsSetRenderPriority_RenderPriorityValue`;
	if ($render_priority < 1)
		intField -e -v 1 nnsSetRenderPriority_RenderPriorityValue;
	else if ($render_priority > 255)
		intField -e -v 255 nnsSetRenderPriority_RenderPriorityValue;

	SetAttribute($use_priority,
		`intField -q -v nnsSetRenderPriority_RenderPriorityValue`);
}

/******************************************************************************
	update window
******************************************************************************/
global proc nnsSetRenderPriority_UpdateWindow(int $set_job_flag)
{

//-----------------------------------------------------------------------------
// get selection
	string $sgs[];
	int $sel_ok_flag = (GetSelectedSG($sgs) > 0);

//-----------------------------------------------------------------------------
// set sg name
	string $sgs_name;
	if ($sel_ok_flag) {
		$sgs_name = $sgs[0];
		int $sg_size = size($sgs);
		for ($isg = 1; $isg < $sg_size; ++$isg) {
			if ($isg == 3) {
				$sgs_name += ", ...";
				break;
			}
			$sgs_name += ",  " + $sgs[$isg];
		}
	}
	else {
		$sgs_name = "(No shadingEngine node is selected)";
	}
	text -e -l $sgs_name nnsSetRenderPriority_SGName;

//-----------------------------------------------------------------------------
// set current attr (enable state)
	if ($sel_ok_flag) {
		string $sg = $sgs[0];
		int $use_priority = (`attributeQuery -n $sg -ex "nns_use_render_priority"`) ?
			`getAttr ($sg + ".nns_use_render_priority")` : 0;
		int $render_priority = (`attributeQuery -n $sg -ex "nns_render_priority"`) ?
			`getAttr ($sg + ".nns_render_priority")` : 1;
		if ($use_priority)
			radioButtonGrp -e -sl 1 nnsSetRenderPriority_RenderPriorityUse;
		else
			radioButtonGrp -e -sl 1 nnsSetRenderPriority_RenderPriorityFree;
		intField -e -v $render_priority nnsSetRenderPriority_RenderPriorityValue;

		control -e -en 1 nnsSetRenderPriority_RenderPriorityFree;
		control -e -en 1 nnsSetRenderPriority_RenderPriorityUse;
		control -e -en $use_priority nnsSetRenderPriority_RenderPriorityValue;
		control -e -en 1 nnsSetRenderPriority_RenderPriorityDesc;

		//-----------------------------------------------------------------------------
		// set attr change job
		if ($set_job_flag)
			SetAttrChangeJob($sg);
	}
	else {
		control -e -en 0 nnsSetRenderPriority_RenderPriorityFree;
		control -e -en 0 nnsSetRenderPriority_RenderPriorityUse;
		control -e -en 0 nnsSetRenderPriority_RenderPriorityValue;
		control -e -en 0 nnsSetRenderPriority_RenderPriorityDesc;
	}
}

/******************************************************************************
	main
******************************************************************************/
global proc NNS_SetRenderPriority()
{

//-----------------------------------------------------------------------------
// get selection
	string $sgs[];
	if (GetSelectedSG($sgs) == 0)
		error "No shadingEngine (material) node is selected";

//-----------------------------------------------------------------------------
// create window
	int $wd = 400;
	int $ht = 190;
	if (!`window -ex nnsSetRenderPriority_Win`) {
		window -t "NITRO Set Render Priority" -wh $wd $ht -mxb 0
			nnsSetRenderPriority_Win;
		columnLayout -adj 1 -cat "both" 4 -cal "center" -rs 4;

		//-----------------------------------------------------------------------------
		// sg name
		frameLayout -lv 0 -cll 0 -cl 0 -bv 1 -bs "etchedIn";
			columnLayout -adj 1 -cal "center" -rs 8;
			
			text -l "" nnsSetRenderPriority_SGName;

			setParent ..; // columnLayout
		setParent ..; // formLayout

		//-----------------------------------------------------------------------------
		// attribute
		frameLayout -lv 0 -cll 0 -cl 0 -bv 1 -bs "etchedIn";
			string $form = `formLayout -nd 400`;
				radioButtonGrp -l "Render Priority" -nrb 1 -cw2 130 80
					-l1 "Don't care"
					-cc "nnsSetRenderPriority_RenderPriorityCB"
					nnsSetRenderPriority_RenderPriorityFree;

				radioButtonGrp -l "" -nrb 1 -cw2 130 18
					-l1 ""
					-cc "nnsSetRenderPriority_RenderPriorityCB"
					-scl nnsSetRenderPriority_RenderPriorityFree
					nnsSetRenderPriority_RenderPriorityUse;
				intField -w 50 -v 1
					-cc "nnsSetRenderPriority_RenderPriorityCB"
					nnsSetRenderPriority_RenderPriorityValue;

				text -l "1 (High Priority) - 255 (Low Priority)"
					nnsSetRenderPriority_RenderPriorityDesc;
			setParent ..; // formLayout
			formLayout -e
				-af nnsSetRenderPriority_RenderPriorityFree "top" 8
				-af nnsSetRenderPriority_RenderPriorityFree "left" 0
				-an nnsSetRenderPriority_RenderPriorityFree "bottom"
				-an nnsSetRenderPriority_RenderPriorityFree "right"

				-ac nnsSetRenderPriority_RenderPriorityUse "top" 8 nnsSetRenderPriority_RenderPriorityFree
				-af nnsSetRenderPriority_RenderPriorityUse "left" 0
				-an nnsSetRenderPriority_RenderPriorityUse "bottom"
				-an nnsSetRenderPriority_RenderPriorityUse "right"

				-ac nnsSetRenderPriority_RenderPriorityValue "top" 4 nnsSetRenderPriority_RenderPriorityFree
				-ac nnsSetRenderPriority_RenderPriorityValue "left" 0 nnsSetRenderPriority_RenderPriorityUse
				-an nnsSetRenderPriority_RenderPriorityValue "bottom"
				-an nnsSetRenderPriority_RenderPriorityValue "right"

				-ac nnsSetRenderPriority_RenderPriorityDesc "top" 4 nnsSetRenderPriority_RenderPriorityValue
				-ac nnsSetRenderPriority_RenderPriorityDesc "left" 4 nnsSetRenderPriority_RenderPriorityUse
				-af nnsSetRenderPriority_RenderPriorityDesc "bottom" 8
				-an nnsSetRenderPriority_RenderPriorityDesc "right"
				$form;
		setParent ..; // frameLayout

		//-----------------------------------------------------------------------------
		// close button
		string $close_cmd = "deleteUI nnsSetRenderPriority_Win";
		string $form = `formLayout -nd 100`;
			string $btn_close       = `button -h 26 -l "Close" -c $close_cmd`;
			formLayout -e
				-af $btn_close "top" 0
				-af $btn_close "left" 0
				-af $btn_close "bottom" 0
				-af $btn_close "right" 0
				$form;
		setParent ..; // formLayout

		setParent ..; // columnLayout

		//-----------------------------------------------------------------------------
		// set window delete job
		scriptJob -uid nnsSetRenderPriority_Win
			("$nnsSetRenderPriority_AttrJob_Use = -1;" +
			 "$nnsSetRenderPriority_AttrJob_Value = -1;");

		//-----------------------------------------------------------------------------
		// set selection change job
		scriptJob -p nnsSetRenderPriority_Win
			-e "SelectionChanged" "nnsSetRenderPriority_UpdateWindow(1)";
	}
	window -e -wh $wd $ht nnsSetRenderPriority_Win;

//-----------------------------------------------------------------------------
// update window
	nnsSetRenderPriority_UpdateWindow(1);

//-----------------------------------------------------------------------------
// set focus to value field
	if (`control -q -en nnsSetRenderPriority_RenderPriorityValue`)
		setFocus nnsSetRenderPriority_RenderPriorityValue;

//-----------------------------------------------------------------------------
// show window
	showWindow nnsSetRenderPriority_Win;

}
