Advertisement
Guest User

SimSet Sorting

a guest
Dec 26th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. package SimSetSortSupport
  2. {
  3.     function SimSet::sort(%this,%comparator)  
  4.     {  //source: https://www.garagegames.com/community/forums/viewthread/85003
  5.         %objectCount=%this.getCount();
  6.         if(%objectCount<=1)
  7.             return(0);
  8.         for(%j=0;%j<%objectCount;%j++)  
  9.         {  
  10.             for(%i=%objectCount-1;%i>%j;%i--)  
  11.             {  
  12.                 %objectA=%this.getObject(%i);  
  13.                 %objectB=%this.getObject(%i-1);  
  14.                 %check=call(%comparator,%objectA,%objectB);
  15.                 if(%check<0)  //if %check is less than zero then %objectB will be sorted before %objectA
  16.                     %error=%this.reorderChild(%objectA,%objectB);  
  17.                 else
  18.                     %error=%this.reorderChild(%objectB,%objectA);
  19.                 if(%error!=1)
  20.                     return(%error); //reorderChild didn't work
  21.             }  
  22.         }
  23.         return(1);
  24.     }  
  25.     function SimSet::reorderChild(%this,%childA,%childB)
  26.     {   //ensures that childB precedes childA within the SimSet
  27.         //childB is the reference, childA is moved
  28.         //Will return:
  29.             //1 if successful
  30.             //-1 if at least one of the objects used does not exist
  31.             //0 if one (or both) of the input objects is not in the set (or if both objects are the same)
  32.             //a null value if the function failed
  33.         if(!isObject(%this) || !isObject(%childA) || !isObject(%childB))
  34.             return(-1);
  35.         if(!%this.isMember(%childA) || !%this.isMember(%childB) || %childA==%childB)
  36.             return(0);
  37.         if((%refIndex=%this.getObjectIndex(%childA))-1==(%refIndexB=%this.getObjectIndex(%childB)))
  38.             return(1); //already done (that was easy)
  39.         if(%refIndex==-1 || %refIndexB==-1)
  40.             return; //the getObjectIndex function failed (shouldn't happen)
  41.         if((%count=%this.getCount())<=2 || ((%count-1)==%refIndexB))
  42.         {   //save some time if the set only contains two objects, or if %childB is the last object in the set
  43.             %this.pushToBack(%childA);
  44.             return(1);
  45.         }
  46.         if(%refIndexB==0)
  47.         {   //also a time saving device if the first child is already at the front of the set
  48.             %this.bringToFront(%childA);
  49.             %this.bringToFront(%childB);
  50.             return(1);
  51.         }
  52.         if(%refIndexB==1 && %refIndex==0)
  53.         {   //another time saving device
  54.             %this.bringToFront(%childB);
  55.             return(1);
  56.         }
  57.         while(%x$="")
  58.         {
  59.             %next=(%first$="" ? %first=%this.getObject(0) : %this.getObject(0));
  60.             if((%next==%first && %looped) || !isObject(%first))
  61.                 break;
  62.             %this.pushToBack(%next);
  63.             if(%next==%childB)
  64.                 %this.bringToFront(%childA);
  65.             %looped=true;
  66.         }
  67.         return((%this.getObjectIndex(%childA)-1)==%this.getObjectIndex(%childB) ? true : "");
  68.     }
  69.     function SimSet::getObjectIndex(%this,%obj)
  70.     {   //returns -1 if the object is not a member of the simset, and a null value if at least one of the input objects does not exist
  71.         if(!isObject(%this) || !isObject(%obj))
  72.             return;
  73.         if(!%this.isMember(%obj))
  74.             return(-1);
  75.         for(%i=0;%i<%this.getCount();%i++)
  76.         {
  77.             %check=(%obj==%this.getObject(%i));
  78.             if(%check)
  79.                 break;
  80.         }
  81.         return(%check ? %i : -1);
  82.     }
  83. };
  84. if(isPackage(SimSetSortSupport))
  85.     deactivatePackage(SimSetSortSupport);
  86. activatePackage(SimSetSortSupport);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement