Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package SimSetSortSupport
- {
- function SimSet::sort(%this,%comparator)
- { //source: https://www.garagegames.com/community/forums/viewthread/85003
- %objectCount=%this.getCount();
- if(%objectCount<=1)
- return(0);
- for(%j=0;%j<%objectCount;%j++)
- {
- for(%i=%objectCount-1;%i>%j;%i--)
- {
- %objectA=%this.getObject(%i);
- %objectB=%this.getObject(%i-1);
- %check=call(%comparator,%objectA,%objectB);
- if(%check<0) //if %check is less than zero then %objectB will be sorted before %objectA
- %error=%this.reorderChild(%objectA,%objectB);
- else
- %error=%this.reorderChild(%objectB,%objectA);
- if(%error!=1)
- return(%error); //reorderChild didn't work
- }
- }
- return(1);
- }
- function SimSet::reorderChild(%this,%childA,%childB)
- { //ensures that childB precedes childA within the SimSet
- //childB is the reference, childA is moved
- //Will return:
- //1 if successful
- //-1 if at least one of the objects used does not exist
- //0 if one (or both) of the input objects is not in the set (or if both objects are the same)
- //a null value if the function failed
- if(!isObject(%this) || !isObject(%childA) || !isObject(%childB))
- return(-1);
- if(!%this.isMember(%childA) || !%this.isMember(%childB) || %childA==%childB)
- return(0);
- if((%refIndex=%this.getObjectIndex(%childA))-1==(%refIndexB=%this.getObjectIndex(%childB)))
- return(1); //already done (that was easy)
- if(%refIndex==-1 || %refIndexB==-1)
- return; //the getObjectIndex function failed (shouldn't happen)
- if((%count=%this.getCount())<=2 || ((%count-1)==%refIndexB))
- { //save some time if the set only contains two objects, or if %childB is the last object in the set
- %this.pushToBack(%childA);
- return(1);
- }
- if(%refIndexB==0)
- { //also a time saving device if the first child is already at the front of the set
- %this.bringToFront(%childA);
- %this.bringToFront(%childB);
- return(1);
- }
- if(%refIndexB==1 && %refIndex==0)
- { //another time saving device
- %this.bringToFront(%childB);
- return(1);
- }
- while(%x$="")
- {
- %next=(%first$="" ? %first=%this.getObject(0) : %this.getObject(0));
- if((%next==%first && %looped) || !isObject(%first))
- break;
- %this.pushToBack(%next);
- if(%next==%childB)
- %this.bringToFront(%childA);
- %looped=true;
- }
- return((%this.getObjectIndex(%childA)-1)==%this.getObjectIndex(%childB) ? true : "");
- }
- function SimSet::getObjectIndex(%this,%obj)
- { //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
- if(!isObject(%this) || !isObject(%obj))
- return;
- if(!%this.isMember(%obj))
- return(-1);
- for(%i=0;%i<%this.getCount();%i++)
- {
- %check=(%obj==%this.getObject(%i));
- if(%check)
- break;
- }
- return(%check ? %i : -1);
- }
- };
- if(isPackage(SimSetSortSupport))
- deactivatePackage(SimSetSortSupport);
- activatePackage(SimSetSortSupport);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement