1/******************************************************************************
2	NintendoWare for CTR Maya Plugin
3
4	File:         NW4C_SetRenderPriority.mel
5	Description:  set render priority
6	Date:         2010/02/12
7	Author:       Takashi Endo
8
9	Copyright (C)2009-2010 Nintendo Co., Ltd. / HAL Laboratory, Inc. All rights reserved.
10******************************************************************************/
11
12// UpdateWindow DoSearch
13// UpdateList SortList ListButtonCB ListDropCB SetListColor
14
15/******************************************************************************
16	variables
17******************************************************************************/
18global int $nw4cSetRenderPriority_SortKind  = 0; // 0:priority, 1:name
19global int $nw4cSetRenderPriority_SortOrder = 0; // 0:ascend, 1:descend
20global int $nw4cSetRenderPriority_KeepOrder = 0;
21
22/******************************************************************************
23	get selected SG
24
25		return SG size
26******************************************************************************/
27proc int GetSelectedSG(string $sgs[])
28{
29	$sgs = `ls -sl -typ shadingEngine`;
30	if (size($sgs) == 0)
31	{
32		// get SG from selected material
33		string $mats[] = `ls -sl -mat`;
34		for ($mat in $mats)
35		{
36			if (!`attributeQuery -n $mat -ex "oc"`)
37			{
38				continue;
39			}
40			string $connectedSgs[] =
41				`listConnections -s 0 -d 1 -type shadingEngine ($mat + ".oc")`;
42			for ($sg in $connectedSgs)
43			{
44				if ($sg != "initialParticleSE" &&
45					$sg != "swatchShadingGroup")
46				{
47					$sgs[size($sgs)] = $sg;
48				}
49			}
50		}
51	}
52	return size($sgs);
53}
54
55/******************************************************************************
56	use render priority
57******************************************************************************/
58global proc int nw4cSetRenderPriority_Get_UseRenderPriority(string $node)
59{
60	return (`attributeQuery -n $node -ex "nw4cUseRenderPriority"`) ?
61		`getAttr ($node + ".nw4cUseRenderPriority")` : 0;
62}
63
64global proc nw4cSetRenderPriority_Add_UseRenderPriority(string $node)
65{
66	if (!`attributeQuery -n $node -ex "nw4cUseRenderPriority"`)
67	{
68		addAttr -ln "nw4cUseRenderPriority" -at "bool" -dv 0 -h 1 $node;
69	}
70}
71
72global proc nw4cSetRenderPriority_Set_UseRenderPriority(string $node, int $val)
73{
74	nw4cSetRenderPriority_Add_UseRenderPriority($node);
75	setAttr ($node + ".nw4cUseRenderPriority") $val;
76}
77
78proc SetJob_UseRenderPriority(string $node)
79{
80	if (!`attributeQuery -n $node -ex "nw4cUseRenderPriority"`)
81	{
82		return;
83	}
84	scriptJob -p nw4cSetRenderPriority_UseRenderPriority -rp -ac ($node + ".nw4cUseRenderPriority")
85		("if (`nw4cSetRenderPriority_Get_UseRenderPriority " + $node +
86		 "`) radioButtonGrp -e -sl 1 nw4cSetRenderPriority_UseRenderPriority;" +
87		 "else radioButtonGrp -e -sl 1 nw4cSetRenderPriority_DontCare;" +
88		 "control -e -en `nw4cSetRenderPriority_Get_UseRenderPriority " + $node + "` nw4cSetRenderPriority_RenderPriority;");
89}
90
91global proc nw4cSetRenderPriority_UseRenderPriorityCB(string $mainNode)
92{
93	string $sgs[];
94	GetSelectedSG($sgs);
95
96	int $val = `radioButtonGrp -q -sl nw4cSetRenderPriority_UseRenderPriority`;
97	control -e -en $val nw4cSetRenderPriority_RenderPriority;
98
99	for ($node in $sgs)
100	{
101		nw4cSetRenderPriority_Set_UseRenderPriority($node, $val);
102	}
103
104	SetJob_UseRenderPriority($mainNode);
105}
106
107/******************************************************************************
108	render priority
109******************************************************************************/
110global proc int nw4cSetRenderPriority_Get_RenderPriority(string $node)
111{
112	return (`attributeQuery -n $node -ex "nw4cRenderPriority"`) ?
113		`getAttr ($node + ".nw4cRenderPriority")` : 1;
114}
115
116global proc nw4cSetRenderPriority_Add_RenderPriority(string $node)
117{
118	if (!`attributeQuery -n $node -ex "nw4cRenderPriority"`)
119	{
120		addAttr -ln "nw4cRenderPriority" -at "short" -min 1 -max 255 -dv 1 -h 1 $node;
121	}
122}
123
124global proc nw4cSetRenderPriority_Set_RenderPriority(string $node, int $val)
125{
126	nw4cSetRenderPriority_Add_RenderPriority($node);
127	setAttr ($node + ".nw4cRenderPriority") $val;
128}
129
130proc SetJob_RenderPriority(string $node)
131{
132	if (!`attributeQuery -n $node -ex "nw4cRenderPriority"`)
133	{
134		return;
135	}
136	scriptJob -p nw4cSetRenderPriority_RenderPriority -rp -ac ($node + ".nw4cRenderPriority")
137		("intField -e -v `nw4cSetRenderPriority_Get_RenderPriority "
138			+ $node + "` nw4cSetRenderPriority_RenderPriority");
139}
140
141global proc nw4cSetRenderPriority_RenderPriorityCB(string $mainNode)
142{
143	string $sgs[];
144	GetSelectedSG($sgs);
145
146	int $val = `intField -q -v nw4cSetRenderPriority_RenderPriority`;
147	if ($val < 1)
148	{
149		$val = 1;
150		intField -e -v $val nw4cSetRenderPriority_RenderPriority;
151	}
152	else if ($val > 255)
153	{
154		$val = 255;
155		intField -e -v $val nw4cSetRenderPriority_RenderPriority;
156	}
157
158	for ($node in $sgs)
159	{
160		nw4cSetRenderPriority_Set_RenderPriority($node, $val);
161	}
162
163	SetJob_RenderPriority($mainNode);
164}
165
166/******************************************************************************
167	get priority for list
168******************************************************************************/
169proc int GetPriorityForList(string $sg)
170{
171	int $priority = 0;
172	if (objExists($sg))
173	{
174		if (nw4cSetRenderPriority_Get_UseRenderPriority($sg))
175		{
176			$priority = nw4cSetRenderPriority_Get_RenderPriority($sg);
177		}
178	}
179	return $priority;
180}
181
182/******************************************************************************
183	set priority for list
184******************************************************************************/
185proc SetPriorityForList(string $sg, int $val)
186{
187	if (objExists($sg))
188	{
189		nw4cSetRenderPriority_Set_UseRenderPriority($sg, ($val != 0));
190		if ($val != 0)
191		{
192			nw4cSetRenderPriority_Set_RenderPriority($sg, $val);
193		}
194	}
195}
196
197/******************************************************************************
198	find list field for button
199******************************************************************************/
200proc string FindListFieldForButton(string $childs[], string $btn)
201{
202	string $btnS = match("[^|]+$", $btn);
203	int $sgSize = size($childs) / 2;
204	int $isg;
205	for ($isg = 0; $isg < $sgSize; ++$isg)
206	{
207		string $child = $childs[$isg];
208		if ($child == $btn || $child == $btnS)
209		{
210			return $childs[$sgSize + $isg];
211		}
212	}
213	return "";
214}
215
216/******************************************************************************
217	get SG from list label
218******************************************************************************/
219proc string GetSGFromListLabel(string $label)
220{
221	string $mat = match("\\(.*\\)", $label);
222	$mat = substitute("^\\( ", $mat, "");
223	$mat = substitute(" \\)$", $mat, "");
224	//print("[" + $mat + "] " + $label + "\n");
225	return $mat;
226}
227
228/******************************************************************************
229	set list color
230******************************************************************************/
231proc SetListColor()
232{
233	//return;
234	string $childs[] = `layout -q -ca nw4cSetRenderPriority_SGList`;
235	int $sgSize = size($childs) / 2;
236	int $isg;
237	for ($isg = 0; $isg < $sgSize; ++$isg)
238	{
239		string $btn = $childs[$isg];
240		string $label = `button -q -l $btn`;
241		//string $sg = match("[^ ]+", $label); // sgName ( matName )
242		string $sg = GetSGFromListLabel($label); // matName ( sgName )
243		int $selFlag = false;
244		if (size(`ls -sl $sg`))
245		{
246			$selFlag = true;
247		}
248		else if (objExists($sg))
249		{
250			string $ins[] = `listConnections -s 1 -d 0 ($sg + ".ss")`;
251			if (size($ins) && size(`ls -sl $ins[0]`))
252			{
253				string $mat = $ins[0];
254				if (`attributeQuery -n $mat -ex "oc"`)
255				{
256					string $ots[] = `listConnections -s 0 -d 1 ($mat + ".oc")`;
257					if (size($ots) && size(`ls -sl $ots`) == 0)
258					{
259						$selFlag = true;
260					}
261				}
262			}
263		}
264		if ($selFlag)
265		{
266			button -e -bgc 0.9 0.9 0.0 $btn;
267		}
268		else
269		{
270			button -e -bgc 1 1 1 $btn;
271		}
272		control -e -en false $btn; // for update bgc
273		control -e -en true  $btn;
274	}
275}
276
277/******************************************************************************
278	list button CB
279******************************************************************************/
280global proc nw4cSetRenderPriority_ListButtonCB(string $btn, string $sg)
281{
282	int $mod = `getModifiers`;
283	int $shift = ($mod == 1 || $mod == 5 || $mod == 9);
284
285	//-----------------------------------------------------------------------------
286	// select SG & material & polygon
287	if (!$shift)
288	{
289		select -cl;
290	}
291	if (objExists($sg))
292	{
293		select -add -ne $sg; // shadingEngine
294		string $ins[] = `listConnections -s 1 -d 0 ($sg + ".ss")`;
295		if (size($ins))
296		{
297			select -add $ins[0]; // material
298		}
299		select -add $sg; // shape object & face
300
301		// frame objects
302		if (`checkBox -q -v nw4cSetRenderPriority_FrameObjects`)
303		{
304			if (size(`sets -q $sg`))
305			{
306				fitAllPanels -selected;
307			}
308		}
309	}
310
311	//-----------------------------------------------------------------------------
312	// set list color
313	SetListColor();
314
315	//-----------------------------------------------------------------------------
316	// set focus
317	//setFocus $btn;
318	string $childs[] = `layout -q -ca nw4cSetRenderPriority_SGList`;
319	string $field = FindListFieldForButton($childs, $btn);
320	if (size($field))
321	{
322		setFocus $field;
323	}
324}
325
326/******************************************************************************
327	list priority CB
328******************************************************************************/
329global proc nw4cSetRenderPriority_ListPriorityCB(string $sg, int $val)
330{
331	global int $nw4cSetRenderPriority_SortKind;
332	global int $nw4cSetRenderPriority_KeepOrder;
333
334	SetPriorityForList($sg, $val);
335
336	// keep priority sort order
337	if ($nw4cSetRenderPriority_SortKind == 0)
338	{
339		$nw4cSetRenderPriority_KeepOrder = 1;
340	}
341}
342
343/******************************************************************************
344	list drag CB
345******************************************************************************/
346global proc string[] nw4cSetRenderPriority_ListDragCB(
347	string $dragControl, int $x, int $y, int $mods)
348{
349	string $msgs[];
350	//trace ("drag: " + match("[^|]+$", $dragControl) + ": " + $x + ", " + $y + ": " + $mods);
351	$msgs[0] = "dummy"; // set something to enable drop CB
352	return $msgs;
353}
354
355/******************************************************************************
356	list drop CB
357******************************************************************************/
358global proc nw4cSetRenderPriority_ListDropCB(
359	string $dragControl, string $dropControl, string $msgs[],
360	int $x, int $y, int $type) // type is always 2 ?
361{
362	global int $nw4cSetRenderPriority_KeepOrder;
363
364	//-----------------------------------------------------------------------------
365	// check control
366	if ($dragControl == $dropControl)
367	{
368		return;
369	}
370	//trace ("drop: " + match("[^|]+$", $dragControl) + " -> " + match("[^|]+$", $dropControl) + ": " + $x + ", " + $y + ": " + $type + ": " + size($msgs));
371
372	//-----------------------------------------------------------------------------
373	// swap priority
374	string $name0 = `button -q -l $dragControl`;
375	string $name1 = `button -q -l $dropControl`;
376	//string $sg0 = match("[^ ]+", $name0); // sgName ( matName )
377	//string $sg1 = match("[^ ]+", $name1); // sgName ( matName )
378	string $sg0 = GetSGFromListLabel($name0); // matName ( sgName )
379	string $sg1 = GetSGFromListLabel($name1); // matName ( sgName )
380	string $pri0 = GetPriorityForList($sg0);
381	string $pri1 = GetPriorityForList($sg1);
382	SetPriorityForList($sg0, $pri1);
383	SetPriorityForList($sg1, $pri0);
384
385	//-----------------------------------------------------------------------------
386	// swap button name & command
387	button -e -l $name1
388		-c ("nw4cSetRenderPriority_ListButtonCB " + $dragControl + " " + $sg1)
389		$dragControl;
390	button -e -l $name0
391		-c ("nw4cSetRenderPriority_ListButtonCB " + $dropControl + " " + $sg0)
392		$dropControl;
393
394	//-----------------------------------------------------------------------------
395	// swap field command
396	// [notice] can't query intField changeCommand
397	string $childs[] = `layout -q -ca nw4cSetRenderPriority_SGList`;
398	string $field0 = FindListFieldForButton($childs, $dragControl);
399	string $field1 = FindListFieldForButton($childs, $dropControl);
400	//trace ("drop: " + $sg0 + " -> " + $sg1 + " (" + $field0 + " -> " + $field1 + ")");
401	if (size($field0) && size($field1))
402	{
403		string $parentBak = `setParent -q`;
404		setParent nw4cSetRenderPriority_SGList;
405		intField -e -cc ("nw4cSetRenderPriority_ListPriorityCB " + $sg1 + " #1")
406			$field0;
407		intField -e -cc ("nw4cSetRenderPriority_ListPriorityCB " + $sg0 + " #1")
408			$field1;
409		setParent $parentBak;
410	}
411
412	//-----------------------------------------------------------------------------
413	// keep sort order
414	$nw4cSetRenderPriority_KeepOrder = 1;
415
416	//-----------------------------------------------------------------------------
417	// set list color
418	SetListColor();
419}
420
421/******************************************************************************
422	sort list
423******************************************************************************/
424proc SortList(string $sgs[], string $mats[], int $pris[])
425{
426	global int $nw4cSetRenderPriority_SortKind;
427	global int $nw4cSetRenderPriority_SortOrder;
428
429	int $sgSize = size($sgs);
430	for ($isg = 0; $isg < $sgSize - 1; ++$isg)
431	{
432		for ($jsg = $isg + 1; $jsg < $sgSize; ++$jsg)
433		{
434			string $sg0  = $sgs[$isg];
435			string $mat0 = $mats[$isg];
436			int $pri0    = $pris[$isg];
437			string $sg1  = $sgs[$jsg];
438			string $mat1 = $mats[$jsg];
439			int $pri1    = $pris[$jsg];
440			int $cond = 0;
441			if ($nw4cSetRenderPriority_SortKind == 0)	// priority
442			{
443				if ($pri0 == $pri1)
444				{
445					$cond = (strcmp($mat0, $mat1) > 0);
446				}
447				else if ($nw4cSetRenderPriority_SortOrder == 0)
448				{
449					$cond = ($pri0 > $pri1);
450				}
451				else
452				{
453					$cond = ($pri0 < $pri1);
454				}
455			}
456			else										// name
457			{
458				if ($nw4cSetRenderPriority_SortOrder == 0)
459				{
460					$cond = (strcmp($mat0, $mat1) > 0);
461				}
462				else
463				{
464					$cond = (strcmp($mat0, $mat1) < 0);
465				}
466			}
467			if ($cond)
468			{
469				$sgs[$isg]  = $sg1;
470				$mats[$isg] = $mat1;
471				$pris[$isg] = $pri1;
472				$sgs[$jsg]  = $sg0;
473				$mats[$jsg] = $mat0;
474				$pris[$jsg] = $pri0;
475			}
476		}
477	}
478}
479
480/******************************************************************************
481	update sort icon
482******************************************************************************/
483proc UpdateSortIcon()
484{
485	global int $nw4cSetRenderPriority_SortKind;
486	global int $nw4cSetRenderPriority_SortOrder;
487
488	string $img = ($nw4cSetRenderPriority_SortOrder) ?
489		"arrowDown.xpm" : "arrowUp.xpm";
490
491	iconTextStaticLabel -e -vis ($nw4cSetRenderPriority_SortKind == 1)
492		-i1 $img
493		nw4cSetRenderPriority_NameSortIcon;
494	iconTextStaticLabel -e -vis ($nw4cSetRenderPriority_SortKind == 0)
495		-i1 $img
496		nw4cSetRenderPriority_PrioritySortIcon;
497}
498
499/******************************************************************************
500	is SG size changed
501******************************************************************************/
502proc int IsSGSizeChanged()
503{
504	int $sgSize = 0;
505	string $allSgs[] = `ls -typ shadingEngine`;
506	for ($sg in $allSgs)
507	{
508		if ($sg == "initialParticleSE")
509		{
510			continue;
511		}
512		string $ins[] = `listConnections -s 1 -d 0 ($sg + ".ss")`;
513		if (!size($ins))
514		{
515			continue;
516		}
517		++$sgSize;
518	}
519	string $childs[] = `layout -q -ca nw4cSetRenderPriority_SGList`;
520	return ((size($childs) / 2) != $sgSize);
521}
522
523/******************************************************************************
524	update list
525******************************************************************************/
526proc UpdateList()
527{
528	//trace "update list";
529
530	//-----------------------------------------------------------------------------
531	// create list
532	string $sgs[], $mats[];
533	int $pris[];
534	int $isg = 0;
535	string $allSgs[] = `ls -typ shadingEngine`;
536	for ($sg in $allSgs)
537	{
538		if ($sg == "initialParticleSE")
539		{
540			continue;
541		}
542		string $ins[] = `listConnections -s 1 -d 0 ($sg + ".ss")`;
543		if (!size($ins))
544		{
545			continue;
546		}
547		string $mat = $ins[0];
548		$sgs[$isg]  = $sg;
549		$mats[$isg] = $mat;
550		$pris[$isg] = GetPriorityForList($sg);
551		++$isg;
552	}
553
554	//-----------------------------------------------------------------------------
555	// sort list
556	UpdateSortIcon();
557	SortList($sgs, $mats, $pris);
558
559	//-----------------------------------------------------------------------------
560	// resize UI
561	int $sgSize = size($sgs);
562	string $parentBak = `setParent -q`;
563	setParent nw4cSetRenderPriority_SGList;
564	string $childs[] = `layout -q -ca nw4cSetRenderPriority_SGList`;
565	int $rowSize = size($childs) / 2;
566	if ($rowSize != $sgSize)
567	{
568		if (size($childs))
569		{
570			deleteUI $childs;
571		}
572		for ($isg = 0; $isg < $sgSize; ++$isg)
573		{
574			button -w 220 -h 22 -al "left" -rs 0 -bgc 1 1 1; // -rs 0 fix width
575		}
576		for ($isg = 0; $isg < $sgSize; ++$isg)
577		{
578			intField -w 66 -h 22 -min 0 -max 255;
579		}
580		$childs = `layout -q -ca nw4cSetRenderPriority_SGList`;
581		string $lastBtn;
582		for ($isg = 0; $isg < $sgSize; ++$isg)
583		{
584			string $btn = $childs[$isg];
585			string $field  = $childs[$sgSize + $isg];
586			if ($isg == 0)
587			{
588				formLayout -e
589					-af $btn "top" 4
590					-af $btn "left" 4
591					-an $btn "bottom"
592					-ap $btn "right" 0 79
593					-af $field "top" 4
594					-ac $field "left" 4 $btn
595					-an $field "bottom"
596					-an $field "right"
597					nw4cSetRenderPriority_SGList;
598			}
599			else
600			{
601				formLayout -e
602					-ac $btn "top" 2 $lastBtn
603					-af $btn "left" 4
604					-an $btn "bottom"
605					-ap $btn "right" 0 79
606					-ac $field "top" 2 $lastBtn
607					-ac $field "left" 4 $btn
608					-an $field "bottom"
609					-an $field "right"
610					nw4cSetRenderPriority_SGList;
611			}
612			$lastBtn = $btn;
613		}
614		formLayout -e -w 299 nw4cSetRenderPriority_SGList;
615
616		// update button visibility to display button (for -rs 0)
617		for ($isg = 0; $isg < $sgSize; ++$isg)
618		{
619			string $btn = $childs[$isg];
620			control -e -vis false $btn;
621			control -e -vis true  $btn;
622		}
623	}
624
625	//-----------------------------------------------------------------------------
626	// set UI
627	$isg = 0;
628	for ($sg in $sgs)
629	{
630		//trace ("ui: " + $isg + " " + $childs[$isg] + " " + $childs[$sgSize + $isg]);
631		string $btn = $childs[$isg];
632		button -e -l ($mats[$isg] + " ( " + $sg + " ) ")
633			-c ("nw4cSetRenderPriority_ListButtonCB " + $btn + " " + $sg)
634			-dgc ("nw4cSetRenderPriority_ListDragCB")
635			-dpc ("nw4cSetRenderPriority_ListDropCB")
636			$btn;
637		intField -e -v $pris[$isg]
638			-cc ("nw4cSetRenderPriority_ListPriorityCB " + $sg + " #1")
639			$childs[$sgSize + $isg];
640		++$isg;
641	}
642	setParent $parentBak;
643	SetListColor();
644}
645
646/******************************************************************************
647	list priority sort CB
648******************************************************************************/
649global proc nw4cSetRenderPriority_ListPrioritySortCB()
650{
651	global int $nw4cSetRenderPriority_SortKind;
652	global int $nw4cSetRenderPriority_SortOrder;
653	global int $nw4cSetRenderPriority_KeepOrder;
654
655	if ($nw4cSetRenderPriority_KeepOrder)
656	{
657		$nw4cSetRenderPriority_KeepOrder = 0;
658	}
659	else if ($nw4cSetRenderPriority_SortKind == 0)
660	{
661		$nw4cSetRenderPriority_SortOrder = !$nw4cSetRenderPriority_SortOrder;
662	}
663	else
664	{
665		$nw4cSetRenderPriority_SortOrder = 0;
666	}
667	$nw4cSetRenderPriority_SortKind = 0;
668
669	UpdateList();
670}
671
672/******************************************************************************
673	list name sort CB
674******************************************************************************/
675global proc nw4cSetRenderPriority_ListNameSortCB()
676{
677	global int $nw4cSetRenderPriority_SortKind;
678	global int $nw4cSetRenderPriority_SortOrder;
679	global int $nw4cSetRenderPriority_KeepOrder;
680
681	if ($nw4cSetRenderPriority_KeepOrder)
682	{
683		$nw4cSetRenderPriority_KeepOrder = 0;
684	}
685	else if ($nw4cSetRenderPriority_SortKind == 1)
686	{
687		$nw4cSetRenderPriority_SortOrder = !$nw4cSetRenderPriority_SortOrder;
688	}
689	else
690	{
691		$nw4cSetRenderPriority_SortOrder = 0;
692	}
693	$nw4cSetRenderPriority_SortKind = 1;
694
695	UpdateList();
696}
697
698/******************************************************************************
699	get SG name with material
700******************************************************************************/
701proc string GetSGNameWithMat(string $sg)
702{
703	string $name = "( " + $sg + " )";
704	string $ins[] = `listConnections -s 1 -d 0 ($sg + ".ss")`;
705	if (size($ins))
706	{
707		$name = $ins[0] + " " + $name;
708	}
709	return $name;
710}
711
712/******************************************************************************
713	update window
714******************************************************************************/
715global proc nw4cSetRenderPriority_UpdateWindow()
716{
717	//-----------------------------------------------------------------------------
718	// get selection
719	string $sgs[];
720	int $enableFlag = (GetSelectedSG($sgs) > 0);
721
722	//-----------------------------------------------------------------------------
723	// set node name
724	string $nodesName;
725	if ($enableFlag)
726	{
727		$nodesName = GetSGNameWithMat($sgs[0]);
728		int $nodeSize = size($sgs);
729		for ($inode = 1; $inode < $nodeSize; ++$inode)
730		{
731			if ($inode == 3)
732			{
733				$nodesName += ", ... (" + $nodeSize + ")";
734				break;
735			}
736			$nodesName += ",  " + GetSGNameWithMat($sgs[$inode]);
737		}
738	}
739	else
740	{
741		$nodesName = "(None)";
742	}
743	text -e -l $nodesName nw4cSetRenderPriority_NodeName;
744
745	//-----------------------------------------------------------------------------
746	// set current attr & change command
747	int $useRenderPriority = 0;
748	if ($enableFlag)
749	{
750		string $node = $sgs[0];
751
752		$useRenderPriority =
753			`nw4cSetRenderPriority_Get_UseRenderPriority $node`;
754		radioButtonGrp -e -sl (!$useRenderPriority)
755			-on1 ("nw4cSetRenderPriority_UseRenderPriorityCB " + $node)
756			nw4cSetRenderPriority_DontCare;
757		radioButtonGrp -e -sl $useRenderPriority
758			-on1 ("nw4cSetRenderPriority_UseRenderPriorityCB " + $node)
759			nw4cSetRenderPriority_UseRenderPriority;
760
761		intField -e -v `nw4cSetRenderPriority_Get_RenderPriority $node`
762			-cc ("nw4cSetRenderPriority_RenderPriorityCB " + $node)
763			nw4cSetRenderPriority_RenderPriority;
764
765		// setAttr �ȂǂŕύX���ꂽ�ꍇ�ɁA
766		// �R���g���[���̒l���X�V���邽��
767		SetJob_UseRenderPriority($node);
768		SetJob_RenderPriority($node);
769	}
770
771	//-----------------------------------------------------------------------------
772	// set enable state
773	control -e -en $enableFlag nw4cSetRenderPriority_DontCare;
774	control -e -en $enableFlag nw4cSetRenderPriority_UseRenderPriority;
775	control -e -en ($enableFlag && $useRenderPriority) nw4cSetRenderPriority_RenderPriority;
776	control -e -en $enableFlag nw4cSetRenderPriority_Description;
777
778	//-----------------------------------------------------------------------------
779	// update list
780	if (`tabLayout -q -sti nw4cSetRenderPriority_Mode` == 2)
781	{
782		if (IsSGSizeChanged())
783		{
784			//trace "SG size changed";
785			UpdateList();
786		}
787		else
788		{
789			SetListColor();
790		}
791	}
792}
793
794/******************************************************************************
795	search mode callback
796******************************************************************************/
797global proc nw4cSetRenderPriority_SearchModeCB()
798{
799	int $dontCare =
800		(`radioButtonGrp -q -sl nw4cSetRenderPriority_SearchDontCare` == 1);
801	int $useRenderPriority =
802		(`radioButtonGrp -q -sl nw4cSetRenderPriority_SearchUseRenderPriority` == 1);
803
804	control -e -en $useRenderPriority nw4cSetRenderPriority_SearchCompare;
805	control -e -en $useRenderPriority nw4cSetRenderPriority_SearchRenderPriority;
806	control -e -en ($dontCare || $useRenderPriority) nw4cSetRenderPriority_SearchBtn;
807}
808
809/******************************************************************************
810	search value callback
811******************************************************************************/
812global proc nw4cSetRenderPriority_SearchValueCB()
813{
814	int $val = `intField -q -v nw4cSetRenderPriority_SearchRenderPriority`;
815	if ($val < 1)
816	{
817		intField -e -v 1 nw4cSetRenderPriority_SearchRenderPriority;
818	}
819	else if ($val > 255)
820	{
821		intField -e -v 255 nw4cSetRenderPriority_SearchRenderPriority;
822	}
823}
824
825/******************************************************************************
826	do search
827******************************************************************************/
828global proc nw4cSetRenderPriority_DoSearch()
829{
830	int $searchUse =
831		(`radioButtonGrp -q -sl nw4cSetRenderPriority_SearchUseRenderPriority` == 1);
832	int $compare = `optionMenu -q -sl nw4cSetRenderPriority_SearchCompare` - 1;
833	nw4cSetRenderPriority_SearchValueCB(); // clamp value
834	int $searchVal = `intField -q -v nw4cSetRenderPriority_SearchRenderPriority`;
835
836	int $nodeCount = 0;
837	select -cl;
838	string $sgs[] = `ls -typ shadingEngine`;
839	for ($node in $sgs)
840	{
841		if ($node == "initialParticleSE" || $node == "swatchShadingGroup")
842		{
843			continue;
844		}
845		int $useRenderPriority = nw4cSetRenderPriority_Get_UseRenderPriority($node);
846		int $renderPriority = nw4cSetRenderPriority_Get_RenderPriority($node);
847		int $match = 0;
848		if (!$searchUse)
849		{
850			$match = !$useRenderPriority;
851		}
852		else
853		{
854			if (!$useRenderPriority)
855			{
856				$match = 0;
857			}
858			else if ($compare == 0)
859			{
860				$match = ($renderPriority < $searchVal);
861			}
862			else if ($compare == 1)
863			{
864				$match = ($renderPriority <= $searchVal);
865			}
866			else if ($compare == 2)
867			{
868				$match = ($renderPriority == $searchVal);
869			}
870			else if ($compare == 3)
871			{
872				$match = ($renderPriority >= $searchVal);
873			}
874			else
875			{
876				$match = ($renderPriority > $searchVal);
877			}
878		}
879		if ($match)
880		{
881			select -add -ne $node; // shadingEngine
882			string $ins[] = `listConnections -s 1 -d 0 ($node + ".ss")`;
883			if (size($ins))
884			{
885				select -add $ins[0]; // material
886			}
887			select -add $node; // shape object & face
888			++$nodeCount;
889		}
890	}
891
892	if ($nodeCount == 0)
893	{
894		print "Not found\n";
895	}
896	else
897	{
898		string $msg = "Found: " + $nodeCount + " shadingEngine node";
899		if ($nodeCount >= 2)
900		{
901			$msg += "s";
902		}
903		print ($msg + "\n");
904	}
905}
906
907/******************************************************************************
908	tab change CB
909******************************************************************************/
910global proc nw4cSetRenderPriority_TabChangeCB()
911{
912	if (`tabLayout -q -sti nw4cSetRenderPriority_Mode` == 2)
913	{
914		UpdateList();
915	}
916	else
917	{
918		if (`control -q -en nw4cSetRenderPriority_RenderPriority`)
919		{
920			setFocus nw4cSetRenderPriority_RenderPriority;
921		}
922	}
923}
924
925/******************************************************************************
926	undo CB
927******************************************************************************/
928global proc nw4cSetRenderPriority_UndoCB()
929{
930	if (`tabLayout -q -sti nw4cSetRenderPriority_Mode` == 2)
931	{
932		UpdateList();
933	}
934}
935
936/******************************************************************************
937	main
938******************************************************************************/
939global proc NW4C_SetRenderPriority()
940{
941	global int $nw4cSetRenderPriority_SortKind;
942	global int $nw4cSetRenderPriority_SortOrder;
943	global int $nw4cSetRenderPriority_KeepOrder;
944
945	int $afterMaya2011Adjust = 0;
946	if (getApplicationVersionAsFloat() >= 2011)
947		$afterMaya2011Adjust = 1;
948
949	//-----------------------------------------------------------------------------
950	// create window
951	int $wd = 400;
952	int $ht = 380;
953	if (!`window -ex nw4cSetRenderPriority_Win`)
954	{
955		window -t "NW4C Set Render Priority" -wh $wd $ht -mxb 0 -mb 1
956			nw4cSetRenderPriority_Win;
957		menu -l "Help";
958		menuItem -l ("Help on " + `window -q -t nw4cSetRenderPriority_Win` + "...")
959			-c "NW4C_ShowHelp \"html/NW4C_SetRenderPriority.html\" \"\"";
960
961		string $form1 = `formLayout -nd 100`;
962
963		//-----------------------------------------------------------------------------
964		// tab
965		tabLayout -imh 0
966			-cc "nw4cSetRenderPriority_TabChangeCB" // reload at change
967			nw4cSetRenderPriority_Mode;
968
969			//-----------------------------------------------------------------------------
970			// set & search
971			//-----------------------------------------------------------------------------
972			columnLayout -adj 1 -cat "both" 4 -rs 4
973				nw4cSetRenderPriority_SetLayout;
974
975			//-----------------------------------------------------------------------------
976			// node name
977			frameLayout -l "Selected Material ( Shading Group )" -cll 0 -cl 0 -bv 1 -bs "etchedIn";
978				columnLayout -adj 1 -cal "center" -rs 8;
979
980				text -l "" nw4cSetRenderPriority_NodeName;
981
982				setParent ..; // columnLayout
983			setParent ..; // formLayout
984
985			//-----------------------------------------------------------------------------
986			// set
987			frameLayout -l "Set" -cll 0 -cl 0 -bv 1 -bs "etchedIn";
988				string $form2 = `formLayout -nd 400`;
989					radioButtonGrp -l "Render Priority" -nrb 1 -cw2 110 80
990						-l1 "Don't care"
991						nw4cSetRenderPriority_DontCare;
992
993					radioButtonGrp -l "" -nrb 1 -cw2 110 18
994						-l1 ""
995						-scl nw4cSetRenderPriority_DontCare
996						nw4cSetRenderPriority_UseRenderPriority;
997
998					intField -w 50 -v 1
999						nw4cSetRenderPriority_RenderPriority;
1000
1001					text -l "1 (High Priority) - 255 (Low Priority)"
1002						nw4cSetRenderPriority_Description;
1003				setParent ..; // formLayout
1004				formLayout -e
1005					-af nw4cSetRenderPriority_DontCare "top" 8
1006					-af nw4cSetRenderPriority_DontCare "left" 0
1007					-an nw4cSetRenderPriority_DontCare "bottom"
1008					-an nw4cSetRenderPriority_DontCare "right"
1009
1010					-ac nw4cSetRenderPriority_UseRenderPriority "top" 8 nw4cSetRenderPriority_DontCare
1011					-af nw4cSetRenderPriority_UseRenderPriority "left" 0
1012					-an nw4cSetRenderPriority_UseRenderPriority "bottom"
1013					-an nw4cSetRenderPriority_UseRenderPriority "right"
1014
1015					-ac nw4cSetRenderPriority_RenderPriority "top" 4 nw4cSetRenderPriority_DontCare
1016					-ac nw4cSetRenderPriority_RenderPriority "left" 0 nw4cSetRenderPriority_UseRenderPriority
1017					-an nw4cSetRenderPriority_RenderPriority "bottom"
1018					-an nw4cSetRenderPriority_RenderPriority "right"
1019
1020					-ac nw4cSetRenderPriority_Description "top" 4 nw4cSetRenderPriority_RenderPriority
1021					-ac nw4cSetRenderPriority_Description "left" 4 nw4cSetRenderPriority_UseRenderPriority
1022					-af nw4cSetRenderPriority_Description "bottom" 8
1023					-an nw4cSetRenderPriority_Description "right"
1024					$form2;
1025			setParent ..; // frameLayout
1026
1027			//-----------------------------------------------------------------------------
1028			// search
1029			frameLayout -l "Search" -cll 0 -cl 0 -bv 1 -bs "etchedIn";
1030				columnLayout -adj 1 -cal "center" -rs 0;
1031				string $form3 = `formLayout -nd 400`;
1032					radioButtonGrp -l "" -nrb 1 -cw2 110 80
1033						-l1 "Don't care"
1034						-on1 "nw4cSetRenderPriority_SearchModeCB"
1035						nw4cSetRenderPriority_SearchDontCare;
1036
1037					radioButtonGrp -l "" -nrb 1 -cw2 110 18
1038						-l1 ""
1039						-scl nw4cSetRenderPriority_SearchDontCare
1040						-on1 "nw4cSetRenderPriority_SearchModeCB"
1041						nw4cSetRenderPriority_SearchUseRenderPriority;
1042
1043					optionMenu -w 110
1044						nw4cSetRenderPriority_SearchCompare;
1045						menuItem -l "<  : ( under )";
1046						menuItem -l "<= : ( below )";
1047						menuItem -l "=  : ( exactly )";
1048						menuItem -l ">= : ( above )";
1049						menuItem -l ">  : ( over )";
1050
1051					intField -w 50 -h 26 -v 1
1052						-cc "nw4cSetRenderPriority_SearchValueCB"
1053						nw4cSetRenderPriority_SearchRenderPriority;
1054				setParent ..; // formLayout
1055				formLayout -e
1056					-af nw4cSetRenderPriority_SearchDontCare "top" 8
1057					-af nw4cSetRenderPriority_SearchDontCare "left" 0
1058					-an nw4cSetRenderPriority_SearchDontCare "bottom"
1059					-an nw4cSetRenderPriority_SearchDontCare "right"
1060
1061					-ac nw4cSetRenderPriority_SearchUseRenderPriority "top" 10 nw4cSetRenderPriority_SearchDontCare
1062					-af nw4cSetRenderPriority_SearchUseRenderPriority "left" 0
1063					-an nw4cSetRenderPriority_SearchUseRenderPriority "bottom"
1064					-an nw4cSetRenderPriority_SearchUseRenderPriority "right"
1065
1066					-ac nw4cSetRenderPriority_SearchCompare "top" (4 + $afterMaya2011Adjust * 3) nw4cSetRenderPriority_SearchDontCare
1067					-ac nw4cSetRenderPriority_SearchCompare "left" 0 nw4cSetRenderPriority_SearchUseRenderPriority
1068					-an nw4cSetRenderPriority_SearchCompare "bottom"
1069					-an nw4cSetRenderPriority_SearchCompare "right"
1070
1071					-ac nw4cSetRenderPriority_SearchRenderPriority "top" 4 nw4cSetRenderPriority_SearchDontCare
1072					-ac nw4cSetRenderPriority_SearchRenderPriority "left" 4 nw4cSetRenderPriority_SearchCompare
1073					-af nw4cSetRenderPriority_SearchRenderPriority "bottom" 8
1074					-an nw4cSetRenderPriority_SearchRenderPriority "right"
1075					$form3;
1076				rowColumnLayout -nc 2 -cw 1 200 -cw 2 150;
1077					text -l ""; // dummy
1078					button -l "Search"
1079						-c "nw4cSetRenderPriority_DoSearch"
1080						nw4cSetRenderPriority_SearchBtn;
1081				setParent ..; // rowColumnLayout
1082				separator -h 8 -st "none";
1083
1084				setParent ..; // columnLayout
1085			setParent ..; // formLayout
1086			optionMenu -e -sl 3 nw4cSetRenderPriority_SearchCompare;
1087
1088			setParent ..; // columnLayout
1089
1090			//-----------------------------------------------------------------------------
1091			// list
1092			//-----------------------------------------------------------------------------
1093			formLayout -nd 100 nw4cSetRenderPriority_ListLayout;
1094
1095			//-----------------------------------------------------------------------------
1096			string $labelRow = `rowColumnLayout -nc 4
1097				-cw 1 240 -cw 2 16 -cw 3 49 -cw 4 16
1098				-cs 1 7 -cs 3 5`;
1099				//iconTextButton -h 24 -l "Shading Group ( Material )"
1100				//	-st "iconAndTextHorizontal" -i1 "arrowUp.xpm"
1101				//	-c "nw4cSetRenderPriority_ListNameSortCB";
1102				button -l "Material ( Shading Group )" -al "left"
1103					-c "nw4cSetRenderPriority_ListNameSortCB";
1104				iconTextStaticLabel -h 24 -st "iconOnly" -i1 "arrowUp.xpm" -vis 0
1105					nw4cSetRenderPriority_NameSortIcon;
1106				button -l "Priority" -al "left"
1107					-c "nw4cSetRenderPriority_ListPrioritySortCB";
1108				iconTextStaticLabel -h 24 -st "iconOnly" -i1 "arrowDown.xpm" -vis 0
1109					nw4cSetRenderPriority_PrioritySortIcon;
1110			setParent ..; // rowColumnLayout;
1111
1112			string $scroll = `scrollLayout -hst 0`;
1113				formLayout -nd 100 nw4cSetRenderPriority_SGList;
1114				setParent ..; // formLayout;
1115			setParent ..; // scrollLayout
1116
1117			checkBox -l "Frame Objects" -v 0
1118				nw4cSetRenderPriority_FrameObjects;
1119
1120			formLayout -e
1121				-af $labelRow "top" 4
1122				-af $labelRow "left" 4
1123				-an $labelRow "bottom"
1124				-af $labelRow "right" 4
1125
1126				-ac $scroll "top" 4 $labelRow
1127				-af $scroll "left" 4
1128				-ac $scroll "bottom" 4 nw4cSetRenderPriority_FrameObjects
1129				-af $scroll "right" 4
1130
1131				-an nw4cSetRenderPriority_FrameObjects "top"
1132				-af nw4cSetRenderPriority_FrameObjects "left" 10
1133				-af nw4cSetRenderPriority_FrameObjects "bottom" 4
1134				-af nw4cSetRenderPriority_FrameObjects "right" 4
1135
1136				nw4cSetRenderPriority_ListLayout;
1137			setParent ..; // formLayout
1138
1139		setParent ..; // tabLayout
1140		tabLayout -e -tl nw4cSetRenderPriority_SetLayout "Set and Search"
1141			nw4cSetRenderPriority_Mode;
1142		tabLayout -e -tl nw4cSetRenderPriority_ListLayout "List"
1143			nw4cSetRenderPriority_Mode;
1144		//tabLayout -e -st nw4cSetRenderPriority_ListLayout
1145		//	nw4cSetRenderPriority_Mode; // select List default
1146
1147		//-----------------------------------------------------------------------------
1148		// close button
1149		string $closeCmd = "deleteUI nw4cSetRenderPriority_Win";
1150		string $closeBtn = `button -h 26 -l "Close" -c $closeCmd`;
1151
1152		formLayout -e
1153			-af nw4cSetRenderPriority_Mode "top" 4
1154			-af nw4cSetRenderPriority_Mode "left" 4
1155			-ac nw4cSetRenderPriority_Mode "bottom" 4 $closeBtn
1156			-af nw4cSetRenderPriority_Mode "right" 4
1157
1158			-an $closeBtn "top"
1159			-af $closeBtn "left" 4
1160			-af $closeBtn "bottom" 4
1161			-af $closeBtn "right" 4
1162			$form1;
1163		setParent ..; // formLayout
1164
1165		setFocus $closeBtn;
1166
1167		//-----------------------------------------------------------------------------
1168		// set selection change job
1169		scriptJob -p nw4cSetRenderPriority_Win
1170			-e "SelectionChanged" "nw4cSetRenderPriority_UpdateWindow";
1171
1172		//-----------------------------------------------------------------------------
1173		// set undo / redo job
1174		scriptJob -p nw4cSetRenderPriority_Win
1175			-e "Undo" "nw4cSetRenderPriority_UndoCB";
1176		scriptJob -p nw4cSetRenderPriority_Win
1177			-e "Redo" "nw4cSetRenderPriority_UndoCB";
1178
1179		//-----------------------------------------------------------------------------
1180		// set name change job
1181		scriptJob -p nw4cSetRenderPriority_Win
1182			-e "NameChanged" "nw4cSetRenderPriority_UndoCB";
1183
1184		//-----------------------------------------------------------------------------
1185		// init sort state
1186		$nw4cSetRenderPriority_SortKind = 0;
1187		$nw4cSetRenderPriority_SortOrder = 0;
1188		$nw4cSetRenderPriority_KeepOrder = 0;
1189	}
1190	if (`window -q -w nw4cSetRenderPriority_Win` < $wd)
1191	{
1192		window -e -w $wd nw4cSetRenderPriority_Win;
1193	}
1194	if (`window -q -h nw4cSetRenderPriority_Win` < $ht)
1195	{
1196		window -e -h $ht nw4cSetRenderPriority_Win;
1197	}
1198	//window -e -wh $wd $ht nw4cSetRenderPriority_Win;
1199
1200	//-----------------------------------------------------------------------------
1201	// update window
1202	nw4cSetRenderPriority_UpdateWindow();
1203	nw4cSetRenderPriority_SearchModeCB();
1204	nw4cSetRenderPriority_TabChangeCB();
1205
1206	//-----------------------------------------------------------------------------
1207	// show window
1208	showWindow nw4cSetRenderPriority_Win;
1209}
1210