/****************************************************************************
Copyright (c) 2013-2017 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.
****************************************************************************/
'use strict';
var PrefabHelper = require('./utils/prefab-helper');
var SgHelper = require('./utils/scene-graph-helper');
var Flags = cc.Object.Flags;
var Destroying = Flags.Destroying;
var POSITION_CHANGED = 'position-changed';
var SIZE_CHANGED = 'size-changed';
var ANCHOR_CHANGED = 'anchor-changed';
var ROTATION_CHANGED = 'rotation-changed';
var SCALE_CHANGED = 'scale-changed';
var CHILD_ADDED = 'child-added';
var CHILD_REMOVED = 'child-removed';
var CHILD_REORDER = 'child-reorder';
var ERR_INVALID_NUMBER = CC_EDITOR && 'The %s is invalid';
var Misc = require('./utils/misc');
//var RegisteredInEditor = Flags.RegisteredInEditor;
/**
* !#en The event type supported by Node
* !#zh Node 支持的事件类型
* @enum Node.EventType
* @static
* @namespace Node
*/
var EventType = cc.Enum({
/**
* !#en The event type for touch start event, you can use its value directly: 'touchstart'
* !#zh 当手指触摸到屏幕时。
* @property TOUCH_START
* @type {String}
* @static
*/
TOUCH_START: 'touchstart',
/**
* !#en The event type for touch move event, you can use its value directly: 'touchmove'
* !#zh 当手指在屏幕上目标节点区域内移动时。
* @property TOUCH_MOVE
* @type {String}
* @static
*/
TOUCH_MOVE: 'touchmove',
/**
* !#en The event type for touch end event, you can use its value directly: 'touchend'
* !#zh 当手指在目标节点区域内离开屏幕时。
* @property TOUCH_END
* @type {String}
* @static
*/
TOUCH_END: 'touchend',
/**
* !#en The event type for touch end event, you can use its value directly: 'touchcancel'
* !#zh 当手指在目标节点区域外离开屏幕时。
* @property TOUCH_CANCEL
* @type {String}
* @static
*/
TOUCH_CANCEL: 'touchcancel',
/**
* !#en The event type for mouse down events, you can use its value directly: 'mousedown'
* !#zh 当鼠标按下时触发一次。
* @property MOUSE_DOWN
* @type {String}
* @static
*/
MOUSE_DOWN: 'mousedown',
/**
* !#en The event type for mouse move events, you can use its value directly: 'mousemove'
* !#zh 当鼠标在目标节点在目标节点区域中移动时,不论是否按下。
* @property MOUSE_MOVE
* @type {String}
* @static
*/
MOUSE_MOVE: 'mousemove',
/**
* !#en The event type for mouse enter target events, you can use its value directly: 'mouseenter'
* !#zh 当鼠标移入目标节点区域时,不论是否按下。
* @property MOUSE_ENTER
* @type {String}
* @static
*/
MOUSE_ENTER: 'mouseenter',
/**
* !#en The event type for mouse leave target events, you can use its value directly: 'mouseleave'
* !#zh 当鼠标移出目标节点区域时,不论是否按下。
* @property MOUSE_LEAVE
* @type {String}
* @static
*/
MOUSE_LEAVE: 'mouseleave',
/**
* !#en The event type for mouse up events, you can use its value directly: 'mouseup'
* !#zh 当鼠标从按下状态松开时触发一次。
* @property MOUSE_UP
* @type {String}
* @static
*/
MOUSE_UP: 'mouseup',
/**
* !#en The event type for mouse wheel events, you can use its value directly: 'mousewheel'
* !#zh 当鼠标滚轮滚动时。
* @property MOUSE_WHEEL
* @type {String}
* @static
*/
MOUSE_WHEEL: 'mousewheel',
});
var _touchEvents = [
EventType.TOUCH_START,
EventType.TOUCH_MOVE,
EventType.TOUCH_END,
EventType.TOUCH_CANCEL,
];
var _mouseEvents = [
EventType.MOUSE_DOWN,
EventType.MOUSE_ENTER,
EventType.MOUSE_MOVE,
EventType.MOUSE_LEAVE,
EventType.MOUSE_UP,
EventType.MOUSE_WHEEL,
];
var _currentHovered = null;
var _touchStartHandler = function (touch, event) {
var pos = touch.getLocation();
var node = this.owner;
if (node._hitTest(pos, this)) {
event.type = EventType.TOUCH_START;
event.touch = touch;
event.bubbles = true;
node.dispatchEvent(event);
return true;
}
return false;
};
var _touchMoveHandler = function (touch, event) {
var node = this.owner;
event.type = EventType.TOUCH_MOVE;
event.touch = touch;
event.bubbles = true;
node.dispatchEvent(event);
};
var _touchEndHandler = function (touch, event) {
var pos = touch.getLocation();
var node = this.owner;
if (node._hitTest(pos, this)) {
event.type = EventType.TOUCH_END;
}
else {
event.type = EventType.TOUCH_CANCEL;
}
event.touch = touch;
event.bubbles = true;
node.dispatchEvent(event);
};
var _mouseDownHandler = function (event) {
var pos = event.getLocation();
var node = this.owner;
if (node._hitTest(pos, this)) {
event.type = EventType.MOUSE_DOWN;
node.dispatchEvent(event);
event.stopPropagation();
}
};
var _mouseMoveHandler = function (event) {
var pos = event.getLocation();
var node = this.owner;
if (node._hitTest(pos, this)) {
event.stopPropagation();
if (!this._previousIn) {
// Fix issue when hover node switched, previous hovered node won't get MOUSE_LEAVE notification
if (_currentHovered) {
event.type = EventType.MOUSE_LEAVE;
_currentHovered.dispatchEvent(event);
_currentHovered._mouseListener._previousIn = false;
}
_currentHovered = this.owner;
event.type = EventType.MOUSE_ENTER;
node.dispatchEvent(event);
this._previousIn = true;
}
event.type = EventType.MOUSE_MOVE;
node.dispatchEvent(event);
}
else if (this._previousIn) {
event.type = EventType.MOUSE_LEAVE;
node.dispatchEvent(event);
this._previousIn = false;
_currentHovered = null;
}
};
var _mouseUpHandler = function (event) {
var pos = event.getLocation();
var node = this.owner;
if (node._hitTest(pos, this)) {
event.type = EventType.MOUSE_UP;
node.dispatchEvent(event);
event.stopPropagation();
}
};
var _mouseWheelHandler = function (event) {
var pos = event.getLocation();
var node = this.owner;
if (node._hitTest(pos, this)) {
event.type = EventType.MOUSE_WHEEL;
node.dispatchEvent(event);
//FIXME: separate wheel event and other mouse event.
// event.stopPropagation();
}
};
function _searchMaskInParent (node) {
var Mask = cc.Mask;
if (Mask) {
var index = 0;
for (var curr = node; curr && cc.Node.isNode(curr); curr = curr._parent, ++index) {
if (curr.getComponent(Mask)) {
return {
index: index,
node: curr
};
}
}
}
return null;
}
function updateOrder (node) {
node._parent._delaySort();
if (!CC_JSB) {
cc.eventManager._setDirtyForNode(node);
}
}
/**
* !#en
* Class of all entities in Cocos Creator scenes.
* Node also inherits from {{#crossLink "EventTarget"}}Event Target{{/crossLink}}, it permits Node to dispatch events.
* For events supported by Node, please refer to {{#crossLink "Node.EventType"}}{{/crossLink}}
* !#zh
* Cocos Creator 场景中的所有节点类。节点也继承了 {{#crossLink "EventTarget"}}EventTarget{{/crossLink}},它允许节点发送事件。
* 支持的节点事件,请参阅 {{#crossLink "Node.EventType"}}{{/crossLink}}。
* @class Node
* @extends _BaseNode
*/
var Node = cc.Class({
name: 'cc.Node',
extends: require('./utils/base-node'),
properties: {
// SERIALIZABLE
_opacity: 255,
_color: cc.Color.WHITE,
_cascadeOpacityEnabled: true,
_anchorPoint: cc.p(0.5, 0.5),
_contentSize: cc.size(0, 0),
_rotationX: 0,
_rotationY: 0.0,
_scaleX: 1.0,
_scaleY: 1.0,
_position: cc.p(0, 0),
_skewX: 0,
_skewY: 0,
_localZOrder: 0,
_globalZOrder: 0,
_opacityModifyRGB: false,
// internal properties
/**
* !#en
* Group index of node.
* Which Group this node belongs to will resolve that this node's collision components can collide with which other collision componentns.
* !#zh
* 节点的分组索引。
* 节点的分组将关系到节点的碰撞组件可以与哪些碰撞组件相碰撞。
* @property groupIndex
* @type {Integer}
* @default 0
*/
groupIndex: {
default: 0,
type: cc.Integer
},
/**
* !#en
* Group of node.
* Which Group this node belongs to will resolve that this node's collision components can collide with which other collision componentns.
* !#zh
* 节点的分组。
* 节点的分组将关系到节点的碰撞组件可以与哪些碰撞组件相碰撞。
* @property group
* @type {String}
*/
group: {
get () {
return cc.game.groupList[this.groupIndex] || '';
},
set (value) {
this.groupIndex = cc.game.groupList.indexOf(value);
this.emit('group-changed');
}
},
//properties moved from base node begin
/**
* !#en x axis position of node.
* !#zh 节点 X 轴坐标。
* @property x
* @type {Number}
* @example
* node.x = 100;
* cc.log("Node Position X: " + node.x);
*/
x: {
get () {
return this._position.x;
},
set (value) {
var localPosition = this._position;
if (value !== localPosition.x) {
if (!CC_EDITOR || isFinite(value)) {
if (CC_EDITOR) {
var oldValue = localPosition.x;
}
localPosition.x = value;
this._sgNode.setPositionX(value);
// fast check event
var capListeners = this._capturingListeners &&
this._capturingListeners._callbackTable[POSITION_CHANGED];
var bubListeners = this._bubblingListeners &&
this._bubblingListeners._callbackTable[POSITION_CHANGED];
if ((capListeners && capListeners.length > 0) || (bubListeners && bubListeners.length > 0)) {
// send event
if (CC_EDITOR) {
this.emit(POSITION_CHANGED, new cc.Vec2(oldValue, localPosition.y));
}
else {
this.emit(POSITION_CHANGED);
}
}
}
else {
cc.error(ERR_INVALID_NUMBER, 'new x');
}
}
},
},
/**
* !#en y axis position of node.
* !#zh 节点 Y 轴坐标。
* @property y
* @type {Number}
* @example
* node.y = 100;
* cc.log("Node Position Y: " + node.y);
*/
y: {
get () {
return this._position.y;
},
set (value) {
var localPosition = this._position;
if (value !== localPosition.y) {
if (!CC_EDITOR || isFinite(value)) {
if (CC_EDITOR) {
var oldValue = localPosition.y;
}
localPosition.y = value;
this._sgNode.setPositionY(value);
// fast check event
var capListeners = this._capturingListeners &&
this._capturingListeners._callbackTable[POSITION_CHANGED];
var bubListeners = this._bubblingListeners &&
this._bubblingListeners._callbackTable[POSITION_CHANGED];
if ((capListeners && capListeners.length > 0) || (bubListeners && bubListeners.length > 0)) {
// send event
if (CC_EDITOR) {
this.emit(POSITION_CHANGED, new cc.Vec2(localPosition.x, oldValue));
}
else {
this.emit(POSITION_CHANGED);
}
}
}
else {
cc.error(ERR_INVALID_NUMBER, 'new y');
}
}
},
},
/**
* !#en Rotation of node.
* !#zh 该节点旋转角度。
* @property rotation
* @type {Number}
* @example
* node.rotation = 90;
* cc.log("Node Rotation: " + node.rotation);
*/
rotation: {
get () {
if (this._rotationX !== this._rotationY)
cc.logID(1602);
return this._rotationX;
},
set (value) {
if (this._rotationX !== value || this._rotationY !== value) {
this._rotationX = this._rotationY = value;
this._sgNode.rotation = value;
this.emit(ROTATION_CHANGED);
}
}
},
/**
* !#en Rotation on x axis.
* !#zh 该节点 X 轴旋转角度。
* @property rotationX
* @type {Number}
* @example
* node.rotationX = 45;
* cc.log("Node Rotation X: " + node.rotationX);
*/
rotationX: {
get () {
return this._rotationX;
},
set (value) {
if (this._rotationX !== value) {
this._rotationX = value;
this._sgNode.rotationX = value;
this.emit(ROTATION_CHANGED);
}
},
},
/**
* !#en Rotation on y axis.
* !#zh 该节点 Y 轴旋转角度。
* @property rotationY
* @type {Number}
* @example
* node.rotationY = 45;
* cc.log("Node Rotation Y: " + node.rotationY);
*/
rotationY: {
get () {
return this._rotationY;
},
set (value) {
if (this._rotationY !== value) {
this._rotationY = value;
this._sgNode.rotationY = value;
this.emit(ROTATION_CHANGED);
}
},
},
/**
* !#en Scale on x axis.
* !#zh 节点 X 轴缩放。
* @property scaleX
* @type {Number}
* @example
* node.scaleX = 0.5;
* cc.log("Node Scale X: " + node.scaleX);
*/
scaleX: {
get () {
return this._scaleX;
},
set (value) {
if (this._scaleX !== value) {
this._scaleX = value;
this._sgNode.scaleX = value;
this.emit(SCALE_CHANGED);
}
},
},
/**
* !#en Scale on y axis.
* !#zh 节点 Y 轴缩放。
* @property scaleY
* @type {Number}
* @example
* node.scaleY = 0.5;
* cc.log("Node Scale Y: " + node.scaleY);
*/
scaleY: {
get () {
return this._scaleY;
},
set (value) {
if (this._scaleY !== value) {
this._scaleY = value;
this._sgNode.scaleY = value;
this.emit(SCALE_CHANGED);
}
},
},
/**
* !#en Skew x
* !#zh 该节点 Y 轴倾斜角度。
* @property skewX
* @type {Number}
* @example
* node.skewX = 0;
* cc.log("Node SkewX: " + node.skewX);
*/
skewX: {
get () {
return this._skewX;
},
set (value) {
this._skewX = value;
this._sgNode.skewX = value;
}
},
/**
* !#en Skew y
* !#zh 该节点 X 轴倾斜角度。
* @property skewY
* @type {Number}
* @example
* node.skewY = 0;
* cc.log("Node SkewY: " + node.skewY);
*/
skewY: {
get () {
return this._skewY;
},
set (value) {
this._skewY = value;
this._sgNode.skewY = value;
}
},
/**
* !#en Opacity of node, default value is 255.
* !#zh 节点透明度,默认值为 255。
* @property opacity
* @type {Number}
* @example
* node.opacity = 255;
*/
opacity: {
get () {
return this._opacity;
},
set (value) {
if (this._opacity !== value) {
this._opacity = value;
this._sgNode.setOpacity(value);
if (!this._cascadeOpacityEnabled) {
var sizeProvider = this._sizeProvider;
if (sizeProvider instanceof _ccsg.Node && sizeProvider !== this._sgNode) {
sizeProvider.setOpacity(value);
}
}
}
},
range: [0, 255]
},
/**
* !#en Indicate whether node's opacity value affect its child nodes, default value is true.
* !#zh 节点的不透明度值是否影响其子节点,默认值为 true。
* @property cascadeOpacity
* @type {Boolean}
* @example
* cc.log("CascadeOpacity: " + node.cascadeOpacity);
*/
cascadeOpacity: {
get () {
return this._cascadeOpacityEnabled;
},
set (value) {
if (this._cascadeOpacityEnabled !== value) {
this._cascadeOpacityEnabled = value;
this._sgNode.cascadeOpacity = value;
var opacity = value ? 255 : this._opacity;
var sizeProvider = this._sizeProvider;
if (sizeProvider instanceof _ccsg.Node) {
sizeProvider.setOpacity(opacity);
}
}
},
},
/**
* !#en Color of node, default value is white: (255, 255, 255).
* !#zh 节点颜色。默认为白色,数值为:(255,255,255)。
* @property color
* @type {Color}
* @example
* node.color = new cc.Color(255, 255, 255);
*/
color: {
get () {
return this._color.clone()
},
set (value) {
if (!this._color.equals(value)) {
this._color.fromColor(value);
if (CC_DEV && value.a !== 255) {
cc.warnID(1626);
}
if (this._sizeProvider instanceof _ccsg.Node) {
this._sizeProvider.setColor(value);
}
}
},
},
/**
* !#en Anchor point's position on x axis.
* !#zh 节点 X 轴锚点位置。
* @property anchorX
* @type {Number}
* @example
* node.anchorX = 0;
*/
anchorX: {
get () {
return this._anchorPoint.x;
},
set (value) {
var anchorPoint = this._anchorPoint;
if (anchorPoint.x !== value) {
anchorPoint.x = value;
var sizeProvider = this._sizeProvider;
if (sizeProvider instanceof _ccsg.Node) {
sizeProvider.setAnchorPoint(anchorPoint);
}
this.emit(ANCHOR_CHANGED);
}
},
},
/**
* !#en Anchor point's position on y axis.
* !#zh 节点 Y 轴锚点位置。
* @property anchorY
* @type {Number}
* @example
* node.anchorY = 0;
*/
anchorY: {
get () {
return this._anchorPoint.y;
},
set (value) {
var anchorPoint = this._anchorPoint;
if (anchorPoint.y !== value) {
anchorPoint.y = value;
var sizeProvider = this._sizeProvider;
if (sizeProvider instanceof _ccsg.Node) {
sizeProvider.setAnchorPoint(anchorPoint);
}
this.emit(ANCHOR_CHANGED);
}
},
},
/**
* !#en Width of node.
* !#zh 节点宽度。
* @property width
* @type {Number}
* @example
* node.width = 100;
*/
width: {
get () {
if (this._sizeProvider) {
var w = this._sizeProvider._getWidth();
this._contentSize.width = w;
return w;
}
else {
return this._contentSize.width;
}
},
set (value) {
if (value !== this._contentSize.width) {
var sizeProvider = this._sizeProvider;
if (sizeProvider) {
sizeProvider.setContentSize(value, sizeProvider._getHeight());
}
if (CC_EDITOR) {
var clone = cc.size(this._contentSize);
}
this._contentSize.width = value;
if (CC_EDITOR) {
this.emit(SIZE_CHANGED, clone);
}
else {
this.emit(SIZE_CHANGED);
}
}
},
},
/**
* !#en Height of node.
* !#zh 节点高度。
* @property height
* @type {Number}
* @example
* node.height = 100;
*/
height: {
get () {
if (this._sizeProvider) {
var h = this._sizeProvider._getHeight();
this._contentSize.height = h;
return h;
}
else {
return this._contentSize.height;
}
},
set (value) {
if (value !== this._contentSize.height) {
var sizeProvider = this._sizeProvider;
if (sizeProvider) {
sizeProvider.setContentSize(sizeProvider._getWidth(), value);
}
if (CC_EDITOR) {
var clone = cc.size(this._contentSize);
}
this._contentSize.height = value;
if (CC_EDITOR) {
this.emit(SIZE_CHANGED, clone);
}
else {
this.emit(SIZE_CHANGED);
}
}
},
},
/**
* Indicate whether ignore the anchor point property for positioning.
* @property _ignoreAnchor
* @type {Boolean}
* @private
*/
_ignoreAnchor: {
get () {
return this.__ignoreAnchor;
},
set (value) {
if (this.__ignoreAnchor !== value) {
this.__ignoreAnchor = value;
this._sgNode.ignoreAnchor = value;
var sizeProvider = this._sizeProvider;
if (sizeProvider instanceof _ccsg.Node && sizeProvider !== this._sgNode) {
sizeProvider.ignoreAnchor = value;
}
this.emit(ANCHOR_CHANGED);
}
},
},
/**
* !#en Z order in depth which stands for the drawing order.
* !#zh 该节点渲染排序的 Z 轴深度。
* @property zIndex
* @type {Number}
* @example
* node.zIndex = 1;
* cc.log("Node zIndex: " + node.zIndex);
*/
zIndex: {
get () {
return this._localZOrder;
},
set (value) {
if (this._localZOrder !== value) {
this._localZOrder = value;
this._sgNode.zIndex = value;
if (this._parent) {
updateOrder(this);
}
}
}
},
//properties moved from base node end
},
ctor (name) {
/**
* Current scene graph node for this node.
*
* @property _sgNode
* @type {_ccsg.Node}
* @private
*/
var sgNode = this._sgNode = new _ccsg.Node();
if (CC_JSB) {
sgNode.retain();
sgNode._entity = this;
sgNode.onEnter = function () {
_ccsg.Node.prototype.onEnter.call(this);
if (this._entity && !this._entity._active) {
cc.director.getActionManager().pauseTarget(this);
cc.eventManager.pauseTarget(this);
}
};
}
if (!cc.game._isCloning) {
sgNode.cascadeOpacity = true;
}
/**
* Current active size provider for this node.
* Size provider can equals to this._sgNode.
*
* @property _sizeProvider
* @type {_ccsg.Node}
* @private
*/
this._sizeProvider = null;
this.__ignoreAnchor = false;
this._reorderChildDirty = false;
// cache component
this._widget = null;
// Touch event listener
this._touchListener = null;
// Mouse event listener
this._mouseListener = null;
// Retained actions for JSB
if (CC_JSB) {
this._retainedActions = [];
}
},
statics: {
// is node but not scene
isNode (obj) {
return obj instanceof Node && (obj.constructor === Node || !(obj instanceof cc.Scene));
}
},
// OVERRIDES
_onSetParent (value) {
var sgNode = this._sgNode;
if (sgNode.parent) {
sgNode.parent.removeChild(sgNode, false);
}
if (value) {
value._sgNode.addChild(sgNode);
value._delaySort();
}
},
_onSiblingIndexChanged (index) {
// update rendering scene graph, sort them by arrivalOrder
var parent = this._parent;
var siblings = parent._children;
var i = 0, len = siblings.length, sibling;
if (CC_JSB) {
if (cc.runtime) {
for (; i < len; i++) {
sibling = siblings[i]._sgNode;
// Reset zorder to update their arrival order
var zOrder = sibling.getLocalZOrder();
sibling.setLocalZOrder(zOrder + 1);
sibling.setLocalZOrder(zOrder);
}
}
else {
parent._sgNode.removeChild(this._sgNode, false);
if (index + 1 < siblings.length) {
var nextSibling = siblings[index + 1];
parent._sgNode.insertChildBefore(this._sgNode, nextSibling._sgNode);
}
else {
parent._sgNode.addChild(this._sgNode);
}
}
}
else {
for (; i < len; i++) {
sibling = siblings[i]._sgNode;
sibling._arrivalOrder = i;
cc.eventManager._setDirtyForNode(sibling);
}
cc.renderer.childrenOrderDirty = true;
parent._sgNode._reorderChildDirty = true;
parent._delaySort();
}
},
_onPreDestroy () {
var destroyByParent = this._onPreDestroyBase();
cc.director.getActionManager().removeAllActionsFromTarget(this);
// Remove Node.currentHovered
if (_currentHovered === this) {
_currentHovered = null;
}
if (CC_JSB) {
this._releaseAllActions();
if (this._touchListener) {
this._touchListener.release();
this._touchListener.owner = null;
this._touchListener.mask = null;
this._touchListener = null;
}
if (this._mouseListener) {
this._mouseListener.release();
this._mouseListener.owner = null;
this._mouseListener.mask = null;
this._mouseListener = null;
}
}
if (this._reorderChildDirty) {
cc.director.__fastOff(cc.Director.EVENT_AFTER_UPDATE, this.sortAllChildren, this);
}
cc.eventManager.removeListeners(this);
if (!destroyByParent) {
this._removeSgNode();
// simulate some destruct logic to make undo system work correctly
if (CC_EDITOR) {
// ensure this node can reattach to scene by undo system
this._parent = null;
}
}
else if (CC_JSB) {
this._sgNode.release();
this._sgNode._entity = null;
this._sgNode = null;
}
},
_onPostActivated (active) {
if (active) {
// activate
cc.director.getActionManager().resumeTarget(this);
cc.eventManager.resumeTarget(this);
if (this._touchListener) {
var mask = this._touchListener.mask = _searchMaskInParent(this);
if (this._mouseListener) {
this._mouseListener.mask = mask;
}
}
else if (this._mouseListener) {
this._mouseListener.mask = _searchMaskInParent(this);
}
}
else {
// deactivate
cc.director.getActionManager().pauseTarget(this);
cc.eventManager.pauseTarget(this);
}
},
_onHierarchyChanged (oldParent) {
this._onHierarchyChangedBase(oldParent);
cc._widgetManager._nodesOrderDirty = true;
},
// INTERNAL
/*
* The initializer for Node which will be called before all components onLoad
*/
_onBatchCreated () {
var prefabInfo = this._prefab;
if (prefabInfo && prefabInfo.sync && !prefabInfo._synced) {
// checks to ensure no recursion, recursion will caused only on old data.
if (prefabInfo.root === this) {
PrefabHelper.syncWithPrefab(this);
}
}
this._updateDummySgNode();
if (this._parent) {
this._parent._sgNode.addChild(this._sgNode);
}
if (!this._activeInHierarchy) {
// deactivate ActionManager and EventManager by default
cc.director.getActionManager().pauseTarget(this);
cc.eventManager.pauseTarget(this);
}
var children = this._children;
for (var i = 0, len = children.length; i < len; i++) {
children[i]._onBatchCreated();
}
},
// EVENTS
/**
* !#en
* Register a callback of a specific event type on Node.
* Use this method to register touch or mouse event permit propagation based on scene graph,
* you can propagate the event to the parents or swallow it by calling stopPropagation on the event.
* It's the recommended way to register touch/mouse event for Node,
* please do not use cc.eventManager directly for Node.
* !#zh
* 在节点上注册指定类型的回调函数,也可以设置 target 用于绑定响应函数的调用者。
* 同时您可以将事件派发到父节点或者通过调用 stopPropagation 拦截它。
* 推荐使用这种方式来监听节点上的触摸或鼠标事件,请不要在节点上直接使用 cc.eventManager。
* @method on
* @param {String} type - A string representing the event type to listen for.
* See {{#crossLink "Node/position-changed:event"}}Node Events{{/crossLink}} for all builtin events.
* @param {Function} callback - The callback that will be invoked when the event is dispatched.
* The callback is ignored if it is a duplicate (the callbacks are unique).
* @param {Event} callback.param event
* @param {Object} [target] - The target to invoke the callback, can be null
* @param {Boolean} useCapture - When set to true, the capture argument prevents callback
* from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE.
* When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE.
* Either way, callback will be invoked when event's eventPhase attribute value is AT_TARGET.
* @return {Function} - Just returns the incoming callback so you can save the anonymous function easier.
* @example
* this.node.on(cc.Node.EventType.TOUCH_START, this.memberFunction, this); // if "this" is component and the "memberFunction" declared in CCClass.
* node.on(cc.Node.EventType.TOUCH_START, callback, this.node);
* node.on(cc.Node.EventType.TOUCH_MOVE, callback, this.node);
* node.on(cc.Node.EventType.TOUCH_END, callback, this.node);
* node.on(cc.Node.EventType.TOUCH_CANCEL, callback, this.node);
* node.on("anchor-changed", callback, this);
*/
on (type, callback, target, useCapture) {
var newAdded = false;
if (_touchEvents.indexOf(type) !== -1) {
if (!this._touchListener) {
this._touchListener = cc.EventListener.create({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true,
owner: this,
mask: _searchMaskInParent(this),
onTouchBegan: _touchStartHandler,
onTouchMoved: _touchMoveHandler,
onTouchEnded: _touchEndHandler
});
if (CC_JSB) {
this._touchListener.retain();
}
cc.eventManager.addListener(this._touchListener, this);
newAdded = true;
}
}
else if (_mouseEvents.indexOf(type) !== -1) {
if (!this._mouseListener) {
this._mouseListener = cc.EventListener.create({
event: cc.EventListener.MOUSE,
_previousIn: false,
owner: this,
mask: _searchMaskInParent(this),
onMouseDown: _mouseDownHandler,
onMouseMove: _mouseMoveHandler,
onMouseUp: _mouseUpHandler,
onMouseScroll: _mouseWheelHandler,
});
if (CC_JSB) {
this._mouseListener.retain();
}
cc.eventManager.addListener(this._mouseListener, this);
newAdded = true;
}
}
if (newAdded && !this._activeInHierarchy) {
cc.director.getScheduler().schedule(function () {
if (!this._activeInHierarchy) {
cc.eventManager.pauseTarget(this);
}
}, this, 0, 0, 0, false);
}
return this._EventTargetOn(type, callback, target, useCapture);
},
/**
* !#en
* Removes the callback previously registered with the same type, callback, target and or useCapture.
* This method is merely an alias to removeEventListener.
* !#zh 删除之前与同类型,回调,目标或 useCapture 注册的回调。
* @method off
* @param {String} type - A string representing the event type being removed.
* @param {Function} callback - The callback to remove.
* @param {Object} [target] - The target to invoke the callback, if it's not given, only callback without target will be removed
* @param {Boolean} useCapture - Specifies whether the callback being removed was registered as a capturing callback or not.
* If not specified, useCapture defaults to false. If a callback was registered twice,
* one with capture and one without, each must be removed separately. Removal of a capturing callback
* does not affect a non-capturing version of the same listener, and vice versa.
* @example
* this.node.off(cc.Node.EventType.TOUCH_START, this.memberFunction, this);
* node.off(cc.Node.EventType.TOUCH_START, callback, this.node);
* node.off("anchor-changed", callback, this);
*/
off (type, callback, target, useCapture) {
this._EventTargetOff(type, callback, target, useCapture);
if (_touchEvents.indexOf(type) !== -1) {
this._checkTouchListeners();
}
else if (_mouseEvents.indexOf(type) !== -1) {
this._checkMouseListeners();
}
},
/**
* !#en Removes all callbacks previously registered with the same target.
* !#zh 移除目标上的所有注册事件。
* @method targetOff
* @param {Object} target - The target to be searched for all related callbacks
* @example
* node.targetOff(target);
*/
targetOff (target) {
this._EventTargetTargetOff(target);
this._checkTouchListeners();
this._checkMouseListeners();
},
_checkTouchListeners () {
if (!(this._objFlags & Destroying) && this._touchListener) {
var i = 0;
if (this._bubblingListeners) {
for (; i < _touchEvents.length; ++i) {
if (this._bubblingListeners.has(_touchEvents[i])) {
return;
}
}
}
if (this._capturingListeners) {
for (; i < _touchEvents.length; ++i) {
if (this._capturingListeners.has(_touchEvents[i])) {
return;
}
}
}
cc.eventManager.removeListener(this._touchListener);
this._touchListener = null;
}
},
_checkMouseListeners () {
if (!(this._objFlags & Destroying) && this._mouseListener) {
var i = 0;
if (this._bubblingListeners) {
for (; i < _mouseEvents.length; ++i) {
if (this._bubblingListeners.has(_mouseEvents[i])) {
return;
}
}
}
if (this._capturingListeners) {
for (; i < _mouseEvents.length; ++i) {
if (this._capturingListeners.has(_mouseEvents[i])) {
return;
}
}
}
if (_currentHovered === this) {
_currentHovered = null;
}
cc.eventManager.removeListener(this._mouseListener);
this._mouseListener = null;
}
},
_hitTest (point, listener) {
var w = this.width,
h = this.height;
var rect = cc.rect(0, 0, w, h);
var trans = this.getNodeToWorldTransform();
cc._rectApplyAffineTransformIn(rect, trans);
var left = point.x - rect.x,
right = rect.x + rect.width - point.x,
bottom = point.y - rect.y,
top = rect.y + rect.height - point.y;
if (left >= 0 && right >= 0 && top >= 0 && bottom >= 0) {
if (listener && listener.mask) {
var mask = listener.mask;
var parent = this;
for (var i = 0; parent && i < mask.index; ++i, parent = parent.parent) {
}
// find mask parent, should hit test it
if (parent === mask.node) {
var comp = parent.getComponent(cc.Mask);
return (comp && comp.enabledInHierarchy) ? comp._hitTest(point) : true;
}
// mask parent no longer exists
else {
listener.mask = null;
return true;
}
}
else {
return true;
}
}
else {
return false;
}
},
// Store all capturing parents that are listening to the same event in the array
_getCapturingTargets (type, array) {
var parent = this.parent;
while (parent) {
if (parent.hasEventListener(type, true)) {
array.push(parent);
}
parent = parent.parent;
}
},
// Store all bubbling parents that are listening to the same event in the array
_getBubblingTargets (type, array) {
var parent = this.parent;
while (parent) {
if (parent.hasEventListener(type)) {
array.push(parent);
}
parent = parent.parent;
}
},
// for event manager
isRunning () {
return this._activeInHierarchy;
},
// ACTIONS
/**
* !#en
* Executes an action, and returns the action that is executed.
* The node becomes the action's target. Refer to cc.Action's getTarget()
* Calling runAction while the node is not active won't have any effect.
* Note:You shouldn't modify the action after runAction, that won't take any effect.
* if you want to modify, when you define action plus.
* !#zh
* 执行并返回该执行的动作。该节点将会变成动作的目标。
* 调用 runAction 时,节点自身处于不激活状态将不会有任何效果。
* 注意:你不应该修改 runAction 后的动作,将无法发挥作用,如果想进行修改,请在定义 action 时加入。
* @method runAction
* @param {Action} action
* @return {Action} An Action pointer
* @example
* var action = cc.scaleTo(0.2, 1, 0.6);
* node.runAction(action);
* node.runAction(action).repeatForever(); // fail
* node.runAction(action.repeatForever()); // right
*/
runAction (action) {
if (!this.active)
return;
cc.assertID(action, 1618);
if (!cc.macro.ENABLE_GC_FOR_NATIVE_OBJECTS) {
this._retainAction(action);
}
if (CC_JSB) {
this._sgNode._owner = this;
}
cc.director.getActionManager().addAction(action, this, false);
return action;
},
/**
* !#en Stops and removes all actions from the running action list .
* !#zh 停止并且移除所有正在运行的动作列表。
* @method stopAllActions
* @example
* node.stopAllActions();
*/
stopAllActions () {
cc.director.getActionManager().removeAllActionsFromTarget(this);
},
/**
* !#en Stops and removes an action from the running action list.
* !#zh 停止并移除指定的动作。
* @method stopAction
* @param {Action} action An action object to be removed.
* @example
* var action = cc.scaleTo(0.2, 1, 0.6);
* node.stopAction(action);
*/
stopAction (action) {
cc.director.getActionManager().removeAction(action);
},
/**
* !#en Removes an action from the running action list by its tag.
* !#zh 停止并且移除指定标签的动作。
* @method stopActionByTag
* @param {Number} tag A tag that indicates the action to be removed.
* @example
* node.stopAction(1);
*/
stopActionByTag (tag) {
if (tag === cc.Action.TAG_INVALID) {
cc.logID(1612);
return;
}
cc.director.getActionManager().removeActionByTag(tag, this);
},
/**
* !#en Returns an action from the running action list by its tag.
* !#zh 通过标签获取指定动作。
* @method getActionByTag
* @see cc.Action#getTag and cc.Action#setTag
* @param {Number} tag
* @return {Action} The action object with the given tag.
* @example
* var action = node.getActionByTag(1);
*/
getActionByTag (tag) {
if (tag === cc.Action.TAG_INVALID) {
cc.logID(1613);
return null;
}
return cc.director.getActionManager().getActionByTag(tag, this);
},
/**
* !#en
* Returns the numbers of actions that are running plus the ones that are schedule to run (actions in actionsToAdd and actions arrays).
* Composable actions are counted as 1 action. Example:
* If you are running 1 Sequence of 7 actions, it will return 1.
* If you are running 7 Sequences of 2 actions, it will return 7.