From 98cc058392f23c14b5fc52aff275792316fba489 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Sun, 17 Apr 2016 14:21:58 +0800 Subject: [PATCH 1/2] support the live reloading of undo history --- cocos2d/core/load-pipeline/uuid-loader.js | 21 +++++---- cocos2d/core/platform/CCAssetLibrary.js | 2 +- cocos2d/core/platform/CCObject.js | 55 +++++++++++------------ cocos2d/core/utils/base-node.js | 32 +++++++++++-- test/qunit/lib/polyfill-for-phantom.js | 13 +++++- test/qunit/unit/test-object.js | 23 ++++++++++ 6 files changed, 102 insertions(+), 44 deletions(-) diff --git a/cocos2d/core/load-pipeline/uuid-loader.js b/cocos2d/core/load-pipeline/uuid-loader.js index d72762a4d1..9a8d717e44 100644 --- a/cocos2d/core/load-pipeline/uuid-loader.js +++ b/cocos2d/core/load-pipeline/uuid-loader.js @@ -29,8 +29,15 @@ require('../platform/deserialize'); // temp deserialize info var _tdInfo = new cc.deserialize.Details(); -var SCENE_ID = 'cc.Scene'; -var PREFAB_ID = 'cc.Prefab'; +function isSceneObj (json) { + var SCENE_ID = 'cc.Scene'; + var PREFAB_ID = 'cc.Prefab'; + return json && ( + (json[0] && json[0].__type__ === SCENE_ID) || + (json[1] && json[1].__type__ === SCENE_ID) || + (json[0] && json[0].__type__ === PREFAB_ID) + ); +} function loadDepends (pipeline, item, asset, tdInfo, callback) { var uuid = item.id, @@ -127,12 +134,7 @@ function loadUuid (item, callback) { return; } - var isInScene = json && ( - (json[0] && json[0].__type__ === SCENE_ID) || - (json[1] && json[1].__type__ === SCENE_ID) || - (json[0] && json[0].__type__ === PREFAB_ID) - ); - var classFinder = isInScene ? cc._MissingScript.safeFindClass : function (id) { + var classFinder = isSceneObj(json) ? cc._MissingScript.safeFindClass : function (id) { var cls = JS._getClassById(id); if (cls) { return cls; @@ -158,4 +160,5 @@ function loadUuid (item, callback) { loadDepends(this.pipeline, item, asset, tdInfo, callback); } -module.exports = loadUuid; \ No newline at end of file +module.exports = loadUuid; +loadUuid.isSceneObj = isSceneObj; diff --git a/cocos2d/core/platform/CCAssetLibrary.js b/cocos2d/core/platform/CCAssetLibrary.js index 3ead43823e..a8c3d4341e 100644 --- a/cocos2d/core/platform/CCAssetLibrary.js +++ b/cocos2d/core/platform/CCAssetLibrary.js @@ -88,7 +88,7 @@ var AssetLibrary = { _queryAssetInfoInEditor: function (uuid, callback) { if (CC_EDITOR) { - Editor.Ipc.sendToMain( 'scene:query-asset-info-by-uuid', uuid, function (err,info) { + Editor.Ipc.sendToMain('scene:query-asset-info-by-uuid', uuid, function (err, info) { if (info) { Editor.UuidCache.cache(info.url, uuid); var ctor = Editor.assets[info.type]; diff --git a/cocos2d/core/platform/CCObject.js b/cocos2d/core/platform/CCObject.js index ead5ff6208..4d8918ad1f 100644 --- a/cocos2d/core/platform/CCObject.js +++ b/cocos2d/core/platform/CCObject.js @@ -3,14 +3,14 @@ var JS = require('./js'); // definitions for CCObject.Flags var Destroyed = 1 << 0; -var ToDestroy = 1 << 1; -var DontSave = 1 << 2; -var EditorOnly = 1 << 3; -var Dirty = 1 << 4; -var DontDestroy = 1 << 5; -var Destroying = 1 << 6; -var Activating = 1 << 7; -//var RegisteredInEditor = 1 << 8; +var RealDestroyed = 1 << 1; +var ToDestroy = 1 << 2; +var DontSave = 1 << 3; +var EditorOnly = 1 << 4; +var Dirty = 1 << 5; +var DontDestroy = 1 << 6; +var Destroying = 1 << 7; +var Activating = 1 << 8; var HideInGame = 1 << 9; var HideInEditor = 1 << 10; @@ -227,6 +227,12 @@ JS.get(prototype, 'isValid', function () { return !(this._objFlags & Destroyed); }); +if (CC_DEV) { + JS.get(prototype, 'isRealValid', function () { + return !(this._objFlags & RealDestroyed); + }); +} + var deferredDestroyTimer = null; /** @@ -264,33 +270,27 @@ prototype.destroy = function () { }; if (CC_DEV) { - /** + /* * !#en * In fact, Object's "destroy" will not trigger the destruct operation in Firebal Editor. * The destruct operation will be executed by Undo system later. * !#zh * 事实上,对象的 “destroy” 不会在编辑器中触发析构操作, - * 析构操作将在 Undo 系统中延后执行。 + * 析构操作将在 Undo 系统中**延后**执行。 * @method realDestroyInEditor - * @example - * cc.log(obj.realDestroyInEditor()); + * @private */ prototype.realDestroyInEditor = function () { - if (this._objFlags & Destroyed) { + if ( !(this._objFlags & Destroyed) ) { + cc.warn('object not yet destroyed'); + return; + } + if (this._objFlags & RealDestroyed) { cc.warn('object already destroyed'); - return false; + return; } - if (this._objFlags & ToDestroy) { - return false; - } - this._objFlags |= ToDestroy; - objectsToDestroy.push(this); - - if (deferredDestroyTimer === null && cc.engine && ! cc.engine._isUpdating && CC_EDITOR) { - // auto destroy immediate in edit mode - deferredDestroyTimer = setImmediate(deferredDestroy); - } - return true; + this._destruct(); + this._objFlags |= RealDestroyed; }; } @@ -303,9 +303,6 @@ if (CC_DEV) { * @private */ prototype._destruct = function () { - if (CC_EDITOR && !(this._objFlags & Destroyed)) { - return cc.error('object not yet destroyed'); - } // 所有可枚举到的属性,都会被清空 for (var key in this) { if (this.hasOwnProperty(key)) { @@ -340,11 +337,9 @@ prototype._destroyImmediate = function () { } if (!CC_EDITOR || cc.engine._isPlaying) { - // 这里 _destruct 将由编辑器进行调用 this._destruct(); } - // mark destroyed this._objFlags |= Destroyed; }; diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js index 7a21910086..65ac17d650 100644 --- a/cocos2d/core/utils/base-node.js +++ b/cocos2d/core/utils/base-node.js @@ -23,7 +23,7 @@ THE SOFTWARE. ****************************************************************************/ var JS = cc.js; -var SceneGraphHelper = require('./scene-graph-helper'); +var SgHelper = require('./scene-graph-helper'); var Destroying = require('../platform/CCObject').Flags.Destroying; var DirtyFlags = require('./misc').DirtyFlags; var IdGenerater = require('../platform/id-generater'); @@ -1911,7 +1911,10 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ } }, - // The deserializer for sgNode which will be called before components onLoad + /* + * The deserializer for sgNode which will be called before components onLoad + * @param {Boolean} [skipChildrenInEditor=false] + */ _onBatchCreated: function () { this._updateDummySgNode(); @@ -1931,7 +1934,30 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ } }, - _removeSgNode: SceneGraphHelper.removeSgNode, + onRestore: CC_EDITOR && function () { + this._updateDummySgNode(); + + var sgParent = this._parent && this._parent._sgNode; + if (this._sgNode._parent !== sgParent) { + if (this._sgNode._parent) { + this._sgNode.removeFromParent(); + } + if (sgParent) { + sgParent.addChild(this._sgNode); + } + } + + if (this._activeInHierarchy) { + cc.director.getActionManager().resumeTarget(this); + cc.eventManager.resumeTarget(this); + } + else { + cc.director.getActionManager().pauseTarget(this); + cc.eventManager.pauseTarget(this); + } + }, + + _removeSgNode: SgHelper.removeSgNode, }); diff --git a/test/qunit/lib/polyfill-for-phantom.js b/test/qunit/lib/polyfill-for-phantom.js index 0346544f53..fdb666b26f 100644 --- a/test/qunit/lib/polyfill-for-phantom.js +++ b/test/qunit/lib/polyfill-for-phantom.js @@ -4,7 +4,7 @@ if (typeof CustomEvent === 'undefined') { } } -if (typeof Set == 'undefined') { +if (typeof Set === 'undefined') { // very simple polyfill Set = function () { this.values = []; @@ -17,6 +17,17 @@ if (typeof Set == 'undefined') { }; } +if (typeof setImmediate === 'undefined') { + window.setImmediate = function (func) { + 'use strict'; + console.assert(arguments.length <= 1, 'not support params'); + return setTimeout(func, 0); + }; + window.clearImmediate = function (immediateID) { + clearTimeout(immediateID); + }; +} + // http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind.html if (!Function.prototype.bind) { Function.prototype.bind = function (oThis) { diff --git a/test/qunit/unit/test-object.js b/test/qunit/unit/test-object.js index 099a4879a8..838688f5b3 100644 --- a/test/qunit/unit/test-object.js +++ b/test/qunit/unit/test-object.js @@ -79,6 +79,29 @@ test('deferred destroy', function () { strictEqual(obj2.isValid, false, 'deleted at the end of frame 2'); }); +test('realDestroyInEditor', function () { + var isEditor = CC_EDITOR; + var isUpdating = cc.engine._isUpdating; + cc.engine._isUpdating = false; + CC_EDITOR = true; + + var obj = new CCObject(); + obj.name = 'wocou'; + obj.destroy(); + CCObject._deferredDestroy(); + + strictEqual(obj.name, 'wocou', 'should not destroyed really'); + strictEqual(obj.isRealValid, true, 'isRealValid should be true'); + + obj.realDestroyInEditor(); + + strictEqual(obj.name, '', 'should be destroyed really'); + strictEqual(obj.isRealValid, false, 'isRealValid should be false'); + + CC_EDITOR = isEditor; + cc.engine._isUpdating = isUpdating; +}); + test('multiply deferred destroy', function () { var obj1 = new CCObject(); var obj2 = new CCObject(); From 2bc25644fd89a9c490330500da0d7ee3099d6494 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Sun, 17 Apr 2016 14:22:35 +0800 Subject: [PATCH 2/2] small optimization --- cocos2d/core/value-types/CCColor.js | 6 +++--- cocos2d/core/value-types/CCRect.js | 8 ++++---- cocos2d/core/value-types/CCSize.js | 4 ++-- cocos2d/core/value-types/CCVec2.js | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/cocos2d/core/value-types/CCColor.js b/cocos2d/core/value-types/CCColor.js index 2df8f5d586..3bbdda10f0 100644 --- a/cocos2d/core/value-types/CCColor.js +++ b/cocos2d/core/value-types/CCColor.js @@ -61,9 +61,9 @@ var Color = (function () { a = r.a; r = r.r; } - this.r = typeof r === 'number' ? r : 0; - this.g = typeof g === 'number' ? g : 0; - this.b = typeof b === 'number' ? b : 0; + this.r = r || 0; + this.g = g || 0; + this.b = b || 0; this.a = typeof a === 'number' ? a : 255; } JS.extend(Color, ValueType); diff --git a/cocos2d/core/value-types/CCRect.js b/cocos2d/core/value-types/CCRect.js index 82da656629..d163fb91d6 100644 --- a/cocos2d/core/value-types/CCRect.js +++ b/cocos2d/core/value-types/CCRect.js @@ -53,10 +53,10 @@ function Rect (x, y, w, h) { h = x.height; x = x.x; } - this.x = typeof x === 'number' ? x : 0.0; - this.y = typeof y === 'number' ? y : 0.0; - this.width = typeof w === 'number' ? w : 0.0; - this.height = typeof h === 'number' ? h : 0.0; + this.x = x || 0; + this.y = y || 0; + this.width = w || 0; + this.height = h || 0; } JS.extend(Rect, ValueType); require('../platform/CCClass').fastDefine('cc.Rect', Rect, ['x', 'y', 'width', 'height']); diff --git a/cocos2d/core/value-types/CCSize.js b/cocos2d/core/value-types/CCSize.js index a16e12b4ab..c15532fc55 100644 --- a/cocos2d/core/value-types/CCSize.js +++ b/cocos2d/core/value-types/CCSize.js @@ -52,8 +52,8 @@ function Size (width, height) { height = width.height; width = width.width; } - this.width = typeof width === 'number' ? width : 0; - this.height = typeof height === 'number' ? height : 0; + this.width = width || 0; + this.height = height || 0; } JS.extend(Size, ValueType); require('../platform/CCClass').fastDefine('cc.Size', Size, ['width', 'height']); diff --git a/cocos2d/core/value-types/CCVec2.js b/cocos2d/core/value-types/CCVec2.js index 4565714724..6119a67d19 100644 --- a/cocos2d/core/value-types/CCVec2.js +++ b/cocos2d/core/value-types/CCVec2.js @@ -52,8 +52,8 @@ function Vec2 (x, y) { y = x.y; x = x.x; } - this.x = (typeof x === 'number' ? x : 0.0); - this.y = (typeof y === 'number' ? y : 0.0); + this.x = x || 0; + this.y = y || 0; } JS.extend(Vec2, ValueType); FireClass.fastDefine('cc.Vec2', Vec2, ['x', 'y']);