From 692cc795000bfbcdeee1348e8a72e1c61fb7e30b Mon Sep 17 00:00:00 2001 From: pandamicro Date: Tue, 5 Jan 2016 18:11:51 +0800 Subject: [PATCH] Separate JSB polyfills into different modules --- gulpfile.js | 6 +- jsb/index.js | 29 + jsb_apis.js => jsb/jsb-apis.js | 0 jsb/jsb-director.js | 319 ++++++ jsb/jsb-enums.js | 308 +++++ jsb/jsb-etc.js | 118 ++ jsb/jsb-event.js | 160 +++ jsb/jsb-game.js | 240 ++++ jsb/jsb-label.js | 126 ++ jsb/jsb-particle.js | 94 ++ jsb_predefine.js => jsb/jsb-predefine.js | 13 +- jsb/jsb-tex-sprite-frame.js | 186 +++ jsb/versions/jsb-polyfill-v3.8.js | 36 + jsb_engine.js | 1338 ---------------------- 14 files changed, 1622 insertions(+), 1351 deletions(-) create mode 100644 jsb/index.js rename jsb_apis.js => jsb/jsb-apis.js (100%) create mode 100644 jsb/jsb-director.js create mode 100644 jsb/jsb-enums.js create mode 100644 jsb/jsb-etc.js create mode 100644 jsb/jsb-event.js create mode 100644 jsb/jsb-game.js create mode 100644 jsb/jsb-label.js create mode 100644 jsb/jsb-particle.js rename jsb_predefine.js => jsb/jsb-predefine.js (90%) create mode 100644 jsb/jsb-tex-sprite-frame.js create mode 100644 jsb/versions/jsb-polyfill-v3.8.js delete mode 100644 jsb_engine.js diff --git a/gulpfile.js b/gulpfile.js index b9f32ac15f..2dea6199df 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -9,8 +9,7 @@ paths = { src: './src', jsEntry: './index.js', JSBEntries: [ - './jsb_predefine.js', - './jsb_engine.js', + './jsb/index.js', './extends.js' ], outDir: './bin', @@ -34,9 +33,6 @@ paths = { destEditorExtends: 'bin/cocos2d-js-extends-for-test.js' }, - originCocos2dCompileDir: './tools', - originCocos2d: './lib/cocos2d-js-v3.9-min.js', - //originSourcemap: './lib/cocos2d-js-v3.9-sourcemap', modularCocos2d: './bin/modular-cocos2d.js', }; diff --git a/jsb/index.js b/jsb/index.js new file mode 100644 index 0000000000..df3df1a707 --- /dev/null +++ b/jsb/index.js @@ -0,0 +1,29 @@ +'use strict'; + +require('./jsb-predefine'); +require('./jsb-game'); +require('./jsb-director'); +require('./jsb-tex-sprite-frame'); +require('./jsb-label'); +require('./jsb-particle'); +require('./jsb-enums'); +require('./jsb-event'); +require('./jsb-etc'); + +// Check version +var _engineNumberVersion = (function () { + var result = /Cocos2d\-JS\sv([\.\d]+)/.exec(cc.ENGINE_VERSION); + if (result && result[1]) { + return parseFloat(result[1]); + } + else { + return null; + } +})(); + +// Version polyfills +if (_engineNumberVersion) { + if (_engineNumberVersion < 3.9) { + require('./versions/jsb-polyfill-v3.8'); + } +} \ No newline at end of file diff --git a/jsb_apis.js b/jsb/jsb-apis.js similarity index 100% rename from jsb_apis.js rename to jsb/jsb-apis.js diff --git a/jsb/jsb-director.js b/jsb/jsb-director.js new file mode 100644 index 0000000000..d5a9ef5bfd --- /dev/null +++ b/jsb/jsb-director.js @@ -0,0 +1,319 @@ +/**************************************************************************** + Copyright (c) 2015 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +'use strict'; + +// 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. + */ + sharedInit: function () { + // Animation manager + if (cc.AnimationManager) { + this._animationManager = new cc.AnimationManager(); + this.getScheduler().scheduleUpdate(this._animationManager, cc.Scheduler.PRIORITY_SYSTEM, false); + } + else { + this._animationManager = null; + } + + // WidgetManager + cc._widgetManager.init(this); + }, + + /** + * Reset the cc.director, can be used to restart the director after purge + */ + reset: function () { + this.purgeDirector(); + + if (cc.eventManager) + cc.eventManager.setEnabled(true); + + // Action manager + var actionManager = this.getActionManager(); + if (actionManager) { + this.getScheduler().scheduleUpdate(actionManager, cc.Scheduler.PRIORITY_SYSTEM, false); + } + + // Animation manager + if (this._animationManager) { + this.getScheduler().scheduleUpdate(this._animationManager, cc.Scheduler.PRIORITY_SYSTEM, false); + } + + this.startAnimation(); + }, + + /** + * Returns the cc.AnimationManager associated with this director. + * @method getAnimationManager + * @return {AnimationManager} + */ + getAnimationManager: function () { + return this._animationManager; + }, + + /** + * 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 + * @param {EScene} scene - The need run scene. + * @param {Function} [onBeforeLoadScene] - The function at the scene before loading. + */ + runScene: function (scene, onBeforeLoadScene) { + cc.assert(scene, cc._LogInfos.Director.pushScene); + + // detach persist nodes + var i, node, game = cc.game; + var persistNodes = game._persistRootNodes; + for (i = persistNodes.length - 1; i >= 0; --i) { + node = persistNodes[i]; + game._ignoreRemovePersistNode = node; + node.parent = null; + game._ignoreRemovePersistNode = null; + } + + // unload scene + var oldScene = this._scene; + if (cc.isValid(oldScene)) { + oldScene.destroy(); + } + this._scene = null; + + // purge destroyed nodes belongs to old scene + cc.Object._deferredDestroy(); + + if (onBeforeLoadScene) { + onBeforeLoadScene(); + } + this.emit(cc.Director.EVENT_BEFORE_SCENE_LAUNCH, scene); + + var sgScene = scene; + + // Run an Entity Scene + if (scene instanceof cc.Scene) { + // ensure scene initialized + scene._load(); + + this._scene = scene; + sgScene = scene._sgNode; + + // Re-attach persist nodes + for (i = 0; i < persistNodes.length; ++i) { + node = persistNodes[i]; + node.parent = scene; + } + } + + // Run or replace rendering scene + if (!this.getRunningScene()) { + this.runWithScene(sgScene); + } + else { + this.replaceScene(sgScene); + } + + // Activate + if (scene instanceof cc.Scene) { + scene._activate(); + } + }, + + // @Scene loading section + + /** + * Loads the scene by its name. + * @method loadScene + * @param {String} sceneName - The name of the scene to load. + * @param {Function} [onLaunched] - callback, will be called after scene launched. + * @param {Function} [onUnloaded] - callback, will be called when the previous scene was unloaded. + * @return {Boolean} if error, return false + */ + loadScene: function (sceneName, onLaunched, onUnloaded) { + var uuid, info; + if (this._loadingScene) { + cc.error('[loadScene] Failed to load scene "%s" because "%s" is already loading', sceneName, this._loadingScene); + return false; + } + if (typeof sceneName === 'string') { + if (!sceneName.endsWith('.fire')) { + sceneName += '.fire'; + } + if (sceneName[0] !== '/' && !sceneName.startsWith('db://assets/')) { + sceneName = '/' + sceneName; // 使用全名匹配 + } + // search scene + for (var i = 0; i < cc.game._sceneInfos.length; i++) { + info = cc.game._sceneInfos[i]; + var url = info.url; + if (url.endsWith(sceneName)) { + uuid = info.uuid; + break; + } + } + } + else { + info = cc.game._sceneInfos[sceneName]; + if (typeof info === 'object') { + uuid = info.uuid; + } + else { + cc.error('[loadScene] The scene index to load (%s) is out of range.', sceneName); + return false; + } + } + if (uuid) { + this._loadingScene = sceneName; + this._loadSceneByUuid(uuid, onLaunched, onUnloaded); + return true; + } + else { + cc.error('[loadScene] Can not load the scene "%s" because it has not been added to the build settings before play.', sceneName); + return false; + } + }, + + // 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 + * @param {String} uuid - the uuid of the scene asset to load + * @param {Function} [onLaunched] + * @param {Function} [onUnloaded] + * @private + */ + _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; + cc.error(error); + if (CC_EDITOR) { + console.assert(false, error); + } + } + else { + var uuid = sceneAsset._uuid; + scene = sceneAsset.scene; + if (scene instanceof cc.Scene) { + scene._id = uuid; + self.runScene(scene, onUnloaded); + } + else { + error = 'The asset ' + uuid + ' is not a scene'; + cc.error(error); + scene = null; + } + } + self._loadingScene = ''; + if (onLaunched) { + onLaunched(error, scene); + } + }, { recordAssets: true }); + } +}); + +cc.EventTarget.call(cc.director); +cc.js.addon(cc.director, cc.EventTarget.prototype); + +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.eventManager.addCustomListener(cc.Director.EVENT_BEFORE_UPDATE, function () { + var dt = 1 / 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, dt); +}); +cc.eventManager.addCustomListener(cc.Director.EVENT_AFTER_UPDATE, function () { + var dt = 1 / cc.game.config[cc.game.CONFIG_KEY.frameRate]; + // Late update for components + cc.director.emit(cc.Director.EVENT_COMPONENT_LATE_UPDATE, dt); + // 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); +}); \ No newline at end of file diff --git a/jsb/jsb-enums.js b/jsb/jsb-enums.js new file mode 100644 index 0000000000..459c4aa61a --- /dev/null +++ b/jsb/jsb-enums.js @@ -0,0 +1,308 @@ +/**************************************************************************** + Copyright (c) 2015 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +'use strict'; + +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 +}); \ No newline at end of file diff --git a/jsb/jsb-etc.js b/jsb/jsb-etc.js new file mode 100644 index 0000000000..e310791685 --- /dev/null +++ b/jsb/jsb-etc.js @@ -0,0 +1,118 @@ +/**************************************************************************** + Copyright (c) 2015 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +'use strict'; + +// cc.path +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.Scheduler +cc.Scheduler.prototype.scheduleUpdate = cc.Scheduler.prototype.scheduleUpdateForTarget; +cc.Scheduler.prototype._unschedule = cc.Scheduler.prototype.unschedule; +cc.Scheduler.prototype.unschedule = function (callback, target) { + if (typeof target === 'function') { + var tmp = target; + target = callback; + callback = tmp; + } + this._unschedule(target, callback); +}; + +// 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); +}; + +// 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; diff --git a/jsb/jsb-event.js b/jsb/jsb-event.js new file mode 100644 index 0000000000..6eec9db7b1 --- /dev/null +++ b/jsb/jsb-event.js @@ -0,0 +1,160 @@ +/**************************************************************************** + Copyright (c) 2015 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +'use strict'; + +// cc.Event +cc.Event.NO_TYPE = 'no_type'; +cc.Event.NONE = 0; +cc.Event.CAPTURING_PHASE = 1; +cc.Event.AT_TARGET = 2; +cc.Event.BUBBLING_PHASE = 3; + +cc.Event.prototype._getCurrentTarget = cc.Event.prototype.getCurrentTarget; +cc.Event.prototype.getCurrentTarget = function () { + return this.currentTarget || this._getCurrentTarget(); +}; +cc.Event.prototype._stopPropagation = cc.Event.prototype.stopPropagation; +cc.Event.prototype.stopPropagation = function () { + this._propagationStopped = true; + this._stopPropagation(); +}; +cc.Event.prototype._isStopped = cc.Event.prototype.isStopped; +cc.Event.prototype.isStopped = function () { + return this._propagationStopped || this._propagationImmediateStopped || this._isStopped(); +}; +cc.js.mixin(cc.Event.prototype, { + type: 'no_type', + target: null, + currentTarget: null, + eventPhase: 0, + bubbles: false, + _defaultPrevented: false, + _propagationStopped: false, + _propagationImmediateStopped: false, + + unuse: function () { + this.type = cc.Event.NO_TYPE; + this.target = null; + this.currentTarget = null; + this.eventPhase = cc.Event.NONE; + this._defaultPrevented = false; + this._propagationStopped = false; + this._propagationImmediateStopped = false; + }, + reuse: function (type, bubbles) { + this.type = type; + this.bubbles = bubbles || false; + }, + preventDefault: function () { + this._defaultPrevented = true; + }, + stopPropagationImmediate: function () { + this._propagationImmediateStopped = true; + }, +}); + +// cc.Event.EventCustom +cc.Event.EventCustom = function (type, bubbles) { + cc.Event.call(this, cc.Event.CUSTOM); + this.type = type; + this.bubbles = bubbles || false; + this.detail = null; +}; +cc.js.extend(cc.Event.EventCustom, cc.Event); +cc.js.mixin(cc.Event.EventCustom.prototype, { + setUserData: function (data) { + this.detail = data; + }, + getUserData: function () { + return this.detail; + }, + getEventName: cc.Event.prototype.getType +}); + +// 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; + // Augment event object to fit cc.Event + if (event) { + event.target = nodeOrPriority; + event.currentTarget = nodeOrPriority; + event.bubbles = false; + event.eventPhase = 0; + event._defaultPrevented = false; + event._propagationStopped = false; + event._propagationImmediateStopped = false; + } + return realCallback.call(this, event1, event2); + }; + })(value); + } + } + } + cc.eventManager.addEventListenerWithSceneGraphPriority(listener, node); + } + + return listener; +}; +cc.eventManager._removeListeners = cc.eventManager.removeListeners; +cc.eventManager.removeListeners = function (target, recursive) { + if (target instanceof cc.Component) { + target = target.node._sgNode; + } + if (target instanceof cc.Node) { + target = target._sgNode; + } + this._removeListeners(target, recursive); +}; \ No newline at end of file diff --git a/jsb/jsb-game.js b/jsb/jsb-game.js new file mode 100644 index 0000000000..628ecf2933 --- /dev/null +++ b/jsb/jsb-game.js @@ -0,0 +1,240 @@ +/**************************************************************************** + Copyright (c) 2015 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +'use strict'; + +// cc.game +cc.js.mixin(cc.game, { + + // states + _paused: false, //whether the game is paused + + // Scenes list + _sceneInfos: [], + + _persistRootNodes: [], + _ignoreRemovePersistNode: null, + + CONFIG_KEY: { + width: 'width', + height: 'height', + engineDir: 'engineDir', + modules: 'modules', + debugMode: 'debugMode', + showFPS: 'showFPS', + frameRate: 'frameRate', + id: 'id', + renderMode: 'renderMode', + registerSystemEvent: 'registerSystemEvent', + jsList: 'jsList', + scenes: 'scenes' + }, + + pause: function () { + this._paused = true; + cc.director.pause(); + }, + + /** + * Resume the game from pause. + */ + resume: function () { + this._paused = false; + cc.director.resume(); + }, + + /** + * Check whether the game is paused. + */ + isPaused: function () { + return this._paused; + }, + + prepare: function (cb) { + var self = this, + config = self.config, + CONFIG_KEY = self.CONFIG_KEY; + + this._loadConfig(); + + // Already prepared + if (this._prepared) { + if (cb) cb(); + return; + } + // Prepare called, but not done yet + if (this._prepareCalled) { + return; + } + // 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) { + cc.loader.loadJs(jsList, function (err) { + if (err) throw new Error(err); + self._prepared = true; + if (cb) cb(); + }); + } + else { + if (cb) cb(); + } + + return; + } + + // Engine not loaded yet + cc.initEngine(this.config, function () { + self.prepare(cb); + }); + }, + + /** + * Run game with configuration object and onStart function. + * @param {Object|Function} [config] Pass configuration object or onStart function + * @param {onStart} [onStart] onStart function to be executed after game initialized + */ + run: function (config, onStart) { + if (typeof config === 'function') { + cc.game.onStart = config; + } + else { + if (config) { + cc.game.config = config; + } + if (typeof onStart === 'function') { + cc.game.onStart = onStart; + } + } + + this.prepare(cc.game.onStart && cc.game.onStart.bind(cc.game)); + }, + +// @ Persist root node section + /** + * Add a persistent root node to the game, the persistent node won't be destroyed during scene transition + * @method addPersistRootNode + * @param {ENode} node - The node to be made persistent + */ + addPersistRootNode: function (node) { + if (!(node instanceof cc.Node)) + return; + var index = this._persistRootNodes.indexOf(node); + if (index === -1) { + var scene = cc.director._scene; + if (cc.isValid(scene)) { + if (!node.parent) { + node.parent = scene; + } + else if ( !(node.parent instanceof cc.Scene) ) { + cc.warn('The node can not be made persist because it\'s not under root node.'); + return; + } + else if (node.parent !== scene) { + cc.warn('The node can not be made persist because it\'s not in current scene.'); + return; + } + this._persistRootNodes.push(node); + node._persistNode = true; + } + } + }, + + /** + * Remove a persistent root node + * @method removePersistRootNode + * @param {ENode} node - The node to be removed from persistent node list + */ + removePersistRootNode: function (node) { + if (node !== this._ignoreRemovePersistNode) { + var index = this._persistRootNodes.indexOf(node); + if (index !== -1) { + this._persistRootNodes.splice(index, 1); + } + node._persistNode = false; + } + }, + + /** + * Check whether the node is a persistent root node + * @method isPersistRootNode + * @param {ENode} node - The node to be checked + * @return {Boolean} + */ + isPersistRootNode: function (node) { + return node._persistNode; + }, + +//@Private Methods + + _loadConfig: function () { + // Load config + // Already loaded + if (this.config) { + this._initConfig(this.config); + } + // Load from project.json + else { + try { + var txt = jsb.fileUtils.getStringFromFile('project.json'); + var data = JSON.parse(txt); + this._initConfig(data || {}); + } catch (e) { + console.log('Failed to read or parse project.json'); + this._initConfig({}); + } + } + }, + + _initConfig: function (config) { + var CONFIG_KEY = this.CONFIG_KEY; + + // Configs adjustment + config[CONFIG_KEY.showFPS] = config[CONFIG_KEY.showFPS] || true; + config[CONFIG_KEY.engineDir] = config[CONFIG_KEY.engineDir] || 'frameworks/cocos2d-html5'; + if (config[CONFIG_KEY.debugMode] == null) + config[CONFIG_KEY.debugMode] = 0; + config[CONFIG_KEY.frameRate] = config[CONFIG_KEY.frameRate] || 60; + if (config[CONFIG_KEY.renderMode] == null) + config[CONFIG_KEY.renderMode] = 0; + + // Scene parser + this._sceneInfos = this._sceneInfos.concat(config[CONFIG_KEY.scenes]); + + cc.director.setDisplayStats(this.config[CONFIG_KEY.showFPS]); + cc.director.setAnimationInterval(1.0/this.config[CONFIG_KEY.frameRate]); + + this.config = config; + } +}); + + +cc.EventTarget.call(cc.game); +cc.js.addon(cc.game, cc.EventTarget.prototype); \ No newline at end of file diff --git a/jsb/jsb-label.js b/jsb/jsb-label.js new file mode 100644 index 0000000000..faa4d9a453 --- /dev/null +++ b/jsb/jsb-label.js @@ -0,0 +1,126 @@ +/**************************************************************************** + Copyright (c) 2015 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +'use strict'; + +// cc.Label +var jsbLabel = cc.Label; +jsbLabel.prototype.setHorizontalAlign = jsbLabel.prototype.setHorizontalAlignment; +jsbLabel.prototype.setVerticalAlign = jsbLabel.prototype.setVerticalAlignment; +//for back compatibility +if (!jsbLabel.prototype.setBMFontSize) { + jsbLabel.prototype.setBMFontSize = function(){}; +} +if (!jsbLabel.prototype.getBMFontSize) { + jsbLabel.prototype.getBMFontSize = function(){}; +} +if (!jsbLabel.prototype.setOverflow) { + jsbLabel.prototype.setOverflow = function(){}; +} +if (!jsbLabel.prototype.getOverflow) { + jsbLabel.prototype.getOverflow = function(){}; +} + +jsbLabel.prototype.setFontSize = function (size) { + if (this._labelType === _ccsg.Label.Type.SystemFont) { + this.setSystemFontSize(size); + } + else if (this._labelType === _ccsg.Label.Type.BMFont) { + this.setBMFontSize(size); + } + else if (this._labelType === _ccsg.Label.Type.TTF) { + var ttfConfig = this.getTTFConfig(); + ttfConfig.fontSize = size; + this.setTTFConfig(ttfConfig); + } +}; + +jsbLabel.prototype.enableWrapText = jsbLabel.prototype.enableWrap || function(){}; +jsbLabel.prototype.isWrapTextEnabled = jsbLabel.prototype.isWrapEnabled || function(){}; + +jsbLabel.prototype._setLineHeight = jsbLabel.prototype.setLineHeight; +jsbLabel.prototype.setLineHeight = function (height) { + if (this._labelType !== _ccsg.Label.Type.SystemFont) { + this._setLineHeight(height); + } +}; + +jsbLabel.prototype._setTTFConfig = jsbLabel.prototype.setTTFConfig; +jsbLabel.prototype.setTTFConfig = function (config) { + this._setTTFConfig(config); + this._ttfConfig = config; +}; +jsbLabel.prototype.getTTFConfig = function () { + return this._ttfConfig; +}; + +jsbLabel.prototype.setContentSize = function(size, height) { + if(height !== undefined){ + this.setDimensions(size, height); + } + else{ + this.setDimensions(size.width, size.height); + } +}; +cc.Label = function (string, fontHandle) { + fontHandle = fontHandle || "Arial"; + var extName = cc.path.extname(fontHandle); + + var type = _ccsg.Label.Type.TTF; + + var label; + if (extName === ".ttf") { + var ttfConfig = { + fontFilePath: fontHandle, + fontSize: 40, + outlineSize: 0, + glyphs: 0, + customGlyphs: "", + distanceFieldEnable: false + }; + label = jsbLabel.createWithTTF(ttfConfig, string, 40); + label._ttfConfig = ttfConfig; + } + else if (extName === ".fnt") { + label = jsbLabel.createWithBMFont(fontHandle, string); + type = _ccsg.Label.Type.BMFont; + } + else { + label = jsbLabel.createWithSystemFont(string, fontHandle, 40); + type = _ccsg.Label.Type.SystemFont; + } + label._labelType = type; + return label; +}; +cc.Label.Type = cc.Enum({ + TTF: 0, + BMFont: 1 +}); +cc.Label.Overflow = cc.Enum({ + //TODO: uncomment if normal is supported in web + // NONE: 0, + CLAMP: 1, + SHRINK: 2, + RESIZE_HEIGHT: 3 +}); \ No newline at end of file diff --git a/jsb/jsb-particle.js b/jsb/jsb-particle.js new file mode 100644 index 0000000000..8f4f4d4bf3 --- /dev/null +++ b/jsb/jsb-particle.js @@ -0,0 +1,94 @@ +/**************************************************************************** + Copyright (c) 2015 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +'use strict'; + +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 +}); + +var funcNames = [ + { + 'tangentialAccel' : 'setTangentialAccel', + 'tangentialAccelVar' : 'setTangentialAccelVar', + 'radialAccel' : 'setRadialAccel', + 'radialAccelVar' : 'setRadialAccelVar', + 'rotationIsDir' : 'setRotationIsDir', + 'gravity' : 'setGravity', + 'speed' : 'setSpeed', + 'speedVar' : 'setSpeedVar' + }, + { + 'startRadius' : 'setStartRadius', + 'startRadiusVar' : 'setStartRadiusVar', + 'endRadius' : 'setEndRadius', + 'endRadiusVar' : 'setEndRadiusVar', + 'rotatePerS' : 'setRotatePerSecond', + 'rotatePerSVar' : 'setRotatePerSecondVar' + } +]; + +function getReplacer (oldFunc, mode) { + return function (value) { + if (this.getEmitterMode() === mode) { + oldFunc.call(this, value); + } + }; +} + +var proto = cc.ParticleSystem.prototype; + +for (var mode = 0; mode < funcNames.length; mode++) { + var modeFuncs = funcNames[mode]; + for (var propName in modeFuncs) { + var funcName = modeFuncs[propName]; + var func = proto[funcName]; + proto[funcName] = getReplacer(func, mode); + + var getter = funcName.replace('set', 'get'); + cc.defineGetterSetter(proto, propName, proto[getter], proto[funcName]); + } +} diff --git a/jsb_predefine.js b/jsb/jsb-predefine.js similarity index 90% rename from jsb_predefine.js rename to jsb/jsb-predefine.js index 9326545608..4ee476b5f5 100644 --- a/jsb_predefine.js +++ b/jsb/jsb-predefine.js @@ -22,9 +22,6 @@ THE SOFTWARE. ****************************************************************************/ - -// define cc - // Run jsb.js before all polyfills cc.initEngine({ debugMode: cc.game.DEBUG_MODE_INFO @@ -53,8 +50,8 @@ if (CC_DEV) { // predefine some modules for cocos -require('./cocos2d/core/platform/js'); -require('./cocos2d/core/value-types'); -require('./cocos2d/core/utils/find'); -require('./cocos2d/core/event'); -require('./CCDebugger'); +require('../cocos2d/core/platform/js'); +require('../cocos2d/core/value-types'); +require('../cocos2d/core/utils/find'); +require('../cocos2d/core/event'); +require('../CCDebugger'); diff --git a/jsb/jsb-tex-sprite-frame.js b/jsb/jsb-tex-sprite-frame.js new file mode 100644 index 0000000000..a0b67e4e43 --- /dev/null +++ b/jsb/jsb-tex-sprite-frame.js @@ -0,0 +1,186 @@ +/**************************************************************************** + Copyright (c) 2015 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +'use strict'; + +cc.spriteFrameAnimationCache = cc.animationCache; +cc.SpriteFrameAnimation = cc.Animation; + +// 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); + } + } +}); + +// Assets +cc.js.setClassName('cc.SpriteFrame', cc.SpriteFrame); diff --git a/jsb/versions/jsb-polyfill-v3.8.js b/jsb/versions/jsb-polyfill-v3.8.js new file mode 100644 index 0000000000..0545adbd09 --- /dev/null +++ b/jsb/versions/jsb-polyfill-v3.8.js @@ -0,0 +1,36 @@ +/**************************************************************************** + Copyright (c) 2015 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +'use strict'; + +// Overwrite main loop +var scheduleTarget = { + update: function (dt) { + // 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, dt); + } +}; +cc.Director.getInstance().getScheduler().scheduleUpdateForTarget(scheduleTarget, -1000, false); \ No newline at end of file diff --git a/jsb_engine.js b/jsb_engine.js deleted file mode 100644 index b931dc991d..0000000000 --- a/jsb_engine.js +++ /dev/null @@ -1,1338 +0,0 @@ -'use strict'; - -var _engineNumberVersion = (function () { - var result = /Cocos2d\-JS\sv([\.\d]+)/.exec(cc.ENGINE_VERSION); - if (result && result[1]) { - return parseFloat(result[1]); - } - else { - return null; - } -})(); - -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.game -cc.js.mixin(cc.game, { - - // states - _paused: false, //whether the game is paused - - // Scenes list - _sceneInfos: [], - - _persistRootNodes: [], - _ignoreRemovePersistNode: null, - - CONFIG_KEY: { - width: 'width', - height: 'height', - engineDir: 'engineDir', - modules: 'modules', - debugMode: 'debugMode', - showFPS: 'showFPS', - frameRate: 'frameRate', - id: 'id', - renderMode: 'renderMode', - registerSystemEvent: 'registerSystemEvent', - jsList: 'jsList', - scenes: 'scenes' - }, - - pause: function () { - this._paused = true; - cc.director.pause(); - }, - - /** - * Resume the game from pause. - */ - resume: function () { - this._paused = false; - cc.director.resume(); - }, - - /** - * Check whether the game is paused. - */ - isPaused: function () { - return this._paused; - }, - - prepare: function (cb) { - var self = this, - config = self.config, - CONFIG_KEY = self.CONFIG_KEY; - - this._loadConfig(); - - // Already prepared - if (this._prepared) { - if (cb) cb(); - return; - } - // Prepare called, but not done yet - if (this._prepareCalled) { - return; - } - // 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) { - cc.loader.loadJs(jsList, function (err) { - if (err) throw new Error(err); - self._prepared = true; - if (cb) cb(); - }); - } - else { - if (cb) cb(); - } - - return; - } - - // Engine not loaded yet - cc.initEngine(this.config, function () { - self.prepare(cb); - }); - }, - - /** - * Run game with configuration object and onStart function. - * @param {Object|Function} [config] Pass configuration object or onStart function - * @param {onStart} [onStart] onStart function to be executed after game initialized - */ - run: function (config, onStart) { - if (typeof config === 'function') { - cc.game.onStart = config; - } - else { - if (config) { - cc.game.config = config; - } - if (typeof onStart === 'function') { - cc.game.onStart = onStart; - } - } - - this.prepare(cc.game.onStart && cc.game.onStart.bind(cc.game)); - }, - -// @ Persist root node section - /** - * Add a persistent root node to the game, the persistent node won't be destroyed during scene transition - * @method addPersistRootNode - * @param {ENode} node - The node to be made persistent - */ - addPersistRootNode: function (node) { - if (!(node instanceof cc.Node)) - return; - var index = this._persistRootNodes.indexOf(node); - if (index === -1) { - var scene = cc.director._scene; - if (cc.isValid(scene)) { - if (!node.parent) { - node.parent = scene; - } - else if ( !(node.parent instanceof cc.Scene) ) { - cc.warn('The node can not be made persist because it\'s not under root node.'); - return; - } - else if (node.parent !== scene) { - cc.warn('The node can not be made persist because it\'s not in current scene.'); - return; - } - this._persistRootNodes.push(node); - node._persistNode = true; - } - } - }, - - /** - * Remove a persistent root node - * @method removePersistRootNode - * @param {ENode} node - The node to be removed from persistent node list - */ - removePersistRootNode: function (node) { - if (node !== this._ignoreRemovePersistNode) { - var index = this._persistRootNodes.indexOf(node); - if (index !== -1) { - this._persistRootNodes.splice(index, 1); - } - node._persistNode = false; - } - }, - - /** - * Check whether the node is a persistent root node - * @method isPersistRootNode - * @param {ENode} node - The node to be checked - * @return {Boolean} - */ - isPersistRootNode: function (node) { - return node._persistNode; - }, - -//@Private Methods - - _loadConfig: function () { - // Load config - // Already loaded - if (this.config) { - this._initConfig(this.config); - } - // Load from project.json - else { - try { - var txt = jsb.fileUtils.getStringFromFile('project.json'); - var data = JSON.parse(txt); - this._initConfig(data || {}); - } catch (e) { - console.log('Failed to read or parse project.json'); - this._initConfig({}); - } - } - }, - - _initConfig: function (config) { - var CONFIG_KEY = this.CONFIG_KEY; - - // Configs adjustment - config[CONFIG_KEY.showFPS] = config[CONFIG_KEY.showFPS] || true; - config[CONFIG_KEY.engineDir] = config[CONFIG_KEY.engineDir] || 'frameworks/cocos2d-html5'; - if (config[CONFIG_KEY.debugMode] == null) - config[CONFIG_KEY.debugMode] = 0; - config[CONFIG_KEY.frameRate] = config[CONFIG_KEY.frameRate] || 60; - if (config[CONFIG_KEY.renderMode] == null) - config[CONFIG_KEY.renderMode] = 0; - - // Scene parser - this._sceneInfos = this._sceneInfos.concat(config[CONFIG_KEY.scenes]); - - cc.director.setDisplayStats(this.config[CONFIG_KEY.showFPS]); - cc.director.setAnimationInterval(1.0/this.config[CONFIG_KEY.frameRate]); - - this.config = config; - } -}); - -// 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. - */ - sharedInit: function () { - // Animation manager - if (cc.AnimationManager) { - this._animationManager = new cc.AnimationManager(); - this.getScheduler().scheduleUpdate(this._animationManager, cc.Scheduler.PRIORITY_SYSTEM, false); - } - else { - this._animationManager = null; - } - - // WidgetManager - cc._widgetManager.init(this); - }, - - /** - * Reset the cc.director, can be used to restart the director after purge - */ - reset: function () { - this.purgeDirector(); - - if (cc.eventManager) - cc.eventManager.setEnabled(true); - - // Action manager - var actionManager = this.getActionManager(); - if (actionManager) { - this.getScheduler().scheduleUpdate(actionManager, cc.Scheduler.PRIORITY_SYSTEM, false); - } - - // Animation manager - if (this._animationManager) { - this.getScheduler().scheduleUpdate(this._animationManager, cc.Scheduler.PRIORITY_SYSTEM, false); - } - - this.startAnimation(); - }, - - /** - * Returns the cc.AnimationManager associated with this director. - * @method getAnimationManager - * @return {AnimationManager} - */ - getAnimationManager: function () { - return this._animationManager; - }, - - /** - * 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 - * @param {EScene} scene - The need run scene. - * @param {Function} [onBeforeLoadScene] - The function at the scene before loading. - */ - runScene: function (scene, onBeforeLoadScene) { - cc.assert(scene, cc._LogInfos.Director.pushScene); - - // detach persist nodes - var i, node, game = cc.game; - var persistNodes = game._persistRootNodes; - for (i = persistNodes.length - 1; i >= 0; --i) { - node = persistNodes[i]; - game._ignoreRemovePersistNode = node; - node.parent = null; - game._ignoreRemovePersistNode = null; - } - - // unload scene - var oldScene = this._scene; - if (cc.isValid(oldScene)) { - oldScene.destroy(); - } - this._scene = null; - - // purge destroyed nodes belongs to old scene - cc.Object._deferredDestroy(); - - if (onBeforeLoadScene) { - onBeforeLoadScene(); - } - this.emit(cc.Director.EVENT_BEFORE_SCENE_LAUNCH, scene); - - var sgScene = scene; - - // Run an Entity Scene - if (scene instanceof cc.Scene) { - // ensure scene initialized - scene._load(); - - this._scene = scene; - sgScene = scene._sgNode; - - // Re-attach persist nodes - for (i = 0; i < persistNodes.length; ++i) { - node = persistNodes[i]; - node.parent = scene; - } - } - - // Run or replace rendering scene - if (!this.getRunningScene()) { - this.runWithScene(sgScene); - } - else { - this.replaceScene(sgScene); - } - - // Activate - if (scene instanceof cc.Scene) { - scene._activate(); - } - }, - - // @Scene loading section - - /** - * Loads the scene by its name. - * @method loadScene - * @param {String} sceneName - The name of the scene to load. - * @param {Function} [onLaunched] - callback, will be called after scene launched. - * @param {Function} [onUnloaded] - callback, will be called when the previous scene was unloaded. - * @return {Boolean} if error, return false - */ - loadScene: function (sceneName, onLaunched, onUnloaded) { - var uuid, info; - if (this._loadingScene) { - cc.error('[loadScene] Failed to load scene "%s" because "%s" is already loading', sceneName, this._loadingScene); - return false; - } - if (typeof sceneName === 'string') { - if (!sceneName.endsWith('.fire')) { - sceneName += '.fire'; - } - if (sceneName[0] !== '/' && !sceneName.startsWith('db://assets/')) { - sceneName = '/' + sceneName; // 使用全名匹配 - } - // search scene - for (var i = 0; i < cc.game._sceneInfos.length; i++) { - info = cc.game._sceneInfos[i]; - var url = info.url; - if (url.endsWith(sceneName)) { - uuid = info.uuid; - break; - } - } - } - else { - info = cc.game._sceneInfos[sceneName]; - if (typeof info === 'object') { - uuid = info.uuid; - } - else { - cc.error('[loadScene] The scene index to load (%s) is out of range.', sceneName); - return false; - } - } - if (uuid) { - this._loadingScene = sceneName; - this._loadSceneByUuid(uuid, onLaunched, onUnloaded); - return true; - } - else { - cc.error('[loadScene] Can not load the scene "%s" because it has not been added to the build settings before play.', sceneName); - return false; - } - }, - - // 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 - * @param {String} uuid - the uuid of the scene asset to load - * @param {Function} [onLaunched] - * @param {Function} [onUnloaded] - * @private - */ - _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; - cc.error(error); - if (CC_EDITOR) { - console.assert(false, error); - } - } - else { - var uuid = sceneAsset._uuid; - scene = sceneAsset.scene; - if (scene instanceof cc.Scene) { - scene._id = uuid; - self.runScene(scene, onUnloaded); - } - else { - error = 'The asset ' + uuid + ' is not a scene'; - cc.error(error); - scene = null; - } - } - self._loadingScene = ''; - if (onLaunched) { - onLaunched(error, scene); - } - }, { recordAssets: true }); - } -}); - -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'; - -// Overwrite main loop -if (_engineNumberVersion && _engineNumberVersion >= 3.9) { - var scheduleTarget = { - update: function (dt) { - // 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, dt); - } - }; - cc.Director.getInstance().getScheduler().scheduleUpdateForTarget(scheduleTarget, -1000, false); -} -else { - cc.eventManager.addCustomListener(cc.Director.EVENT_BEFORE_UPDATE, function () { - var dt = 1 / 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, dt); - }); -} -cc.eventManager.addCustomListener(cc.Director.EVENT_AFTER_UPDATE, function () { - var dt = 1 / cc.game.config[cc.game.CONFIG_KEY.frameRate]; - // Late update for components - cc.director.emit(cc.Director.EVENT_COMPONENT_LATE_UPDATE, dt); - // 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 -var jsbLabel = cc.Label; -jsbLabel.prototype.setHorizontalAlign = jsbLabel.prototype.setHorizontalAlignment; -jsbLabel.prototype.setVerticalAlign = jsbLabel.prototype.setVerticalAlignment; -//for back compatibility -if (!jsbLabel.prototype.setBMFontSize) { - jsbLabel.prototype.setBMFontSize = function(){}; -} -if (!jsbLabel.prototype.getBMFontSize) { - jsbLabel.prototype.getBMFontSize = function(){}; -} -if (!jsbLabel.prototype.setOverflow) { - jsbLabel.prototype.setOverflow = function(){}; -} -if (!jsbLabel.prototype.getOverflow) { - jsbLabel.prototype.getOverflow = function(){}; -} - -jsbLabel.prototype.setFontSize = function (size) { - if (this._labelType === _ccsg.Label.Type.SystemFont) { - this.setSystemFontSize(size); - } - else if (this._labelType === _ccsg.Label.Type.BMFont) { - this.setBMFontSize(size); - } - else if (this._labelType === _ccsg.Label.Type.TTF) { - var ttfConfig = this.getTTFConfig(); - ttfConfig.fontSize = size; - this.setTTFConfig(ttfConfig); - } -}; - -jsbLabel.prototype.enableWrapText = jsbLabel.prototype.enableWrap || function(){}; -jsbLabel.prototype.isWrapTextEnabled = jsbLabel.prototype.isWrapEnabled || function(){}; - -jsbLabel.prototype._setLineHeight = jsbLabel.prototype.setLineHeight; -jsbLabel.prototype.setLineHeight = function (height) { - if (this._labelType !== _ccsg.Label.Type.SystemFont) { - this._setLineHeight(height); - } -}; - -jsbLabel.prototype._setTTFConfig = jsbLabel.prototype.setTTFConfig; -jsbLabel.prototype.setTTFConfig = function (config) { - this._setTTFConfig(config); - this._ttfConfig = config; -}; -jsbLabel.prototype.getTTFConfig = function () { - return this._ttfConfig; -}; - -jsbLabel.prototype.setContentSize = function(size, height) { - if(height !== undefined){ - this.setDimensions(size, height); - } - else{ - this.setDimensions(size.width, size.height); - } -}; -cc.Label = function (string, fontHandle) { - fontHandle = fontHandle || "Arial"; - var extName = cc.path.extname(fontHandle); - - var type = _ccsg.Label.Type.TTF; - - var label; - if (extName === ".ttf") { - var ttfConfig = { - fontFilePath: fontHandle, - fontSize: 40, - outlineSize: 0, - glyphs: 0, - customGlyphs: "", - distanceFieldEnable: false - }; - label = jsbLabel.createWithTTF(ttfConfig, string, 40); - label._ttfConfig = ttfConfig; - } - else if (extName === ".fnt") { - label = jsbLabel.createWithBMFont(fontHandle, string); - type = _ccsg.Label.Type.BMFont; - } - else { - label = jsbLabel.createWithSystemFont(string, fontHandle, 40); - type = _ccsg.Label.Type.SystemFont; - } - label._labelType = type; - return label; -}; -cc.Label.Type = cc.Enum({ - TTF: 0, - BMFont: 1 -}); -cc.Label.Overflow = cc.Enum({ - //TODO: uncomment if normal is supported in web - // NONE: 0, - CLAMP: 1, - SHRINK: 2, - RESIZE_HEIGHT: 3 -}); - -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 -cc.Event.NO_TYPE = 'no_type'; -cc.Event.NONE = 0; -cc.Event.CAPTURING_PHASE = 1; -cc.Event.AT_TARGET = 2; -cc.Event.BUBBLING_PHASE = 3; - -cc.Event.prototype._getCurrentTarget = cc.Event.prototype.getCurrentTarget; -cc.Event.prototype.getCurrentTarget = function () { - return this.currentTarget || this._getCurrentTarget(); -}; -cc.Event.prototype._stopPropagation = cc.Event.prototype.stopPropagation; -cc.Event.prototype.stopPropagation = function () { - this._propagationStopped = true; - this._stopPropagation(); -}; -cc.Event.prototype._isStopped = cc.Event.prototype.isStopped; -cc.Event.prototype.isStopped = function () { - return this._propagationStopped || this._propagationImmediateStopped || this._isStopped(); -}; -cc.js.mixin(cc.Event.prototype, { - type: 'no_type', - target: null, - currentTarget: null, - eventPhase: 0, - bubbles: false, - _defaultPrevented: false, - _propagationStopped: false, - _propagationImmediateStopped: false, - - unuse: function () { - this.type = cc.Event.NO_TYPE; - this.target = null; - this.currentTarget = null; - this.eventPhase = cc.Event.NONE; - this._defaultPrevented = false; - this._propagationStopped = false; - this._propagationImmediateStopped = false; - }, - reuse: function (type, bubbles) { - this.type = type; - this.bubbles = bubbles || false; - }, - preventDefault: function () { - this._defaultPrevented = true; - }, - stopPropagationImmediate: function () { - this._propagationImmediateStopped = true; - }, -}); - -// cc.Event.EventCustom -cc.Event.EventCustom = function (type, bubbles) { - cc.Event.call(this, cc.Event.CUSTOM); - this.type = type; - this.bubbles = bubbles || false; - this.detail = null; -}; -cc.js.extend(cc.Event.EventCustom, cc.Event); -cc.js.mixin(cc.Event.EventCustom.prototype, { - setUserData: function (data) { - this.detail = data; - }, - getUserData: function () { - return this.detail; - }, - getEventName: cc.Event.prototype.getType -}); - -// 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; - // Augment event object to fit cc.Event - if (event) { - event.target = nodeOrPriority; - event.currentTarget = nodeOrPriority; - event.bubbles = false; - event.eventPhase = 0; - event._defaultPrevented = false; - event._propagationStopped = false; - event._propagationImmediateStopped = false; - } - return realCallback.call(this, event1, event2); - }; - })(value); - } - } - } - cc.eventManager.addEventListenerWithSceneGraphPriority(listener, node); - } - - return listener; -}; -cc.eventManager._removeListeners = cc.eventManager.removeListeners; -cc.eventManager.removeListeners = function (target, recursive) { - if (target instanceof cc.Component) { - target = target.node._sgNode; - } - if (target instanceof cc.Node) { - target = target._sgNode; - } - this._removeListeners(target, recursive); -}; - -// cc.Scheduler -cc.Scheduler.prototype.scheduleUpdate = cc.Scheduler.prototype.scheduleUpdateForTarget; -cc.Scheduler.prototype._unschedule = cc.Scheduler.prototype.unschedule; -cc.Scheduler.prototype.unschedule = function (callback, target) { - if (typeof target === 'function') { - var tmp = target; - target = callback; - callback = tmp; - } - this._unschedule(target, callback); -}; - -// 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); -};