// 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.

// Description:  This procedure is called to apply the preset to 
//  the node.  If the node's createNodeProc is specified, we use
//  it to create the node before we apply the preset to the node.  
//  If the node's deleteNodeProc is specified,
//  we use it to delete the node afterwards.
//
global proc applyPresetToNode( 
    string $node, 
    string $createNodeProc,
    string $deleteNodeProc,
    string $presetName,
    float $blend)
{
    // Create the node if createNodeProc is specified
    //
    if ($createNodeProc != "")
    {
        $node = eval ($createNodeProc);
    }

    applyAttrPreset($node, $presetName, $blend);

    // Delete the node if the deleteNodeProc is specified
    //
    if ($deleteNodeProc != "")
    {
        if ($node != "")
        {
            eval($deleteNodeProc+" "+$node);
        }
    }
}

// Description:  This procedure is called to build the preset menu for
//      existing preset of the same node type as the given node.
//
global proc int presetMenuForDir( int $numPresetsInMenu[], // arrays pass by reference
									string $ppath, 
									string $node, 
									int $callWithFullPath,
                                    string $createNodeProc,
                                    string $deleteNodeProc )
{
	int $itemLimit = 30;
	int $numItems = 0;
	if( `file -q -ex $ppath` ){
		string $files;
		if( `about -nt` ){
			$files = `system ( "dir /b \"" + $ppath + "\""  )`;
		} else if (`about -irix` || `about -linux`) {
			$files = `system ( "ls " + $ppath )`;
		}else if (`about -mac`){
			string $filesList[];
			int $i;
			
			$filesList = `getFileList -folder $ppath`;
			for($i = 0; $i < size($filesList); $i++){
				$files = $files + $filesList[$i] + "\n";
			}
		} else {
			warning( "Operating system is not recognized." );
		}
	
		string $fileList[];
		$numTokens = tokenize( $files, $fileList );
		// create Directory for current node type
		if ($numTokens > 0 && !($numTokens == 1 && $fileList[0] == "unknown")) {
			string $file;
			for ( $file in $fileList ) {
				// only show .mel files
				if(  size( match( ".mel", $file ) ) ){
					string $presetName = `substitute ".mel" $file ""`;
					string $menuCommand = ("applyPresetToNode \"" + $node +"\" \""+$createNodeProc+"\" \""+$deleteNodeProc+ "\" \"" );
					if( $callWithFullPath ){
						$menuCommand = $menuCommand + $ppath + "/" + $file + "\" ";
					} else {
						$menuCommand = $menuCommand + $presetName + "\" ";
					}
					$numItems++;
					menuItem
					-subMenu true
					-l ($presetName);
						menuItem
							-l "replace" 
							-c ($menuCommand + "1");
						menuItem
							-l "blend 90%" 
							-c ($menuCommand + ".9");
						menuItem
							-l "blend 75%" 
							-c ($menuCommand + ".7");
						menuItem
							-l "blend 50%" 
							-c ($menuCommand + ".51");
						menuItem
							-l "blend 25%" 
							-c ($menuCommand + ".25");
						menuItem
							-l "blend 10%" 
							-c ($menuCommand + ".1");
					setParent -menu ..;
	
					$numPresetsInMenu[0] = $numPresetsInMenu[0] + 1;

					// If there are more than $itemLimit presets
					// we need to split the menu into submenus 
					// - otherwise it does not fit on the screen.
					//
					if( $numPresetsInMenu[0] >= $itemLimit ) {
						menuItem -subMenu true -l "more ... ";
						$numPresetsInMenu[0] = 0;
					}
				}
			}
    	}
	}

	return $numItems;

}
