starling-framework
Version:
A fast, productive library for 2D cross-platform development.
426 lines (411 loc) • 14.6 kB
JavaScript
// Class: starling.display.DisplayObjectContainer
var $global = typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this
$global.Object.defineProperty(exports, "__esModule", {value: true});
var __map_reserved = {};
// Imports
var $hxClasses = require("./../../hxClasses_stub").default;
var $hxEnums = require("./../../hxEnums_stub").default;
var $import = require("./../../import_stub").default;
var $extend = require("./../../extend_stub").default;
function starling_display_DisplayObject() {return require("./../../starling/display/DisplayObject");}
function openfl__$Vector_Vector_$Impl_$() {return require("./../../openfl/_Vector/Vector_Impl_");}
function js__$Boot_HaxeError() {return require("./../../js/_Boot/HaxeError");}
function openfl_errors_RangeError() {return $import(require("openfl/errors/RangeError"));}
function openfl_errors_ArgumentError() {return $import(require("openfl/errors/ArgumentError"));}
function openfl_geom_Rectangle() {return $import(require("openfl/geom/Rectangle"));}
function starling_utils_MatrixUtil() {return require("./../../starling/utils/MatrixUtil");}
function starling_events_Event() {return require("./../../starling/events/Event");}
function openfl_geom_Matrix() {return $import(require("openfl/geom/Matrix"));}
function openfl_geom_Point() {return $import(require("openfl/geom/Point"));}
function starling_rendering_BatchToken() {return require("./../../starling/rendering/BatchToken");}
function Std() {return require("./../../Std");}
// Constructor
var DisplayObjectContainer = function() {
(starling_display_DisplayObject().default).call(this);
this.__children = (openfl__$Vector_Vector_$Impl_$().default)._new();
}
// Meta
DisplayObjectContainer.__name__ = "starling.display.DisplayObjectContainer";
DisplayObjectContainer.__isInterface__ = false;
DisplayObjectContainer.__super__ = (starling_display_DisplayObject().default);
DisplayObjectContainer.prototype = $extend((starling_display_DisplayObject().default).prototype, {
dispose: function() {
var i = this.__children.length - 1;
while(i >= 0) {
this.__children[i].dispose();
--i;
}
(starling_display_DisplayObject().default).prototype.dispose.call(this);
},
addChild: function(child) {
return this.addChildAt(child,this.__children.length);
},
addChildAt: function(child,index) {
var numChildren = this.__children.length;
if(index >= 0 && index <= numChildren) {
this.setRequiresRedraw();
if(child.get_parent() == this) {
this.setChildIndex(child,index);
} else {
(openfl__$Vector_Vector_$Impl_$().default).insertAt(this.__children,index,child);
child.removeFromParent();
child.__setParent(this);
child.dispatchEventWith("added",true);
if(this.get_stage() != null) {
var container = ((child) instanceof DisplayObjectContainer) ? child : null;
if(container != null) {
container.broadcastEventWith("addedToStage");
} else {
child.dispatchEventWith("addedToStage");
}
}
}
return child;
} else {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_RangeError().default)("Invalid child index"));
}
},
removeChild: function(child,dispose) {
if(dispose == null) {
dispose = false;
}
var childIndex = this.getChildIndex(child);
if(childIndex != -1) {
return this.removeChildAt(childIndex,dispose);
} else {
return null;
}
},
removeChildAt: function(index,dispose) {
if(dispose == null) {
dispose = false;
}
if(index >= 0 && index < this.__children.length) {
this.setRequiresRedraw();
var child = this.__children[index];
child.dispatchEventWith("removed",true);
if(this.get_stage() != null) {
var container = ((child) instanceof DisplayObjectContainer) ? child : null;
if(container != null) {
container.broadcastEventWith("removedFromStage");
} else {
child.dispatchEventWith("removedFromStage");
}
}
child.__setParent(null);
var this1 = this.__children;
index = (Array.prototype.indexOf.call)(this1,child,0);
if(index >= 0) {
(openfl__$Vector_Vector_$Impl_$().default).removeAt(this.__children,index);
}
if(dispose) {
child.dispose();
}
return child;
} else {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_RangeError().default)("Invalid child index"));
}
},
removeChildren: function(beginIndex,endIndex,dispose) {
if(dispose == null) {
dispose = false;
}
if(endIndex == null) {
endIndex = -1;
}
if(beginIndex == null) {
beginIndex = 0;
}
if(endIndex < 0 || endIndex >= this.get_numChildren()) {
endIndex = this.get_numChildren() - 1;
}
var i = beginIndex;
while(i <= endIndex) {
this.removeChildAt(beginIndex,dispose);
++i;
}
},
getChildAt: function(index) {
var numChildren = this.__children.length;
if(index < 0) {
index = numChildren + index;
}
if(index >= 0 && index < numChildren) {
return this.__children[index];
} else {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_RangeError().default)("Invalid child index"));
}
},
getChildByName: function(name) {
var numChildren = this.__children.length;
var _g = 0;
var _g1 = numChildren;
while(_g < _g1) {
var i = _g++;
if(this.__children[i].get_name() == name) {
return this.__children[i];
}
}
return null;
},
getChildIndex: function(child) {
var this1 = this.__children;
return (Array.prototype.indexOf.call)(this1,child,0);
},
setChildIndex: function(child,index) {
var oldIndex = this.getChildIndex(child);
if(oldIndex == index) {
return;
}
if(oldIndex == -1) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Not a child of this container"));
}
(openfl__$Vector_Vector_$Impl_$().default).removeAt(this.__children,oldIndex);
(openfl__$Vector_Vector_$Impl_$().default).insertAt(this.__children,index,child);
this.setRequiresRedraw();
},
swapChildren: function(child1,child2) {
var index1 = this.getChildIndex(child1);
var index2 = this.getChildIndex(child2);
if(index1 == -1 || index2 == -1) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Not a child of this container"));
}
this.swapChildrenAt(index1,index2);
},
swapChildrenAt: function(index1,index2) {
var child1 = this.getChildAt(index1);
var child2 = this.getChildAt(index2);
(openfl__$Vector_Vector_$Impl_$().default).set(this.__children,index1,child2);
(openfl__$Vector_Vector_$Impl_$().default).set(this.__children,index2,child1);
this.setRequiresRedraw();
},
sortChildren: function(compareFunction) {
DisplayObjectContainer.sSortBuffer.length = this.__children.length;
DisplayObjectContainer.mergeSort(this.__children,compareFunction,0,this.__children.length,DisplayObjectContainer.sSortBuffer);
DisplayObjectContainer.sSortBuffer.length = 0;
this.setRequiresRedraw();
},
contains: function(child) {
while(child != null) if(child == this) {
return true;
} else {
child = child.get_parent();
}
return false;
},
getBounds: function(targetSpace,out) {
if(out == null) {
out = new (openfl_geom_Rectangle().default)();
}
var numChildren = this.__children.length;
if(numChildren == 0) {
this.getTransformationMatrix(targetSpace,DisplayObjectContainer.sBoundsMatrix);
(starling_utils_MatrixUtil().default).transformCoords(DisplayObjectContainer.sBoundsMatrix,0.0,0.0,DisplayObjectContainer.sBoundsPoint);
out.setTo(DisplayObjectContainer.sBoundsPoint.x,DisplayObjectContainer.sBoundsPoint.y,0,0);
} else if(numChildren == 1) {
this.__children[0].getBounds(targetSpace,out);
} else {
var minX = 1.79e+308;
var maxX = -1.79e+308;
var minY = 1.79e+308;
var maxY = -1.79e+308;
var i = 0;
var _g = 0;
var _g1 = numChildren;
while(_g < _g1) {
var i1 = _g++;
this.__children[i1].getBounds(targetSpace,out);
if(minX > out.x) {
minX = out.x;
}
if(maxX < out.get_right()) {
maxX = out.get_right();
}
if(minY > out.y) {
minY = out.y;
}
if(maxY < out.get_bottom()) {
maxY = out.get_bottom();
}
}
out.setTo(minX,minY,maxX - minX,maxY - minY);
}
return out;
},
hitTest: function(localPoint) {
if(!this.get_visible() || !this.get_touchable() || !this.hitTestMask(localPoint)) {
return null;
}
var target = null;
var localX = localPoint.x;
var localY = localPoint.y;
var numChildren = this.__children.length;
var child;
var i = numChildren - 1;
while(i >= 0) {
child = this.__children[i];
if(child.get_isMask()) {
--i;
continue;
}
DisplayObjectContainer.sHitTestMatrix.copyFrom(child.get_transformationMatrix());
DisplayObjectContainer.sHitTestMatrix.invert();
(starling_utils_MatrixUtil().default).transformCoords(DisplayObjectContainer.sHitTestMatrix,localX,localY,DisplayObjectContainer.sHitTestPoint);
target = child.hitTest(DisplayObjectContainer.sHitTestPoint);
if(target != null) {
if(this.__touchGroup) {
return this;
} else {
return target;
}
}
--i;
}
return null;
},
render: function(painter) {
var numChildren = this.__children.length;
var frameID = painter.get_frameID();
var cacheEnabled = frameID != 0;
var selfOrParentChanged = this.__lastParentOrSelfChangeFrameID == frameID;
painter.pushState();
var child;
var filter;
var mask;
var pushToken;
var popToken;
var _g = 0;
var _g1 = numChildren;
while(_g < _g1) {
var i = _g++;
child = this.__children[i];
if(child.__hasVisibleArea) {
if(i != 0) {
painter.restoreState();
}
if(selfOrParentChanged) {
child.__lastParentOrSelfChangeFrameID = frameID;
}
if(child.__lastParentOrSelfChangeFrameID != frameID && child.__lastChildChangeFrameID != frameID && child.__tokenFrameID == frameID - 1 && cacheEnabled) {
painter.fillToken(DisplayObjectContainer.sCacheToken);
painter.drawFromCache(child.__pushToken,child.__popToken);
painter.fillToken(child.__popToken);
child.__pushToken.copyFrom(DisplayObjectContainer.sCacheToken);
} else {
pushToken = cacheEnabled ? child.__pushToken : null;
popToken = cacheEnabled ? child.__popToken : null;
filter = child.__filter;
mask = child.__mask;
painter.fillToken(pushToken);
painter.setStateTo(child.get_transformationMatrix(),child.get_alpha(),child.get_blendMode());
if(mask != null) {
painter.drawMask(mask,child);
}
if(filter != null) {
filter.render(painter);
} else {
child.render(painter);
}
if(mask != null) {
painter.eraseMask(mask,child);
}
painter.fillToken(popToken);
}
if(cacheEnabled) {
child.__tokenFrameID = frameID;
}
}
}
painter.popState();
},
broadcastEvent: function(event) {
if(event.bubbles) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Broadcast of bubbling events is prohibited"));
}
var fromIndex = DisplayObjectContainer.sBroadcastListeners.length;
this.__getChildEventListeners(this,event.type,DisplayObjectContainer.sBroadcastListeners);
var toIndex = DisplayObjectContainer.sBroadcastListeners.length;
var _g = fromIndex;
var _g1 = toIndex;
while(_g < _g1) {
var i = _g++;
DisplayObjectContainer.sBroadcastListeners[i].dispatchEvent(event);
}
DisplayObjectContainer.sBroadcastListeners.length = fromIndex;
},
broadcastEventWith: function(eventType,data) {
var event = (starling_events_Event().default).fromPool(eventType,false,data);
this.broadcastEvent(event);
(starling_events_Event().default).toPool(event);
},
get_numChildren: function() {
return this.__children.length;
},
get_touchGroup: function() {
return this.__touchGroup;
},
set_touchGroup: function(value) {
return this.__touchGroup = value;
},
__getChildEventListeners: function(object,eventType,listeners) {
var container = ((object) instanceof DisplayObjectContainer) ? object : null;
if(object == null) {
return;
}
if(object.hasEventListener(eventType)) {
(openfl__$Vector_Vector_$Impl_$().default).set(listeners,listeners.length,object);
}
if(container != null) {
var children = container.__children;
var numChildren = children.length;
var _g = 0;
var _g1 = numChildren;
while(_g < _g1) {
var i = _g++;
this.__getChildEventListeners(children[i],eventType,listeners);
}
}
}
});
DisplayObjectContainer.prototype.__class__ = DisplayObjectContainer.prototype.constructor = $hxClasses["starling.display.DisplayObjectContainer"] = DisplayObjectContainer;
// Init
Object.defineProperties(DisplayObjectContainer.prototype,{ numChildren : { get : function () { return this.get_numChildren (); }}, touchGroup : { get : function () { return this.get_touchGroup (); }, set : function (v) { return this.set_touchGroup (v); }}});
// Statics
DisplayObjectContainer.mergeSort = function(input,compareFunc,startIndex,length,buffer) {
if(length > 1) {
var i = 0;
var endIndex = startIndex + length;
var halfLength = (Std().default).int(length / 2);
var l = startIndex;
var r = startIndex + halfLength;
DisplayObjectContainer.mergeSort(input,compareFunc,startIndex,halfLength,buffer);
DisplayObjectContainer.mergeSort(input,compareFunc,startIndex + halfLength,length - halfLength,buffer);
var _g = 0;
var _g1 = length;
while(_g < _g1) {
var i1 = _g++;
if(l < startIndex + halfLength && (r == endIndex || compareFunc(input[l],input[r]) <= 0)) {
(openfl__$Vector_Vector_$Impl_$().default).set(buffer,i1,input[l]);
++l;
} else {
(openfl__$Vector_Vector_$Impl_$().default).set(buffer,i1,input[r]);
++r;
}
}
var _g2 = startIndex;
var _g3 = endIndex;
while(_g2 < _g3) {
var i2 = _g2++;
(openfl__$Vector_Vector_$Impl_$().default).set(input,i2,buffer[i2 - startIndex]);
}
}
}
DisplayObjectContainer.sHitTestMatrix = new (openfl_geom_Matrix().default)()
DisplayObjectContainer.sHitTestPoint = new (openfl_geom_Point().default)()
DisplayObjectContainer.sBoundsMatrix = new (openfl_geom_Matrix().default)()
DisplayObjectContainer.sBoundsPoint = new (openfl_geom_Point().default)()
DisplayObjectContainer.sBroadcastListeners = (openfl__$Vector_Vector_$Impl_$().default)._new()
DisplayObjectContainer.sSortBuffer = (openfl__$Vector_Vector_$Impl_$().default)._new()
DisplayObjectContainer.sCacheToken = new (starling_rendering_BatchToken().default)()
// Export
exports.default = DisplayObjectContainer;