diff --git a/cocos2d/core/CCDirector.js b/cocos2d/core/CCDirector.js index c86b6502a6..c92f763bad 100644 --- a/cocos2d/core/CCDirector.js +++ b/cocos2d/core/CCDirector.js @@ -149,6 +149,16 @@ cc.Director = Class.extend(/** @lends cc.Director# */{ this._actionManager = null; } + this.sharedInit(); + + return true; + }, + + /** + * 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(); @@ -160,8 +170,6 @@ cc.Director = Class.extend(/** @lends cc.Director# */{ // WidgetManager cc._widgetManager.init(this); - - return true; }, /** @@ -397,6 +405,8 @@ cc.Director = Class.extend(/** @lends cc.Director# */{ this._runningScene.onExitTransitionDidStart(); this._runningScene.onExit(); this._runningScene.cleanup(); + + cc.renderer.clearRenderCommands(); } this._runningScene = null; @@ -414,6 +424,28 @@ cc.Director = Class.extend(/** @lends cc.Director# */{ cc.checkGLErrorDebug(); }, + /** + * 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 + if(this._actionManager){ + this._scheduler.scheduleUpdate(this._actionManager, cc.Scheduler.PRIORITY_SYSTEM, false); + } + + // Animation manager + if (this._animationManager) { + this._scheduler.scheduleUpdate(this._animationManager, cc.Scheduler.PRIORITY_SYSTEM, false); + } + + this.startAnimation(); + }, + /** * Suspends the execution of the running scene, pushing it on the stack of suspended scenes.
* The new scene will be executed.
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index 3f706b3ee1..9e28c63811 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -22,6 +22,8 @@ THE SOFTWARE. ****************************************************************************/ +'use strict'; + var JS = cc.js; var Flags = cc.Object.Flags; var Destroying = Flags.Destroying; @@ -248,11 +250,11 @@ var Node = cc.Class({ var err, existing = this.getComponent(ctor._disallowMultiple); if (existing) { if (existing.constructor === ctor) { - err = "Can't add component '%s' because %s already contains the same component."; + err = 'Can\'t add component \'%s\' because %s already contains the same component.'; cc.error(err, JS.getClassName(ctor), this._name); } else { - err = "Can't add component '%s' to %s because it conflicts with the existing '%s' derived component."; + err = 'Can\'t add component \'%s\' to %s because it conflicts with the existing \'%s\' derived component.'; cc.error(err, JS.getClassName(ctor), this._name, JS.getClassName(existing)); } return false; @@ -298,11 +300,11 @@ var Node = cc.Class({ // check component if (typeof constructor !== 'function') { - cc.error("addComponent: The component to add must be a constructor"); + cc.error('addComponent: The component to add must be a constructor'); return null; } if (!cc.isChildClassOf(constructor, cc.Component)) { - cc.error("addComponent: The component to add must be child class of cc.Component"); + cc.error('addComponent: The component to add must be child class of cc.Component'); return null; } @@ -349,10 +351,10 @@ var Node = cc.Class({ return cc.error('isDestroying'); } if ( !(comp instanceof cc.Component) ) { - return cc.error("_addComponentAt: The component to add must be a constructor"); + return cc.error('_addComponentAt: The component to add must be a constructor'); } if (index > this._components.length) { - return cc.error("_addComponentAt: Index out of range"); + return cc.error('_addComponentAt: Index out of range'); } // recheck attributes because script may changed @@ -432,7 +434,7 @@ var Node = cc.Class({ component.node = null; } else if (component.node !== this) { - cc.error("Component not owned by this entity"); + cc.error('Component not owned by this entity'); } } }, @@ -467,7 +469,12 @@ var Node = cc.Class({ if (! (component instanceof cc.Component) && CC_EDITOR) { cc.error('Sorry, the component of entity "%s" which with an index of %s is corrupted! It has been removed.\nSee DevTools for details.', this.name, c); console.log('Corrupted component value:', component); - this._removeComponent(component); + if (component) { + this._removeComponent(component); + } + else { + this._components.splice(c, 1); + } --c; --originCount; } diff --git a/cocos2d/core/components/CCScrollBar.js b/cocos2d/core/components/CCScrollBar.js index 6cd6947c53..e2d7e6e6a2 100644 --- a/cocos2d/core/components/CCScrollBar.js +++ b/cocos2d/core/components/CCScrollBar.js @@ -57,7 +57,10 @@ var Scrollbar = cc.Class({ properties: { _scrollView: null, _touching: false, - _autoHideRemainingTime: 0, + _autoHideRemainingTime: { + default: 0, + serializable: false + }, _opacity: 255, /** @@ -124,16 +127,21 @@ var Scrollbar = cc.Class({ _onScroll: function(outOfBoundary) { if (this._scrollView) { - if (this.enableAutoHide) { - this._autoHideRemainingTime = this.autoHideTime; - this._setOpacity(this._opacity); - } var content = this._scrollView.content; if(content){ var contentSize = content.getContentSize(); var scrollViewSize = this._scrollView.node.getContentSize(); + if(this._conditionalDisableScrollBar(contentSize, scrollViewSize)) { + return; + } + + if (this.enableAutoHide) { + this._autoHideRemainingTime = this.autoHideTime; + this._setOpacity(this._opacity); + } + var contentMeasure = 0; var scrollViewMeasure = 0; var outOfBoundaryValue = 0; @@ -195,7 +203,17 @@ var Scrollbar = cc.Class({ return; } this._touching = true; + }, + _conditionalDisableScrollBar: function (contentSize, scrollViewSize) { + if(contentSize.width <= scrollViewSize.width && this.direction === Direction.HORIZONTAL){ + return true; + } + + if(contentSize.height <= scrollViewSize.height && this.direction === Direction.VERTICAL){ + return true; + } + return false; }, _onTouchEnded: function() { @@ -209,6 +227,19 @@ var Scrollbar = cc.Class({ return; } + + if (this._scrollView) { + var content = this._scrollView.content; + if(content){ + var contentSize = content.getContentSize(); + var scrollViewSize = this._scrollView.node.getContentSize(); + + if(this._conditionalDisableScrollBar(contentSize, scrollViewSize)) { + return; + } + } + } + this._autoHideRemainingTime = this.autoHideTime; }, diff --git a/cocos2d/core/components/CCScrollView.js b/cocos2d/core/components/CCScrollView.js index 0ace6d005b..220533564f 100644 --- a/cocos2d/core/components/CCScrollView.js +++ b/cocos2d/core/components/CCScrollView.js @@ -22,11 +22,10 @@ THE SOFTWARE. ****************************************************************************/ -var EventTarget = require("../event/event-target"); +var EventTarget = require('../event/event-target'); var NUMBER_OF_GATHERED_TOUCHES_FOR_MOVE_SPEED = 5; var OUT_OF_BOUNDARY_BREAKING_FACTOR = 0.05; -var BOUNCE_BACK_DURATION = 1.0; var EPSILON = 1e-7; var MOVEMENT_FACTOR = 0.7; @@ -41,7 +40,8 @@ var getTimeInMilliseconds = function() { }; /** - * Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display. + * Layout container for a view hierarchy that can be scrolled by the user, + * allowing it to be larger than the physical display. * * @class ScrollView * @extends Component @@ -179,6 +179,245 @@ var ScrollView = cc.Class({ } }, + /** + * Scroll the content to the bottom boundary of ScrollView. + * @method scrollToBottom + * @param {Number} [timeInSecond=0] - Scroll time in second, if you don't pass timeInSecond, + * the content will jump to the bottom boundary immediately. + * @param {Boolean} [attenuated=true] - Whether the scroll acceleration attenuated, default is true. + */ + scrollToBottom: function(timeInSecond, attenuated) { + var moveDelta = this._calculateMovePercentDelta({ + anchor: cc.p(0, 0), + applyToHorizontal: false, + applyToVertical: true, + }); + + if(timeInSecond) { + this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false); + }else { + this._moveContent(moveDelta, true); + } + }, + + /** + * Scroll the content to the top boundary of ScrollView. + * @method scrollToTop + * @param {Number} [timeInSecond=0] - Scroll time in second, if you don't pass timeInSecond, + * the content will jump to the top boundary immediately. + * @param {Boolean} [attenuated=true] - Whether the scroll acceleration attenuated, default is true. + */ + scrollToTop: function(timeInSecond, attenuated) { + var moveDelta = this._calculateMovePercentDelta({ + anchor: cc.p(0, 1), + applyToHorizontal: false, + applyToVertical: true, + }); + + if(timeInSecond) { + this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false); + }else { + this._moveContent(moveDelta); + } + }, + + /** + * Scroll the content to the left boundary of ScrollView. + * @method scrollToLeft + * @param {Number} [timeInSecond=0] - Scroll time in second, if you don't pass timeInSecond, + * the content will jump to the left boundary immediately. + * @param {Boolean} [attenuated=true] - Whether the scroll acceleration attenuated, default is true. + */ + scrollToLeft: function(timeInSecond, attenuated) { + var moveDelta = this._calculateMovePercentDelta({ + anchor: cc.p(0, 0), + applyToHorizontal: true, + applyToVertical: false, + }); + + if(timeInSecond) { + this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false); + }else { + this._moveContent(moveDelta); + } + }, + + /** + * Scroll the content to the right boundary of ScrollView. + * @method scrollToRight + * @param {Number} [timeInSecond=0] - Scroll time in second, if you don't pass timeInSecond, + * the content will jump to the right boundary immediately. + * @param {Boolean} [attenuated=true] - Whether the scroll acceleration attenuated, default is true. + */ + scrollToRight: function(timeInSecond, attenuated) { + var moveDelta = this._calculateMovePercentDelta({ + anchor: cc.p(1, 0), + applyToHorizontal: true, + applyToVertical: false, + }); + + if(timeInSecond) { + this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false); + }else { + this._moveContent(moveDelta); + } + }, + + /** + * Scroll the content to the top left boundary of ScrollView. + * @method scrollToTopLeft + * @param {Number} [timeInSecond=0] - Scroll time in second, if you don't pass timeInSecond, + * the content will jump to the top left boundary immediately. + * @param {Boolean} [attenuated=true] - Whether the scroll acceleration attenuated, default is true. + */ + scrollToTopLeft: function(timeInSecond, attenuated) { + var moveDelta = this._calculateMovePercentDelta({ + anchor: cc.p(0, 1), + applyToHorizontal: true, + applyToVertical: true, + }); + + if(timeInSecond) { + this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false); + }else { + this._moveContent(moveDelta); + } + }, + + /** + * Scroll the content to the top right boundary of ScrollView. + * @method scrollToTopRight + * @param {Number} [timeInSecond=0] - Scroll time in second, if you don't pass timeInSecond, + * the content will jump to the top right boundary immediately. + * @param {Boolean} [attenuated=true] - Whether the scroll acceleration attenuated, default is true. + */ + scrollToTopRight: function(timeInSecond, attenuated) { + var moveDelta = this._calculateMovePercentDelta({ + anchor: cc.p(1, 1), + applyToHorizontal: true, + applyToVertical: true, + }); + + if(timeInSecond) { + this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false); + }else { + this._moveContent(moveDelta); + } + }, + + /** + * Scroll the content to the bottom left boundary of ScrollView. + * @method scrollToBottomLeft + * @param {Number} [timeInSecond=0] - Scroll time in second, if you don't pass timeInSecond, + * the content will jump to the bottom left boundary immediately. + * @param {Boolean} [attenuated=true] - Whether the scroll acceleration attenuated, default is true. + */ + scrollToBottomLeft: function(timeInSecond, attenuated) { + var moveDelta = this._calculateMovePercentDelta({ + anchor: cc.p(0, 0), + applyToHorizontal: true, + applyToVertical: true, + }); + + if(timeInSecond) { + this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false); + }else { + this._moveContent(moveDelta); + } + }, + + /** + * Scroll the content to the bottom right boundary of ScrollView. + * @method scrollToBottomRight + * @param {Number} [timeInSecond=0] - Scroll time in second, if you don't pass timeInSecond, + * the content will jump to the bottom right boundary immediately. + * @param {Boolean} [attenuated=true] - Whether the scroll acceleration attenuated, default is true. + */ + scrollToBottomRight: function(timeInSecond, attenuated) { + var moveDelta = this._calculateMovePercentDelta({ + anchor: cc.p(1, 0), + applyToHorizontal: true, + applyToVertical: true, + }); + + if(timeInSecond) { + this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false); + }else { + this._moveContent(moveDelta); + } + }, + + /** + * Scroll the content to the horizontal percent position of ScrollView. + * @method scrollToPercentHorizontal + * @param {Number} percent - A value between 0 and 1. + * @param {Number} [timeInSecond=0] - Scroll time in second, if you don't pass timeInSecond, + * the content will jump to the horizontal percent position of ScrollView immediately. + * @param {Boolean} [attenuated=true] - Whether the scroll acceleration attenuated, default is true. + */ + scrollToPercentHorizontal: function(percent, timeInSecond, attenuated) { + var moveDelta = this._calculateMovePercentDelta({ + anchor: cc.p(percent, 0), + applyToHorizontal: true, + applyToVertical: false, + }); + + if(timeInSecond) { + this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false); + }else { + this._moveContent(moveDelta); + } + }, + + /** + * Scroll the content to the percent position of ScrollView in any direction. + * @method scrollTo + * @param {Point} anchor - A point which will be clamp between cc.p(0,0) and cc.p(1,1). + * @param {Number} [timeInSecond=0] - Scroll time in second, if you don't pass timeInSecond, + * the content will jump to the percent position of ScrollView immediately. + * @param {Boolean} [attenuated=true] - Whether the scroll acceleration attenuated, default is true. + */ + scrollTo: function(anchor, timeInSecond, attenuated){ + var moveDelta = this._calculateMovePercentDelta({ + anchor: anchor, + applyToHorizontal: true, + applyToVertical: true, + }); + + if(timeInSecond) { + this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false); + }else { + this._moveContent(moveDelta); + } + }, + + /** + * Scroll the content to the vertical percent position of ScrollView. + * @method scrollToPercentVertical + * @param {Number} percent - A value between 0 and 1. + * @param {Number} [timeInSecond=0] - Scroll time in second, if you don't pass timeInSecond, + * the content will jump to the vertical percent position of ScrollView immediately. + * @param {Boolean} [attenuated=true] - Whether the scroll acceleration attenuated, default is true. + */ + scrollToPercentVertical: function(percent, timeInSecond, attenuated) { + var moveDelta = this._calculateMovePercentDelta({ + anchor: cc.p(0, percent), + applyToHorizontal: false, + applyToVertical: true, + }); + + if(timeInSecond) { + this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false); + }else { + this._moveContent(moveDelta); + } + }, + + /** + * Modify the content position. + * @method setContentPosition + * @param {Point} position - The position in content's parent space. + */ setContentPosition: function(position) { if (cc.pFuzzyEqual(position, this.getContentPosition(), EPSILON)) { return; @@ -191,6 +430,11 @@ var ScrollView = cc.Class({ //TODO: process bouncing and container move event }, + /** + * Query the content's position in its parent space. + * @method getContentPosition + * @returns {Position} - The content's position in its parent space. + */ getContentPosition: function() { return this.content.getPosition(); }, @@ -208,10 +452,29 @@ var ScrollView = cc.Class({ cc.eventManager.addListener(this._touchListener, this.node._sgNode); }, - onLoad: function() { - if (!CC_EDITOR) { - this._registerEvent(); + _calculateMovePercentDelta: function(options) { + var anchor = options.anchor; + var applyToHorizontal = options.applyToHorizontal; + var applyToVertical = options.applyToVertical; + + anchor = cc.pClamp(anchor, cc.p(0, 0), cc.p(1, 1)); + + var scrollSize = this.node.getContentSize(); + var contentSize = this.content.getContentSize(); + var bottomDeta = Math.abs(this._getContentBottomBoundary() - this._bottomBoundary); + var leftDeta = Math.abs(this._getContentLeftBoundary() - this._leftBoundary); + + var moveDelta = cc.p(0, 0); + if(applyToHorizontal) { + moveDelta.x = (contentSize.width - scrollSize.width) * anchor.x - leftDeta; } + + if(applyToVertical) { + moveDelta.y = (contentSize.height - scrollSize.height) * anchor.y - bottomDeta; + } + + moveDelta = cc.pNeg(moveDelta); + return moveDelta; }, _calculateBoundary: function() { @@ -233,17 +496,6 @@ var ScrollView = cc.Class({ return contentParent.convertToNodeSpaceAR(scrollViewPositionInWorldSpace); }, - start: function() { - this._calculateBoundary(); - - this._updateScrollBar(0); - }, - - update: function(dt) { - if (this._autoScrolling) { - this._processAutoScrolling(dt); - } - }, _hitTest: function(pos) { var target = this.node; @@ -322,7 +574,7 @@ var ScrollView = cc.Class({ this._onScrollBarTouchBegan(); }, - _clampDelta: function(delta){ + _clampDelta: function(delta) { var contentSize = this.content.getContentSize(); var scrollViewSize = this.node.getContentSize(); if (contentSize.width <= scrollViewSize.width) { @@ -466,7 +718,7 @@ var ScrollView = cc.Class({ var scrollviewSize = this.node.getContentSize(); targetDelta = cc.p(targetDelta.x * (contentSize.width - scrollviewSize.width) * (1 - this.brake), - targetDelta.y * (contentSize.height - scrollviewSize.height) * (1 - this.brake)); + targetDelta.y * (contentSize.height - scrollviewSize.height) * (1 - this.brake)); targetDelta = cc.pAdd(deltaMove, targetDelta); var factor = cc.pLength(targetDelta) / originalMoveLength; @@ -497,7 +749,8 @@ var ScrollView = cc.Class({ if (!cc.pFuzzyEqual(currentOutOfBoundary, cc.p(0, 0), EPSILON)) { this._autoScrollCurrentlyOutOfBoundary = true; var afterOutOfBoundary = this._getHowMuchOutOfBoundary(adjustedDeltaMove); - if (currentOutOfBoundary.x * afterOutOfBoundary.x > 0 || currentOutOfBoundary.y * afterOutOfBoundary.y > 0) { + if (currentOutOfBoundary.x * afterOutOfBoundary.x > 0 || + currentOutOfBoundary.y * afterOutOfBoundary.y > 0) { this._autoScrollBraking = true; } } @@ -627,6 +880,22 @@ var ScrollView = cc.Class({ } }, + //component life cycle methods + onLoad: function() { + if (!CC_EDITOR) { + this._registerEvent(); + } + }, + + start: function() { + this._calculateBoundary(); + }, + + update: function(dt) { + if (this._autoScrolling) { + this._processAutoScrolling(dt); + } + }, onDestroy: function() { if (this._touchListener) cc.eventManager.removeListener(this._touchListener); diff --git a/gulp/tasks/test.js b/gulp/tasks/test.js index 7884f17cf7..11a7bed73e 100644 --- a/gulp/tasks/test.js +++ b/gulp/tasks/test.js @@ -1,6 +1,7 @@ var Fs = require('fire-fs'); var gulp = require('gulp'); var fb = require('gulp-fb'); +var shell = require('gulp-shell'); var TimeOutInSeconds = 5; @@ -33,4 +34,7 @@ function test () { } gulp.task('test', ['build-test', 'unit-runner'], test); +gulp.task('visual-test', ['build-test'], shell.task([ + 'sh ./test/visual-tests/run.sh' +])); gulp.task('rerun-test', test); diff --git a/jsb_engine.js b/jsb_engine.js new file mode 100644 index 0000000000..0f20719525 --- /dev/null +++ b/jsb_engine.js @@ -0,0 +1,448 @@ +'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); + } + else { + return null; + } +} + +function _copyprop(name, source, target) { + var pd = _getPropertyDescriptor(source, name); + Object.defineProperty(target, name, pd); +} + +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); + } + } + } + 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, { + + // states + _paused: false, //whether the game is paused + + // Scenes list + _sceneInfos: [], + + _persistRootNodes: [], + + 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; + // 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 !== scene) { + cc.warn('The node can not be made persist because it\'s not under root node.'); + 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) { + 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 +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._scheduler.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(); + }, + + /** + * 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); + + // 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); + + // Re-add persist node root + var persistNodes = cc.game._persistRootNodes; + for (var i = 0; i < persistNodes.length; ++i) { + var node = persistNodes[i]; + node.parent = scene; + } + + var sgScene = scene; + + // Run an Entity Scene + if (scene instanceof cc.Scene) { + // ensure scene initialized + scene._load(); + + this._scene = scene; + sgScene = scene._sgNode; + } + + // 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; + } + }, + + /** + * 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 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; + cc.director.runScene(scene, onUnloaded); + } + else { + error = 'The asset ' + uuid + ' is not a scene'; + cc.error(error); + scene = null; + } + } + cc.director._loadingScene = ''; + if (onLaunched) { + onLaunched(error, scene); + } + }); + } +}); + +cc.director.sharedInit(); + +cc._LogInfos = {}; + diff --git a/test/visual-tests/src/ExtensionsTest/S9SpriteTest/S9SpriteTest.js b/test/visual-tests/src/ExtensionsTest/S9SpriteTest/S9SpriteTest.js index 7ad78832c0..b44bd03847 100644 --- a/test/visual-tests/src/ExtensionsTest/S9SpriteTest/S9SpriteTest.js +++ b/test/visual-tests/src/ExtensionsTest/S9SpriteTest/S9SpriteTest.js @@ -110,15 +110,20 @@ var S9CreationBasic = S9SpriteTestDemo.extend({ var x = winSize.width; var y = 0 + (winSize.height/2); + var texture = cc.textureCache.addImage("Images/blocks9.png"); + var spriteFrame = cc.SpriteFrame.createWithTexture(texture); + texture.once('load', function() { + cc.log("sprite frame is loaded"); + }); var s9sprite1 = cc.Scale9Sprite.create("Images/blocks9.png"); - var s9sprite2 = cc.Scale9Sprite.create(cc.rect(20,20,30,30),"Images/blocks9.png"); + var s9sprite2 = cc.Scale9Sprite.create("Images/blocks9.png"); - var s9sprite3 = cc.Scale9Sprite.create("Images/blocks9.png", cc.rect(0,0,60,60), cc.rect(20,20,30,30)); - s9sprite1.setPreferredSize(cc.size(150,150)); - s9sprite2.setPreferredSize(cc.size(150,150)); - s9sprite3.setPreferredSize(cc.size(150,150)); + var s9sprite3 = cc.Scale9Sprite.create("Images/blocks9.png"); + s9sprite1.setContentSize(cc.size(150,150)); + s9sprite2.setContentSize(cc.size(150,150)); + s9sprite3.setContentSize(cc.size(150,150)); s9sprite1.x = x/6; s9sprite1.y = y; s9sprite2.x = x/2; s9sprite2.y = y; @@ -145,10 +150,10 @@ var S9creationSpriteFrame = S9SpriteTestDemo.extend({ var s9sprite1 = cc.Scale9Sprite.create("blocks9.png"); - var s9sprite2 = cc.Scale9Sprite.create("blocks9.png",cc.rect(20,20,30,30)); + var s9sprite2 = cc.Scale9Sprite.create("blocks9.png"); - s9sprite1.setPreferredSize(cc.size(150,150)); - s9sprite2.setPreferredSize(cc.size(150,150)); + s9sprite1.setContentSize(cc.size(150,150)); + s9sprite2.setContentSize(cc.size(150,150)); s9sprite1.x = x/4; s9sprite1.y = y; @@ -173,17 +178,16 @@ var S9LaterUpdate = S9SpriteTestDemo.extend({ var s9sprite2 = cc.Scale9Sprite.create(); - s9sprite1.setPreferredSize(cc.size(150,150)); - s9sprite2.setPreferredSize(cc.size(150,150)); + s9sprite1.setContentSize(cc.size(150,150)); + s9sprite2.setContentSize(cc.size(150,150)); s9sprite1.x = x/4; s9sprite1.y = y; s9sprite2.x = 3 * x/4; s9sprite2.y = y; - var sprite1 = _ccsg.Sprite.create("Images/blocks9.png"); - s9sprite1.updateWithSprite(sprite1,sprite1.getTextureRect(),sprite1.textureRectRotated,cc.p(0,0),cc.size(0,0),cc.rect(20,20,30,30)); - + //s9sprite1.updateWithSprite(sprite1,sprite1.getTextureRect(),sprite1.textureRectRotated,cc.p(0,0),cc.size(0,0),cc.rect(20,20,30,30)); + s9sprite1.setTexture("Images/blocks9.png"); var frame = cc.spriteFrameCache.getSpriteFrame("blocks9.png"); - s9sprite2.setSpriteFrame(frame,cc.rect(20,20,30,30)); + s9sprite2.setSpriteFrame(frame); this.addChild(s9sprite1); this.addChild(s9sprite2); @@ -203,12 +207,12 @@ var S9RenderingTypeTest = S9SpriteTestDemo.extend({ var x = winSize.width; var y = 0 + (winSize.height/2); - var s9sprite1 = cc.Scale9Sprite.create(cc.rect(20,20,30,30),"Images/blocks9.png"); + var s9sprite1 = cc.Scale9Sprite.create("Images/blocks9.png"); - var s9sprite2 = cc.Scale9Sprite.create(cc.rect(20,20,30,30),"Images/blocks9.png"); + var s9sprite2 = cc.Scale9Sprite.create("Images/blocks9.png"); - s9sprite1.setPreferredSize(cc.size(150,150)); - s9sprite2.setPreferredSize(cc.size(150,150)); + s9sprite1.setContentSize(cc.size(150,150)); + s9sprite2.setContentSize(cc.size(150,150)); s9sprite2.setRenderingType(ccui.Scale9Sprite.RenderingType.SIMPLE); @@ -233,12 +237,12 @@ var S9StateTest = S9SpriteTestDemo.extend({ var x = winSize.width; var y = 0 + (winSize.height/2); - var s9sprite1 = cc.Scale9Sprite.create(cc.rect(20,20,30,30),"Images/blocks9.png"); + var s9sprite1 = cc.Scale9Sprite.create("Images/blocks9.png"); - var s9sprite2 = cc.Scale9Sprite.create(cc.rect(20,20,30,30),"Images/blocks9.png"); + var s9sprite2 = cc.Scale9Sprite.create("Images/blocks9.png"); - s9sprite1.setPreferredSize(cc.size(150,150)); - s9sprite2.setPreferredSize(cc.size(150,150)); + s9sprite1.setContentSize(cc.size(150,150)); + s9sprite2.setContentSize(cc.size(150,150)); s9sprite2.setState(ccui.Scale9Sprite.state.GRAY); @@ -264,8 +268,8 @@ var S9Actiontest = S9SpriteTestDemo.extend({ var x = winSize.width ; var y = 0 + (winSize.height / 2); - var s9sprite1 = cc.Scale9Sprite.create(cc.rect(20,20,30,30),"Images/blocks9.png"); - s9sprite1.setPreferredSize(cc.size(150,150)); + var s9sprite1 = cc.Scale9Sprite.create("Images/blocks9.png"); + s9sprite1.setContentSize(cc.size(150,150)); s9sprite1.x = x/4; s9sprite1.y = y; this.addChild(s9sprite1); @@ -290,9 +294,9 @@ var S9ChangeFiletest = S9SpriteTestDemo.extend({ var x = winSize.width ; var y = 0 + (winSize.height / 2); - _s9sprite1 = cc.Scale9Sprite.create(cc.rect(20,20,30,30),"Images/blocks9.png"); - _s9sprite1.setPreferredSize(cc.size(150,150)); - _s9sprite1.x = x/4; _s9sprite1.y = y; + _s9sprite1 = cc.Scale9Sprite.create("Images/blocks9.png"); + _s9sprite1.setContentSize(cc.size(300,200)); + _s9sprite1.x = x/2; _s9sprite1.y = y; this.addChild(_s9sprite1); var actionArray = []; @@ -310,7 +314,7 @@ var S9ChangeFiletest = S9SpriteTestDemo.extend({ }, changeFile: function(file) { console.log("I'am going to change a new file"); - _s9sprite1.initWithFile(file); + _s9sprite1.initWithTexture(file); _s9sprite1.setInsetTop(10); _s9sprite1.setInsetBottom(10); _s9sprite1.setInsetLeft(10); @@ -332,13 +336,13 @@ var S9ChangePreferredSizeTest = S9SpriteTestDemo.extend({ var x = winSize.width; var y = 0 + (winSize.height/2); - this._s9sprite1 = cc.Scale9Sprite.create(cc.rect(20,20,30,30),"Images/blocks9.png"); + this._s9sprite1 = cc.Scale9Sprite.create("Images/blocks9.png"); - this._s9sprite2 = cc.Scale9Sprite.create(cc.rect(20,20,30,30),"Images/blocks9.png"); + this._s9sprite2 = cc.Scale9Sprite.create("Images/blocks9.png"); var s9sprite1 = this._s9sprite1; var s9sprite2 = this._s9sprite2; - s9sprite1.setPreferredSize(cc.size(96,96)); - s9sprite2.setPreferredSize(cc.size(96,96)); + s9sprite1.setContentSize(cc.size(96,96)); + s9sprite2.setContentSize(cc.size(96,96)); this.scheduleUpdate(); s9sprite2.setRenderingType(ccui.Scale9Sprite.RenderingType.SIMPLE); @@ -355,15 +359,15 @@ var S9ChangePreferredSizeTest = S9SpriteTestDemo.extend({ this._dt += dt; if(this._dt > 3) this._dt = 3; var newSize = cc.size(96 + this._dt * 20, 96 + this._dt * 20); - this._s9sprite1.setPreferredSize(newSize); - this._s9sprite2.setPreferredSize(newSize); + this._s9sprite1.setContentSize(newSize); + this._s9sprite2.setContentSize(newSize); if(this._dt === 3) this.unscheduleUpdate(); }, }); //S9ChangeCapInsetsTest var S9ChangeCapInsetsTest = S9SpriteTestDemo.extend({ - + //todo add a new test case _title:"Scale9 Change CapInsets Test", _subtitle:"Slice and Simple s9sprite will behave differently", _s9sprite1: null, @@ -375,14 +379,14 @@ var S9ChangeCapInsetsTest = S9SpriteTestDemo.extend({ var x = winSize.width; var y = 0 + (winSize.height/2); - this._s9sprite1 = cc.Scale9Sprite.create(cc.rect(20,20,30,30),"Images/blocks9.png"); + this._s9sprite1 = cc.Scale9Sprite.create("Images/blocks9.png"); - this._s9sprite2 = cc.Scale9Sprite.create(cc.rect(20,20,30,30),"Images/blocks9.png"); + this._s9sprite2 = cc.Scale9Sprite.create("Images/blocks9.png"); var s9sprite1 = this._s9sprite1; var s9sprite2 = this._s9sprite2; - s9sprite1.setPreferredSize(cc.size(150,150)); - s9sprite2.setPreferredSize(cc.size(150,150)); - this.scheduleUpdate(); + s9sprite1.setContentSize(cc.size(150,150)); + s9sprite2.setContentSize(cc.size(150,150)); + //this.scheduleUpdate(); s9sprite2.setRenderingType(ccui.Scale9Sprite.RenderingType.SIMPLE); @@ -395,20 +399,20 @@ var S9ChangeCapInsetsTest = S9SpriteTestDemo.extend({ }, update: function(dt) { - var rect = cc.rect(20,20,30,30); - this._dt += dt; - if(this._dt > 3) this._dt = 3; - rect.x = rect.x + this._dt * 20; - this._s9sprite1.setCapInsets(rect); - this._s9sprite2.setCapInsets(rect); - if(this._dt === 3) this.unscheduleUpdate(); + //var rect = cc.rect(20,20,30,30); + //this._dt += dt; + //if(this._dt > 3) this._dt = 3; + //rect.x = rect.x + this._dt * 20; + //this._s9sprite1.setCapInsets(rect); + //this._s9sprite2.setCapInsets(rect); + //if(this._dt === 3) this.unscheduleUpdate(); }, }); //S9ChangeColorOpacityTest var S9ChangeColorOpacityTest = S9SpriteTestDemo.extend({ - _title:"Scale9 Change CapInsets Test", + _title:"Scale9 Change Color Opacity Test", _subtitle:"Slice and Simple s9sprite will behave differently", _s9sprite1: null, _s9sprite2: null, @@ -419,13 +423,13 @@ var S9ChangeColorOpacityTest = S9SpriteTestDemo.extend({ var x = winSize.width; var y = 0 + (winSize.height/2); - this._s9sprite1 = cc.Scale9Sprite.create(cc.rect(20,20,30,30),"Images/blocks9.png"); + this._s9sprite1 = cc.Scale9Sprite.create("Images/blocks9.png"); - this._s9sprite2 = cc.Scale9Sprite.create(cc.rect(20,20,30,30),"Images/blocks9.png"); + this._s9sprite2 = cc.Scale9Sprite.create("Images/blocks9.png"); var s9sprite1 = this._s9sprite1; var s9sprite2 = this._s9sprite2; - s9sprite1.setPreferredSize(cc.size(150,150)); - s9sprite2.setPreferredSize(cc.size(150,150)); + s9sprite1.setContentSize(cc.size(150,150)); + s9sprite2.setContentSize(cc.size(150,150)); s9sprite1.setBlendFunc(cc.BlendFunc.ALPHA_NON_PREMULTIPLIED); s9sprite2.setBlendFunc(cc.BlendFunc.ALPHA_NON_PREMULTIPLIED); this.scheduleUpdate();