// 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.

//  Alias Script File
//  MODIFY THIS AT YOUR OWN RISK
//
//  Creation Date:  12 January, 1999
//
//  Description:
//		Option box for subdivision surfaces tessellate
//

//
//  Procedure Name:
//      setOptionVars
//
//  Description:
//		Initialize the option values.
//
//  Input Arguments:
//		Whether to set the options to default values.
//
//  Return Value:
//      None.
//
//  Note:
//     Commented out Type option for this rev
//
proc setOptionVars(int $forceFactorySettings)
{

	//	Traversal Method
	//	
	if ($forceFactorySettings ||
		!`optionVar -exists subdivTessellateTraversalMethod`) {
		optionVar -intValue subdivTessellateTraversalMethod 2; 
	}

	int $method = `optionVar -query subdivTessellateTraversalMethod`;

	//	Level - if uniform level tessellation
	//	
	if ($forceFactorySettings ||
		!`optionVar -exists subdivTessellateUniformLevel`) {
		optionVar -intValue subdivTessellateUniformLevel 0;
	}

	//	Sample count - if adaptive tessellation method
	//	
	if ($forceFactorySettings ||
		!`optionVar -exists subdivTessellateSampleCount`) {
		optionVar -intValue subdivTessellateSampleCount 1;
	}

	//	Max polygons - if polygon count tessellation method
	//	
	if ($forceFactorySettings ||
		!`optionVar -exists subdivTessellateMaxPolygons`) {
		optionVar -intValue subdivTessellateMaxPolygons 1;
	}

	//  Original Object
	//	
	if ($forceFactorySettings ||
		!`optionVar -exists subdivTessOrigObject`) {
		optionVar -intValue subdivTessOrigObject 1;
	}

	// Share UVs between faces
	// 
	if ($forceFactorySettings ||
		!`optionVar -exists shareUVsCBox`) {
		optionVar -intValue shareUVsCBox true;
	}
}


//
//  Procedure Name:
//      subdivTessellateSetup
//
//  Description:
//		Update the state of the option box UI to reflect the option values.
//
//  Input Arguments:
//      parent               - Top level parent layout of the option box UI.
//                             Required so that UI object names can be 
//                             successfully resolved.
//
//	forceFactorySettings - Whether the option values should be set to
//                             default values.
//
//  Return Value:
//      None.
//
global proc subdivTessellateSetup(string $parent, int $forceFactorySettings)
{
    int $method;

	//	Retrieve the option settings
	//
    setOptionVars($forceFactorySettings);

	setParent $parent;

	//	Query the optionVar's and set the values into the controls.

	// Traversal Method
	int $method = `optionVar -query subdivTessellateTraversalMethod`;
	if($method <= 2) {
		radioButtonGrp -edit -select $method subdivTessellateTraversalMethod;
	} else {
		radioButtonGrp -edit -select ($method-2) subdivTessellateTraversalMethod2;
	}

	// Level (for traversal by uniform level
	intSliderGrp -edit 
		-value `optionVar -query subdivTessellateUniformLevel`
		subdivTessellateUniformLevel;

	// Sample count 
	intSliderGrp -edit 
		-value `optionVar -query subdivTessellateSampleCount`
		subdivTessellateSampleCount;

	// Max polygons
	intSliderGrp -edit 
		-value `optionVar -query subdivTessellateMaxPolygons`
		subdivTessellateMaxPolygons;

	// enable/disable sliders
	int $method = `optionVar -query subdivTessellateTraversalMethod`;
	if ($method == 1){
  		intSliderGrp -edit -enable true "subdivTessellateUniformLevel";
  		intSliderGrp -edit -enable true "subdivTessellateSampleCount";
  		intSliderGrp -edit -enable false "subdivTessellateMaxPolygons";
	}
  	else if ($method == 2){
  		intSliderGrp -edit -enable false "subdivTessellateUniformLevel";
  		intSliderGrp -edit -enable true "subdivTessellateSampleCount";
  		intSliderGrp -edit -enable false "subdivTessellateMaxPolygons";
	}
	else if ($method == 3){
  		intSliderGrp -edit -enable false "subdivTessellateUniformLevel";
  		intSliderGrp -edit -enable false "subdivTessellateSampleCount";
  		intSliderGrp -edit -enable true "subdivTessellateMaxPolygons";
	}
	else if ($method == 4){
  		intSliderGrp -edit -enable true "subdivTessellateUniformLevel";
  		intSliderGrp -edit -enable false "subdivTessellateSampleCount";
  		intSliderGrp -edit -enable false "subdivTessellateMaxPolygons";
	}

	//  Original Object
	int $origObject = `optionVar -query subdivTessOrigObject`;
	radioButtonGrp -edit -select $origObject subdivTessOrigObjectWidget;

	// Share UVs between faces 
	//
	int $share = `optionVar -query shareUVsCBox`;
	checkBoxGrp -edit -v1 $share shareUVsCBox;
}

//
//  Procedure Name:
//      subdivTessellateCallback
//
//  Description:
//		Update the option values with the current state of the option box UI.
//
//  Input Arguments:
//      parent - Top level parent layout of the option box UI.  Required so
//               that UI object names can be successfully resolved.
//
//	doIt   - Whether the command should execute.
//
//  Return Value:
//      None.
//
global proc subdivTessellateCallback(string $parent, int $doIt)
{
    setParent $parent;

	//	Set the optionVar's from the control values, and then
	//	perform the command.

	//	Traversal Method
	//	
	int $val = `radioButtonGrp -query -select subdivTessellateTraversalMethod`;
	if(0 == $val) {
		$val = `radioButtonGrp -query -select subdivTessellateTraversalMethod2`;
		if ($val > 0) $val += `radioButtonGrp -query -nrb subdivTessellateTraversalMethod`;
	}
	optionVar -intValue subdivTessellateTraversalMethod $val;

	//	Level
	//	
	optionVar -intValue subdivTessellateUniformLevel
		`intSliderGrp -query -value subdivTessellateUniformLevel`;

	//	Sample count
	//	
	optionVar -intValue subdivTessellateSampleCount
		`intSliderGrp -query -value subdivTessellateSampleCount`;

	//	Max polygons
	//	
	optionVar -intValue subdivTessellateMaxPolygons
		`intSliderGrp -query -value subdivTessellateMaxPolygons`;

	optionVar -intValue subdivTessOrigObject
		`radioButtonGrp -q -select subdivTessOrigObjectWidget`;

	optionVar -intValue shareUVsCBox
		`checkBoxGrp -q -v1 shareUVsCBox`;

	if ($doIt) {
		performSubdivTessellate 0; 
		addToRecentCommandQueue "performSubdivTessellate 0" "Tessellate";
	}
}

