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

	File:         NNS_ShowRenderPriority.mel
	Description:  show render priority
	Date:         2004/11/30
	Author:       T.Endo

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

/******************************************************************************
	show	
******************************************************************************/
global proc nnsShowRenderPriority_Show()
{

//-----------------------------------------------------------------------------
// get mode & value
	int $use_priority_mode =
		(`radioButtonGrp -q -sl nnsShowRenderPriority_RenderPriorityUse` == 1);
	int $compare_mode = `optionMenu -q -sl nnsShowRenderPriority_CompareMode` - 1;
	int $compare_value = `intField -q -v nnsShowRenderPriority_CompareValue`;

//-----------------------------------------------------------------------------
// clamp value
	if ($use_priority_mode) {
		if ($compare_value < 1)
			$compare_value = 1;
		else if ($compare_value > 255)
			$compare_value = 255;
		intField -e -v $compare_value nnsShowRenderPriority_CompareValue;
	}

//-----------------------------------------------------------------------------
// set enable state
	control -e -en $use_priority_mode nnsShowRenderPriority_CompareMode;
	control -e -en $use_priority_mode nnsShowRenderPriority_CompareValue;

//-----------------------------------------------------------------------------
// select
	int $sg_count = 0;
	select -cl;
	string $sgs[] = `ls -typ shadingEngine`;
	for ($sg in $sgs) {
		if ($sg == "initialParticleSE" || $sg == "swatchShadingGroup")
			continue;
		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;
		int $match_flag = 0;
		if ($use_priority_mode) {
			if (!$use_priority || $render_priority == 0)
				$match_flag = 0;
			else if ($compare_mode == 0)
				$match_flag = ($render_priority < $compare_value);
			else if ($compare_mode == 1)
				$match_flag = ($render_priority <= $compare_value);
			else if ($compare_mode == 2)
				$match_flag = ($render_priority == $compare_value);
			else if ($compare_mode == 3)
				$match_flag = ($render_priority >= $compare_value);
			else
				$match_flag = ($render_priority > $compare_value);
			//print ("show: " + $compare_mode + ": " + $render_priority + " " + $compare_value + ", " + $match_flag + "\n");
		}
		else {
			$match_flag = !$use_priority;
		}
		if ($match_flag) {
			select -add $sg;		// face
			select -add -ne $sg;	// shadingEngine
			++$sg_count;
		}
	}

	if ($sg_count == 0)
		print "Not found\n";
	else {
		string $msg = "Found: " + $sg_count + " shadingEngine node";
		if ($sg_count >= 2)
			$msg += "s";
		print ($msg + "\n");
	}
}

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

//-----------------------------------------------------------------------------
// backup selection
	string $sel_bak[] = `ls -sl`;
	string $sel_bak_str;
	for ($sel in $sel_bak)
		$sel_bak_str += ($sel + " ");
	select -cl;
	
//-----------------------------------------------------------------------------
// create window
	int $wd = 400;
	int $ht = 183;
	if (!`window -ex nnsShowRenderPriority_Win`) {
		string $close_cmd = "deleteUI nnsShowRenderPriority_Win";
		string $remain_cmd = "if (!`checkBoxGrp -q -v1 nnsShowRenderPriority_KeepSelecion`) {"
			+ "select -cl;select -ne " + $sel_bak_str + ";}";

		window -t "NITRO Show Render Priority" -wh $wd $ht -mxb 0
			nnsShowRenderPriority_Win;

		columnLayout -adj 1 -cat "both" 4 -cal "center" -rs 15;
			string $form = `formLayout -nd 400`;
				radioButtonGrp -l "Render Priority" -nrb 1 -cw2 148 80
					-l1 "Don't care"
					-on1 "nnsShowRenderPriority_Show"
					nnsShowRenderPriority_RenderPriorityFree;

				radioButtonGrp -l "" -nrb 1 -cw2 148 18
					-l1 ""
					-on1 "nnsShowRenderPriority_Show"
					-scl nnsShowRenderPriority_RenderPriorityFree
					nnsShowRenderPriority_RenderPriorityUse;
				optionMenu -w 100 -en 1
					-cc "nnsShowRenderPriority_Show"
					nnsShowRenderPriority_CompareMode;
					menuItem -l "<  : ( under )";
					menuItem -l "<= : ( below )";
					menuItem -l "=  : ( exactly )";
					menuItem -l ">= : ( above )";
					menuItem -l ">  : ( over )";
				intField -w 50 -h 26 -v 1 -en 1
					-cc "nnsShowRenderPriority_Show"
					nnsShowRenderPriority_CompareValue;
			setParent ..; // formLayout
			formLayout -e
				-af nnsShowRenderPriority_RenderPriorityFree "top" 8
				-af nnsShowRenderPriority_RenderPriorityFree "left" 0
				-an nnsShowRenderPriority_RenderPriorityFree "bottom"
				-an nnsShowRenderPriority_RenderPriorityFree "right"

				-ac nnsShowRenderPriority_RenderPriorityUse "top" 10 nnsShowRenderPriority_RenderPriorityFree
				-af nnsShowRenderPriority_RenderPriorityUse "left" 0
				-an nnsShowRenderPriority_RenderPriorityUse "bottom"
				-an nnsShowRenderPriority_RenderPriorityUse "right"

				-ac nnsShowRenderPriority_CompareMode "top" 4 nnsShowRenderPriority_RenderPriorityFree
				-ac nnsShowRenderPriority_CompareMode "left" 0 nnsShowRenderPriority_RenderPriorityUse
				-an nnsShowRenderPriority_CompareMode "bottom"
				-an nnsShowRenderPriority_CompareMode "right"

				-ac nnsShowRenderPriority_CompareValue "top" 4 nnsShowRenderPriority_RenderPriorityFree
				-ac nnsShowRenderPriority_CompareValue "left" 4 nnsShowRenderPriority_CompareMode
				-af nnsShowRenderPriority_CompareValue "bottom" 8
				-an nnsShowRenderPriority_CompareValue "right"
				$form;

			checkBoxGrp -ncb 1 -l "" -l1 "Keep Selection" -v1 1
				-cw 1 148
				nnsShowRenderPriority_KeepSelecion;

			button -h 26 -l "Close"
				-c ($remain_cmd + $close_cmd);
		setParent ..;	// columnLayout

		radioButtonGrp -e -sl 1 nnsShowRenderPriority_RenderPriorityUse;
		optionMenu -e -sl 3 nnsShowRenderPriority_CompareMode;
	}
	window -e -wh $wd $ht nnsShowRenderPriority_Win;

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

//-----------------------------------------------------------------------------
// show at opening window
	nnsShowRenderPriority_Show();

//-----------------------------------------------------------------------------
// show window
	showWindow nnsShowRenderPriority_Win;
}
