Merge pull request #23 from jareguo/master

mixin EventTarget instead of inherit by polyfill
This commit is contained in:
pandamicro
2015-12-09 22:07:15 +08:00
16 changed files with 59 additions and 60 deletions
+5 -3
View File
@@ -106,6 +106,8 @@ cc.Director = Class.extend(/** @lends cc.Director# */{
ctor: function () {
var self = this;
EventTarget.call(self);
self._lastUpdate = Date.now();
cc.eventManager.addCustomListener(cc.game.EVENT_SHOW, function () {
self._lastUpdate = Date.now();
@@ -156,9 +158,6 @@ cc.Director = Class.extend(/** @lends cc.Director# */{
this._animationManager = null;
}
// Event target
EventTarget.polyfill(this);
// WidgetManager
cc._widgetManager.init(this);
@@ -967,6 +966,9 @@ cc.Director = Class.extend(/** @lends cc.Director# */{
}
});
// Event target
cc.js.addon(cc.Director.prototype, EventTarget.prototype);
/**
* The event projection changed of cc.Director
* @constant
+2 -1
View File
@@ -598,6 +598,7 @@ var game = /** @lends cc.game# */{
}
};
EventTarget.polyfill(game);
EventTarget.call(game);
cc.js.addon(game, EventTarget.prototype);
cc.game = module.exports = game;
+2 -1
View File
@@ -38,6 +38,7 @@ var DontDestroy = Flags.DontDestroy;
var Node = cc.Class({
name: 'cc.Node',
extends: require('./utils/base-node'),
mixins: [cc.EventTarget],
properties: {
/**
@@ -185,7 +186,7 @@ var Node = cc.Class({
// Remove all listeners
for (i = 0, len = this.__eventTargets.length; i < len; ++i) {
var target = this.__eventTargets[i];
target && target.targetOff && target.targetOff(this);
target && target.targetOff(this);
}
this.__eventTargets.length = 0;
+3 -1
View File
@@ -82,6 +82,8 @@ cc.AtlasNode = _ccsg.Node.extend(/** @lends cc.AtlasNode# */{
*/
ctor: function (tile, tileWidth, tileHeight, itemsToRender) {
_ccsg.Node.prototype.ctor.call(this);
EventTarget.call(this);
this._blendFunc = {src: cc.BLEND_SRC, dst: cc.BLEND_DST};
this._ignoreContentScaleFactor = false;
itemsToRender !== undefined && this.initWithTileFile(tile, tileWidth, tileHeight, itemsToRender);
@@ -279,7 +281,7 @@ _p.textureAtlas;
/** @expose */
_p.quadsToDraw;
EventTarget.polyfill(_p);
cc.js.addon(_p, EventTarget.prototype);
/**
* Creates a cc.AtlasNode with an Atlas file the width and height of each item and the quantity of items to render
+5 -2
View File
@@ -22,6 +22,8 @@
THE SOFTWARE.
****************************************************************************/
var EventTarget = require("../event/event-target");
/**
* Enum for transition type
* @enum EButton.Transition
@@ -117,6 +119,8 @@ var Button = cc.Class({
extends: require('./CCComponent'),
ctor: function () {
EventTarget.call(this);
this._touchListener = null;
this._mouseListener = null;
@@ -490,7 +494,6 @@ var Button = cc.Class({
});
var EventTarget = require("../event/event-target");
EventTarget.polyfill(Button.prototype);
cc.js.addon(Button.prototype, EventTarget.prototype);
cc.EButton = module.exports = Button;
+1 -1
View File
@@ -536,7 +536,7 @@ var Component = cc.Class({
// Remove all listeners
for (i = 0, l = this.__eventTargets.length; i < l; ++i) {
target = this.__eventTargets[i];
target && target.targetOff && target.targetOff(this);
target && target.targetOff(this);
}
this.__eventTargets.length = 0;
// onDestroy
+11 -33
View File
@@ -96,16 +96,13 @@ var _doSendEvent = function (owner, event) {
* @class EventTarget
*/
var EventTarget = function () {
};
JS.mixin(EventTarget.prototype, {
/**
* @property _capturingListeners
* @type {EventListeners}
* @default null
* @private
*/
_capturingListeners: null,
this._capturingListeners = null;
/**
* @property _bubblingListeners
@@ -113,7 +110,10 @@ JS.mixin(EventTarget.prototype, {
* @default null
* @private
*/
_bubblingListeners: null,
this._bubblingListeners = null;
};
JS.mixin(EventTarget.prototype, {
/**
* Checks whether the EventTarget object has any callback registered for a specific type of event.
@@ -209,8 +209,12 @@ JS.mixin(EventTarget.prototype, {
* @param {Object} target - The target to be searched for all related callbacks
*/
targetOff: function (target) {
this._capturingListeners.removeAll(target);
this._bubblingListeners.removeAll(target);
if (this._capturingListeners) {
this._capturingListeners.removeAll(target);
}
if (this._bubblingListeners) {
this._bubblingListeners.removeAll(target);
}
},
/**
@@ -314,30 +318,4 @@ JS.mixin(EventTarget.prototype, {
}
});
/**
* Polyfill the functionalities of EventTarget into a existing object.
* @static
* @memberof EventTarget
* @param {Object} object - An object to be extended with EventTarget capability
*/
EventTarget.polyfill = function (object) {
var proto = EventTarget.prototype;
// Can't use cc.js.mixin because we don't want to inject polyfill or overwrite _getXXXTargets
object._capturingListeners = null;
object._bubblingListeners = null;
object.hasEventListener = proto.hasEventListener;
object.on = proto.on;
object.off = proto.off;
object.once = proto.once;
object.dispatchEvent = proto.dispatchEvent;
object.emit = proto.emit;
if (!object._isTargetActive)
object._isTargetActive = proto._isTargetActive;
if (!object._getCapturingTargets)
object._getCapturingTargets = proto._getCapturingTargets;
if (!object._getBubblingTargets)
object._getBubblingTargets = proto._getBubblingTargets;
};
cc.EventTarget = module.exports = EventTarget;
+4 -1
View File
@@ -125,6 +125,8 @@ cc.Sprite = _ccsg.Node.extend(/** @lends cc.Sprite# */{
ctor: function (fileName, rect, rotated) {
var self = this;
_ccsg.Node.prototype.ctor.call(self);
EventTarget.call(self);
self._shouldBeHidden = false;
self._offsetPosition = cc.p(0, 0);
self._unflippedOffsetPositionFromCenter = cc.p(0, 0);
@@ -1025,7 +1027,8 @@ cc.Sprite.createWithSpriteFrame = cc.Sprite.create;
*/
cc.Sprite.INDEX_NOT_INITIALIZED = -1;
EventTarget.polyfill(cc.Sprite.prototype);
cc.js.addon(cc.Sprite.prototype, EventTarget.prototype);
cc.assert(cc.js.isFunction(cc._tmp.PrototypeSprite), cc._LogInfos.MissingFile, "SpritesPropertyDefine.js");
cc._tmp.PrototypeSprite();
+1 -2
View File
@@ -49,6 +49,7 @@ var EventTarget = require("../event/event-target");
cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
name:'cc.SpriteFrame',
extends:require('../assets/CCAsset'),
mixins: [EventTarget],
//properties:{
// /**
@@ -560,8 +561,6 @@ proto.copyWithZone = proto.clone;
*/
proto.copy = proto.clone;
EventTarget.polyfill(proto);
/**
* <p>
* Create a cc.SpriteFrame with a texture filename, rect, rotated, offset and originalSize in pixels.<br/>
+1 -2
View File
@@ -143,6 +143,7 @@ var Texture2D = cc.Class(/** @lends cc.Texture2D# */{
name: 'cc.Texture2D',
extends: require('../assets/CCRawAsset'),
mixins: [EventTarget],
ctor: function () {
this.url = null;
@@ -592,8 +593,6 @@ cc.defineGetterSetter(_p, "width", _p._getWidth);
_p.height;
cc.defineGetterSetter(_p, "height", _p._getHeight);
EventTarget.polyfill(Texture2D.prototype);
game.once(game.EVENT_RENDERER_INITED, function () {
if(cc._renderType === game.RENDER_TYPE_CANVAS) {
+3 -1
View File
@@ -131,6 +131,8 @@ cc.Label = _ccsg.Node.extend({
//fontHandle it is a font name or bmfont file.
ctor: function(string, fontHandle, type) {
EventTarget.call(this);
fontHandle = fontHandle || "";
this._fontHandle = fontHandle;
type = type || 0;
@@ -1087,7 +1089,7 @@ cc.BMFontHelper = {
var _p = cc.Label.prototype;
EventTarget.polyfill(_p);
cc.js.addon(_p, EventTarget.prototype);
cc.js.mixin(_p, cc.BMFontHelper);
cc.Label.Type = cc.Enum({
+3 -1
View File
@@ -155,6 +155,8 @@ cc.LabelBMFont = cc.SpriteBatchNode.extend(/** @lends cc.LabelBMFont# */{
*/
ctor: function (str, fntFile, width, alignment, imageOffset) {
cc.SpriteBatchNode.prototype.ctor.call(this);
EventTarget.call(this);
this._imageOffset = cc.p(0, 0);
this._reusedChar = [];
this._cascadeColorEnabled = true;
@@ -841,7 +843,7 @@ cc.LabelBMFont = cc.SpriteBatchNode.extend(/** @lends cc.LabelBMFont# */{
(function(){
var p = cc.LabelBMFont.prototype;
EventTarget.polyfill(p);
cc.js.addon(p, EventTarget.prototype);
/** @expose */
p.string;
@@ -95,6 +95,8 @@ ccui.Scale9Sprite = cc.Scale9Sprite = _ccsg.Node.extend(/** @lends ccui.Scale9Sp
ctor: function(file, rect, capInsets) {
_ccsg.Node.prototype.ctor.call(this);
EventTarget.call(this);
this._nonSliceSpriteAnchor = cc.p(0.5,0.5);
this._originalSize = cc.size(0,0);
this._preferredSize = cc.rect(0,0,0,0);
@@ -1043,7 +1045,7 @@ ccui.Scale9Sprite = cc.Scale9Sprite = _ccsg.Node.extend(/** @lends ccui.Scale9Sp
});
var _p = ccui.Scale9Sprite.prototype;
EventTarget.polyfill(_p);
cc.js.addon(_p, EventTarget.prototype);
// Extended properties
/** @expose */
+3 -1
View File
@@ -163,6 +163,8 @@ ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
*/
ctor: function () {
cc.ProtectedNode.prototype.ctor.call(this);
EventTarget.call(this);
this._brightStyle = ccui.Widget.BRIGHT_STYLE_NONE;
this._touchBeganPosition = cc.p(0, 0);
this._touchMovePosition = cc.p(0, 0);
@@ -1903,7 +1905,7 @@ ccui.Widget.create = function () {
return new ccui.Widget();
};
EventTarget.polyfill(ccui.Widget.prototype);
cc.js.addon(ccui.Widget.prototype, EventTarget.prototype);
ccui.Widget._focusedWidget = null; //both layout & widget will be stored in this variable
ccui.Widget._focusNavigationController = null;
+3 -1
View File
@@ -242,6 +242,8 @@ gaf.Asset = cc._Class.extend
ctor : function()
{
EventTarget.call(this);
this._header = {};
this._timeLines = [];
this._textFields = [];
@@ -425,4 +427,4 @@ gaf.Asset.createWithBundle = function (zipFilePath, entryFile, delegate)
return asset;
};
EventTarget.polyfill(gaf.Asset.prototype);
cc.js.addon(gaf.Asset.prototype, EventTarget.prototype);
+9 -8
View File
@@ -109,22 +109,23 @@ var TestScript = cc.Class({
// polyfills to test engine extends
cc.engine = {
attachedObjsForEditor: {},
cc.engine = new (cc.Class({
extends: cc.EventTarget,
properties: {
attachedObjsForEditor: {
default: {}
},
},
getInstanceById: function (uuid) {
return this.attachedObjsForEditor[uuid] || null;
},
};
}
}))();
Editor.log = cc.log;
Editor.warn = cc.warn;
Editor.error = cc.error;
Editor.info = cc.info;
//
cc.EventTarget.polyfill(cc.engine);
var assetDir = '../test/qunit/assets';
var canvas;