/** @language english
* <p>The LDisplayObjectContainer class is the base class for all objects that can serve as display object containers on the display list. The display list manages all objects displayed in the runtimes. Use the DisplayObjectContainer class to arrange the display objects in the display list. Each DisplayObjectContainer object has its own child list for organizing the z-order of the objects. The z-order is the front-to-back order that determines which object is drawn in front, which is behind, and so on.</p>
* <p>LDisplayObjectContainer is an abstract base class; therefore, you cannot call LDisplayObjectContainer directly. </p>
* <p>The LDisplayObjectContainer class is an abstract base class for all objects that can contain child objects. It cannot be instantiated directly.</p>
* @class LDisplayObjectContainer
* @extends LInteractiveObject
* @constructor
* @since 1.9.0
* @public
*/
var LDisplayObjectContainer = (function () {
function LDisplayObjectContainer () {
var s = this;
LExtends(s, LInteractiveObject, []);
/** @language english
* the child display object list
* @property childList
* @type Array
* @since 1.0.0
* @public
*/
s.childList = new Array();
/** @language english
* Returns the number of children of this object.
* @property numChildren
* @type int
* @since 1.9.0
* @example
* var container1 = new LSprite();
* var container2 = new LSprite();
* var circle1 = new LSprite();
* circle1.graphics.drawRect(1,"#000000",[0,0,50,50]);
* var circle2 = new LSprite();
* circle2.graphics.drawRect(1,"#000000",[100,100,50,50]);
* container2.addChild(container1);
* container1.addChild(circle1);
* container1.addChild(circle2);
* trace(container1.numChildren); // 2
* trace(container2.numChildren); // 1
* trace(circle1.numChildren); // 0
* trace(circle2.numChildren); // 0
* @examplelink <p><a href="../../../api/LDisplayObjectContainer/numChildren.html" target="_blank">Try it »</a></p>
* @public
*/
s.numChildren = 0;
/** @language english
* Determines whether or not the children of the object are mouse, enabled.
* @property mouseChildren
* @type Boolean
* @since 1.9.0
* @example
* LGlobal.setDebug(true);
* var container1 = new LSprite();
* container1.x = container1.y = 20;
* var container2 = new LSprite();
* container2.x = 20;
* container2.y = 100;
* container2.mouseChildren = false;
* addChild(container1);
* addChild(container2);
* var button01 = new LButtonSample1("mouseChildren=true");
* container1.addChild(button01);
* button01.addEventListener(LMouseEvent.MOUSE_DOWN,function(e){
* trace("button01 click");
* });
* var button02 = new LButtonSample1("mouseChildren=false");
* container2.addChild(button02);
* button02.addEventListener(LMouseEvent.MOUSE_DOWN,function(e){
* trace("button02 click");
* });
* @examplelink <p><a href="../../../api/LDisplayObjectContainer/mouseChildren.html" target="_blank">Try it »</a></p>
* @public
*/
s.mouseChildren = true;
}
var p = {
/** @language english
* <p>Adds a child DisplayObject instance to this LDisplayObjectContainer instance. The child is added to the front (top) of all other children in this LDisplayObjectContainer instance. (To add a child to a specific index position, use the addChildAt() method.)</p>
* <p>If you add a child object that already has a different display object container as a parent, the object is removed from the child list of the other display object container.</p>
* @method addChild
* @param {LDisplayObject} child The LDisplayObject instance to add as a child of this LDisplayObjectContainer instance.
* @return {LDisplayObject} The LDisplayObject instance that you pass in the child parameter.
* @since 1.0.0
* @public
* @example
* var bitmapdata = new LBitmapData("#FF0000",0,0,100,100);
* var bitmap = new LBitmap(bitmapdata);
* var layer = new LSprite();
* addChild(layer);
* layer.addChild(bitmap);
* @examplelink <p><a href="../../../api/LDisplayObjectContainer/addChild.html" target="_blank">Try it »</a></p>
*/
addChild : function (d) {
var s = this,t;
if (d.parent) {
t = LGlobal.destroy;
LGlobal.destroy = false;
d.parent.removeChild(d);
LGlobal.destroy = t;
}
d.parent = s;
s.childList.push(d);
s.numChildren = s.childList.length;
return d;
},
/** @language english
* <p>Adds a child LDisplayObject instance to this LDisplayObjectContainer instance. The child is added at the index position specified. An index of 0 represents the back (bottom) of the display list for this LDisplayObjectContainer object.</p>
* <p>For example, the following example shows three display objects, labeled a, b, and c, at index positions 0, 2, and 1, respectively:</p>
* <p><img src="../../../api/LDisplayObjectContainer/LDisplayObjectContainer_layers.jpg" /></p>
* <p>If you add a child object that already has a different display object container as a parent, the object is removed from the child list of the other display object container.</p>
* @method addChildAt
* @param {LDisplayObject} child child The LDisplayObject instance to add as a child of this LDisplayObjectContainer instance.
* @param {int} index The index position to which the child is added. If you specify a currently occupied index position, the child object that exists at that position and all higher positions are moved up one position in the child list.
* @return {LDisplayObject} The LDisplayObject instance that you pass in the child parameter.
* @since 1.8.0
* @public
* @example
* var container = new LSprite();
* var circle1 = new LSprite();
* var circle2 = new LSprite();
* container.addChild(circle1);
* container.addChildAt(circle2, 0);
* trace(container.getChildAt(0) == circle2); // true
* trace(container.getChildAt(1) == circle1); // true
* @examplelink <p><a href="../../../api/LDisplayObjectContainer/addChildAt.html" target="_blank">Try it »</a></p>
*/
addChildAt : function (d, i) {
var s = this,t;
if (i < 0 || i > s.childList.length) {
return;
}
if (typeof d.remove == "function") {
t = LGlobal.destroy;
LGlobal.destroy = false;
d.remove();
LGlobal.destroy = t;
}
d.parent = s;
s.childList.splice(i, 0, d);
s.numChildren = s.childList.length;
return d;
},
/** @language english
* <p>Removes the specified child LDisplayObject instance from the child list of the LDisplayObjectContainer instance. The parent property of the removed child is set to null , and the object is garbage collected if no other references to the child exist. The index positions of any display objects above the child in the LDisplayObjectContainer are decreased by 1.</p>
* @method removeChild
* @param {LDisplayObject} The LDisplayObject instance to remove.
* @return {LDisplayObject} The LDisplayObject instance that you pass in the child parameter.
* @since 1.0.0
* @public
* @example
* function main () {
* var container = new LSprite();
* addChild(container);
* var circle1 = new LSprite();
* circle1.graphics.drawRect(1,"#000000",[0,0,50,50]);
* var circle2 = new LSprite();
* circle2.graphics.drawRect(1,"#000000",[100,100,50,50]);
* container.addChild(circle1);
* container.addChild(circle2);
* container.addEventListener(LMouseEvent.MOUSE_DOWN, clicked);
* }
* function clicked (event) {
* event.currentTarget.removeChild(event.target);
* }
* @examplelink <p><a href="../../../api/LDisplayObjectContainer/removeChild.html" target="_blank">Try it »</a></p>
*/
removeChild : function (d) {
var s = this, c = s.childList, i, l;
for (i = 0, l = c.length; i < l; i++) {
if (d.objectIndex == c[i].objectIndex) {
if (LGlobal.destroy && d.die) {
d.die();
}
s.childList.splice(i, 1);
break;
}
}
s.numChildren = s.childList.length;
delete d.parent;
},
/** @language english
* Returns the child display object instance that exists at the specified index.
* @method getChildAt
* @param {int} index The index position of the child object.
* @return {LDisplayObject} The child display object at the specified index position.
* @since 1.8.0
* @public
* @example
* var container = new LSprite();
* addChild(container);
* var sprite1 = new LSprite();
* var sprite2 = new LSprite();
* var sprite3 = new LSprite();
* container.addChild(sprite1);
* container.addChild(sprite2);
* container.addChildAt(sprite3, 0);
* trace(container.getChildAt(0) == sprite3); // true
* trace(container.getChildAt(1) == sprite1); // true
* trace(container.getChildAt(2) == sprite2); // true
* @examplelink <p><a href="../../../api/LDisplayObjectContainer/getChildAt.html" target="_blank">Try it »</a></p>
*/
getChildAt : function (i) {
var s = this, c = s.childList;
if (c.length == 0 || c.length <= i) {
return null;
}
return c[i];
},
/** @language english
* <p>Returns the child display object that exists with the specified name. If more that one child display object has the specified name, the method returns the first object in the child list.</p>
* <p>The getChildAt() method is faster than the getChildByName() method. The getChildAt() method accesses a child from a cached array, whereas the getChildByName() method has to traverse a linked list to access a child.</p>
* @method getChildByName
* @param {String} name The name of the child to return.
* @return {LDisplayObject} The child display object with the specified name.
* @since 1.9.0
* @public
* @example
* var container = new LSprite();
* var sprite1 = new LSprite();
* sprite1.name = "sprite1";
* var sprite2 = new LSprite();
* sprite2.name = "sprite2";
* container.addChild(sprite1);
* container.addChild(sprite2);
* var target = container.getChildByName("sprite1");
* trace(container.getChildIndex(target)); // 0
* @examplelink <p><a href="../../../api/LDisplayObjectContainer/getChildByName.html" target="_blank">Try it »</a></p>
*/
getChildByName : function (n) {
var s = this, c = s.childList;
for (i = 0, l = c.length; i < l; i++) {
if (!c[i]) {
continue;
}
if (c[i].name == n) {
return c[i];
}
}
return null;
},
/** @language english
* Removes a child LDisplayObject from the specified index position in the child list of the LDisplayObjectContainer. The parent property of the removed child is set to null, and the object is garbage collected if no other references to the child exist. The index positions of any display objects above the child in the LDisplayObjectContainer are decreased by 1.
* @method removeChildAt
* @param {int} index The child index of the LDisplayObject to remove.
* @return {LDisplayObject} The LDisplayObject instance that was removed.
* @since 1.8.0
* @public
* @example
* var container = new LSprite();
* addChild(container);
* var sprite1 = new LSprite();
* sprite1.name = "sprite1";
* var sprite2 = new LSprite();
* sprite2.name = "sprite2";
* container.addChild(sprite1);
* container.addChild(sprite2);
* trace(container.numChildren) // 2
* container.removeChildAt(0);
* trace(container.numChildren) // 1
* trace(container.getChildAt(0).name); // sprite2
* @examplelink <p><a href="../../../api/LDisplayObjectContainer/removeChildAt.html" target="_blank">Try it »</a></p>
*/
removeChildAt : function (i) {
var s = this, c = s.childList;
if (c.length <= i) {
return;
}
if (LGlobal.destroy && c[i].die) {
c[i].die();
}
var d = s.childList.splice(i, 1);
s.numChildren = s.childList.length;
return d;
},
/** @language english
* Returns the index position of a child LDisplayObject instance.
* @method getChildIndex
* @param {LDisplayObject} The DisplayObject instance to identify.
* @return {int} The index position of the child display object to identify.
* @since 1.8.0
* @public
* @example
* var container = new LSprite();
* addChild(container);
* var sprite1 = new LSprite();
* sprite1.name = "sprite1";
* var sprite2 = new LSprite();
* sprite2.name = "sprite2";
* container.addChild(sprite1);
* container.addChild(sprite2);
* trace(container.getChildIndex(sprite1)); // 0
* @examplelink <p><a href="../../../api/LDisplayObjectContainer/getChildIndex.html" target="_blank">Try it »</a></p>
*/
getChildIndex : function (child) {
if (!child) {
return -1;
}
var s = this, c = s.childList, i, l = c.length;
for (i = 0; i < l; i++) {
if (c[i].objectIndex == child.objectIndex) {
return i;
}
}
return -1;
},
/** @language english
* <p>Changes the position of an existing child in the display object container. This affects the layering of child objects. For example, the following example shows three display objects, labeled a, b, and c, at index positions 0, 1, and 2, respectively:</p>
* <p><img src="../../../api/LDisplayObjectContainer/DisplayObjectContainerSetChildIndex1.jpg" /></p>
* <p>When you use the setChildIndex() method and specify an index position that is already occupied, the only positions that change are those in between the display object's former and new position. All others will stay the same. If a child is moved to an index LOWER than its current index, all children in between will INCREASE by 1 for their index reference. If a child is moved to an index HIGHER than its current index, all children in between will DECREASE by 1 for their index reference. For example, if the display object container in the previous example is named container, you can swap the position of the display objects labeled a and b by calling the following code:</p>
* <p>container.setChildIndex(container.getChildAt(1), 0);</p>
* <p>This code results in the following arrangement of objects:</p>
* <p><img src="../../../api/LDisplayObjectContainer/DisplayObjectContainerSetChildIndex2.jpg" /></p>
* @method setChildIndex
* @param {LDisplayObject} child The child LDisplayObject instance for which you want to change the index number.
* @param {int} index The resulting index number for the child display object.
* @return {int} The resulting index number for the child display object.
* @since 1.8.0
* @public
* @example
* LInit(50, "legend", 800, 480, main);
* var container;
* function main () {
* container = new LSprite();
* addChild(container);
* var circle1 = new LSprite();
* circle1.graphics.drawRect(1,"#000000",[0,0,100,100],true,"#000000");
* circle1.addEventListener(LMouseEvent.MOUSE_DOWN, clicked);
* var circle2 = new LSprite();
* circle2.graphics.drawRect(1,"#FF0000",[40,80,100,100],true,"#FF0000");
* circle2.addEventListener(LMouseEvent.MOUSE_DOWN, clicked);
* var circle3 = new LSprite();
* circle3.graphics.drawRect(1,"#008800",[80,0,100,100],true,"#008800");
* circle3.addEventListener(LMouseEvent.MOUSE_DOWN, clicked);
* container.addChild(circle1);
* container.addChild(circle2);
* container.addChild(circle3);
* }
* function clicked (event) {
* var circle = event.target;
* var topPosition = container.numChildren - 1;
* container.setChildIndex(circle, topPosition);
* }
* @examplelink <p><a href="../../../api/LDisplayObjectContainer/setChildIndex.html" target="_blank">Try it »</a></p>
*/
setChildIndex : function (child, index) {
var s = this, c = s.childList, i, l = c.length;
if (child.parent == "root" || child.parent.objectIndex != s.objectIndex || index < 0 || index >= l) {
return -1;
}
for (i = 0; i < l; i++) {
if(c[i].objectIndex == child.objectIndex){
break;
}
}
s.childList.splice(i,1);
s.childList.splice(index, 0, child);
return index;
},
resize : function () {
var s = this;
s.width = s.getWidth();
s.height = s.getHeight();
},
/** @language english
* <p>Removes all the child LDisplayObject instance from the child list of the LDisplayObjectContainer instance. </p>
* @method removeAllChild
*/
removeAllChild : function () {
var s = this, c = s.childList, i, l;
for (i = 0, l = c.length; i < l; i++) {
if (LGlobal.destroy && c[i].die) {
c[i].die();
}
}
s.childList.length = 0;
s.width = 0;
s.height = 0;
s.numChildren = 0;
}
};
for (var k in p) {
LDisplayObjectContainer.prototype[k] = p[k];
}
return LDisplayObjectContainer;
})();