var ArrayUtils = require('../../utils/array'); var BlendModes = require('../../renderer/BlendModes'); var Class = require('../../utils/Class'); var Components = require('../components'); var Events = require('../events'); var GameObject = require('../GameObject'); var Rectangle = require('../../geom/rectangle/Rectangle'); var Render = require('./ContainerRender'); var Union = require('../../geom/rectangle/Union'); var Vector2 = require('../../math/Vector2'); var Container = new Class({ Extends: GameObject, Mixins: [Components.AlphaSingle, Components.BlendMode, Components.ComputedSize, Components.Depth, Components.Mask, Components.Pipeline, Components.Transform, Components.Visible, Render] , initialize: function Container(scene, x, y, children){ GameObject.call(this, scene, 'Container'); this.list = [] ; this.exclusive = true ; this.maxSize = -1; this.position = 0; this.localTransform = new Components.TransformMatrix(); this.tempTransformMatrix = new Components.TransformMatrix(); this._sortKey = ''; this._sysEvents = scene.sys.events; this.scrollFactorX = 1; this.scrollFactorY = 1; this.initPipeline(); this.setPosition(x, y); this.clearAlpha(); this.setBlendMode(BlendModes.SKIP_CHECK); if (children) { this.add(children); } } , originX: { get: function (){ return 0.5; } } , originY: { get: function (){ return 0.5; } } , displayOriginX: { get: function (){ return this.width * 0.5; } } , displayOriginY: { get: function (){ return this.height * 0.5; } } , setExclusive: function (value){ if (value === undefined) { value = true ; } this.exclusive = value; return this; } , getBounds: function (output){ if (output === undefined) { output = new Rectangle(); } output.setTo(this.x, this.y, 0, 0); if (this.parentContainer) { var parentMatrix = this.parentContainer.getBoundsTransformMatrix(); var transformedPosition = parentMatrix.transformPoint(this.x, this.y); output.setTo(transformedPosition.x, transformedPosition.y, 0, 0); } if ((_AN_Read_length('length', this.list)) > 0) { var children = this.list; var tempRect = new Rectangle(); var hasSetFirst = false ; output.setEmpty(); for (var i = 0; i < (_AN_Read_length('length', children)); i++ ){ var entry = children[i]; if (entry.getBounds) { entry.getBounds(tempRect); if (!hasSetFirst) { output.setTo(tempRect.x, tempRect.y, tempRect.width, tempRect.height); hasSetFirst = true ; } else { Union(tempRect, output, output); } } } } return output; } , addHandler: function (gameObject){ gameObject.once(Events.DESTROY, this.remove, this); if (this.exclusive) { if (gameObject.displayList) { gameObject.displayList.remove(gameObject); } if (gameObject.parentContainer) { gameObject.parentContainer.remove(gameObject); } if (this.displayList) { gameObject.displayList = this.displayList; } else { gameObject.displayList = this.scene.sys.displayList; } gameObject.parentContainer = this; } if (!this.scene.sys.displayList.exists(gameObject)) { gameObject.emit(Events.ADDED_TO_SCENE, gameObject, this.scene); } } , removeHandler: function (gameObject){ gameObject.off(Events.DESTROY, this.remove); if (this.exclusive) { gameObject.parentContainer = null ; } if (!this.scene.sys.displayList.exists(gameObject)) { gameObject.emit(Events.REMOVED_FROM_SCENE, gameObject, this.scene); } } , pointToContainer: function (source, output){ if (output === undefined) { output = new Vector2(); } if (this.parentContainer) { this.parentContainer.pointToContainer(source, output); } else { output = new Vector2(source.x, source.y); } var tempMatrix = this.tempTransformMatrix; tempMatrix.applyITRS(this.x, this.y, this.rotation, this.scaleX, this.scaleY); tempMatrix.invert(); tempMatrix.transformPoint(source.x, source.y, output); return output; } , getBoundsTransformMatrix: function (){ return this.getWorldTransformMatrix(this.tempTransformMatrix, this.localTransform); } , add: function (child){ ArrayUtils.Add(this.list, child, this.maxSize, this.addHandler, this); return this; } , addAt: function (child, index){ ArrayUtils.AddAt(this.list, child, index, this.maxSize, this.addHandler, this); return this; } , getAt: function (index){ return this.list[index]; } , getIndex: function (child){ return this.list.indexOf(child); } , sort: function (property, handler){ if (!property) { return this; } if (handler === undefined) { handler = function (childA, childB){ return childA[property] - childB[property]; } ; } ArrayUtils.StableSort(this.list, handler); return this; } , getByName: function (name){ return ArrayUtils.GetFirst(this.list, 'name', name); } , getRandom: function (startIndex, length){ return ArrayUtils.GetRandom(this.list, startIndex, length); } , getFirst: function (property, value, startIndex, endIndex){ return ArrayUtils.GetFirst(this.list, property, value, startIndex, endIndex); } , getAll: function (property, value, startIndex, endIndex){ return ArrayUtils.GetAll(this.list, property, value, startIndex, endIndex); } , count: function (property, value, startIndex, endIndex){ return ArrayUtils.CountAllMatching(this.list, property, value, startIndex, endIndex); } , swap: function (child1, child2){ ArrayUtils.Swap(this.list, child1, child2); return this; } , moveTo: function (child, index){ ArrayUtils.MoveTo(this.list, child, index); return this; } , remove: function (child, destroyChild){ var removed = ArrayUtils.Remove(this.list, child, this.removeHandler, this); if (destroyChild && removed) { if (!Array.isArray(removed)) { removed = [removed] ; } for (var i = 0; i < (_AN_Read_length('length', removed)); i++ ){ removed[i].destroy(); } } return this; } , removeAt: function (index, destroyChild){ var removed = ArrayUtils.RemoveAt(this.list, index, this.removeHandler, this); if (destroyChild && removed) { removed.destroy(); } return this; } , removeBetween: function (startIndex, endIndex, destroyChild){ var removed = ArrayUtils.RemoveBetween(this.list, startIndex, endIndex, this.removeHandler, this); if (destroyChild) { for (var i = 0; i < (_AN_Read_length('length', removed)); i++ ){ removed[i].destroy(); } } return this; } , removeAll: function (destroyChild){ var removed = ArrayUtils.RemoveBetween(this.list, 0, _AN_Read_length('length', this.list), this.removeHandler, this); if (destroyChild) { for (var i = 0; i < (_AN_Read_length('length', removed)); i++ ){ removed[i].destroy(); } } return this; } , bringToTop: function (child){ ArrayUtils.BringToTop(this.list, child); return this; } , sendToBack: function (child){ ArrayUtils.SendToBack(this.list, child); return this; } , moveUp: function (child){ ArrayUtils.MoveUp(this.list, child); return this; } , moveDown: function (child){ ArrayUtils.MoveDown(this.list, child); return this; } , reverse: function (){ this.list.reverse(); return this; } , shuffle: function (){ ArrayUtils.Shuffle(this.list); return this; } , replace: function (oldChild, newChild, destroyChild){ var moved = _AN_Call_replace('Replace', ArrayUtils, this.list, oldChild, newChild); if (moved) { this.addHandler(newChild); this.removeHandler(oldChild); if (destroyChild) { oldChild.destroy(); } } return this; } , exists: function (child){ return (this.list.indexOf(child) > -1); } , setAll: function (property, value, startIndex, endIndex){ ArrayUtils.SetAll(this.list, property, value, startIndex, endIndex); return this; } , each: function (callback, context){ var args = [null ] ; var i; var temp = this.list.slice(); var len = _AN_Read_length('length', temp); for (i = 2; i < (_AN_Read_length('length', arguments)); i++ ){ args.push(arguments[i]); } for (i = 0; i < len; i++ ){ args[0] = temp[i]; callback.apply(context, args); } return this; } , iterate: function (callback, context){ var args = [null ] ; var i; for (i = 2; i < (_AN_Read_length('length', arguments)); i++ ){ args.push(arguments[i]); } for (i = 0; i < (_AN_Read_length('length', this.list)); i++ ){ args[0] = this.list[i]; callback.apply(context, args); } return this; } , setScrollFactor: function (x, y, updateChildren){ if (y === undefined) { y = x; } if (updateChildren === undefined) { updateChildren = false ; } this.scrollFactorX = x; this.scrollFactorY = y; if (updateChildren) { ArrayUtils.SetAll(this.list, 'scrollFactorX', x); ArrayUtils.SetAll(this.list, 'scrollFactorY', y); } return this; } , length: { get: function (){ return (_AN_Read_length('length', this.list)); } } , first: { get: function (){ this.position = 0; if ((_AN_Read_length('length', this.list)) > 0) { return this.list[0]; } else { return null ; } } } , last: { get: function (){ if ((_AN_Read_length('length', this.list)) > 0) { this.position = _AN_Read_length('length', this.list) - 1; return this.list[this.position]; } else { return null ; } } } , next: { get: function (){ if (this.position < (_AN_Read_length('length', this.list))) { this.position++ ; return this.list[this.position]; } else { return null ; } } } , previous: { get: function (){ if (this.position > 0) { this.position-- ; return this.list[this.position]; } else { return null ; } } } , preDestroy: function (){ this.removeAll(!!this.exclusive); this.localTransform.destroy(); this.tempTransformMatrix.destroy(); this.list = [] ; } } ); module.exports = Container;