/**************************************************************************** Copyright (c) 2015 Chukong Technologies Inc. http://www.cocos2d-x.org Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ var JS = cc.js; var SceneGraphHelper = require('./scene-graph-helper'); var Destroying = require('../platform/CCObject').Flags.Destroying; var DirtyFlags = require('./misc').DirtyFlags; // called after changing parent function setMaxZOrder (node) { var siblings = node.parent.getChildren(); var z = 0; if (siblings.length >= 2) { var prevNode = siblings[siblings.length - 2]; z = prevNode.getOrderOfArrival() + 1; } node.setOrderOfArrival(z); return z; } var POSITION_CHANGED = 'position-changed'; var ROTATION_CHANGED = 'rotation-changed'; var SCALE_CHANGED = 'scale-changed'; var SIZE_CHANGED = 'size-changed'; var ANCHOR_CHANGED = 'anchor-changed'; var COLOR_CHANGED = 'color-changed'; var OPACITY_CHANGED = 'opacity-changed'; var CHILD_ADDED = 'child-added'; var CHILD_REMOVED = 'child-removed'; /** * A base node for CCNode and CCEScene, it will: * - provide the same api with origin cocos2d rendering node (SGNode) * - maintains properties of the internal SGNode * - retain and release the SGNode * - serialize datas for SGNode (but SGNode itself will not being serialized) * - notifications if some properties changed * - define some interfaces shares between CCNode and CCEScene * * * @class _BaseNode * @extends Object * @private */ var BaseNode = cc.Class(/** @lends cc.Node# */{ extends: cc.Object, mixins: [cc.EventTarget], properties: { // SERIALIZABLE _opacity: 255, _color: cc.Color.WHITE, _cascadeOpacityEnabled: true, _parent: null, _anchorPoint: cc.p(0.5, 0.5), _contentSize: cc.size(0, 0), _children: [], _rotationX: 0, _rotationY: 0.0, _scaleX: 1.0, _scaleY: 1.0, _position: cc.p(0, 0), _skewX: 0, _skewY: 0, _localZOrder: 0, _globalZOrder: 0, _ignoreAnchorPointForPosition: false, _tag: cc.NODE_TAG_INVALID, _opacityModifyRGB: false, // API /** * Name of node * @property name * @type {String} */ name: { get: function () { return this._name; }, set: function (value) { this._name = value; }, }, /** * The parent of the node. * @property parent * @type {Node} * @default null */ parent: { get: function () { return this._parent; }, set: function (value) { if (this._parent === value) { return; } var node = this._sgNode; if (node.parent) { node.parent.removeChild(node, false); } if (value) { var parent = value._sgNode; parent.addChild(node); setMaxZOrder(node); value._children.push(this); value.emit(CHILD_ADDED, this); } // var oldParent = this._parent; this._parent = value || null; if (oldParent) { if (!(oldParent._objFlags & Destroying)) { var removeAt = oldParent._children.indexOf(this); if (removeAt < 0 && CC_EDITOR) { return cc.error('Internal error, should not remove unknown node from parent.'); } oldParent._children.splice(removeAt, 1); oldParent.emit(CHILD_REMOVED, removeAt); this._onHierarchyChanged(oldParent); } } else if (value) { this._onHierarchyChanged(null); } }, }, /** * uuid * @property _id * @type {String} * @private */ _id: { default: '', editorOnly: true }, /** * The uuid for editor, will be stripped before building project * @property uuid * @type {String} * @readOnly */ uuid: { get: function () { return this._id || (this._id = window.Editor ? Editor.uuid() : ''); } }, /** * Skew x * @property skewX * @type {Number} */ skewX: { get: function () { return this._skewX; }, set: function (value) { this._skewX = value; this._sgNode.skewX = value; } }, /** * Skew y * @property skewY * @type {Number} */ skewY: { get: function () { return this._skewY; }, set: function (value) { this._skewY = value; this._sgNode.skewY = value; } }, /** * Z order in depth which stands for the drawing order. * @property zIndex * @type {Number} */ zIndex: { get: function () { return this._localZOrder; }, set: function (value) { this._localZOrder = value; this._sgNode.zIndex = value; } }, /** * Rotation of node * @property rotation * @type {Number} */ rotation: { get: function () { if (this._rotationX !== this._rotationY) cc.log(cc._LogInfos.Node.getRotation); return this._rotationX; }, set: function (value) { if (this._rotationX !== value || this._rotationY !== value ) { var old = this._rotationX; this._rotationX = this._rotationY = value; this._sgNode.rotation = value; this.emit(ROTATION_CHANGED, old); } } }, /** * Rotation on x axis * @property rotationX * @type {Number} */ rotationX: { get: function () { return this._rotationX; }, set: function (value) { if (this._rotationX !== value) { var old = this._rotationX; this._rotationX = value; this._sgNode.rotationX = value; this.emit(ROTATION_CHANGED, old); } }, }, /** * Rotation on y axis * @property rotationX * @type {Number} */ rotationY: { get: function () { return this._rotationY; }, set: function (value) { if (this._rotationX !== value) { // yes, ROTATION_CHANGED always send last rotation x... var oldX = this._rotationX; this._rotationY = value; this._sgNode.rotationY = value; this.emit(ROTATION_CHANGED, oldX); } }, }, /** * Scale on x axis * @property scaleX * @type {Number} */ scaleX: { get: function () { return this._scaleX; }, set: function (value) { if (this._scaleX !== value) { var oldX = this._scaleX; this._scaleX = value; this._sgNode.scaleX = value; this.emit(SCALE_CHANGED, new cc.Vec2(oldX, this._scaleY)); } }, }, /** * Scale on y axis * @property scaleY * @type {Number} */ scaleY: { get: function () { return this._scaleY; }, set: function (value) { if (this._scaleY !== value) { var oldY = this._scaleY; this._scaleY = value; this._sgNode.scaleY = value; this.emit(SCALE_CHANGED, new cc.Vec2(this._scaleX, oldY)); } }, }, /** * x axis position of node. * @property x * @type {Number} */ x: { get: function () { return this._position.x; }, set: function (value) { var localPosition = this._position; if (value !== localPosition.x) { var oldValue = localPosition.x; localPosition.x = value; this._sgNode.x = value; if (this.emit) { this.emit(POSITION_CHANGED, new cc.Vec2(oldValue, localPosition.y)); } } }, }, /** * y axis position of node. * @property y * @type {Number} */ y: { get: function () { return this._position.y; }, set: function (value) { var localPosition = this._position; if (value !== localPosition.y) { var oldValue = localPosition.y; localPosition.y = value; this._sgNode.y = value; if (this.emit) { this.emit(POSITION_CHANGED, new cc.Vec2(localPosition.x, oldValue)); } } }, }, /** * All children nodes * @property children * @type {Node[]} * @readOnly */ children: { get: function () { return this._children; } }, /** * All children nodes. * @property childrenCount * @type {Number} * @readOnly */ childrenCount: { get: function () { return this._children.length; } }, /** * Anchor point's position on x axis. * @property anchorX * @type {Number} */ anchorX: { get: function () { return this._anchorPoint.x; }, set: function (value) { if (this._anchorPoint.x !== value) { var old = cc.v2(this._anchorPoint); this._anchorPoint.x = value; this._onAnchorChanged(); this.emit(ANCHOR_CHANGED, old); } }, }, /** * Anchor point's position on y axis. * @property anchorY * @type {Number} */ anchorY: { get: function () { return this._anchorPoint.y; }, set: function (value) { if (this._anchorPoint.y !== value) { var old = cc.v2(this._anchorPoint); this._anchorPoint.y = value; this._onAnchorChanged(); this.emit(ANCHOR_CHANGED, old); } }, }, /** * Width of node. * @property width * @type {Number} */ width: { get: function () { if (this._sizeProvider) { var w = this._sizeProvider._getWidth(); this._contentSize.width = w; return w; } else { return this._contentSize.width; } }, set: function (value) { if (value !== this._contentSize.width) { if (this._sizeProvider) { this._sizeProvider.setContentSize(value, this._sizeProvider._getHeight()); } var clone = cc.v2(this._contentSize); this._contentSize.width = value; this.emit(SIZE_CHANGED, clone); } }, }, /** * Height of node. * @property height * @type {Number} */ height: { get: function () { if (this._sizeProvider) { var h = this._sizeProvider._getHeight(); this._contentSize.height = h; return h; } else { return this._contentSize.height; } }, set: function (value) { if (value !== this._contentSize.height) { if (this._sizeProvider) { this._sizeProvider.setContentSize(this._sizeProvider._getWidth(), value); } var clone = cc.v2(this._contentSize); this._contentSize.height = value; this.emit(SIZE_CHANGED, clone); } }, }, //running: { // get: //}, /** * Indicate whether ignore the anchor point property for positioning. * @property ignoreAnchor * @type {Boolean} */ ignoreAnchor: { get: function () { return this._ignoreAnchorPointForPosition; }, set: function (value) { if (this._ignoreAnchorPointForPosition !== value) { this._ignoreAnchorPointForPosition = value; this._sgNode.ignoreAnchor = value; this._onAnchorChanged(); this.emit(ANCHOR_CHANGED, this._anchorPoint); } }, }, /** * Tag of node. * @property tag * @type {Number} */ tag: { get: function () { return this._tag; }, set: function (value) { this._tag = value; this._sgNode.tag = value; }, }, /** * Opacity of node, default value is 255. * @property opacity * @type {Number} */ opacity: { get: function () { return this._opacity; }, set: function (value) { if (this._opacity !== value) { var old = this._opacity; this._opacity = value; this._sgNode.opacity = value; this._onColorChanged(); this.emit(OPACITY_CHANGED, old); } }, range: [0, 255] }, /** * Indicate whether node's opacity value affect its child nodes, default value is false. * @property cascadeOpacity * @type {Boolean} */ cascadeOpacity: { get: function () { return this._cascadeOpacityEnabled; }, set: function (value) { if (this._cascadeOpacityEnabled !== value) { this._cascadeOpacityEnabled = value; this._sgNode.cascadeOpacity = value; this._onCascadeChanged(); } }, }, /** * Color of node, default value is white: (255, 255, 255). * @property color * @type {Color} */ color: { get: function () { var color = this._color; return new cc.Color(color.r, color.g, color.b, color.a); }, set: function (value) { if ( !this._color.equals(value) ) { var color = this._color; var old = cc.color(color); color.r = value.r; color.g = value.g; color.b = value.b; if (CC_DEV && value.a !== 255) { cc.warn('Should not set alpha via "color", use "opacity" please.'); } this._onColorChanged(); this.emit(COLOR_CHANGED, old); } }, }, }, ctor: function () { // dont reset _id when destroyed Object.defineProperty(this, '_id', { value: '', enumerable: false }); var sgNode = this._sgNode = new _ccsg.Node(); sgNode.retain(); if (!cc.game._isCloning) { sgNode.cascadeOpacity = true; } this._dirtyFlags = DirtyFlags.ALL; /** * Current active scene graph node which provides content size. * * @property _sizeProvider * @type {_ccsg.Node} * @private */ this._sizeProvider = null; }, destroy: function () { this._sgNode.release(); this._super(); }, // ABSTRACT INTERFACES // called when the node's parent changed _onHierarchyChanged: null, // called when the node's color or opacity changed _onColorChanged: null, // called when the node's anchor changed _onAnchorChanged: null, // called when the node's cascadeOpacity or cascadeColor changed _onCascadeChanged: null, // called when the node's isOpacityModifyRGB changed _onOpacityModifyRGBChanged: null, /** * Initializes the instance of cc.Node * @method init * @returns {Boolean} Whether the initialization was successful. * @deprecated, no need anymore */ init: function () { return true; }, /** *
Properties configuration function * All properties in attrs will be set to the node, * when the setter of the node is available, * the property will be set via setter function. *
* @method attr * @param {Object} attrs - Properties to be set to node */ attr: function (attrs) { for (var key in attrs) { this[key] = attrs[key]; } }, /** *Defines the oder in which the nodes are renderer.
* Nodes that have a Global Z Order lower, are renderer first.
*
* In case two or more nodes have the same Global Z Order, the oder is not guaranteed.
* The only exception if the Nodes have a Global Z Order == 0. In that case, the Scene Graph order is used.
*
* By default, all nodes have a Global Z Order = 0. That means that by default, the Scene Graph order is used to render the nodes.
*
* Global Z Order is useful when you need to render nodes in an order different than the Scene Graph order.
*
* Limitations: Global Z Order can't be used used by Nodes that have SpriteBatchNode as one of their ancestors.
* And if ClippingNode is one of the ancestors, then "global Z order" will be relative to the ClippingNode.
Returns a copy of the position (x,y) of the node in cocos2d coordinates. (0,0) is the left-bottom corner.
* @method getPosition * @return {Vec2} The position (x,y) of the node in OpenGL coordinates */ getPosition: function () { return cc.p(this._position); }, /** *
* Changes the position (x,y) of the node in cocos2d coordinates.
* The original point (0,0) is at the left-bottom corner of screen.
* Usually we use cc.p(x,y) to compose CCPoint object.
* and Passing two numbers (x,y) is more efficient than passing CCPoint object.
*
Returns a copy of the anchor point.
* Anchor point is the point around which all transformations and positioning manipulations take place.
* It's like a pin in the node where it is "attached" to its parent.
* The anchorPoint is normalized, like a percentage. (0,0) means the bottom-left corner and (1,1) means the top-right corner.
* But you can use values higher than (1,1) and lower than (0,0) too.
* The default anchor point is (0.5,0.5), so it starts at the center of the node.
* Sets the anchor point in percent.
*
* anchor point is the point around which all transformations and positioning manipulations take place.
* It's like a pin in the node where it is "attached" to its parent.
* The anchorPoint is normalized, like a percentage. (0,0) means the bottom-left corner and (1,1) means the top-right corner.
* But you can use values higher than (1,1) and lower than (0,0) too.
* The default anchor point is (0.5,0.5), so it starts at the center of the node.
*
Returns a copy the untransformed size of the node.
* The contentSize remains the same no matter the node is scaled or rotated.
* All nodes has a size. Layer and Scene has the same size of the screen by default.
* Sets the untransformed size of the node.
*
* The contentSize remains the same no matter the node is scaled or rotated.
* All nodes has a size. Layer and Scene has the same size of the screen.
*
"add" logic MUST only be in this method
If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately.
* @method addChild * @param {Node} child - A child node * @param {Number} [localZOrder=] - Z order for drawing priority. Please refer to setZOrder(int) * @param {Number|String} [tag=] - An integer or a name to identify the node easily. Please refer to setTag(int) and setName(string) */ addChild: function (child, localZOrder, tag) { localZOrder = localZOrder === undefined ? child._localZOrder : localZOrder; var name, setTag = false; if(cc.js.isUndefined(tag)){ tag = undefined; name = child._name; } else if(cc.js.isString(tag)){ name = tag; tag = undefined; } else if(cc.js.isNumber(tag)){ setTag = true; name = ""; } cc.assert(child, cc._LogInfos.Node.addChild_3); cc.assert(child._parent === null, "child already added. It can't be added again"); this._addChildHelper(child, localZOrder, tag, name, setTag); }, _addChildHelper: function(child, localZOrder, tag, name, setTag){ this._insertChild(child, localZOrder); if (setTag) child.setTag(tag); else child.setName(name); }, _insertChild: function (child, z) { child.parent = this; child.zIndex = z; }, // composition: REMOVE /** * Remove itself from its parent node. If cleanup is true, then also remove all actions and callbacks.Removes a child from the container. It will also cleanup all running actions depending on the cleanup parameter.
* If the cleanup parameter is not passed, it will force a cleanup. "remove" logic MUST only be on this method
* If a class wants to extend the 'removeChild' behavior it only needs
* to override this method
Sets the x axis position of the node in cocos2d coordinates.
* @method getPositionX * @param {Number} x - The new position in x axis */ /** *Returns the x axis position of the node in cocos2d coordinates.
* @method setPositionX * @return {Number} */ /** *Returns the y axis position of the node in cocos2d coordinates.
* @method getPositionY * @return {Number} */ /** *Sets the y axis position of the node in cocos2d coordinates.
* @method setPositionY * @param {Number} y - The new position in y axis */ /** * Returns the local Z order of this node. * @method getLocalZOrder * @returns {Number} The local (relative to its siblings) Z order. */ /** * LocalZOrder is the 'key' used to sort the node relative to its siblings.
*
* The Node's parent will sort all its children based ont the LocalZOrder value.
* If two nodes have the same LocalZOrder, then the node that was added first to the children's array
* will be in front of the other node in the array.
*
* Also, the Scene Graph is traversed using the "In-Order" tree traversal algorithm ( http://en.wikipedia.org/wiki/Tree_traversal#In-order )
*
* And Nodes that have LocalZOder values smaller than 0 are the "left" subtree
* While Nodes with LocalZOder greater than 0 are the "right" subtree.
* Sets whether the anchor point will be ignored when you position this node.
* When anchor point ignored, position will be calculated based on the origin point (0, 0) in parent's coordinates.
* This is an internal method, only used by CCLayer and CCScene. Don't call it outside framework.
* The default value is false, while in CCLayer and CCScene are true
*