//Here we declare a global variable
global string $gWindow;
//For static UI
global string $objName;
global string $gShapeOptions;
global string $gIndideShapeRadio;
global string $chAnimationTrigger;
global string $gCountSlider;
  
//For dynamic UI
global string $objNameToBeEdited;
global string $gEntireObjectResizeSlider;
global string $gTwistedCornResizeSlider;
global string $gRandSphereResizeSlider;
global string $gSubObjSphereResizeSlider;
global string $gSubObjEdgeResizeSlider;
global string $gSubObjFillerResizeSlider;
  
global proc staticUI(){
    //using the already declared variable outside the proc
    global string $gWindow;
    
    int $exists = `window -exists $gWindow`;
    if($exists == 0) {
        $gWindow = `window -width 400
                       -height 500
                       -retain
                       -topLeftCorner 500 1000
                       -title  "Micro Pollen UI"`;
        //Add any sliders, buttons and other UI elements
        addWidgets();    
    }
    showWindow $gWindow;
}
  
global proc addWidgets(){
    //For static UI - To create (a) Micro Pollen object(s).
    global string $objName;
    global string $gShapeOptions;
    global string $gIndideShapeRadio;
    global string $chAnimationTrigger;
    global string $gCountSlider;
    
    columnLayout -adjustableColumn true;
    image -image "/main.jpg" -width 400 -height 250;
  
    frameLayout -label "CREAT MICRO POLLEN" -collapsable true -borderStyle "etchedIn";
    columnLayout -columnAttach "both" 5 -rowSpacing 5 -columnWidth 400;
            separator -height 2 -style none;
            
            $objName = `textFieldGrp -columnWidth 1 65 
                                     -columnAlign 1 "right"
                                     -text "myMicroPollen" -label "Object name"`;
            
            separator -height 2 -style none;
            $gShapeOptions = `optionMenuGrp -label "Shape of the object"
                                           -columnWidth 1 97`;
                              menuItem -label "polyPlatonicSolid";
                              menuItem -label "polyPrimitive";
                              menuItem -label "polyCube";
            separator -height 2 -style none;
            $gIndideShapeRadio = `radioButtonGrp -numberOfRadioButtons 2
                                                -columnWidth 1 105
                                                -cal 1 "right"
                                                -label "Inside Filling Shape" 
                                                -labelArray2 "Pyramid" "Sphere"
                                                -select 1`;
            separator -height 2 -style none;
            $chAnimationTrigger = `checkBoxGrp   -value1 true
                                                -cal 1 "right"
                                                -columnWidth 1 40
                                                -cl2 "right" "right"
                                                -numberOfCheckBoxes 1                           
                                                -label1 "Want Animation?"`;
            separator -height 2 -style none;
            $gCountSlider = `intSliderGrp  -label "Number of Objects"
                                          -columnWidth 1 95
                                          -field true
                                          -value 1
                                          -min 1 -max 3`;
            separator -height 2 -style none;
            button -label "Generate Micro Pollen!" -command "makeMicroPollen";
            button -label "Delete Object(s)" -command "deleteMicroPollen";
            separator -style none -h 20;
        setParent ..;
    setParent ..;
    
    
    //For dynamic UI - To adjust existing object's size
    global string $objNameToBeEdited;
    global string $gEntireObjectResizeSlider;
    global string $gTwistedCornResizeSlider;
    global string $gRandSphereResizeSlider;
    global string $gSubObjSphereResizeSlider;
    global string $gSubObjEdgeResizeSlider;
    global string $gSubObjFillerResizeSlider;
  
    frameLayout -label "ADJUST SIZES" -collapsable true -borderStyle "etchedIn";
    columnLayout -columnAttach "both" 5 -rowSpacing 5 -columnWidth 400;
            separator -height 2 -style none;
            scrollField -wordWrap true
                        -height 38  
                        -width 300 
                        -editable false 
                        -text "Type object name or select the object before you start to resize.";
            separator -height 2 -style none;           
            $objNameToBeEdited = `textFieldGrp -columnWidth 1 65 
                                     -columnAlign 1 "right"
                                     -text "myMicroPollen" -label "Object name"`;
            separator -height 2 -style none;
            $gEntireObjectResizeSlider = `floatSliderGrp -label "Entire Object Size " 
                                          -field true 
                                          -min 0.01 
                                          -max 3.0 
                                          -value 1 
                                          -step 1 
                                          -columnWidth 1 93 
                                          -dc "changeEntireObjectSize" sizeMainObject`;
            separator -height 2 -style none;
            $gTwistedCornResizeSlider = `floatSliderGrp -label "Twisted Corns Size " 
                                          -field true 
                                          -min 0.01 
                                          -max 3.0 
                                          -value 1 
                                          -step 1 
                                          -columnWidth 1 100 
                                          -dc "changeTwistedCornsSize" sizeMainCorns`;
            separator -height 2 -style none;
            $gRandSphereResizeSlider = `floatSliderGrp -label "Edge Spheres Size " 
                                          -field true 
                                          -min 0.01 
                                          -max 3.0 
                                          -value 1 
                                          -step 1 
                                          -columnWidth 1 100 
                                          -dc "changeEdgeSpheresSize" sizeMainSpheres`;
            separator -height 2 -style none;
            $gSubObjSphereResizeSlider = `floatSliderGrp -label "Subobj Spheres Size " 
                                          -field true 
                                          -min 0.01 
                                          -max 3.0 
                                          -value 1 
                                          -step 1 
                                          -columnWidth 1 108 
                                          -dc "changeSubObjSpheresSize" sizeSubSpheres`;
            separator -height 2 -style none;
            $gSubObjEdgeResizeSlider = `floatSliderGrp -label "Subobj Edge Size " 
                                          -field true 
                                          -min 0.01 
                                          -max 3.0 
                                          -value 1 
                                          -step 1 
                                          -columnWidth 1 93 
                                          -dc "changeSubObjEdgesSize" sizeSubEdges`;
            separator -height 2 -style none;
            $gSubObjFillerResizeSlider = `floatSliderGrp -label "Subobj Filler Size " 
                                          -field true 
                                          -min 0.01 
                                          -max 3.0 
                                          -value 1 
                                          -step 1 
                                          -columnWidth 1 93 
                                          -dc "changeSubObjFillerSize" sizeSubFiller`;  
            separator -style none -h 5;
        setParent ..;
    setParent ..;
}
  
