Skip to content

Commit

Permalink
Merge pull request #465 from beyond-code-github/sort-children-algorithm
Browse files Browse the repository at this point in the history
Added the ability to specify a custom algorithm to depth sort children of an IgeObject
  • Loading branch information
Irrelon authored Jul 6, 2018
2 parents 0c7fa0d + df8d8d3 commit cd4f470
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions engine/core/IgeObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ var IgeObject = IgeEventingClass.extend({
'_parent',
'_children'
];

// Default sorting behavior
this._sortChildren = function (compareFn) {
return this._children.sort(compareFn);
}
},

/**
Expand Down Expand Up @@ -1375,7 +1380,7 @@ var IgeObject = IgeEventingClass.extend({
arr[sortObj.order[i]].depth(i);
}

this._children.sort(function (a, b) {
this._sortChildren(function (a, b) {
var layerIndex = b._layer - a._layer;

if (layerIndex === 0) {
Expand All @@ -1390,7 +1395,7 @@ var IgeObject = IgeEventingClass.extend({

if (this._depthSortMode === 1) { // Medium speed, optimised for almost-cube shaped 3d bounds
// Now sort the entities by depth
this._children.sort(function (a, b) {
this._sortChildren(function (a, b) {
var layerIndex = b._layer - a._layer;

if (layerIndex === 0) {
Expand Down Expand Up @@ -1420,7 +1425,7 @@ var IgeObject = IgeEventingClass.extend({
}

// Now sort the entities by depth
this._children.sort(function (a, b) {
this._sortChildren(function (a, b) {
var layerIndex = b._layer - a._layer;

if (layerIndex === 0) {
Expand All @@ -1434,7 +1439,7 @@ var IgeObject = IgeEventingClass.extend({
}
} else { // 2d mode
// Now sort the entities by depth
this._children.sort(function (a, b) {
this._sortChildren(function (a, b) {
var layerIndex = b._layer - a._layer;

if (layerIndex === 0) {
Expand Down Expand Up @@ -1968,6 +1973,25 @@ var IgeObject = IgeEventingClass.extend({
}
},

/**
* Gets or sets the function used to sort children for example in depth sorting. This allows us to optionally use
* a stable sort (for browsers where the native implementation is not stable) or something more specific such as
* insertion sort for a speedup when we know data is going to be already mostly sorted.
* @param {Function=} val Sorting function - must operate on this._children and sort the array in place.
* @example #Set the child sorting algorthm
* var entity = new IgeEntity();
* entity.childSortingAlgorithm(function (compareFn) { this._children.sort(compareFn); });
* @return {*}
*/
childSortingAlgorithm: function (val) {
if (val !== undefined) {
this._sortChildren = val;
return this;
}

return this._sortChildren;
},

/**
* Returns a string containing a code fragment that when
* evaluated will reproduce this object.
Expand Down

0 comments on commit cd4f470

Please sign in to comment.