/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
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';
const BaseNode = require('./utils/base-node');
const PrefabHelper = require('./utils/prefab-helper');
const mathPools = require('./utils/math-pools');
const renderEngine = require('./renderer/render-engine');
const AffineTrans = require('./utils/affine-transform');
const math = renderEngine.math;
const eventManager = require('./event-manager');
const macro = require('./platform/CCMacro');
const misc = require('./utils/misc');
const js = require('./platform/js');
const Event = require('./event/event');
const EventTarget = require('./event/event-target');
const RenderFlow = require('./renderer/render-flow');
const Flags = cc.Object.Flags;
const Destroying = Flags.Destroying;
const ERR_INVALID_NUMBER = CC_EDITOR && 'The %s is invalid';
const ONE_DEGREE = Math.PI / 180;
var ActionManagerExist = !!cc.ActionManager;
var emptyFunc = function () {};
var _mat4_temp = math.mat4.create();
var _vec3_temp = math.vec3.create();
var _quat_temp = math.quat.create();
var _globalOrderOfArrival = 1;
var _cachedArray = new Array(16);
_cachedArray.length = 0;
const POSITION_ON = 1 << 0;
const SCALE_ON = 1 << 1;
const ROTATION_ON = 1 << 2;
const SIZE_ON = 1 << 3;
const ANCHOR_ON = 1 << 4;
let BuiltinGroupIndex = cc.Enum({
DEBUG: 31
})
/**
* !#en Node's local dirty properties flag
* !#zh Node 的本地属性 dirty 状态位
* @enum Node._LocalDirtyFlag
* @static
* @private
* @namespace Node
*/
var LocalDirtyFlag = cc.Enum({
/**
* !#en Flag for position dirty
* !#zh 位置 dirty 的标记位
* @property {Number} POSITION
* @static
*/
POSITION: 1 << 0,
/**
* !#en Flag for scale dirty
* !#zh 缩放 dirty 的标记位
* @property {Number} SCALE
* @static
*/
SCALE: 1 << 1,
/**
* !#en Flag for rotation dirty
* !#zh 旋转 dirty 的标记位
* @property {Number} ROTATION
* @static
*/
ROTATION: 1 << 2,
/**
* !#en Flag for skew dirty
* !#zh skew dirty 的标记位
* @property {Number} SKEW
* @static
*/
SKEW: 1 << 3,
/**
* !#en Flag for position or rotation dirty
* !#zh 旋转或位置 dirty 的标记位
* @property {Number} RT
* @static
*/
RT: 1 << 0 | 1 << 1 | 1 << 2,
/**
* !#en Flag for all dirty properties
* !#zh 覆盖所有 dirty 状态的标记位
* @property {Number} ALL
* @static
*/
ALL: 0xffff,
});
/**
* !#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 {String} TOUCH_START
* @static
*/
TOUCH_START: 'touchstart',
/**
* !#en The event type for touch move event, you can use its value directly: 'touchmove'
* !#zh 当手指在屏幕上移动时。
* @property {String} TOUCH_MOVE
* @static
*/
TOUCH_MOVE: 'touchmove',
/**
* !#en The event type for touch end event, you can use its value directly: 'touchend'
* !#zh 当手指在目标节点区域内离开屏幕时。
* @property {String} TOUCH_END
* @static
*/
TOUCH_END: 'touchend',
/**
* !#en The event type for touch end event, you can use its value directly: 'touchcancel'
* !#zh 当手指在目标节点区域外离开屏幕时。
* @property {String} TOUCH_CANCEL
* @static
*/
TOUCH_CANCEL: 'touchcancel',
/**
* !#en The event type for mouse down events, you can use its value directly: 'mousedown'
* !#zh 当鼠标按下时触发一次。
* @property {String} MOUSE_DOWN
* @static
*/
MOUSE_DOWN: 'mousedown',
/**
* !#en The event type for mouse move events, you can use its value directly: 'mousemove'
* !#zh 当鼠标在目标节点在目标节点区域中移动时,不论是否按下。
* @property {String} MOUSE_MOVE
* @static
*/
MOUSE_MOVE: 'mousemove',
/**
* !#en The event type for mouse enter target events, you can use its value directly: 'mouseenter'
* !#zh 当鼠标移入目标节点区域时,不论是否按下。
* @property {String} MOUSE_ENTER
* @static
*/
MOUSE_ENTER: 'mouseenter',
/**
* !#en The event type for mouse leave target events, you can use its value directly: 'mouseleave'
* !#zh 当鼠标移出目标节点区域时,不论是否按下。
* @property {String} MOUSE_LEAVE
* @static
*/
MOUSE_LEAVE: 'mouseleave',
/**
* !#en The event type for mouse up events, you can use its value directly: 'mouseup'
* !#zh 当鼠标从按下状态松开时触发一次。
* @property {String} MOUSE_UP
* @static
*/
MOUSE_UP: 'mouseup',
/**
* !#en The event type for mouse wheel events, you can use its value directly: 'mousewheel'
* !#zh 当鼠标滚轮滚动时。
* @property {String} MOUSE_WHEEL
* @static
*/
MOUSE_WHEEL: 'mousewheel',
/**
* !#en The event type for position change events.
* Performance note, this event will be triggered every time corresponding properties being changed,
* if the event callback have heavy logic it may have great performance impact, try to avoid such scenario.
* !#zh 当节点位置改变时触发的事件。
* 性能警告:这个事件会在每次对应的属性被修改时触发,如果事件回调损耗较高,有可能对性能有很大的负面影响,请尽量避免这种情况。
* @property {String} POSITION_CHANGED
* @static
*/
POSITION_CHANGED: 'position-changed',
/**
* !#en The event type for rotation change events.
* Performance note, this event will be triggered every time corresponding properties being changed,
* if the event callback have heavy logic it may have great performance impact, try to avoid such scenario.
* !#zh 当节点旋转改变时触发的事件。
* 性能警告:这个事件会在每次对应的属性被修改时触发,如果事件回调损耗较高,有可能对性能有很大的负面影响,请尽量避免这种情况。
* @property {String} ROTATION_CHANGED
* @static
*/
ROTATION_CHANGED: 'rotation-changed',
/**
* !#en The event type for scale change events.
* Performance note, this event will be triggered every time corresponding properties being changed,
* if the event callback have heavy logic it may have great performance impact, try to avoid such scenario.
* !#zh 当节点缩放改变时触发的事件。
* 性能警告:这个事件会在每次对应的属性被修改时触发,如果事件回调损耗较高,有可能对性能有很大的负面影响,请尽量避免这种情况。
* @property {String} SCALE_CHANGED
* @static
*/
SCALE_CHANGED: 'scale-changed',
/**
* !#en The event type for size change events.
* Performance note, this event will be triggered every time corresponding properties being changed,
* if the event callback have heavy logic it may have great performance impact, try to avoid such scenario.
* !#zh 当节点尺寸改变时触发的事件。
* 性能警告:这个事件会在每次对应的属性被修改时触发,如果事件回调损耗较高,有可能对性能有很大的负面影响,请尽量避免这种情况。
* @property {String} SIZE_CHANGED
* @static
*/
SIZE_CHANGED: 'size-changed',
/**
* !#en The event type for anchor point change events.
* Performance note, this event will be triggered every time corresponding properties being changed,
* if the event callback have heavy logic it may have great performance impact, try to avoid such scenario.
* !#zh 当节点锚点改变时触发的事件。
* 性能警告:这个事件会在每次对应的属性被修改时触发,如果事件回调损耗较高,有可能对性能有很大的负面影响,请尽量避免这种情况。
* @property {String} ANCHOR_CHANGED
* @static
*/
ANCHOR_CHANGED: 'anchor-changed',
/**
* !#en The event type for new child added events.
* !#zh 当新的子节点被添加时触发的事件。
* @property {String} CHILD_ADDED
* @static
*/
CHILD_ADDED: 'child-added',
/**
* !#en The event type for child removed events.
* !#zh 当子节点被移除时触发的事件。
* @property {String} CHILD_REMOVED
* @static
*/
CHILD_REMOVED: 'child-removed',
/**
* !#en The event type for children reorder events.
* !#zh 当子节点顺序改变时触发的事件。
* @property {String} CHILD_REORDER
* @static
*/
CHILD_REORDER: 'child-reorder',
/**
* !#en The event type for node group changed events.
* !#zh 当节点归属群组发生变化时触发的事件。
* @property {String} GROUP_CHANGED
* @static
*/
GROUP_CHANGED: 'group-changed',
});
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 _touchCancelHandler = function (touch, event) {
var pos = touch.getLocation();
var node = this.owner;
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;
event.bubbles = true;
node.dispatchEvent(event);
}
};
var _mouseMoveHandler = function (event) {
var pos = event.getLocation();
var node = this.owner;
var hit = node._hitTest(pos, this);
if (hit) {
if (!this._previousIn) {
// Fix issue when hover node switched, previous hovered node won't get MOUSE_LEAVE notification
if (_currentHovered && _currentHovered._mouseListener) {
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;
event.bubbles = true;
node.dispatchEvent(event);
}
else if (this._previousIn) {
event.type = EventType.MOUSE_LEAVE;
node.dispatchEvent(event);
this._previousIn = false;
_currentHovered = null;
}
else {
// continue dispatching
return;
}
// Event processed, cleanup
event.stopPropagation();
};
var _mouseUpHandler = function (event) {
var pos = event.getLocation();
var node = this.owner;
if (node._hitTest(pos, this)) {
event.type = EventType.MOUSE_UP;
event.bubbles = true;
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;
event.bubbles = true;
node.dispatchEvent(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 _checkListeners (node, events) {
if (!(node._objFlags & Destroying)) {
var i = 0;
if (node._bubblingListeners) {
for (; i < events.length; ++i) {
if (node._bubblingListeners.hasEventListener(events[i])) {
return true;
}
}
}
if (node._capturingListeners) {
for (; i < events.length; ++i) {
if (node._capturingListeners.hasEventListener(events[i])) {
return true;
}
}
}
return false;
}
return true;
}
function _doDispatchEvent (owner, event) {
var target, i;
event.target = owner;
// Event.CAPTURING_PHASE
_cachedArray.length = 0;
owner._getCapturingTargets(event.type, _cachedArray);
// capturing
event.eventPhase = 1;
for (i = _cachedArray.length - 1; i >= 0; --i) {
target = _cachedArray[i];
if (target._capturingListeners) {
event.currentTarget = target;
// fire event
target._capturingListeners.invoke(event, _cachedArray);
// check if propagation stopped
if (event._propagationStopped) {
_cachedArray.length = 0;
return;
}
}
}
_cachedArray.length = 0;
// Event.AT_TARGET
// checks if destroyed in capturing callbacks
event.eventPhase = 2;
event.currentTarget = owner;
if (owner._capturingListeners) {
owner._capturingListeners.invoke(event);
}
if (!event._propagationImmediateStopped && owner._bubblingListeners) {
owner._bubblingListeners.invoke(event);
}
if (!event._propagationStopped && event.bubbles) {
// Event.BUBBLING_PHASE
owner._getBubblingTargets(event.type, _cachedArray);
// propagate
event.eventPhase = 3;
for (i = 0; i < _cachedArray.length; ++i) {
target = _cachedArray[i];
if (target._bubblingListeners) {
event.currentTarget = target;
// fire event
target._bubblingListeners.invoke(event);
// check if propagation stopped
if (event._propagationStopped) {
_cachedArray.length = 0;
return;
}
}
}
}
_cachedArray.length = 0;
};
/**
* !#en
* Class of all entities in Cocos Creator scenes.
* For events supported by Node, please refer to {{#crossLink "Node.EventType"}}{{/crossLink}}
* !#zh
* Cocos Creator 场景中的所有节点类。
* 支持的节点事件,请参阅 {{#crossLink "Node.EventType"}}{{/crossLink}}。
* @class Node
* @extends _BaseNode
*/
var Node = cc.Class({
name: 'cc.Node',
extends: BaseNode,
properties: {
// SERIALIZABLE
_opacity: 255,
_color: cc.Color.WHITE,
_contentSize: cc.Size,
_anchorPoint: cc.v2(0.5, 0.5),
_position: cc.Vec3,
_scaleX: {
default: undefined,
type: cc.Float
},
_scaleY: {
default: undefined,
type: cc.Float
},
_scale: cc.Vec3,
_rotationX: 0.0,
_rotationY: 0.0,
_quat: cc.Quat,
_skewX: 0.0,
_skewY: 0.0,
_localZOrder: 0,
// 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(EventType.GROUP_CHANGED);
}
},
//properties moved from base node begin
/**
* !#en The position (x, y) of the node in its parent's coordinates.
* !#zh 节点在父节点坐标系中的位置(x, y)。
* @property {Vec2} position
* @example
* cc.log("Node Position: " + node.position);
*/
/**
* !#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.setLocalDirty(LocalDirtyFlag.POSITION);
this._renderFlag |= RenderFlow.FLAG_WORLD_TRANSFORM;
// fast check event
if (this._eventMask & POSITION_ON) {
// send event
if (CC_EDITOR) {
this.emit(EventType.POSITION_CHANGED, new cc.Vec2(oldValue, localPosition.y));
}
else {
this.emit(EventType.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.setLocalDirty(LocalDirtyFlag.POSITION);
this._renderFlag |= RenderFlow.FLAG_WORLD_TRANSFORM;
// fast check event
if (this._eventMask & POSITION_ON) {
// send event
if (CC_EDITOR) {
this.emit(EventType.POSITION_CHANGED, new cc.Vec2(localPosition.x, oldValue));
}
else {
this.emit(EventType.POSITION_CHANGED);
}
}
}
else {
cc.error(ERR_INVALID_NUMBER, 'new y');
}
}
},
},
z: {
get () {
return this._position.z;
},
set (value) {
var localPosition = this._position;
if (value !== localPosition.z) {
if (!CC_EDITOR || isFinite(value)) {
localPosition.z = value;
this.setLocalDirty(LocalDirtyFlag.POSITION);
this._renderFlag |= RenderFlow.FLAG_WORLD_TRANSFORM;
// fast check event
if (this._eventMask & POSITION_ON) {
this.emit(EventType.POSITION_CHANGED);
}
}
else {
cc.error(ERR_INVALID_NUMBER, 'new z');
}
}
},
},
/**
* !#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;
// Update quaternion from rotation
math.quat.fromEuler(this._quat, 0, 0, -value);
this.setLocalDirty(LocalDirtyFlag.ROTATION);
this._renderFlag |= RenderFlow.FLAG_TRANSFORM;
if (this._eventMask & ROTATION_ON) {
this.emit(EventType.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;
// Update quaternion from rotation
if (this._rotationX === this._rotationY) {
math.quat.fromEuler(this._quat, 0, 0, -value);
}
else {
math.quat.fromEuler(this._quat, value, this._rotationY, 0);
}
this.setLocalDirty(LocalDirtyFlag.ROTATION);
this._renderFlag |= RenderFlow.FLAG_TRANSFORM;
if (this._eventMask & ROTATION_ON) {
this.emit(EventType.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;
// Update quaternion from rotation
if (this._rotationX === this._rotationY) {
math.quat.fromEuler(this._quat, 0, 0, -value);
}
else {
math.quat.fromEuler(this._quat, this._rotationX, value, 0);
}
this.setLocalDirty(LocalDirtyFlag.ROTATION);
this._renderFlag |= RenderFlow.FLAG_TRANSFORM;
if (this._eventMask & ROTATION_ON) {
this.emit(EventType.ROTATION_CHANGED);
}
}
},
},
/**
* !#en The local scale relative to the parent.
* !#zh 节点相对父节点的缩放。
* @property scale
* @type {Number}
* @example
* node.scale = 1;
*/
/**
* !#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._scale.x;
},
set (value) {
if (this._scale.x !== value) {
this._scale.x = value;
this.setLocalDirty(LocalDirtyFlag.SCALE);
this._renderFlag |= RenderFlow.FLAG_TRANSFORM;
if (this._eventMask & SCALE_ON) {
this.emit(EventType.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._scale.y;
},
set (value) {
if (this._scale.y !== value) {
this._scale.y = value;
this.setLocalDirty(LocalDirtyFlag.SCALE);
this._renderFlag |= RenderFlow.FLAG_TRANSFORM;
if (this._eventMask & SCALE_ON) {
this.emit(EventType.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.setLocalDirty(LocalDirtyFlag.SKEW);
this._renderFlag |= RenderFlow.FLAG_TRANSFORM;
}
},
/**
* !#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.setLocalDirty(LocalDirtyFlag.SKEW);
this._renderFlag |= RenderFlow.FLAG_TRANSFORM;
}
},
/**
* !#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._renderFlag |= RenderFlow.FLAG_OPACITY | RenderFlow.FLAG_COLOR;
}
},
range: [0, 255]
},
/**
* !#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._renderComponent) {
this._renderFlag |= RenderFlow.FLAG_COLOR;
}
}
},
},
/**
* !#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;
if (this._eventMask & ANCHOR_ON) {
this.emit(EventType.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;
if (this._eventMask & ANCHOR_ON) {
this.emit(EventType.ANCHOR_CHANGED);
}
}
},
},
/**
* !#en Width of node.
* !#zh 节点宽度。
* @property width
* @type {Number}
* @example
* node.width = 100;
*/
width: {
get () {
return this._contentSize.width;
},
set (value) {
if (value !== this._contentSize.width) {
if (CC_EDITOR) {
var clone = cc.size(this._contentSize.width, this._contentSize.height);
}
this._contentSize.width = value;
if (this._eventMask & SIZE_ON) {
if (CC_EDITOR) {
this.emit(EventType.SIZE_CHANGED, clone);
}
else {
this.emit(EventType.SIZE_CHANGED);
}
}
}
},
},
/**
* !#en Height of node.
* !#zh 节点高度。
* @property height
* @type {Number}
* @example
* node.height = 100;
*/
height: {
get () {
return this._contentSize.height;
},
set (value) {
if (value !== this._contentSize.height) {
if (CC_EDITOR) {
var clone = cc.size(this._contentSize.width, this._contentSize.height);
}
this._contentSize.height = value;
if (this._eventMask & SIZE_ON) {
if (CC_EDITOR) {
this.emit(EventType.SIZE_CHANGED, clone);
}
else {
this.emit(EventType.SIZE_CHANGED);
}
}
}
},
},
/**
* !#en zIndex is the 'key' used to sort the node relative to its siblings.
* The Node's parent will sort all its children based on the zIndex value and the arrival order.
* Nodes with greater zIndex will be sorted after nodes with smaller zIndex.
* If two nodes have the same zIndex, then the node that was added first to the children's array
* will be in front of the other node in the array.
*
* Node's order in children list will affect its rendering order.
* !#zh zIndex 是用来对节点进行排序的关键属性,它决定一个节点在兄弟节点之间的位置。
* 父节点主要根据节点的 zIndex 和添加次序来排序,拥有更高 zIndex 的节点将被排在后面,
* 如果两个节点的 zIndex 一致,先添加的节点会稳定排在另一个节点之前。
*
* 节点在 children 中的顺序决定了其渲染顺序。
* @property zIndex
* @type {Number}
* @example
* node.zIndex = 1;
* cc.log("Node zIndex: " + node.zIndex);
*/
zIndex: {
get () {
// high bits for zIndex, lower bits for arrival order
return (this._localZOrder & 0xffff0000) >> 16;
},
set (value) {
if (value > macro.MAX_ZINDEX) {
cc.warnID(1636);
value = macro.MAX_ZINDEX;
}
else if (value < macro.MIN_ZINDEX) {
cc.warnID(1637);
value = macro.MIN_ZINDEX;
}
var zIndex = (this._localZOrder & 0xffff0000) >> 16;
if (zIndex !== value) {
this._localZOrder = (this._localZOrder & 0x0000ffff) | (value << 16);
if (this._parent) {
this._parent._delaySort();
}
}
}
},
},
/**
* @method constructor
* @param {String} [name]
*/
ctor (name) {
this._reorderChildDirty = false;
// cache component
this._widget = null;
// fast render component access
this._renderComponent = null;
// Event listeners
this._capturingListeners = null;
this._bubblingListeners = null;
// Touch event listener
this._touchListener = null;
// Mouse event listener
this._mouseListener = null;
// default scale
this._scale.x = 1;
this._scale.y = 1;
this._scale.z = 1;
this._matrix = mathPools.mat4.get();
this._worldMatrix = mathPools.mat4.get();
this._localMatDirty = LocalDirtyFlag.ALL;
this._worldMatDirty = true;
this._eventMask = 0;
this._cullingMask = 1 << this.groupIndex;
},
statics: {
EventType,
_LocalDirtyFlag: LocalDirtyFlag,
// is node but not scene
isNode (obj) {
return obj instanceof Node && (obj.constructor === Node || !(obj instanceof cc.Scene));
},
BuiltinGroupIndex
},
// OVERRIDES
_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;
for (; i < len; i++) {
sibling = siblings[i];
sibling._updateOrderOfArrival();
eventManager._setDirtyForNode(sibling);
}
parent._delaySort();
},
_onPreDestroy () {
var destroyByParent = this._onPreDestroyBase();
// Actions
if (ActionManagerExist) {
cc.director.getActionManager().removeAllActionsFromTarget(this);
}
// Remove Node.currentHovered
if (_currentHovered === this) {
_currentHovered = null;
}
// Remove all event listeners if necessary
if (this._touchListener || this._mouseListener) {
eventManager.removeListeners(this);
if (this._touchListener) {
this._touchListener.owner = null;
this._touchListener.mask = null;
this._touchListener = null;
}
if (this._mouseListener) {
this._mouseListener.owner = null;
this._mouseListener.mask = null;
this._mouseListener = null;
}
}
// Recycle math objects
mathPools.mat4.put(this._matrix);
mathPools.mat4.put(this._worldMatrix);
this._matrix = this._worldMatrix = null;
if (this._reorderChildDirty) {
cc.director.__fastOff(cc.Director.EVENT_AFTER_UPDATE, this.sortAllChildren, this);
}
if (!destroyByParent) {
// 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;
}
}
},
_onPostActivated (active) {
var actionManager = ActionManagerExist ? cc.director.getActionManager() : null;
if (active) {
// Refresh transform
this._renderFlag |= RenderFlow.FLAG_WORLD_TRANSFORM;
// ActionManager & EventManager
actionManager && actionManager.resumeTarget(this);
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
actionManager && actionManager.pauseTarget(this);
eventManager.pauseTarget(this);
}
},
_onHierarchyChanged (oldParent) {
this._updateOrderOfArrival();
if (this._parent) {
this._parent._delaySort();
}
this._renderFlag |= RenderFlow.FLAG_WORLD_TRANSFORM;
this._onHierarchyChangedBase(oldParent);
cc._widgetManager._nodesOrderDirty = true;
},
// INTERNAL
_upgrade_1x_to_2x () {
// Upgrade scaleX, scaleY from v1.x
// TODO: remove in future version, 3.0 ?
if (this._scaleX !== undefined) {
this._scale.x = this._scaleX;
this._scaleX = undefined;
}
if (this._scaleY !== undefined) {
this._scale.y = this._scaleY;
this._scaleY = undefined;
}
// TODO: remove _rotationX & _rotationY in future version, 3.0 ?
// Update quaternion from rotation, when upgrade from 1.x to 2.0
// If rotation x & y is 0 in old version, then update rotation from default quaternion is ok too
if (this._rotationX !== 0 || this._rotationY !== 0) {
if (this._rotationX === this._rotationY) {
math.quat.fromEuler(this._quat, 0, 0, -this._rotationX);
}
else {
math.quat.fromEuler(this._quat, this._rotationX, this._rotationY, 0);
}
}
// Update rotation from quaternion
else {
let rotx = this._quat.getRoll();
let roty = this._quat.getPitch();
if (rotx === 0 && roty === 0) {
this._rotationX = this._rotationY = -this._quat.getYaw();
}
else {
this._rotationX = rotx;
this._rotationY = roty;
}
}
},
/*
* The initializer for Node which will be called before all components onLoad
*/
_onBatchCreated () {
this._upgrade_1x_to_2x();
this._updateOrderOfArrival();
let prefabInfo = this._prefab;
if (prefabInfo && prefabInfo.sync && prefabInfo.root === this) {
if (CC_DEV) {
// TODO - remove all usage of _synced
cc.assert(!prefabInfo._synced, 'prefab should not synced');
}
PrefabHelper.syncWithPrefab(this);
}
if (!this._activeInHierarchy) {
// deactivate ActionManager and EventManager by default
if (ActionManagerExist) {
cc.director.getActionManager().pauseTarget(this);
}
eventManager.pauseTarget(this);
}
let children = this._children;
for (let i = 0, len = children.length; i < len; i++) {
children[i]._onBatchCreated();
}
if (children.length > 0) {
this._renderFlag |= RenderFlow.FLAG_CHILDREN;
}
},
// the same as _onBatchCreated but untouch prefab
_onBatchRestored () {
this._upgrade_1x_to_2x();
if (!this._activeInHierarchy) {
// deactivate ActionManager and EventManager by default
if (ActionManagerExist) {
cc.director.getActionManager().pauseTarget(this);
}
eventManager.pauseTarget(this);
}
var children = this._children;
for (var i = 0, len = children.length; i < len; i++) {
children[i]._onBatchRestored();
}
if (children.length > 0) {
this._renderFlag |= RenderFlow.FLAG_CHILDREN;
}
},
// EVENT TARGET
/**
* !#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,
* These kinds of event are triggered with dispatchEvent, the dispatch process has three steps:
* 1. Capturing phase: dispatch in capture targets (`_getCapturingTargets`), e.g. parents in node tree, from root to the real target
* 2. At target phase: dispatch to the listeners of the real target
* 3. Bubbling phase: dispatch in bubble targets (`_getBubblingTargets`), e.g. parents in node tree, from the real target to root
* In any moment of the dispatching process, it can be stopped via `event.stopPropagation()` or `event.stopPropagationImmidiate()`.
* It's the recommended way to register touch/mouse event for Node,
* please do not use cc.eventManager directly for Node.
* You can also register custom event and use emit to dispatch custom event on Node.
* For such events, there won't be capturing and bubbling phase, your event will be dispatched directly to its listeners registered on the same node.
* !#zh
* 在节点上注册指定类型的回调函数,也可以设置 target 用于绑定响应函数的 this 对象。
* 鼠标或触摸事件会被系统调用 dispatchEvent 方法触发,触发的过程包含三个阶段:
* 1. 捕获阶段:派发事件给捕获目标(通过 `_getCapturingTargets` 获取),比如,节点树中注册了捕获阶段的父节点,从根节点开始派发直到目标节点。
* 2. 目标阶段:派发给目标节点的监听器。
* 3. 冒泡阶段:派发事件给冒泡目标(通过 `_getBubblingTargets` 获取),比如,节点树中注册了冒泡阶段的父节点,从目标节点开始派发知道根节点。
* 同时您可以将事件派发到父节点或者通过调用 stopPropagation 拦截它。
* 推荐使用这种方式来监听节点上的触摸或鼠标事件,请不要在节点上直接使用 cc.eventManager。
* 你也可以注册自定义事件到节点上,并通过 emit 方法触发此类事件,对于这类事件,不会发生捕获冒泡阶段,只会直接派发给注册在该节点上的监听器
* @method on
* @param {String} type - A string representing the event type to listen for.
* See {{#crossLink "Node/EventTyupe/POSITION_CHANGED"}}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.event event
* @param {Object} [target] - The target (this object) to invoke the callback, can be null
* @param {Boolean} [useCapture=false] - 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.
* @typescript
* on(type: string, callback: (event: Event.EventCustom) => void, target?: any, useCapture?: boolean): (event: Event.EventCustom) => void
* on
* 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: ActionManagerExist ? function (action) {
if (!this.active)
return;
cc.assertID(action, 1618);
cc.director.getActionManager().addAction(action, this, false);
return action;
} : emptyFunc,
/**
* !#en Pause all actions running on the current node. Equals to `cc.director.getActionManager().pauseTarget(node)`.
* !#zh 暂停本节点上所有正在运行的动作。和 `cc.director.getActionManager().pauseTarget(node);` 等价。
* @method pauseAllActions
* @example
* node.pauseAllActions();
*/
pauseAllActions: ActionManagerExist ? function () {
cc.director.getActionManager().pauseTarget(this);
} : emptyFunc,
/**
* !#en Resume all paused actions on the current node. Equals to `cc.director.getActionManager().resumeTarget(node)`.
* !#zh 恢复运行本节点上所有暂停的动作。和 `cc.director.getActionManager().resumeTarget(node);` 等价。
* @method resumeAllActions
* @example
* node.resumeAllActions();
*/
resumeAllActions: ActionManagerExist ? function () {
cc.director.getActionManager().resumeTarget(this);
} : emptyFunc,
/**
* !#en Stops and removes all actions from the running action list .
* !#zh 停止并且移除所有正在运行的动作列表。
* @method stopAllActions
* @example
* node.stopAllActions();
*/
stopAllActions: ActionManagerExist ? function () {
cc.director.getActionManager().removeAllActionsFromTarget(this);
} : emptyFunc,
/**
* !#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: ActionManagerExist ? function (action) {
cc.director.getActionManager().removeAction(action);
} : emptyFunc,
/**
* !#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: ActionManagerExist ? function (tag) {
if (tag === cc.Action.TAG_INVALID) {
cc.logID(1612);
return;
}
cc.director.getActionManager().removeActionByTag(tag, this);
} : emptyFunc,
/**
* !#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: ActionManagerExist ? function (tag) {
if (tag === cc.Action.TAG_INVALID) {
cc.logID(1613);
return null;
}
return cc.director.getActionManager().getActionByTag(tag, this);
} : function () {
return null;
},
/**
* !#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.