//This is automatically called when the button is clicked .
//PROC: Generate Micro Pollen.
global proc makeMicroPollen() {
    
    global string $objName;
    string $obj = `textFieldGrp -q -text $objName`;
    
    //Check whether the name already exists
    string $nameChecker = $obj + "*";
    string $objNames[] = `ls -sl $nameChecker`;
    if(size($objNames)){
        error("That name is already in use. Please choose another name.");
    }
    
    global string $gShapeOptions;
    string $objShape = `optionMenuGrp -q -value $gShapeOptions`;
    
    int $i=0;
    int $isMatch = 1;
    int $objShapeId;
    string $shapeName[] = {"polyPlatonicSolid", "polyPrimitive", "polyCube"};
    while($isMatch != 0){
       $isMatch = `strcmp $shapeName[$i] $objShape`;
       $objShapeId = $i;
       $i++;
    }
  
    global string $gIndideShapeRadio;
    int $isSphereInside = `radioButtonGrp -q -select $gIndideShapeRadio`;
  
    global string $chAnimationTrigger;
    int $isTriggerAnimation = `checkBoxGrp -q -v1 $chAnimationTrigger`;    
  
    global string $gCountSlider;
    int $objCount = `intSliderGrp -q -value $gCountSlider`;
 
    //Go ahead and create the object(s)!
    generateMicroPollens($obj, $objShapeId, $isSphereInside, $isTriggerAnimation, $objCount);   
}
  
//PROC: Delete Micro Pollen
global proc deleteMicroPollen(){
    global string $objName;
    string $obj = `textFieldGrp -q -text $objName`;
    
    string $namesToDelete = $obj + "*";
    select -r $namesToDelete;
    delete;
}
  
//PROC: Checks whether user either provides object name or select the object to adjust its size.
global proc string objectSelectionChecker(){
    
    global string $objNameToBeEdited;
    string $objName = `textFieldGrp -q -text $objNameToBeEdited`;
  
    string $result;
    if(size($objName) > 0){
        $result = $objName;
    }else{
        string $main[] = `ls -selection`;
        if(size($main) != 1){
            error("Please select one object");
        }else{
            $result = $main[0];
        }         
    }
    return $result;
}
  
//PROC: Change the entire obejct size.
global proc changeEntireObjectSize(){
    
    string $main = objectSelectionChecker();  
    float $newSize = `floatSliderGrp -q -value "sizeMainObject"`;
        
    string $cmdX = $main + ".scaleX";
    string $cmdY = $main + ".scaleY";
    string $cmdZ = $main + ".scaleZ";
    
    if ( catch (`setAttr $cmdX $newSize`)) {
        error ("Please provide the right object name");
    } else {
        setAttr $cmdX $newSize;
        setAttr $cmdY $newSize;
        setAttr $cmdZ $newSize; 
    }  
}
  
//PROC: Change twisted corns size.
global proc changeTwistedCornsSize() {
  
    string $main = objectSelectionChecker();  
    float $newSize = `floatSliderGrp -q -value "sizeMainCorns"`;
  
    string $mainChildren[] = `listRelatives -children $main`;
    string $mainGrandChildren[] = `listRelatives -children $mainChildren[0]`;
    string $twistedCorns[] = `listRelatives -children $mainGrandChildren[1]`;
    
    int $i;
    for($i=0; $i<size($twistedCorns); $i++){
        string $cmdX = $twistedCorns[$i] + ".scaleX";
        string $cmdY = $twistedCorns[$i] + ".scaleY";
        string $cmdZ = $twistedCorns[$i] + ".scaleZ";
        
        setAttr $cmdX $newSize;
        setAttr $cmdY $newSize;
        setAttr $cmdZ $newSize;                   
    }
}
  