//
//  Procedure Name:
//      subdivTessellateOptions
//
//  Description:
//		Construct the option box UI.  Involves accessing the standard option
//		box and customizing the UI accordingly.
//
//  Input Arguments:
//      None.
//
//  Return Value:
//      None.
//
proc subdivTessellateOptions()
{
	//	Name of the command for this option box.
	//
	string $commandName = "subdivTessellate";

	//	Build the option box actions.
	//
	string $callback = ($commandName + "Callback");
	string $setup = ($commandName + "Setup");

	//	STEP 1:  Get the option box.
	//	============================
	//
	string $layout = getOptionBox();
	setParent $layout;
	
	//	STEP 2:  Pass the command name to the option box.
	//	=================================================
	//
	setOptionBoxCommandName($commandName);
	
	//	STEP 3:  Activate the default UI template.
	//	==========================================
	//
	setUITemplate -pushTemplate DefaultTemplate;

	//	STEP 4: Create option box contents.
	//	===================================
	//	
	
	//	Turn on the wait cursor.
	//
	waitCursor -state 1;

	tabLayout -tabsVisible 0 -scrollable 1;
	
	string $parent = `columnLayout -adjustableColumn 1`;
	
 	//	Tessellation Method
 	//	
	string $radioBtnGrp1 =`radioButtonGrp
		-nrb 2
		-label "Tessellation Method"
		-l1 "Uniform"
		-l2 "Adaptive"
		-cc "subdivTessellateChangeTraversalMethod"
		subdivTessellateTraversalMethod`;

	radioButtonGrp
		-shareCollection $radioBtnGrp1
		-nrb 2
		-label ""
		-l1 "Polygon Count"
		-l2 "Vertices"
		-cc "subdivTessellateChangeTraversalMethod"
		subdivTessellateTraversalMethod2;

	//	Level - if uniform level 
	//	
	intSliderGrp -label "Level"
		-min 0 -max 5 -fmx 8
		subdivTessellateUniformLevel;

	//	Sample count - goes with Adaptive Levels
	//	
	intSliderGrp -label "Divisions Per Face"
		-min 1 -max 5 -fmx 20
		subdivTessellateSampleCount;

	//	Maximum Number Polygons - goes with Polygon Count
	//	
	intSliderGrp -label "Maximum Number Polygons"
		-min 1 -max 2000 -fmx 100000
		subdivTessellateMaxPolygons;

	//	Original object
	//	
	radioButtonGrp 
		-numberOfRadioButtons 3
		-label "Original Object"
		-l1 "Replace"
		-l2 "Hide"
		-l3 "Show"
		subdivTessOrigObjectWidget;

	checkBoxGrp -label "Share UVs" -l1 ""
		shareUVsCBox;

	//	Turn off the wait cursor.
	//
	waitCursor -state 0;
	
	//	Step 5: Deactivate the default UI template.
	//	===========================================
	//
	setUITemplate -popTemplate;

	//	Step 6: Customize the buttons.  
	//	==============================
	//

	//	'Apply' button.
	//
	string $applyBtn = getOptionBoxApplyBtn();
	button -edit -l "Convert"
		-command ($callback + " " + $parent + " " + 1)
		$applyBtn;

	//	'Save' button.
	//
	string $saveBtn = getOptionBoxSaveBtn();
	button -edit 
		-command ($callback + " " + $parent + " " + 0 + "; hideOptionBox")
		$saveBtn;

	//	'Reset' button.
	//
	string $resetBtn = getOptionBoxResetBtn();
	button -edit 
		-command ($setup + " " + $parent + " " + 1)
		$resetBtn;

	//	Step 7: Set the option box title.
	//	=================================
	//
	setOptionBoxTitle("Convert Subdiv to Polygons Options");

	//	Step 8: Customize the 'Help' menu item text.
	//	============================================
	//
	setOptionBoxHelpTag( "SubdivTessellate" );

	//	Step 9: Set the current values of the option box.
	//	=================================================
	//
	eval (($setup + " " + $parent + " " + 0));	
	
	//	Step 10: Show the option box.
	//	=============================
	//
	showOptionBox();
}

//
//  Procedure Name:
//      subdivTessellateHelp
//
//  Description:
//		Return a short description about this command.
//
//  Input Arguments:
//      None.
//
//  Return Value:
//      string.
//
proc string subdivTessellateHelp()
{
	return 
	"  Command: subdivTessellate - tessellate subdivision surfaces into polygons.\n" +
	"Selection: Subdivision surfaces.";
}

