From f73efdb7ace11fd26b9ace093559c4f4f673c4b4 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Wed, 30 Dec 2015 20:26:12 +0800 Subject: [PATCH 1/6] disabled sprite should not display when changing sprite frame --- cocos2d/core/components/CCSprite.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos2d/core/components/CCSprite.js b/cocos2d/core/components/CCSprite.js index 95b2e440de..1c66e462a0 100644 --- a/cocos2d/core/components/CCSprite.js +++ b/cocos2d/core/components/CCSprite.js @@ -341,7 +341,7 @@ var Sprite = cc.Class({ sgNode.setSpriteFrame(self._spriteFrame); self._applyCapInset(sgNode); self._applySpriteSize(); - if ( !sgNode.isVisible() ) { + if ( this.enabledInHierarchy && !sgNode.isVisible() ) { sgNode.setVisible(true); } }, From 664e32bbb100a4633a6e9289f50c3cb271e69570 Mon Sep 17 00:00:00 2001 From: Johnny Wu Date: Wed, 30 Dec 2015 20:40:47 +0800 Subject: [PATCH 2/6] support text asset --- cocos2d/core/assets/CCText.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 cocos2d/core/assets/CCText.js diff --git a/cocos2d/core/assets/CCText.js b/cocos2d/core/assets/CCText.js new file mode 100644 index 0000000000..e3a27b225c --- /dev/null +++ b/cocos2d/core/assets/CCText.js @@ -0,0 +1,12 @@ +/** + * Class for text file. + * @class TextAsset + * @extends Asset + * @constructor + */ +var TextAsset = cc.Class({ + name: 'cc.TextAsset', + extends: cc.Asset, +}); + +cc.TextAsset = TextAsset; From 8e4a704074716026b53255c5165a5a1192179c4e Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Wed, 30 Dec 2015 21:57:40 +0800 Subject: [PATCH 3/6] force reset property in assetlibrary even if query failed, to make array fully initialized --- cocos2d/core/platform/CCAssetLibrary.js | 2 ++ cocos2d/core/utils/base-node.js | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/cocos2d/core/platform/CCAssetLibrary.js b/cocos2d/core/platform/CCAssetLibrary.js index 61e9f45fef..d151f5e20f 100644 --- a/cocos2d/core/platform/CCAssetLibrary.js +++ b/cocos2d/core/platform/CCAssetLibrary.js @@ -366,6 +366,8 @@ var AssetLibrary = { callback(); callback = null; } + + obj[prop] = obj[prop]; // ensures forEach worked return; } else if (isRawAsset) { diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js index 3d0ab9ef4d..3890b982b8 100644 --- a/cocos2d/core/utils/base-node.js +++ b/cocos2d/core/utils/base-node.js @@ -1063,7 +1063,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ var children = this._children; if (cleanup === undefined) cleanup = true; - for (var i = 0; i < children.length; i++) { + for (var i = children.length - 1; i >= 0; i--) { var node = children[i]; if (node) { //if (this._running) { From 9db531f4611f7c27b2ec591fafb2ab0a10f8197b Mon Sep 17 00:00:00 2001 From: pandamicro Date: Wed, 30 Dec 2015 20:48:22 +0800 Subject: [PATCH 4/6] Fix JSB issues --- CCDebugger.js | 4 ++++ cocos2d/core/platform/CCAssetLibrary.js | 16 +++++++++------- cocos2d/core/utils/base-node.js | 10 +++++++--- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/CCDebugger.js b/CCDebugger.js index 659e92f042..d0bd9a45b9 100644 --- a/CCDebugger.js +++ b/CCDebugger.js @@ -405,6 +405,10 @@ cc._initDebugSetting = function (mode) { } else if(console && console.log.apply){//console is null when user doesn't open dev tool on IE9 //log to console + // For JSB + if (!console.error) console.error = console.log; + if (!console.warn) console.warn = console.log; + /** * Outputs an error message to the Fireball Console (editor) or Web Console (runtime). * - In Fireball, error is red. diff --git a/cocos2d/core/platform/CCAssetLibrary.js b/cocos2d/core/platform/CCAssetLibrary.js index d151f5e20f..6b0fe1ce2d 100644 --- a/cocos2d/core/platform/CCAssetLibrary.js +++ b/cocos2d/core/platform/CCAssetLibrary.js @@ -25,6 +25,8 @@ var _uuidToCallbacks = new CallbacksInvoker(); // temp deserialize info var _tdInfo = new cc.deserialize.Details(); +var _cc_loader_loadJson = cc.loader.loadJson.bind(cc.loader); + // create a loading context which reserves all relevant parameters function LoadingHandle (readMainCache, writeMainCache, recordAssets, deserializeInfo) { //this.readMainCache = readMainCache; @@ -274,7 +276,7 @@ var AssetLibrary = { if (!shouldLoadByEngine) { return callback(new Error('Should not load raw file in AssetLibrary, uuid: ' + uuid)); } - LoadManager.loadByLoader(cc.loader.loadJson.bind(cc.loader), url, function (error, json) { + LoadManager.loadByLoader(_cc_loader_loadJson, url, function (error, json) { onload(error, json, url); }); } @@ -285,7 +287,7 @@ var AssetLibrary = { if (info.raw) { return callback(new Error('Should not load raw file in AssetLibrary, uuid: ' + uuid)); } - LoadManager.loadByLoader(cc.loader.loadJson.bind(cc.loader), info.url, function (error, json) { + LoadManager.loadByLoader(_cc_loader_loadJson, info.url, function (error, json) { onload(error, json, info.url); }); } @@ -371,16 +373,16 @@ var AssetLibrary = { return; } else if (isRawAsset) { - // HACK - https://github.com/fireball-x/fireball/issues/668 - var isImg = cc.loader._checkIsImageURL(dependsUrl); - var loadFunc = isImg ? cc.loader.loadImg : cc.loader.load; - loadFunc.call(cc.loader, dependsUrl, function (err, assets) { + cc.loader.loadImg(dependsUrl, function (err, assets) { if (err) { cc.error('[AssetLibrary] Failed to load "%s"', dependsUrl); obj[prop] = ''; } else { obj[prop] = dependsUrl; + if (!cc.sys.isNative) { + cc.textureCache.handleLoadedTexture(dependsUrl); + } } --pendingCount; if (callback && pendingCount === 0) { @@ -444,7 +446,7 @@ var AssetLibrary = { return Object; }; - var tdInfo = handle.deserializeInfo || _tdInfo; + var tdInfo = cc.sys.isNative ? new cc.deserialize.Details() : (handle.deserializeInfo || _tdInfo); var asset = cc.deserialize(json, tdInfo, { classFinder: classFinder, diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js index 3890b982b8..5e3d894bf6 100644 --- a/cocos2d/core/utils/base-node.js +++ b/cocos2d/core/utils/base-node.js @@ -29,7 +29,7 @@ var DirtyFlags = require('./misc').DirtyFlags; // called after changing parent function setMaxZOrder (node) { - var siblings = node._parent.getChildren(); + var siblings = node.parent.getChildren(); var z = 0; if (siblings.length >= 2) { var prevNode = siblings[siblings.length - 2]; @@ -1316,7 +1316,9 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ var sibling = siblings[i]; sibling._sgNode.arrivalOrder = i; } - cc.renderer.childrenOrderDirty = this._parent._sgNode._reorderChildDirty = true; + if (cc.renderer) { + cc.renderer.childrenOrderDirty = this._parent._sgNode._reorderChildDirty = true; + } } }, @@ -1404,7 +1406,9 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ // insert node parentNode.addChild(sgNode); sgNode.arrivalOrder = oldSgNode.arrivalOrder; - cc.renderer.childrenOrderDirty = this._parent._sgNode._reorderChildDirty = true; + if (cc.renderer) { + cc.renderer.childrenOrderDirty = this._parent._sgNode._reorderChildDirty = true; + } this._sgNode = sgNode; From b527f362222148b5cbc854d9460a03f828b18f22 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Wed, 30 Dec 2015 22:17:26 +0800 Subject: [PATCH 5/6] Fix more bugs in entity components --- cocos2d/core/components/CCSprite.js | 6 +++--- cocos2d/core/utils/base-node.js | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cocos2d/core/components/CCSprite.js b/cocos2d/core/components/CCSprite.js index 1c66e462a0..2690f34d16 100644 --- a/cocos2d/core/components/CCSprite.js +++ b/cocos2d/core/components/CCSprite.js @@ -372,13 +372,13 @@ var Sprite = cc.Class({ _createSgNode: function () { var sgNode = new cc.Scale9Sprite(); + sgNode.setRenderingType(this._type); + this._applySpriteFrame(sgNode, null); + // should keep the size of the sg node the same as entity, // otherwise setContentSize may not take effect sgNode.setContentSize(this.node.getContentSize(true)); - this._applySpriteSize(sgNode); - sgNode.setRenderingType(this._type); - this._applySpriteFrame(sgNode, null); return sgNode; }, diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js index 5e3d894bf6..5d0c9ddfa2 100644 --- a/cocos2d/core/utils/base-node.js +++ b/cocos2d/core/utils/base-node.js @@ -1187,7 +1187,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ * @return {Vec2} */ convertTouchToNodeSpace: function (touch) { - return this.convertToNodeSpace(touch.getPosition()); + return this.convertToNodeSpace(touch.getLocation()); }, /** @@ -1197,7 +1197,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ * @return {Vec2} */ convertTouchToNodeSpaceAR: function (touch) { - return this.convertToNodeSpaceAR(touch.getPosition()); + return this.convertToNodeSpaceAR(touch.getLocation()); }, /** From 349fa29ef91e8915e48c0c53e850d65b99919f16 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Wed, 30 Dec 2015 22:18:08 +0800 Subject: [PATCH 6/6] Merge jsb_polyfill and jsb_engine and made jsb run --- gulp/tasks/build.js | 6 +- gulpfile.js | 2 +- jsb_engine.js | 823 +++++++++++++++++++++++++++++++++++++++++--- jsb_polyfill.js | 583 ------------------------------- jsb_predefine.js | 20 +- 5 files changed, 797 insertions(+), 637 deletions(-) delete mode 100644 jsb_polyfill.js diff --git a/gulp/tasks/build.js b/gulp/tasks/build.js index 2caac0af7b..faa818e59e 100644 --- a/gulp/tasks/build.js +++ b/gulp/tasks/build.js @@ -196,13 +196,13 @@ gulp.task('build-jsb-extends-min', function () { }); gulp.task('build-jsb-extends-dev', function () { - var jsbPolyfill = rebundle_jsb(createBundler(paths.JSBEntries), false, '_polyfill'); + var jsbPolyfill = rebundle_jsb(createBundler(paths.JSBEntries), false, '_polyfill.dev'); return jsbPolyfill; }); -gulp.task('build', ['build-html5', 'build-jsb-extends-min']); +gulp.task('build', ['build-html5', 'build-jsb-extends-min', 'build-jsb-extends-dev']); -gulp.task('fast-build', ['build-test', 'build-jsb-extends-min'], function (done) { +gulp.task('fast-build', ['build-test', 'build-jsb-extends-min', 'build-jsb-extends-dev'], function (done) { Del(['./bin/cocos2d-js.js', './bin/cocos2d-js-min.js',], done); }); diff --git a/gulpfile.js b/gulpfile.js index 2b8cdbc3d8..bc16ca7cdd 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -10,7 +10,7 @@ paths = { jsEntry: './index.js', JSBEntries: [ './jsb_predefine.js', - './jsb_polyfill.js', + './jsb_engine.js', './extends.js' ], outDir: './bin', diff --git a/jsb_engine.js b/jsb_engine.js index 0f20719525..cfdec61dd2 100644 --- a/jsb_engine.js +++ b/jsb_engine.js @@ -1,56 +1,58 @@ 'use strict'; -function _getPropertyDescriptor (obj, name) { - var pd = Object.getOwnPropertyDescriptor(obj, name); - if (pd) { - return pd; - } - var p = Object.getPrototypeOf(obj); - if (p) { - return _getPropertyDescriptor(p, name); +var _engineNumberVersion = (function () { + var result = /Cocos2d\-JS\sv([\.\d]+)/.exec(cc.ENGINE_VERSION); + if (result && result[1]) { + return parseFloat(result[1]); } else { return null; } -} +})(); -function _copyprop(name, source, target) { - var pd = _getPropertyDescriptor(source, name); - Object.defineProperty(target, name, pd); -} +cc.js.mixin(cc.path, { + //todo make public after verification + _normalize: function(url){ + var oldUrl = url = String(url); -function mixin (obj) { - obj = obj || {}; - for (var i = 1, length = arguments.length; i < length; i++) { - var source = arguments[i]; - if (source) { - if (typeof source !== 'object') { - cc.error('mixin: arguments must be type object:', source); - continue; - } - for ( var name in source) { - _copyprop( name, source, obj); - } + //removing all ../ + do { + oldUrl = url; + url = url.replace(this.normalizeRE, ''); + } while(oldUrl.length !== url.length); + return url; + }, + + // The platform-specific file separator. '\\' or '/'. + sep: (cc.sys.os === cc.sys.OS_WINDOWS ? '\\' : '/'), + + // @param {string} path + // @param {boolean|string} [endsWithSep = true] + // @returns {string} + _setEndWithSep: function (path, endsWithSep) { + var sep = cc.path.sep; + if (typeof endsWithSep === 'undefined') { + endsWithSep = true; } + else if (typeof endsWithSep === 'string') { + sep = endsWithSep; + endsWithSep = !!endsWithSep; + } + + var endChar = path[path.length - 1]; + var oldEndWithSep = (endChar === '\\' || endChar === '/'); + if (!oldEndWithSep && endsWithSep) { + path += sep; + } + else if (oldEndWithSep && !endsWithSep) { + path = path.slice(0, -1); + } + return path; } - return obj; -} - -cc.js = { - mixin: mixin -}; - -cc.initEngine = function (config, cb) { - require('script/jsb.js'); - cc._renderType = cc.game.RENDER_TYPE_OPENGL; - cc._initDebugSetting(config[cc.game.CONFIG_KEY.debugMode]); - cc._engineLoaded = true; - cc.log(cc.ENGINE_VERSION); - if (cb) cb(); -}; +}); // cc.game -mixin(cc.game, { +cc.js.mixin(cc.game, { // states _paused: false, //whether the game is paused @@ -114,6 +116,10 @@ mixin(cc.game, { // Prepare never called and engine ready if (cc._engineLoaded) { this._prepareCalled = true; + + // Additional step in JSB + cc.director.sharedInit(); + // Load game scripts var jsList = config[CONFIG_KEY.jsList]; if (jsList) { @@ -250,7 +256,7 @@ mixin(cc.game, { }); // cc.director -mixin(cc.director, { +cc.js.mixin(cc.director, { /** * Manage all init process shared between the web engine and jsb engine. * All platform independent init process should be occupied here. @@ -259,7 +265,7 @@ mixin(cc.director, { // Animation manager if (cc.AnimationManager) { this._animationManager = new cc.AnimationManager(); - this._scheduler.scheduleUpdate(this._animationManager, cc.Scheduler.PRIORITY_SYSTEM, false); + this.getScheduler().scheduleUpdate(this._animationManager, cc.Scheduler.PRIORITY_SYSTEM, false); } else { this._animationManager = null; @@ -292,6 +298,15 @@ mixin(cc.director, { this.startAnimation(); }, + /** + * Returns current running Scene. Director can only run one Scene at the time. + * @method getScene + * @return {Scene} + */ + getScene: function () { + return this._scene; + }, + /** * Run a scene. Replaces the running scene with a new one or enter the first scene. * @method runScene @@ -299,7 +314,7 @@ mixin(cc.director, { * @param {Function} [onBeforeLoadScene] - The function at the scene before loading. */ runScene: function (scene, onBeforeLoadScene) { - // cc.assert(scene, cc._LogInfos.Director.pushScene); + cc.assert(scene, cc._LogInfos.Director.pushScene); // unload scene var oldScene = this._scene; @@ -402,6 +417,43 @@ mixin(cc.director, { } }, + // load raw assets + _loadRawAssets: function (assetObjects, done) { + var urls = assetObjects.map(function (asset) { + return asset.url; + }); + + //var info = 'preload ' + urls; + //console.time(info); + + // currently cocos jsb 3.3 not support preload too much assets + // so we divide assets to 30 a group + var index = 0; + var count = 30; + var total = urls.length; + + function preload () { + if (index + count > total) { + // the last time + count = total - index; + } + + var assets = urls.slice(index, count); + + index += count; + + if (index < total) { + cc.loader.load(assets, preload); + } + else { + //console.timeEnd(info); + done(); + } + } + + preload(); + }, + /** * Loads the scene by its uuid. * @method _loadSceneByUuid @@ -413,6 +465,7 @@ mixin(cc.director, { _loadSceneByUuid: function (uuid, onLaunched, onUnloaded) { //cc.AssetLibrary.unloadAsset(uuid); // force reload cc.AssetLibrary.loadAsset(uuid, function (error, sceneAsset) { + var self = cc.director; var scene; if (error) { error = 'Failed to load scene: ' + error; @@ -426,7 +479,7 @@ mixin(cc.director, { scene = sceneAsset.scene; if (scene instanceof cc.Scene) { scene._id = uuid; - cc.director.runScene(scene, onUnloaded); + self.runScene(scene, onUnloaded); } else { error = 'The asset ' + uuid + ' is not a scene'; @@ -434,15 +487,687 @@ mixin(cc.director, { scene = null; } } - cc.director._loadingScene = ''; + self._loadingScene = ''; if (onLaunched) { onLaunched(error, scene); } - }); + }, { recordAssets: true }); } }); -cc.director.sharedInit(); +cc.Director.EVENT_PROJECTION_CHANGED = 'director_projection_changed'; +cc.Director.EVENT_AFTER_DRAW = 'director_after_draw'; +cc.Director.EVENT_BEFORE_VISIT = 'director_before_visit'; +cc.Director.EVENT_AFTER_VISIT = 'director_after_visit'; +cc.Director.EVENT_BEFORE_UPDATE = 'director_before_update'; +cc.Director.EVENT_AFTER_UPDATE = 'director_after_update'; +cc.Director.EVENT_BEFORE_SCENE_LAUNCH = 'director_before_scene_launch'; +cc.Director.EVENT_COMPONENT_UPDATE = 'director_component_update'; +cc.Director.EVENT_COMPONENT_LATE_UPDATE = 'director_component_late_update'; -cc._LogInfos = {}; +// Overwrite main loop +if (_engineNumberVersion && _engineNumberVersion >= 3.9) { + var scheduleTarget = { + update: function (_frameRate) { + // Call start for new added components + cc.director.emit(cc.Director.EVENT_BEFORE_UPDATE); + // Update for components + cc.director.emit(cc.Director.EVENT_COMPONENT_UPDATE, _frameRate); + } + }; + cc.Director.getInstance().getScheduler().scheduleUpdateForTarget(scheduleTarget, -1000, false); +} +else { + cc.eventManager.addCustomListener(cc.Director.EVENT_BEFORE_UPDATE, function () { + var _frameRate = cc.game.config[cc.game.CONFIG_KEY.frameRate]; + // Call start for new added components + cc.director.emit(cc.Director.EVENT_BEFORE_UPDATE); + // Update for components + cc.director.emit(cc.Director.EVENT_COMPONENT_UPDATE, _frameRate); + }); +} +cc.eventManager.addCustomListener(cc.Director.EVENT_AFTER_UPDATE, function () { + var _frameRate = cc.game.config[cc.game.CONFIG_KEY.frameRate]; + // Late update for components + cc.director.emit(cc.Director.EVENT_COMPONENT_LATE_UPDATE, _frameRate); + // User can use this event to do things after update + cc.director.emit(cc.Director.EVENT_AFTER_UPDATE); + + cc.director.emit(cc.Director.EVENT_BEFORE_VISIT, this); +}); +cc.eventManager.addCustomListener(cc.Director.EVENT_AFTER_VISIT, function () { + cc.director.emit(cc.Director.EVENT_AFTER_VISIT, this); +}); +cc.eventManager.addCustomListener(cc.Director.EVENT_AFTER_DRAW, function () { + cc.director.emit(cc.Director.EVENT_AFTER_DRAW, this); +}); +cc.ParticleSystem.Mode = cc.Enum({ + /** The gravity mode (A mode) */ + GRAVITY: 0, + /** The radius mode (B mode) */ + RADIUS: 1 +}); + + +cc.ParticleSystem.Type = cc.Enum({ + /** + * Living particles are attached to the world and are unaffected by emitter repositioning. + */ + FREE: 0, + + /** + * Living particles are attached to the world but will follow the emitter repositioning.
+ * Use case: Attach an emitter to an sprite, and you want that the emitter follows the sprite. + */ + RELATIVE: 1, + + /** + * Living particles are attached to the emitter and are translated along with it. + */ + GROUPED: 2 +}); + +cc.ProgressTimer.Type = cc.Enum({ + /** + * Radial Counter-Clockwise + */ + RADIAL: 0, + BAR: 1 +}); + + +cc.EditBox.InputMode = cc.Enum({ + + ANY: 0, + + /** + * The user is allowed to enter an e-mail address. + */ + EMAILADDR: 1, + + /** + * The user is allowed to enter an integer value. + */ + NUMERIC: 2, + + /** + * The user is allowed to enter a phone number. + */ + PHONENUMBER: 3, + + /** + * The user is allowed to enter a URL. + */ + URL: 4, + + /** + * The user is allowed to enter a real number value. + * This extends kEditBoxInputModeNumeric by allowing a decimal point. + */ + DECIMAL: 5, + + /** + * The user is allowed to enter any text, except for line breaks. + */ + SINGLELINE: 6 +}); + +/** + * Enum for the EditBox's input flags + * @readonly + * @enum {number} + * @memberof cc.EditBox + */ +cc.EditBox.InputFlag = cc.Enum({ + /** + * Indicates that the text entered is confidential data that should be + * obscured whenever possible. This implies EDIT_BOX_INPUT_FLAG_SENSITIVE. + */ + PASSWORD: 0, + + /** + * Indicates that the text entered is sensitive data that the + * implementation must never store into a dictionary or table for use + * in predictive, auto-completing, or other accelerated input schemes. + * A credit card number is an example of sensitive data. + */ + SENSITIVE: 1, + + /** + * This flag is a hint to the implementation that during text editing, + * the initial letter of each word should be capitalized. + */ + INITIAL_CAPS_WORD: 2, + + /** + * This flag is a hint to the implementation that during text editing, + * the initial letter of each sentence should be capitalized. + */ + INITIAL_CAPS_SENTENCE: 3, + + /** + * Capitalize all characters automatically. + */ + INITIAL_CAPS_ALL_CHARACTERS: 4 +}); + +/** + * Enum for keyboard return types + * @readonly + * @enum {number} + */ +cc.KeyboardReturnType = cc.Enum({ + DEFAULT: 0, + DONE: 1, + SEND: 2, + SEARCH: 3, + GO: 4 +}); + +/** + * Enum for text alignment + * @readonly + * @enum {number} + */ +cc.TextAlignment = cc.Enum({ + LEFT: 0, + CENTER: 1, + RIGHT: 2 +}); + +/** + * Enum for vertical text alignment + * @readonly + * @enum {number} + */ +cc.VerticalTextAlignment = cc.Enum({ + TOP: 0, + CENTER: 1, + BOTTOM: 2 +}); + +/** + * Enum for Relative layout parameter RelativeAlign + * @readonly + * @enum {number} + */ +ccui.RelativeLayoutParameter.Type = cc.Enum({ + /** + * The none of ccui.RelativeLayoutParameter's relative align. + */ + NONE: 0, + /** + * The parent's top left of ccui.RelativeLayoutParameter's relative align. + */ + PARENT_TOP_LEFT: 1, + /** + * The parent's top center horizontal of ccui.RelativeLayoutParameter's relative align. + */ + PARENT_TOP_CENTER_HORIZONTAL: 2, + /** + * The parent's top right of ccui.RelativeLayoutParameter's relative align. + */ + PARENT_TOP_RIGHT: 3, + /** + * The parent's left center vertical of ccui.RelativeLayoutParameter's relative align. + */ + PARENT_LEFT_CENTER_VERTICAL: 4, + + /** + * The center in parent of ccui.RelativeLayoutParameter's relative align. + */ + CENTER_IN_PARENT: 5, + + /** + * The parent's right center vertical of ccui.RelativeLayoutParameter's relative align. + */ + PARENT_RIGHT_CENTER_VERTICAL: 6, + /** + * The parent's left bottom of ccui.RelativeLayoutParameter's relative align. + */ + PARENT_LEFT_BOTTOM: 7, + /** + * The parent's bottom center horizontal of ccui.RelativeLayoutParameter's relative align. + */ + PARENT_BOTTOM_CENTER_HORIZONTAL: 8, + /** + * The parent's right bottom of ccui.RelativeLayoutParameter's relative align. + */ + PARENT_RIGHT_BOTTOM: 9, + + /** + * The location above left align of ccui.RelativeLayoutParameter's relative align. + */ + LOCATION_ABOVE_LEFTALIGN: 10, + /** + * The location above center of ccui.RelativeLayoutParameter's relative align. + */ + LOCATION_ABOVE_CENTER: 11, + /** + * The location above right align of ccui.RelativeLayoutParameter's relative align. + */ + LOCATION_ABOVE_RIGHTALIGN: 12, + /** + * The location left of top align of ccui.RelativeLayoutParameter's relative align. + */ + LOCATION_LEFT_OF_TOPALIGN: 13, + /** + * The location left of center of ccui.RelativeLayoutParameter's relative align. + */ + LOCATION_LEFT_OF_CENTER: 14, + /** + * The location left of bottom align of ccui.RelativeLayoutParameter's relative align. + */ + LOCATION_LEFT_OF_BOTTOMALIGN: 15, + /** + * The location right of top align of ccui.RelativeLayoutParameter's relative align. + */ + LOCATION_RIGHT_OF_TOPALIGN: 16, + /** + * The location right of center of ccui.RelativeLayoutParameter's relative align. + */ + LOCATION_RIGHT_OF_CENTER: 17, + /** + * The location right of bottom align of ccui.RelativeLayoutParameter's relative align. + */ + LOCATION_RIGHT_OF_BOTTOMALIGN: 18, + /** + * The location below left align of ccui.RelativeLayoutParameter's relative align. + */ + LOCATION_BELOW_LEFTALIGN: 19, + /** + * The location below center of ccui.RelativeLayoutParameter's relative align. + */ + LOCATION_BELOW_CENTER: 20, + /** + * The location below right align of ccui.RelativeLayoutParameter's relative align. + */ + LOCATION_BELOW_RIGHTALIGN: 21 +}); + + +/** + * Enum for layout type + * @readonly + * @enum {number} + */ +ccui.Layout.Type = cc.Enum({ + /** + * The absolute of ccui.Layout's layout type. + */ + ABSOLUTE: 0, + /** + * The vertical of ccui.Layout's layout type. + */ + LINEAR_VERTICAL: 1, + /** + * The horizontal of ccui.Layout's layout type. + */ + LINEAR_HORIZONTAL: 2, + /** + * The relative of ccui.Layout's layout type. + */ + RELATIVE: 3 +}); + +/** + * Enum for loadingBar Type + * @readonly + * @enum {number} + */ +ccui.LoadingBar.Type = cc.Enum({ + /** + * The left direction of ccui.LoadingBar. + */ + LEFT: 0, + /** + * The right direction of ccui.LoadingBar. + */ + RIGHT: 1 +}); + +/** + * Enum for ScrollView direction + * @readonly + * @enum {number} + */ +ccui.ScrollView.Dir = cc.Enum({ + /** + * The none flag of ccui.ScrollView's direction. + */ + NONE: 0, + /** + * The vertical flag of ccui.ScrollView's direction. + */ + VERTICAL: 1, + /** + * The horizontal flag of ccui.ScrollView's direction. + */ + HORIZONTAL: 2, + /** + * The both flag of ccui.ScrollView's direction. + */ + BOTH: 3 +}); + +cc.EventTarget.call(cc.game); +cc.js.addon(cc.game, cc.EventTarget.prototype); +cc.EventTarget.call(cc.director); +cc.js.addon(cc.director, cc.EventTarget.prototype); + +// cc.Label +cc._Label = cc.Label; +cc._Label.prototype.setHorizontalAlign = cc._Label.prototype.setHorizontalAlignment; +cc._Label.prototype.setVerticalAlign = cc._Label.prototype.setVerticalAlignment; +cc._Label.prototype.setFontSize = function (size) { + if (this._labelType === _ccsg.Label.Type.TTF) { + this.setSystemFontSize(size); + } + else if (this._labelType === _ccsg.Label.Type.BMFont) { + this.setBMFontSize(size); + } +}; +cc._Label.prototype.enableWrapText = cc._Label.prototype.enableWrap; +cc._Label.prototype.isWrapTextEnabled = cc._Label.prototype.isWrapEnabled; +cc._Label.prototype._setLineHeight = cc._Label.prototype.setLineHeight; +cc._Label.prototype.setLineHeight = function (height) { + if (this._labelType === _ccsg.Label.Type.BMFont) { + this._setLineHeight(height); + } +}; +cc.Label = function (string, fontHandle, type) { + var label; + if (type === _ccsg.Label.Type.TTF) { + label = cc._Label.createWithSystemFont(string, fontHandle, 40); + } + else if (type === _ccsg.Label.Type.BMFont) { + label = cc._Label.createWithBMFont(fontHandle, string); + } + label._labelType = type; + return label; +}; +cc.Label.Type = cc.Enum({ + TTF: 0, + BMFont: 1 +}); +cc.Label.Overflow = cc.Enum({ + CLAMP: 0, + SHRINK: 1, + RESIZE: 2 +}); + +cc.spriteFrameAnimationCache = cc.animationCache; +cc.SpriteFrameAnimation = cc.Animation; + +// Assets +cc.js.setClassName('cc.SpriteFrame', cc.SpriteFrame); + +// ccsg +window._ccsg = { + Node: cc.Node, + Scene: cc.Scene, + Sprite: cc.Sprite, + ParticleSystem: cc.ParticleSystem, + Label: cc.Label, + +}; + +// rename cc.Class to cc._Class +cc._Class = cc.Class; + +// cc.textureCache.cacheImage +cc.textureCache._textures = {}; +cc.textureCache.cacheImage = function (key, texture) { + if (texture instanceof cc.Texture2D) { + this._textures[key] = texture; + } +}; +cc.textureCache._getTextureForKey = cc.textureCache.getTextureForKey; +cc.textureCache.getTextureForKey = function (key) { + var tex = this._getTextureForKey(key); + if (!tex) + tex = this._textures[key]; + return tex || null; +}; + +// cc.Texture2D +cc.Texture2D.prototype.isLoaded = function () { + return true; +}; +cc.Texture2D.prototype.getPixelWidth = cc.Texture2D.prototype.getPixelsWide; +cc.Texture2D.prototype.getPixelHeight = cc.Texture2D.prototype.getPixelsHigh; + +// cc.SpriteFrame +cc.js.mixin(cc.SpriteFrame.prototype, cc.EventTarget.prototype); +cc.SpriteFrame.prototype.textureLoaded = function () { + return this.getTexture() !== null; +}; +cc.SpriteFrame.prototype._initWithTexture = cc.SpriteFrame.prototype.initWithTexture; +cc.SpriteFrame.prototype.initWithTexture = function (texture, rect, rotated, offset, originalSize, _uuid) { + function check(texture) { + if (texture && texture.isLoaded()) { + var _x, _y; + if (rotated) { + _x = rect.x + rect.height; + _y = rect.y + rect.width; + } + else { + _x = rect.x + rect.width; + _y = rect.y + rect.height; + } + if (_x > texture.getPixelWidth()) { + cc.error(cc._LogInfos.RectWidth, _uuid); + } + if (_y > texture.getPixelHeight()) { + cc.error(cc._LogInfos.RectHeight, _uuid); + } + } + } + + if(arguments.length === 2) + rect = cc.rectPointsToPixels(rect); + + offset = cc.p(0, 0); + originalSize = originalSize || rect; + rotated = rotated || false; + + if (this.insetTop === undefined) { + this.insetTop = 0; + this.insetBottom = 0; + this.insetLeft = 0; + this.insetRight = 0; + } + + var locTexture; + if (!texture && _uuid) { + // deserialize texture from uuid + var info = cc.AssetLibrary._getAssetInfoInRuntime(_uuid); + if (!info) { + cc.error('SpriteFrame: Failed to load sprite texture "%s"', _uuid); + return; + } + + this._textureFilename = info.url; + + locTexture = cc.textureCache.addImage(info.url); + this._initWithTexture(locTexture, rect, rotated, offset, originalSize); + } + else { + if (cc.js.isString(texture)){ + this._textureFilename = texture; + locTexture = cc.textureCache.addImage(this._textureFilename); + this._initWithTexture(locTexture, rect, rotated, offset, originalSize); + } else if (texture instanceof cc.Texture2D) { + this._textureFilename = ''; + this._initWithTexture(texture, rect, rotated, offset, originalSize); + } + } + this.emit('load'); + check(this.getTexture()); + return true; +}; +cc.SpriteFrame.prototype._deserialize = function (data, handle) { + var rect = data.rect ? new cc.Rect(data.rect[0], data.rect[1], data.rect[2], data.rect[3]) : undefined; + var offset = data.offset ? new cc.Vec2(data.offset[0], data.offset[1]) : undefined; + var size = data.originalSize ? new cc.Size(data.originalSize[0], data.originalSize[1]) : undefined; + var rotated = data.rotated === 1; + // init properties not included in this._initWithTexture() + this._name = data.name; + var capInsets = data.capInsets; + if (capInsets) { + this.insetLeft = capInsets[0]; + this.insetTop = capInsets[1]; + this.insetRight = capInsets[2]; + this.insetBottom = capInsets[3]; + } + + // load texture via _textureFilenameSetter + var textureUuid = data.texture; + if (textureUuid) { + handle.result.push(this, '_textureFilenameSetter', textureUuid); + } + + this.initWithTexture(null, rect, rotated, offset, size, textureUuid); +}; +cc.SpriteFrame.prototype._checkRect = function (texture) { + var rect = this.getRectInPixels(); + var maxX = rect.x, maxY = rect.y; + if (this._rotated) { + maxX += rect.height; + maxY += rect.width; + } + else { + maxX += rect.width; + maxY += rect.height; + } + if (maxX > texture.getPixelWidth()) { + cc.error(cc._LogInfos.RectWidth, texture.url); + } + if (maxY > texture.getPixelHeight()) { + cc.error(cc._LogInfos.RectHeight, texture.url); + } +}; +var getTextureJSB = cc.SpriteFrame.prototype.getTexture; +cc.SpriteFrame.prototype.getTexture = function () { + var tex = getTextureJSB.call(this); + this._texture = tex; + return tex; +}; +cc.js.set(cc.SpriteFrame.prototype, '_textureFilenameSetter', function (url) { + this._textureFilename = url; + if (url) { + // texture will be init in getTexture() + var texture = this.getTexture(); + if (this.textureLoaded()) { + this._checkRect(texture); + this.emit('load'); + } + else { + // register event in setTexture() + this._texture = null; + this.setTexture(texture); + } + } +}); + +// cc.Label +cc.Label.prototype.setHorizontalAlign = cc.Label.prototype.setHorizontalAlignment; +cc.Label.prototype.setVerticalAlign = cc.Label.prototype.setVerticalAlignment; +cc.Label.prototype.setFontSize = cc.Label.prototype.setSystemFontSize; +cc.Label.prototype.setOverflow = function () {}; +cc.Label.prototype.enableWrapText = function () {}; +cc.Label.prototype.setLineHeight = function () {}; + +// cc.Event#getCurrentTarget +cc.Event.prototype._getCurrentTarget = cc.Event.prototype.getCurrentTarget; +cc.Event.prototype.getCurrentTarget = function () { + return this._currentTarget || this._getCurrentTarget(); +}; + +// cc.eventManager.addListener +cc.eventManager.addListener = function(listener, nodeOrPriority) { + if(!(listener instanceof cc.EventListener)) { + listener = cc.EventListener.create(listener); + } + + if (typeof nodeOrPriority == "number") { + if (nodeOrPriority == 0) { + cc.log("0 priority is forbidden for fixed priority since it's used for scene graph based priority."); + return; + } + + cc.eventManager.addEventListenerWithFixedPriority(listener, nodeOrPriority); + } else { + var node = nodeOrPriority; + if (nodeOrPriority instanceof cc.Component) { + node = nodeOrPriority.node._sgNode; + } + if (nodeOrPriority instanceof cc.Node) { + node = nodeOrPriority._sgNode; + } + // rebind target + if (node !== nodeOrPriority) { + var keys = Object.keys(listener); + // Overwrite all functions + for (var i = 0; i < keys.length; ++i) { + var key = keys[i]; + var value = listener[key]; + if (typeof value === 'function') { + // var _value = value; + listener[key] = (function (realCallback) { + return function (event1, event2) { + // event must be the last argument, and arguments count could be 1 or 2 + var event = event2 || event1; + // Replace event's _currentTarget + if (event) { + event._currentTarget = nodeOrPriority; + } + return realCallback.call(this, event1, event2); + }; + })(value); + } + } + } + cc.eventManager.addEventListenerWithSceneGraphPriority(listener, node); + } + + return listener; +}; + +// cc.Scheduler +cc.Scheduler.prototype.scheduleUpdate = cc.Scheduler.prototype.scheduleUpdateForTarget; + +// cc.Scale9Sprite +cc.Scale9Sprite.prototype.setRenderingType = function (type) { + if (type === cc.SpriteType.SIMPLE) { + this.setScale9Enabled(false); + } + else { + this.setScale9Enabled(true); + } +}; +cc.Scale9Sprite.prototype._setBlendFunc = cc.Scale9Sprite.prototype.setBlendFunc; +cc.Scale9Sprite.prototype.setBlendFunc = function(blendFunc, dst) { + if (dst !== undefined) { + blendFunc = { + src : blendFunc, + dst : dst + }; + } + this._setBlendFunc(blendFunc); +}; +cc.Scale9Sprite.prototype._setContentSize = cc.Scale9Sprite.prototype.setContentSize; +cc.Scale9Sprite.prototype.setContentSize = function(size, height){ + if (height !== undefined) { + size = new cc.Size(size, height); + } + this._setContentSize(size); +}; +cc.Scale9Sprite.prototype._setAnchorPoint = cc.Scale9Sprite.prototype.setAnchorPoint; +cc.Scale9Sprite.prototype.setAnchorPoint = function(anchorPoint, y){ + if (y !== undefined) { + anchorPoint = new cc.Vec2(anchorPoint, y); + } + this._setAnchorPoint(anchorPoint); +}; diff --git a/jsb_polyfill.js b/jsb_polyfill.js deleted file mode 100644 index 1f6cc2ef0b..0000000000 --- a/jsb_polyfill.js +++ /dev/null @@ -1,583 +0,0 @@ -'use strict'; - -cc.ParticleSystem.Mode = cc.Enum({ - /** The gravity mode (A mode) */ - GRAVITY: 0, - /** The radius mode (B mode) */ - RADIUS: 1 -}); - -cc.ParticleSystem.Type = cc.Enum({ - /** - * Living particles are attached to the world and are unaffected by emitter repositioning. - */ - FREE: 0, - - /** - * Living particles are attached to the world but will follow the emitter repositioning.
- * Use case: Attach an emitter to an sprite, and you want that the emitter follows the sprite. - */ - RELATIVE: 1, - - /** - * Living particles are attached to the emitter and are translated along with it. - */ - GROUPED: 2 -}); - -cc.ProgressTimer.Type = cc.Enum({ - /** - * Radial Counter-Clockwise - */ - RADIAL: 0, - BAR: 1 -}); - - -cc.EditBox.InputMode = cc.Enum({ - - ANY: 0, - - /** - * The user is allowed to enter an e-mail address. - */ - EMAILADDR: 1, - - /** - * The user is allowed to enter an integer value. - */ - NUMERIC: 2, - - /** - * The user is allowed to enter a phone number. - */ - PHONENUMBER: 3, - - /** - * The user is allowed to enter a URL. - */ - URL: 4, - - /** - * The user is allowed to enter a real number value. - * This extends kEditBoxInputModeNumeric by allowing a decimal point. - */ - DECIMAL: 5, - - /** - * The user is allowed to enter any text, except for line breaks. - */ - SINGLELINE: 6 -}); - -/** - * Enum for the EditBox's input flags - * @readonly - * @enum {number} - * @memberof cc.EditBox - */ -cc.EditBox.InputFlag = cc.Enum({ - /** - * Indicates that the text entered is confidential data that should be - * obscured whenever possible. This implies EDIT_BOX_INPUT_FLAG_SENSITIVE. - */ - PASSWORD: 0, - - /** - * Indicates that the text entered is sensitive data that the - * implementation must never store into a dictionary or table for use - * in predictive, auto-completing, or other accelerated input schemes. - * A credit card number is an example of sensitive data. - */ - SENSITIVE: 1, - - /** - * This flag is a hint to the implementation that during text editing, - * the initial letter of each word should be capitalized. - */ - INITIAL_CAPS_WORD: 2, - - /** - * This flag is a hint to the implementation that during text editing, - * the initial letter of each sentence should be capitalized. - */ - INITIAL_CAPS_SENTENCE: 3, - - /** - * Capitalize all characters automatically. - */ - INITIAL_CAPS_ALL_CHARACTERS: 4 -}); - -/** - * Enum for keyboard return types - * @readonly - * @enum {number} - */ -cc.KeyboardReturnType = cc.Enum({ - DEFAULT: 0, - DONE: 1, - SEND: 2, - SEARCH: 3, - GO: 4 -}); - -/** - * Enum for text alignment - * @readonly - * @enum {number} - */ -cc.TextAlignment = cc.Enum({ - LEFT: 0, - CENTER: 1, - RIGHT: 2 -}); - -/** - * Enum for vertical text alignment - * @readonly - * @enum {number} - */ -cc.VerticalTextAlignment = cc.Enum({ - TOP: 0, - CENTER: 1, - BOTTOM: 2 -}); - -/** - * Enum for Relative layout parameter RelativeAlign - * @readonly - * @enum {number} - */ -ccui.RelativeLayoutParameter.Type = cc.Enum({ - /** - * The none of ccui.RelativeLayoutParameter's relative align. - */ - NONE: 0, - /** - * The parent's top left of ccui.RelativeLayoutParameter's relative align. - */ - PARENT_TOP_LEFT: 1, - /** - * The parent's top center horizontal of ccui.RelativeLayoutParameter's relative align. - */ - PARENT_TOP_CENTER_HORIZONTAL: 2, - /** - * The parent's top right of ccui.RelativeLayoutParameter's relative align. - */ - PARENT_TOP_RIGHT: 3, - /** - * The parent's left center vertical of ccui.RelativeLayoutParameter's relative align. - */ - PARENT_LEFT_CENTER_VERTICAL: 4, - - /** - * The center in parent of ccui.RelativeLayoutParameter's relative align. - */ - CENTER_IN_PARENT: 5, - - /** - * The parent's right center vertical of ccui.RelativeLayoutParameter's relative align. - */ - PARENT_RIGHT_CENTER_VERTICAL: 6, - /** - * The parent's left bottom of ccui.RelativeLayoutParameter's relative align. - */ - PARENT_LEFT_BOTTOM: 7, - /** - * The parent's bottom center horizontal of ccui.RelativeLayoutParameter's relative align. - */ - PARENT_BOTTOM_CENTER_HORIZONTAL: 8, - /** - * The parent's right bottom of ccui.RelativeLayoutParameter's relative align. - */ - PARENT_RIGHT_BOTTOM: 9, - - /** - * The location above left align of ccui.RelativeLayoutParameter's relative align. - */ - LOCATION_ABOVE_LEFTALIGN: 10, - /** - * The location above center of ccui.RelativeLayoutParameter's relative align. - */ - LOCATION_ABOVE_CENTER: 11, - /** - * The location above right align of ccui.RelativeLayoutParameter's relative align. - */ - LOCATION_ABOVE_RIGHTALIGN: 12, - /** - * The location left of top align of ccui.RelativeLayoutParameter's relative align. - */ - LOCATION_LEFT_OF_TOPALIGN: 13, - /** - * The location left of center of ccui.RelativeLayoutParameter's relative align. - */ - LOCATION_LEFT_OF_CENTER: 14, - /** - * The location left of bottom align of ccui.RelativeLayoutParameter's relative align. - */ - LOCATION_LEFT_OF_BOTTOMALIGN: 15, - /** - * The location right of top align of ccui.RelativeLayoutParameter's relative align. - */ - LOCATION_RIGHT_OF_TOPALIGN: 16, - /** - * The location right of center of ccui.RelativeLayoutParameter's relative align. - */ - LOCATION_RIGHT_OF_CENTER: 17, - /** - * The location right of bottom align of ccui.RelativeLayoutParameter's relative align. - */ - LOCATION_RIGHT_OF_BOTTOMALIGN: 18, - /** - * The location below left align of ccui.RelativeLayoutParameter's relative align. - */ - LOCATION_BELOW_LEFTALIGN: 19, - /** - * The location below center of ccui.RelativeLayoutParameter's relative align. - */ - LOCATION_BELOW_CENTER: 20, - /** - * The location below right align of ccui.RelativeLayoutParameter's relative align. - */ - LOCATION_BELOW_RIGHTALIGN: 21 -}); - - -/** - * Enum for layout type - * @readonly - * @enum {number} - */ -ccui.Layout.Type = cc.Enum({ - /** - * The absolute of ccui.Layout's layout type. - */ - ABSOLUTE: 0, - /** - * The vertical of ccui.Layout's layout type. - */ - LINEAR_VERTICAL: 1, - /** - * The horizontal of ccui.Layout's layout type. - */ - LINEAR_HORIZONTAL: 2, - /** - * The relative of ccui.Layout's layout type. - */ - RELATIVE: 3 -}); - -/** - * Enum for loadingBar Type - * @readonly - * @enum {number} - */ -ccui.LoadingBar.Type = cc.Enum({ - /** - * The left direction of ccui.LoadingBar. - */ - LEFT: 0, - /** - * The right direction of ccui.LoadingBar. - */ - RIGHT: 1 -}); - -/** - * Enum for ScrollView direction - * @readonly - * @enum {number} - */ -ccui.ScrollView.Dir = cc.Enum({ - /** - * The none flag of ccui.ScrollView's direction. - */ - NONE: 0, - /** - * The vertical flag of ccui.ScrollView's direction. - */ - VERTICAL: 1, - /** - * The horizontal flag of ccui.ScrollView's direction. - */ - HORIZONTAL: 2, - /** - * The both flag of ccui.ScrollView's direction. - */ - BOTH: 3 -}); - -require("CCDebugger"); - -cc.js.mixin(cc.path, { - //todo make public after verification - _normalize: function(url){ - var oldUrl = url = String(url); - - //removing all ../ - do { - oldUrl = url; - url = url.replace(this.normalizeRE, ''); - } while(oldUrl.length !== url.length); - return url; - }, - - // The platform-specific file separator. '\\' or '/'. - sep: (cc.sys.os === cc.sys.OS_WINDOWS ? '\\' : '/'), - - // @param {string} path - // @param {boolean|string} [endsWithSep = true] - // @returns {string} - _setEndWithSep: function (path, endsWithSep) { - var sep = cc.path.sep; - if (typeof endsWithSep === 'undefined') { - endsWithSep = true; - } - else if (typeof endsWithSep === 'string') { - sep = endsWithSep; - endsWithSep = !!endsWithSep; - } - - var endChar = path[path.length - 1]; - var oldEndWithSep = (endChar === '\\' || endChar === '/'); - if (!oldEndWithSep && endsWithSep) { - path += sep; - } - else if (oldEndWithSep && !endsWithSep) { - path = path.slice(0, -1); - } - return path; - } -}); - -cc.EventTarget.call(cc.game); -cc.js.addon(cc.game, cc.EventTarget.prototype); -cc.EventTarget.call(cc.director); -cc.js.addon(cc.director, cc.EventTarget.prototype); - -// cc.Label -cc._Label = cc.Label; -cc.Label = function (string, fontHandle, type) { - var label; - if (type === cc.Label.Type.TTF) { - label = cc._Label.createWithSystemFont(string, fontHandle, 40); - } - else if (type === cc.Label.Type.BMFont) { - label = cc._Label.createWithBMFont(fontHandle, string); - } - return label; -}; -cc.Label.Type = cc.Enum({ - TTF: 0, - BMFont: 1 -}); -cc.Label.Overflow = cc.Enum({ - CLAMP: 0, - SHRINK: 1, - RESIZE: 2 -}); - -cc.spriteFrameAnimationCache = cc.animationCache; -cc.SpriteFrameAnimation = cc.Animation; - -// Assets -cc.js.setClassName('cc.SpriteFrame', cc.SpriteFrame); - -// ccsg -window._ccsg = { - Node: cc.Node, - Scene: cc.Scene, - Sprite: cc.Sprite, - ParticleSystem: cc.ParticleSystem, - Label: cc.Label, - -}; - -// rename cc.Class to cc._Class -cc._Class = cc.Class; - -// cc.textureCache.cacheImage -cc.textureCache._textures = {}; -cc.textureCache.cacheImage = function (key, texture) { - if (texture instanceof cc.Texture2D) { - this._textures[key] = texture; - } -}; -cc.textureCache._getTextureForKey = cc.textureCache.getTextureForKey; -cc.textureCache.getTextureForKey = function (key) { - var tex = this._getTextureForKey(key); - if (!tex) - tex = this._textures[key]; - return tex || null; -}; - -// cc.Texture2D -cc.Texture2D.prototype.isLoaded = function () { - return true; -}; -cc.Texture2D.prototype.getPixelWidth = cc.Texture2D.prototype.getPixelsWide; -cc.Texture2D.prototype.getPixelHeight = cc.Texture2D.prototype.getPixelsHigh; - -// cc.SpriteFrame -cc.SpriteFrame.prototype.textureLoaded = function () { - return this.getTexture() !== null; -}; -cc.SpriteFrame.prototype._deserialize = function (data, handle) { - var rect = data.rect; - rect = new cc.Rect(rect[0], rect[1], rect[2], rect[3]); - var rectInP = cc.rectPointsToPixels(rect); - var offset = new cc.Vec2(data.offset[0], data.offset[1]); - var offsetInP = cc.pointPointsToPixels(offset); - var size = new cc.Size(data.originalSize[0], data.originalSize[1]); - var sizeInP = cc.sizePointsToPixels(size); - var rotated = data.rotated === 1; - // init properties not included in this._initWithTexture() - this._name = data.name; - var capInsets = data.capInsets; - if (capInsets) { - this.insetLeft = capInsets[0]; - this.insetTop = capInsets[1]; - this.insetRight = capInsets[2]; - this.insetBottom = capInsets[3]; - } - - // load texture via _textureFilenameSetter - var textureUuid = data.texture; - if (textureUuid) { - handle.result.push(this, '_textureFilenameSetter', textureUuid); - } - - this.initWithTexture(null, rectInP, rotated, offsetInP, sizeInP); -}; -cc.SpriteFrame.prototype._checkRect = function (texture) { - var rect = this._rectInPixels; - var maxX = rect.x, maxY = rect.y; - if (this._rotated) { - maxX += rect.height; - maxY += rect.width; - } - else { - maxX += rect.width; - maxY += rect.height; - } - if (maxX > texture.getPixelWidth()) { - cc.error(cc._LogInfos.RectWidth, texture.url); - } - if (maxY > texture.getPixelHeight()) { - cc.error(cc._LogInfos.RectHeight, texture.url); - } -}; -var getTextureJSB = cc.SpriteFrame.prototype.getTexture; -cc.SpriteFrame.prototype.getTexture = function () { - var tex = getTextureJSB.call(this); - this._texture = tex; - return tex; -}; -cc.js.set(cc.SpriteFrame.prototype, '_textureFilenameSetter', function (url) { - this._textureFilename = url; - if (url) { - // texture will be init in getTexture() - var texture = this.getTexture(); - if (this.textureLoaded()) { - this._checkRect(texture); - this.emit('load'); - } - else { - // register event in setTexture() - this._texture = null; - this.setTexture(texture); - } - } -}); - -// cc.Label -cc.Label.prototype.setHorizontalAlign = cc.Label.prototype.setHorizontalAlignment; -cc.Label.prototype.setVerticalAlign = cc.Label.prototype.setVerticalAlignment; -cc.Label.prototype.setFontSize = cc.Label.prototype.setSystemFontSize; -cc.Label.prototype.setOverflow = function () {}; -cc.Label.prototype.enableWrapText = function () {}; -cc.Label.prototype.setLineHeight = function () {}; - -// cc.Event#getCurrentTarget -cc.Event.prototype._getCurrentTarget = cc.Event.prototype.getCurrentTarget; -cc.Event.prototype.getCurrentTarget = function () { - return this._currentTarget || this._getCurrentTarget(); -}; - -// cc.eventManager.addListener -cc.eventManager.addListener = function(listener, nodeOrPriority) { - if(!(listener instanceof cc.EventListener)) { - listener = cc.EventListener.create(listener); - } - - if (typeof nodeOrPriority == "number") { - if (nodeOrPriority == 0) { - cc.log("0 priority is forbidden for fixed priority since it's used for scene graph based priority."); - return; - } - - cc.eventManager.addEventListenerWithFixedPriority(listener, nodeOrPriority); - } else { - var node = nodeOrPriority; - if (nodeOrPriority instanceof cc.Component) { - node = nodeOrPriority.node._sgNode; - } - if (nodeOrPriority instanceof cc.Node) { - node = nodeOrPriority._sgNode; - } - // rebind target - if (node !== nodeOrPriority) { - var keys = Object.keys(listener); - // Overwrite all functions - for (var i = 0; i < keys.length; ++i) { - var key = keys[i]; - var value = listener[key]; - if (typeof value === 'function') { - // var _value = value; - listener[key] = function (event1, event2) { - // event must be the last argument, and arguments count could be 1 or 2 - var event = event2 || event1; - // Replace event's _currentTarget - if (event) { - event._currentTarget = nodeOrPriority; - } - value.call(this, event1, event2); - }; - } - } - } - cc.eventManager.addEventListenerWithSceneGraphPriority(listener, node); - } - - return listener; -}; - -// cc.Scheduler -cc.Scheduler.prototype.scheduleUpdate = cc.Scheduler.prototype.scheduleUpdateForTarget; - -// cc.Scale9Sprite -cc.Scale9Sprite.prototype._setBlendFunc = cc.Scale9Sprite.prototype.setBlendFunc; -cc.Scale9Sprite.prototype.setBlendFunc = function(blendFunc, dst) { - if (dst !== undefined) { - blendFunc = { - src : blendFunc, - dst : dst - }; - } - this._setBlendFunc(blendFunc); -}; -cc.Scale9Sprite.prototype._setContentSize = cc.Scale9Sprite.prototype.setContentSize; -cc.Scale9Sprite.prototype.setContentSize = function(size, height){ - if (height !== undefined) { - size = new cc.Size(size, height); - } - this._setContentSize(size); -}; -cc.Scale9Sprite.prototype._setAnchorPoint = cc.Scale9Sprite.prototype.setAnchorPoint; -cc.Scale9Sprite.prototype.setAnchorPoint = function(anchorPoint, y){ - if (y !== undefined) { - anchorPoint = new cc.Vec2(anchorPoint, y); - } - this._setAnchorPoint(anchorPoint); -}; diff --git a/jsb_predefine.js b/jsb_predefine.js index 36bb623f71..4f14dd9828 100644 --- a/jsb_predefine.js +++ b/jsb_predefine.js @@ -25,6 +25,24 @@ // define cc +// Run jsb.js before all polyfills +cc.initEngine({ + debugMode: cc.game.DEBUG_MODE_INFO +}); + +if (!cc.ClassManager) { + cc.ClassManager = window.ClassManager || { + id : (0|(Math.random()*998)), + instanceId : (0|(Math.random()*998)), + getNewID : function(){ + return this.id++; + }, + getNewInstanceId : function(){ + return this.instanceId++; + } + }; +} + if (CC_DEV) { /** * contains internal apis for unit tests @@ -35,7 +53,7 @@ if (CC_DEV) { // predefine some modules for cocos -require('./polyfill'); require('./cocos2d/core/platform/js'); require('./cocos2d/core/value-types'); require('./cocos2d/core/event'); +require('./CCDebugger');