//PROC: Change spheres on the edge of the main object's frame.
global proc changeEdgeSpheresSize() {
  
    string $main = objectSelectionChecker();  
    float $newSize = `floatSliderGrp -q -value "sizeMainSpheres"`;
  
    string $mainChildren[] = `listRelatives -children $main`;
    string $mainGrandChildren[] = `listRelatives -children $mainChildren[0]`;
    string $edgeSpheres[] = `listRelatives -children $mainGrandChildren[0]`;
    
    int $i;
    for($i=0; $i<size($edgeSpheres); $i++){
        string $cmdX = $edgeSpheres[$i] + ".scaleX";
        string $cmdY = $edgeSpheres[$i] + ".scaleY";
        string $cmdZ = $edgeSpheres[$i] + ".scaleZ";
 
        setAttr $cmdX $newSize;
        setAttr $cmdY $newSize;
        setAttr $cmdZ $newSize;                   
    }
}
  
//PROC: Change spheres on the edge of the sub object's frame.
global proc changeSubObjSpheresSize() {
  
    string $main = objectSelectionChecker();     
    float $newSize = `floatSliderGrp -q -value "sizeSubSpheres"`;
    
    string $mainChildren[] = `listRelatives -children $main`;
    string $subObjList[] = `listRelatives -children $mainChildren[1]`;
    string $subObjSphereGroupList[];
    
    int $i;
    for($i=0; $i<size($subObjList); $i++){
        string $temp[] = `listRelatives -children $subObjList[$i]`;
        $subObjSphereGroupList[$i] = $temp[0];
    }
  
    for($i=0; $i<size($subObjSphereGroupList); $i++){
        string $subObjSphereList[] = `listRelatives -children $subObjSphereGroupList[$i]`;
        
        int $j;
        for($j=0; $j<size($subObjSphereList); $j++){
            string $cmdX = $subObjSphereList[$j] + ".scaleX";
            string $cmdY = $subObjSphereList[$j] + ".scaleY";
            string $cmdZ = $subObjSphereList[$j] + ".scaleZ";
            
            setAttr $cmdX $newSize;
            setAttr $cmdY $newSize;
            setAttr $cmdZ $newSize;            
        }
    }   
}
  
//PROC: Change edge cylindar's size in each sub object.
global proc changeSubObjEdgesSize(){
  
    string $main = objectSelectionChecker();     
    float $newSize = `floatSliderGrp -q -value "sizeSubEdges"`;
    
    string $mainChildren[] = `listRelatives -children $main`;
    string $subObjList[] = `listRelatives -children $mainChildren[1]`;
    string $subObjEdgeGroupList[];
    
    int $i;
    for($i=0; $i<size($subObjList); $i++){
        string $temp[] = `listRelatives -children $subObjList[$i]`;
        $subObjEdgeGroupList[$i] = $temp[1];
    }
  
    for($i=0; $i<size($subObjEdgeGroupList); $i++){
        string $subObjEdgeList[] = `listRelatives -children $subObjEdgeGroupList[$i]`;
        
        int $j;
        for($j=0; $j<size($subObjEdgeList); $j++){
            string $cmdX = $subObjEdgeList[$j] + ".scaleX";
            string $cmdY = $subObjEdgeList[$j] + ".scaleY";
            string $cmdZ = $subObjEdgeList[$j] + ".scaleZ";
  
            setAttr $cmdX $newSize;
            setAttr $cmdY $newSize;
            setAttr $cmdZ $newSize;            
        }
    }   
}
  
//PROC: Change filler's size in each sub object.
global proc changeSubObjFillerSize(){
  
    string $main = objectSelectionChecker();     
    float $newSize = `floatSliderGrp -q -value "sizeSubFiller"`;
    
    string $mainChildren[] = `listRelatives -children $main`;
    string $subObjList[] = `listRelatives -children $mainChildren[1]`;
    string $subObjFillerGroupList[];
    
    int $i;
    for($i=0; $i<size($subObjList); $i++){
        string $temp[] = `listRelatives -children $subObjList[$i]`;
        $subObjFillerGroupList[$i] = $temp[2];
    }
  
    for($i=0; $i<size($subObjFillerGroupList); $i++){
        string $subObjFillerList[] = `listRelatives -children $subObjFillerGroupList[$i]`;
        
        int $j;
        for($j=0; $j<size($subObjFillerList); $j++){
            string $cmdX = $subObjFillerList[$j] + ".scaleX";
            string $cmdY = $subObjFillerList[$j] + ".scaleY";
            string $cmdZ = $subObjFillerList[$j] + ".scaleZ";
  
            setAttr $cmdX $newSize;
            setAttr $cmdY $newSize;
            setAttr $cmdZ $newSize;                        
        }
    }       
}