//
//  Procedure Name:
//      assembleCmd
//
//  Description:
//		Construct the command that will apply the option box values.
//
//  Input Arguments:
//      None.
//
proc string assembleCmd()
{
	setOptionVars(false);

	// map UI values into subdivToPoly values
	int $method = `optionVar -query subdivTessellateTraversalMethod` - 1;
	int $level = `optionVar -query subdivTessellateUniformLevel`;
	int $sampleCount= `optionVar -query subdivTessellateSampleCount`;
	int $maxPolys= `optionVar -query subdivTessellateMaxPolygons`;
	int $doHistory = `constructionHistory -q -tgl`;
	int $origObject = `optionVar -q subdivTessOrigObject`;
	int $keepNormals = 0;
	int $share  = `optionVar -query shareUVsCBox`;
	int $extractVerts = ($method == 3);

	string $version = "\"3\"";
    string $cmd = "doSubdivTessArgList";
	$cmd = $cmd + "( ";
	$cmd = $cmd + $version;
	$cmd = $cmd + ", { \"" ;
	$cmd = $cmd + $doHistory;
	$cmd = $cmd + "\",\"";
	$cmd = $cmd + $method;
	$cmd = $cmd + "\",\"";
	$cmd = $cmd + $level;
	$cmd = $cmd + "\",\"";
	$cmd = $cmd + $sampleCount;
	$cmd = $cmd + "\",\"";
	$cmd = $cmd + $maxPolys;
	$cmd = $cmd + "\",\"";
	$cmd = $cmd + $origObject;
	$cmd = $cmd + "\",\"";
	$cmd = $cmd + $keepNormals;
	$cmd = $cmd + "\",\"";
	$cmd = $cmd + $extractVerts;
	$cmd = $cmd + "\",\"";
	$cmd = $cmd + $share;
    $cmd = $cmd + "\"} )";

	return $cmd;
}

//
//  Procedure Name:
//      performSubdivTessellate
//
//  Description:
//		Perform the performSubdivTessellate command using the corresponding 
//		option values.  This procedure will also show the option box
//		window if necessary as well as construct the command string
//		that will invoke the performSubdivTessellate command with the current
//		option box values.
//
//  Input Arguments:
//      0 - Execute the command.
//      1 - Show the option box dialog.
//      2 - Return the command.
//
global proc string performSubdivTessellate(int $action)
{
	string $cmd = "";
	switch ($action) {

		//	Execute the command.
		//
		case 0:
			//	Get the command.
			//
			$cmd = `assembleCmd`;

			//	Execute the command with the option settings.
			//
			eval($cmd);

			break;

		//	Show the option box.
		//
		case 1:
			subdivTessellateOptions;
			break;

		//	Return the command string.
		//
		case 2:
			//	Get the command.
			//
			$cmd = `assembleCmd`;
			break;
	}
	return $cmd;
}

//
//  Procedure Name:
//      subdivTessellateChangeTraversalMethod
//
//  Description:
//		Called whenver UI radio btn for traversal method is changed
//      Enables level slider if method is changed to uniform
//      Disables level slider if method changed to leaf-face
//
//  Input Arguments:
//
//  Return Value:
//      None.
//
global proc subdivTessellateChangeTraversalMethod()
{
	int $method = `radioButtonGrp -query -select subdivTessellateTraversalMethod`;
	if(0 == $method) {
		$method = `radioButtonGrp -query -select subdivTessellateTraversalMethod2`;
		if ($method > 0) $method += `radioButtonGrp -query -nrb subdivTessellateTraversalMethod`;
	}

	if ($method == 1){
  		intSliderGrp -edit -enable true  "subdivTessellateUniformLevel";
  		intSliderGrp -edit -enable true "subdivTessellateSampleCount";
  		intSliderGrp -edit -enable false "subdivTessellateMaxPolygons";
	}
  	else if ($method == 2){
  		intSliderGrp -edit -enable false "subdivTessellateUniformLevel";
  		intSliderGrp -edit -enable true "subdivTessellateSampleCount";
  		intSliderGrp -edit -enable false "subdivTessellateMaxPolygons";
	}
	else if ($method == 3){
  		intSliderGrp -edit -enable false "subdivTessellateUniformLevel";
  		intSliderGrp -edit -enable false "subdivTessellateSampleCount";
  		intSliderGrp -edit -enable true "subdivTessellateMaxPolygons";
	}
	else if ($method == 4){
  		intSliderGrp -edit -enable true "subdivTessellateUniformLevel";
  		intSliderGrp -edit -enable false "subdivTessellateSampleCount";
  		intSliderGrp -edit -enable false "subdivTessellateMaxPolygons";

	}

	// dim/undim the normals/share UVs options
	if($method != 4) {
		checkBoxGrp -edit -enable true shareUVsCBox;
	} else {
		checkBoxGrp -edit -enable false shareUVsCBox;
	}
}
