From 93c1b74d440920cdcd6d90544b999a988138d3e3 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Tue, 26 Apr 2016 00:30:47 +0800 Subject: [PATCH 1/5] Skip rendering if nothing changed --- cocos2d/core/CCDirector.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/cocos2d/core/CCDirector.js b/cocos2d/core/CCDirector.js index e1e043800e..a76574b0bf 100644 --- a/cocos2d/core/CCDirector.js +++ b/cocos2d/core/CCDirector.js @@ -218,8 +218,6 @@ cc.Director = cc.Class.extend(/** @lends cc.Director# */{ cc.eventManager.dispatchEvent(this._eventAfterUpdate); } - renderer.clear(); - /* to avoid flickr, nextScene MUST be here: after tick and before draw. XXX: Which bug is this one. It seems that it can't be reproduced with v0.9 */ if (this._nextScene) { @@ -230,6 +228,7 @@ cc.Director = cc.Class.extend(/** @lends cc.Director# */{ this._beforeVisitScene(); // draw the scene + var skipRendering = false; if (this._runningScene) { if (renderer.childrenOrderDirty === true) { cc.renderer.clearRenderCommands(); @@ -237,8 +236,17 @@ cc.Director = cc.Class.extend(/** @lends cc.Director# */{ this._runningScene._renderCmd._curLevel = 0; //level start from 0; this._runningScene.visit(); renderer.resetFlag(); - } else if (renderer.transformDirty() === true) + } + else if (renderer.transformDirty() === true) { renderer.transform(); + } + else { + skipRendering = true; + } + } + + if (!skipRendering) { + renderer.clear(); } // draw the notifications node @@ -251,7 +259,9 @@ cc.Director = cc.Class.extend(/** @lends cc.Director# */{ if (this._afterVisitScene) this._afterVisitScene(); - renderer.rendering(cc._renderContext); + if (!skipRendering) { + renderer.rendering(cc._renderContext); + } this._totalFrames++; cc.eventManager.dispatchEvent(this._eventAfterDraw); From d02fdebe306cbed327919ce3e4e22e1b1b9f812f Mon Sep 17 00:00:00 2001 From: pandamicro Date: Tue, 26 Apr 2016 00:31:28 +0800 Subject: [PATCH 2/5] Add callback queue to solve concurrent loading issue --- CCBoot.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/CCBoot.js b/CCBoot.js index 66ffd6c2d3..1f700a5496 100644 --- a/CCBoot.js +++ b/CCBoot.js @@ -586,6 +586,7 @@ cc.loader = (function () { _register = {}, //register of loaders _langPathCache = {}, //cache for lang path _aliases = {}, //aliases for res url + _queue = {}, // Callback queue for resources already loading _urlRegExp = new RegExp( "^" + // protocol identifier @@ -668,6 +669,10 @@ cc.loader = (function () { return results; }, + isLoading: function (url) { + return (_queue[url] !== undefined); + }, + /** * Load js files. * If the third parameter doesn't exist, then the baseDir turns to be "". @@ -890,6 +895,12 @@ cc.loader = (function () { return img; } + var queue = _queue[url]; + if (queue) { + queue.callbacks.push(callback); + return queue.img; + } + img = new Image(); if (opt.isCrossOrigin && location.origin !== "file://") img.crossOrigin = "Anonymous"; @@ -898,24 +909,53 @@ cc.loader = (function () { this.removeEventListener('load', loadCallback, false); this.removeEventListener('error', errorCallback, false); - cc.loader.cache[url] = img; - if (callback) - callback(null, img); + if (!_urlRegExp.test(url)) { + cc.loader.cache[url] = img; + } + + var queue = _queue[url]; + if (queue) { + callbacks = queue.callbacks; + for (var i = 0; i < callbacks.length; ++i) { + var callback = callbacks[i]; + if (callback) { + callback(null, img); + } + } + queue.img = null; + delete _queue[url]; + } }; var self = this; var errorCallback = function () { this.removeEventListener('error', errorCallback, false); - if(img.crossOrigin && img.crossOrigin.toLowerCase() === "anonymous"){ + if (img.crossOrigin && img.crossOrigin.toLowerCase() === "anonymous") { opt.isCrossOrigin = false; self.release(url); cc.loader.loadImg(url, opt, callback); - }else{ - typeof callback === "function" && callback("load image failed"); + } else { + var queue = _queue[url]; + if (queue) { + callbacks = queue.callbacks; + for (var i = 0; i < callbacks.length; ++i) { + var callback = callbacks[i]; + if (callback) { + callback("load image failed"); + } + } + queue.img = null; + delete _queue[url]; + } } }; + _queue[url] = { + img: img, + callbacks: callback ? [callback] : [] + }; + img.addEventListener("load", loadCallback); img.addEventListener("error", errorCallback); img.src = url; From 8992d8edf9971609e16d00ae4866028154b599a7 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Tue, 26 Apr 2016 00:32:16 +0800 Subject: [PATCH 3/5] Improve WebGL support detection --- CCBoot.js | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/CCBoot.js b/CCBoot.js index 1f700a5496..42b9def807 100644 --- a/CCBoot.js +++ b/CCBoot.js @@ -1843,25 +1843,38 @@ var _initSys = function () { var _supportCanvas = !!_tmpCanvas1.getContext("2d"); var _supportWebGL = false; - var tmpCanvas = document.createElement("CANVAS"); if (win.WebGLRenderingContext) { + var tmpCanvas = document.createElement("CANVAS"); try{ var context = cc.create3DContext(tmpCanvas, {'stencil': true, 'preserveDrawingBuffer': true }); if(context) { _supportWebGL = true; } - // Accept only Android 5+ default browser and QQ Browser 6.2+ if (_supportWebGL && sys.os === sys.OS_ANDROID) { - _supportWebGL = false; - // QQ Brwoser 6.2+ - var browserVer = parseFloat(sys.browserVersion); - if (sys.browserType === sys.BROWSER_TYPE_MOBILE_QQ && browserVer >= 6.2) { - _supportWebGL = true; - } - // Android 5+ default browser - else if (sys.osMainVersion && sys.osMainVersion >= 5 && sys.browserType === sys.BROWSER_TYPE_ANDROID) { - _supportWebGL = true; + switch (sys.browserType) { + case sys.BROWSER_TYPE_MOBILE_QQ: + case sys.BROWSER_TYPE_BAIDU: + case sys.BROWSER_TYPE_BAIDU_APP: + // QQ & Baidu Brwoser 6.2+ (using blink kernel) + var browserVer = parseFloat(sys.browserVersion); + if (browserVer >= 6.2) { + _supportWebGL = true; + } + else { + _supportWebGL = false; + } + break; + case sys.BROWSER_TYPE_ANDROID: + // Android 5+ default browser + if (sys.osMainVersion && sys.osMainVersion >= 5) { + _supportWebGL = true; + } + break; + case sys.BROWSER_TYPE_UNKNOWN: + case sys.BROWSER_TYPE_360: + case sys.BROWSER_TYPE_MIUI: + _supportWebGL = false; } } } From e80dd1cfd7afd6a90270fa5d6d2f4949b56565f8 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Tue, 26 Apr 2016 00:34:54 +0800 Subject: [PATCH 4/5] Improve initialization of UI nodes --- .../UIScale9SpriteCanvasRenderCmd.js | 2 +- extensions/ccui/base-classes/UIWidget.js | 2 +- extensions/ccui/layouts/UIHBox.js | 31 +---- extensions/ccui/layouts/UILayout.js | 34 ++---- .../ccui/layouts/UILayoutCanvasRenderCmd.js | 7 +- extensions/ccui/layouts/UIRelativeBox.js | 30 +---- extensions/ccui/layouts/UIVBox.js | 22 +--- extensions/ccui/uiwidgets/UIButton.js | 17 +-- extensions/ccui/uiwidgets/UICheckBox.js | 24 +--- extensions/ccui/uiwidgets/UIImageView.js | 17 +-- extensions/ccui/uiwidgets/UIRichText.js | 108 ++++-------------- extensions/ccui/uiwidgets/UISlider.js | 19 ++- extensions/ccui/uiwidgets/UIText.js | 29 +---- extensions/ccui/uiwidgets/UITextAtlas.js | 4 +- extensions/ccui/uiwidgets/UITextBMFont.js | 2 +- extensions/ccui/uiwidgets/UITextField.js | 14 +-- .../uiwidgets/scroll-widget/UIListView.js | 16 +-- .../uiwidgets/scroll-widget/UIPageView.js | 17 +-- .../scroll-widget/UIPageViewIndicator.js | 26 +---- .../uiwidgets/scroll-widget/UIScrollView.js | 12 +- .../scroll-widget/UIScrollViewBar.js | 50 ++++---- .../UIScrollViewWebGLRenderCmd.js | 14 ++- 22 files changed, 125 insertions(+), 372 deletions(-) diff --git a/extensions/ccui/base-classes/UIScale9SpriteCanvasRenderCmd.js b/extensions/ccui/base-classes/UIScale9SpriteCanvasRenderCmd.js index aa57c91ab7..e4814ef912 100644 --- a/extensions/ccui/base-classes/UIScale9SpriteCanvasRenderCmd.js +++ b/extensions/ccui/base-classes/UIScale9SpriteCanvasRenderCmd.js @@ -120,7 +120,7 @@ this._cacheScale9Sprite(); this._dirtyFlag = this._dirtyFlag & flags.cacheDirty ^ this._dirtyFlag; } - } + }; proto._cacheScale9Sprite = function() { var node = this._node; diff --git a/extensions/ccui/base-classes/UIWidget.js b/extensions/ccui/base-classes/UIWidget.js index 325b3188bb..d5f01b0eaf 100644 --- a/extensions/ccui/base-classes/UIWidget.js +++ b/extensions/ccui/base-classes/UIWidget.js @@ -174,7 +174,7 @@ ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{ this._positionPercent = cc.p(0, 0); this._nodes = []; this._layoutParameterType = ccui.LayoutParameter.NONE; - this.init(); //TODO + ccui.Widget.prototype.init.call(this); }, /** diff --git a/extensions/ccui/layouts/UIHBox.js b/extensions/ccui/layouts/UIHBox.js index 4e32002dd4..f808668c22 100644 --- a/extensions/ccui/layouts/UIHBox.js +++ b/extensions/ccui/layouts/UIHBox.js @@ -35,37 +35,12 @@ ccui.HBox = ccui.Layout.extend(/** @lends ccui.HBox# */{ * @param {cc.Size} [size] */ ctor: function(size){ - ccui.Layout.prototype.ctor.call(this, size); - if(size !== undefined) - this.initWithSize(size); - else - this.init(); - }, + ccui.Layout.prototype.ctor.call(this); + this.setLayoutType(ccui.Layout.LINEAR_HORIZONTAL); - /** - * Initialize a HBox. please do not call this function by yourself, you should pass the parameters to constructor to initialize it. - * @override - * @returns {boolean} - */ - init: function(){ - if(ccui.Layout.prototype.init.call(this)){ - this.setLayoutType(ccui.Layout.LINEAR_HORIZONTAL); - return true; - } - return false; - }, - - /** - * Initializes a HBox with size. - * @param size - * @returns {boolean} - */ - initWithSize: function(size){ - if(this.init()){ + if(size) { this.setContentSize(size); - return true; } - return false; } }); diff --git a/extensions/ccui/layouts/UILayout.js b/extensions/ccui/layouts/UILayout.js index 8d4db5b441..29a8533a68 100644 --- a/extensions/ccui/layouts/UILayout.js +++ b/extensions/ccui/layouts/UILayout.js @@ -81,10 +81,16 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{ ctor: function () { this._layoutType = ccui.Layout.ABSOLUTE; this._widgetType = ccui.Widget.TYPE_CONTAINER; - this._clippingType = ccui.Layout.CLIPPING_STENCIL; + this._clippingType = ccui.Layout.CLIPPING_SCISSOR; this._colorType = ccui.Layout.BG_COLOR_NONE; ccui.Widget.prototype.ctor.call(this); + + this.ignoreContentAdaptWithSize(false); + this.setContentSize(cc.size(0, 0)); + this.setAnchorPoint(0, 0); + this.onPassFocusToChild = this._findNearestChildWidgetIndex.bind(this); + this._backGroundImageCapInsets = cc.rect(0, 0, 0, 0); this._color = cc.color(255, 255, 255, 255); @@ -236,22 +242,6 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{ */ onPassFocusToChild: null, - /** - * override "init" method of widget. please do not call this function by yourself, you should pass the parameters to constructor to initialize it. - * @returns {boolean} - * @override - */ - init: function () { - if (ccui.Widget.prototype.init.call(this)) { - this.ignoreContentAdaptWithSize(false); - this.setContentSize(cc.size(0, 0)); - this.setAnchorPoint(0, 0); - this.onPassFocusToChild = this._findNearestChildWidgetIndex.bind(this); - return true; - } - return false; - }, - /** * Adds a widget to the container. * @param {ccui.Widget} widget @@ -330,8 +320,9 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{ default: break; } - } else + } else { ccui.Widget.prototype.visit.call(this, parentCmd); + } }, /** @@ -344,6 +335,7 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{ return; this._clippingEnabled = able; switch (this._clippingType) { + case ccui.Layout.CLIPPING_SCISSOR: case ccui.Layout.CLIPPING_STENCIL: if (able){ this._clippingStencil = new cc.DrawNode(); @@ -369,10 +361,6 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{ setClippingType: function (type) { if (type === this._clippingType) return; - if(cc._renderType === cc.game.RENDER_TYPE_CANVAS && type === ccui.Layout.CLIPPING_SCISSOR){ - cc.log("Only supports STENCIL on canvas mode."); - return; - } var clippingEnabled = this.isClippingEnabled(); this.setClippingEnabled(false); this._clippingType = type; @@ -388,7 +376,7 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{ }, _setStencilClippingSize: function (size) { - if (this._clippingEnabled && this._clippingType === ccui.Layout.CLIPPING_STENCIL) { + if (this._clippingEnabled) { var rect = []; rect[0] = cc.p(0, 0); rect[1] = cc.p(size.width, 0); diff --git a/extensions/ccui/layouts/UILayoutCanvasRenderCmd.js b/extensions/ccui/layouts/UILayoutCanvasRenderCmd.js index df53a60560..ea06506246 100644 --- a/extensions/ccui/layouts/UILayoutCanvasRenderCmd.js +++ b/extensions/ccui/layouts/UILayoutCanvasRenderCmd.js @@ -57,8 +57,9 @@ default: break; } - } else + } else { ccui.Widget.CanvasRenderCmd.prototype.visit.call(this, parentCmd); + } }; proto._onRenderSaveCmd = function(ctx, scaleX, scaleY){ @@ -103,7 +104,7 @@ context.globalCompositeOperation = "destination-over"; context.drawImage(this._locCache, 0, 0); context.restore(); - }else{ + } else { wrapper.restore(); //use for restore clip operation } }; @@ -126,7 +127,7 @@ } }; - proto.stencilClippingVisit = proto.scissorClippingVisit = function(parentCmd){ + proto.stencilClippingVisit = proto.scissorClippingVisit = function (parentCmd) { var node = this._node; if (!node._clippingStencil || !node._clippingStencil.isVisible()) return; diff --git a/extensions/ccui/layouts/UIRelativeBox.js b/extensions/ccui/layouts/UIRelativeBox.js index 500c9eee2b..064be6ecb3 100644 --- a/extensions/ccui/layouts/UIRelativeBox.js +++ b/extensions/ccui/layouts/UIRelativeBox.js @@ -35,36 +35,12 @@ ccui.RelativeBox = ccui.Layout.extend(/** @lends ccui.RelativeBox# */{ * @param {cc.Size} [size] */ ctor: function(size){ - if(size) - this.initWithSize(size); - else - this.init(); - }, + ccui.Layout.prototype.ctor.call(this); + this.setLayoutType(ccui.Layout.RELATIVE); - /** - * Initializes a relative box. please do not call this function by yourself, you should pass the parameters to constructor to initialize it. - * @override - * @returns {boolean} - */ - init: function(){ - if(ccui.Layout.prototype.init.call(this)){ - this.setLayoutType(ccui.Layout.RELATIVE); - return true; - } - return false; - }, - - /** - * Initializes a relative box with size - * @param {cc.Size} [size] - * @returns {boolean} - */ - initWithSize: function(size){ - if(this.init()){ + if(size) { this.setContentSize(size); - return true; } - return false; } }); diff --git a/extensions/ccui/layouts/UIVBox.js b/extensions/ccui/layouts/UIVBox.js index d816c729db..d6888befbb 100644 --- a/extensions/ccui/layouts/UIVBox.js +++ b/extensions/ccui/layouts/UIVBox.js @@ -35,24 +35,12 @@ ccui.VBox = ccui.Layout.extend(/** @lends ccui.VBox# */{ * @param {cc.Size} size */ ctor: function(size){ - ccui.Layout.prototype.ctor.call(this, size); - if(size !== undefined) - this.initWithSize(size); - else - this.init(); - }, + ccui.Layout.prototype.ctor.call(this); + this.setLayoutType(ccui.Layout.LINEAR_VERTICAL); - /** - * Initializes a VBox. please do not call this function by yourself, you should pass the parameters to constructor to initialize it. - * @override - * @returns {boolean} - */ - init: function(){ - if(ccui.Layout.prototype.init.call(this)){ - this.setLayoutType(ccui.Layout.LINEAR_VERTICAL); - return true; + if (size) { + this.setContentSize(size); } - return false; }, /** @@ -62,7 +50,7 @@ ccui.VBox = ccui.Layout.extend(/** @lends ccui.VBox# */{ */ initWithSize: function(size){ if(this.init()){ - this.setContentSize(size); + return true; } return false; diff --git a/extensions/ccui/uiwidgets/UIButton.js b/extensions/ccui/uiwidgets/UIButton.js index c1c1602638..6bb7582e2e 100644 --- a/extensions/ccui/uiwidgets/UIButton.js +++ b/extensions/ccui/uiwidgets/UIButton.js @@ -103,25 +103,10 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ this._titleColor = cc.color.WHITE; ccui.Widget.prototype.ctor.call(this); this.setTouchEnabled(true); - this.init(normalImage, selectedImage, disableImage, texType); - }, - /** - * Initializes a button. please do not call this function by yourself, you should pass the parameters to constructor to initialize it. - * @param {String} normalImage - * @param {String} [selectedImage=""] - * @param {String} [disableImage=""] - * @param {Number} [texType=ccui.Widget.LOCAL_TEXTURE] - * @returns {boolean} - * @override - */ - init: function (normalImage, selectedImage,disableImage, texType) { - if (ccui.Widget.prototype.init.call(this)) { - if(normalImage === undefined) - return true; + if (normalImage) { this.loadTextures(normalImage, selectedImage,disableImage, texType); } - return false; }, _initRenderer: function () { diff --git a/extensions/ccui/uiwidgets/UICheckBox.js b/extensions/ccui/uiwidgets/UICheckBox.js index 4f4b68fac1..d76b4d560a 100644 --- a/extensions/ccui/uiwidgets/UICheckBox.js +++ b/extensions/ccui/uiwidgets/UICheckBox.js @@ -102,28 +102,10 @@ ccui.CheckBox = ccui.Widget.extend(/** @lends ccui.CheckBox# */{ backGroundSelected = undefined; } texType = texType === undefined ? 0 : texType; - this.init(backGround, backGroundSelected,cross,backGroundDisabled,frontCrossDisabled,texType); - }, - /** - * Initializes a checkBox. please do not call this function by yourself, you should pass the parameters to constructor to initialize it. - * @param {String} backGround - * @param {String} backGroundSelected - * @param {String} cross - * @param {String} backGroundDisabled - * @param {String} frontCrossDisabled - * @param {Number} [texType=ccui.Widget.LOCAL_TEXTURE] - * @returns {boolean} - * @override - */ - init: function (backGround, backGroundSelected, cross, backGroundDisabled, frontCrossDisabled, texType) { - if (ccui.Widget.prototype.init.call(this)) { - this._isSelected = true; - this.setSelected(false); - this.loadTextures(backGround, backGroundSelected, cross, backGroundDisabled, frontCrossDisabled, texType); - return true; - } - return false; + this._isSelected = true; + this.setSelected(false); + this.loadTextures(backGround, backGroundSelected, cross, backGroundDisabled, frontCrossDisabled, texType); }, _initRenderer: function () { diff --git a/extensions/ccui/uiwidgets/UIImageView.js b/extensions/ccui/uiwidgets/UIImageView.js index 7cecaf762a..b7e6511bf9 100644 --- a/extensions/ccui/uiwidgets/UIImageView.js +++ b/extensions/ccui/uiwidgets/UIImageView.js @@ -53,20 +53,13 @@ ccui.ImageView = ccui.Widget.extend(/** @lends ccui.ImageView# */{ this._imageTextureSize = cc.size(this._capInsets.width, this._capInsets.height); ccui.Widget.prototype.ctor.call(this); texType = texType === undefined ? 0 : texType; - this._init(imageFileName, texType); - }, - /** - * Initializes an imageView. please do not call this function by yourself, you should pass the parameters to constructor to initialize it. - * @param {String} imageFileName - * @param {Number} [texType==ccui.Widget.LOCAL_TEXTURE] - * @returns {boolean} - */ - _init: function(imageFileName, texType){ - if(imageFileName === undefined) - this._imageTexType = ccui.Widget.LOCAL_TEXTURE; - else + if(imageFileName) { this.loadTexture(imageFileName, texType); + } + else { + this._imageTexType = ccui.Widget.LOCAL_TEXTURE; + } }, _initRenderer: function () { diff --git a/extensions/ccui/uiwidgets/UIRichText.js b/extensions/ccui/uiwidgets/UIRichText.js index 61f72cc08e..8d72d8e58e 100644 --- a/extensions/ccui/uiwidgets/UIRichText.js +++ b/extensions/ccui/uiwidgets/UIRichText.js @@ -36,28 +36,22 @@ ccui.RichElement = ccui.Class.extend(/** @lends ccui.RichElement# */{ /** * Constructor of ccui.RichElement */ - ctor: function () { + ctor: function (tag, color, opacity) { this._type = 0; - this._tag = 0; + this._tag = tag || 0; this._color = cc.color(255, 255, 255, 255); - }, - - /** - * Initializes a richElement. - * @param {Number} tag - * @param {cc.Color} color - * @param {Number} opacity - */ - init: function (tag, color, opacity) { - this._tag = tag; - this._color.r = color.r; - this._color.g = color.g; - this._color.b = color.b; - this._opacity = opacity; - if(opacity === undefined) + if (color) { + this._color.r = color.r; + this._color.g = color.g; + this._color.b = color.b; + } + this._opacity = opacity || 0; + if(opacity === undefined) { this._color.a = color.a; - else + } + else { this._color.a = opacity; + } } }); @@ -93,42 +87,18 @@ ccui.RichElementText = ccui.RichElement.extend(/** @lends ccui.RichElementText# * @param {Number} fontSize */ ctor: function (tag, colorOrFontDef, opacity, text, fontName, fontSize) { - ccui.RichElement.prototype.ctor.call(this); + var color = colorOrFontDef; + if (colorOrFontDef && colorOrFontDef instanceof cc.FontDefinition) { + color = colorOrFontDef.fillStyle; + fontName = fontDef.fontName; + fontSize = fontDef.fontSize; + this._fontDefinition = fontDef; + } + ccui.RichElement.prototype.ctor.call(this, tag, color, opacity); this._type = ccui.RichElement.TEXT; - this._text = ""; - this._fontName = ""; - this._fontSize = 0; - - if( colorOrFontDef && colorOrFontDef instanceof cc.FontDefinition) - this.initWithStringAndTextDefinition(tag, text, colorOrFontDef, opacity); - else - fontSize && this.init(tag, colorOrFontDef, opacity, text, fontName, fontSize); - }, - - /** - * Initializes a ccui.RichElementText. - * @param {Number} tag - * @param {cc.Color} color - * @param {Number} opacity - * @param {String} text - * @param {String} fontName - * @param {Number} fontSize - * @override - */ - init: function (tag, color, opacity, text, fontName, fontSize) { - ccui.RichElement.prototype.init.call(this, tag, color, opacity); this._text = text; this._fontName = fontName; this._fontSize = fontSize; - }, - initWithStringAndTextDefinition: function(tag, text, fontDef, opacity){ - - ccui.RichElement.prototype.init.call(this, tag, fontDef.fillStyle, opacity); - this._fontDefinition = fontDef; - this._text = text; - this._fontName = fontDef.fontName; - this._fontSize = fontDef.fontSize; - } }); @@ -165,26 +135,11 @@ ccui.RichElementImage = ccui.RichElement.extend(/** @lends ccui.RichElementImage * @param {String} filePath */ ctor: function (tag, color, opacity, filePath) { - ccui.RichElement.prototype.ctor.call(this); + ccui.RichElement.prototype.ctor.call(this, tag, color, opacity); this._type = ccui.RichElement.IMAGE; - this._filePath = ""; + this._filePath = filePath || ""; this._textureRect = cc.rect(0, 0, 0, 0); this._textureType = 0; - - filePath !== undefined && this.init(tag, color, opacity, filePath); - }, - - /** - * Initializes a ccui.RichElementImage - * @param {Number} tag - * @param {cc.Color} color - * @param {Number} opacity - * @param {String} filePath - * @override - */ - init: function (tag, color, opacity, filePath) { - ccui.RichElement.prototype.init.call(this, tag, color, opacity); - this._filePath = filePath; } }); @@ -217,24 +172,9 @@ ccui.RichElementCustomNode = ccui.RichElement.extend(/** @lends ccui.RichElement * @param {cc.Node} customNode */ ctor: function (tag, color, opacity, customNode) { - ccui.RichElement.prototype.ctor.call(this); + ccui.RichElement.prototype.ctor.call(this, tag, color, opacity); this._type = ccui.RichElement.CUSTOM; - this._customNode = null; - - customNode !== undefined && this.init(tag, color, opacity, customNode); - }, - - /** - * Initializes a ccui.RichElementCustomNode - * @param {Number} tag - * @param {cc.Color} color - * @param {Number} opacity - * @param {cc.Node} customNode - * @override - */ - init: function (tag, color, opacity, customNode) { - ccui.RichElement.prototype.init.call(this, tag, color, opacity); - this._customNode = customNode; + this._customNode = customNode || null; } }); diff --git a/extensions/ccui/uiwidgets/UISlider.js b/extensions/ccui/uiwidgets/UISlider.js index 96c2ee7e73..4bccfdcfbe 100644 --- a/extensions/ccui/uiwidgets/UISlider.js +++ b/extensions/ccui/uiwidgets/UISlider.js @@ -81,19 +81,14 @@ ccui.Slider = ccui.Widget.extend(/** @lends ccui.Slider# */{ this._capInsetsProgressBarRenderer = cc.rect(0, 0, 0, 0); ccui.Widget.prototype.ctor.call(this); - resType = resType == null ? 0 : resType; + resType = resType || 0; this.setTouchEnabled(true); - barTextureName && this.loadBarTexture(barTextureName, resType); - normalBallTextureName && this.loadSlidBallTextures(normalBallTextureName, resType); - }, - - /** - * Initializes a ccui.Slider. Please do not call this function by yourself, you should pass the parameters to constructor to initialize it. - * @returns {boolean} - * @override - */ - init: function () { - return ccui.Widget.prototype.init.call(this); + if (barTextureName) { + this.loadBarTexture(barTextureName, resType); + } + if (normalBallTextureName) { + this.loadSlidBallTextures(normalBallTextureName, resType); + } }, _initRenderer: function () { diff --git a/extensions/ccui/uiwidgets/UIText.js b/extensions/ccui/uiwidgets/UIText.js index 71aea75741..329ccf508e 100644 --- a/extensions/ccui/uiwidgets/UIText.js +++ b/extensions/ccui/uiwidgets/UIText.js @@ -69,30 +69,13 @@ ccui.Text = ccui.Widget.extend(/** @lends ccui.Text# */{ this._textAreaSize = cc.size(0, 0); ccui.Widget.prototype.ctor.call(this); - fontSize !== undefined && this.init(textContent, fontName, fontSize); - - }, - - /** - * Initializes a ccui.Text. Please do not call this function by yourself, you should pass the parameters to constructor to initialize it. - * @param {String} textContent - * @param {String} fontName - * @param {Number} fontSize - * @returns {boolean} - * @override - */ - init: function (textContent, fontName, fontSize) { - if (ccui.Widget.prototype.init.call(this)) { - if(arguments.length > 0){ - this.setFontName(fontName); - this.setFontSize(fontSize); - this.setString(textContent); - }else{ - this.setFontName(this._fontName); - } - return true; + if (fontSize !== undefined) { + this.setFontName(fontName); + this.setFontSize(fontSize); + this.setString(textContent); + } else { + this.setFontName(this._fontName); } - return false; }, _initRenderer: function () { diff --git a/extensions/ccui/uiwidgets/UITextAtlas.js b/extensions/ccui/uiwidgets/UITextAtlas.js index bb7e0d27ca..b13b34d67e 100644 --- a/extensions/ccui/uiwidgets/UITextAtlas.js +++ b/extensions/ccui/uiwidgets/UITextAtlas.js @@ -54,7 +54,9 @@ ccui.TextAtlas = ccui.Widget.extend(/** @lends ccui.TextAtlas# */{ */ ctor: function (stringValue, charMapFile, itemWidth, itemHeight, startCharMap) { ccui.Widget.prototype.ctor.call(this); - startCharMap !== undefined && this.setProperty(stringValue, charMapFile, itemWidth, itemHeight, startCharMap); + if (startCharMap !== undefined) { + this.setProperty(stringValue, charMapFile, itemWidth, itemHeight, startCharMap); + } }, _initRenderer: function () { diff --git a/extensions/ccui/uiwidgets/UITextBMFont.js b/extensions/ccui/uiwidgets/UITextBMFont.js index e77f043765..7da5ceab63 100644 --- a/extensions/ccui/uiwidgets/UITextBMFont.js +++ b/extensions/ccui/uiwidgets/UITextBMFont.js @@ -50,7 +50,7 @@ ccui.LabelBMFont = ccui.TextBMFont = ccui.Widget.extend(/** @lends ccui.TextBMFo ctor: function (text, filename) { ccui.Widget.prototype.ctor.call(this); - if(filename != undefined){ + if (filename !== undefined) { this.setFntFile(filename); this.setString(text); } diff --git a/extensions/ccui/uiwidgets/UITextField.js b/extensions/ccui/uiwidgets/UITextField.js index 8fd34e0173..7d59983238 100644 --- a/extensions/ccui/uiwidgets/UITextField.js +++ b/extensions/ccui/uiwidgets/UITextField.js @@ -252,6 +252,7 @@ ccui.TextField = ccui.Widget.extend(/** @lends ccui.TextField# */{ */ ctor: function (placeholder, fontName, fontSize) { ccui.Widget.prototype.ctor.call(this); + this.setTouchEnabled(true); if (fontName) this.setFontName(fontName); if (fontSize) @@ -260,19 +261,6 @@ ccui.TextField = ccui.Widget.extend(/** @lends ccui.TextField# */{ this.setPlaceHolder(placeholder); }, - /** - * Initializes a ccui.TextField. Please do not call this function by yourself, you should pass the parameters to constructor to initialize it. - * @returns {boolean} - * @override - */ - init: function(){ - if(ccui.Widget.prototype.init.call(this)){ - this.setTouchEnabled(true); - return true; - } - return false; - }, - /** * Calls parent class' onEnter and schedules update function. * @override diff --git a/extensions/ccui/uiwidgets/scroll-widget/UIListView.js b/extensions/ccui/uiwidgets/scroll-widget/UIListView.js index c69608370f..68ee424783 100644 --- a/extensions/ccui/uiwidgets/scroll-widget/UIListView.js +++ b/extensions/ccui/uiwidgets/scroll-widget/UIListView.js @@ -66,21 +66,7 @@ ccui.ListView = ccui.ScrollView.extend(/** @lends ccui.ListView# */{ ccui.ScrollView.prototype.ctor.call(this); this._gravity = ccui.ListView.GRAVITY_CENTER_VERTICAL; this.setTouchEnabled(true); - - this.init(); - }, - - /** - * Initializes a ccui.ListView. Please do not call this function by yourself, you should pass the parameters to constructor to initialize it. - * @returns {boolean} - * @override - */ - init: function () { - if (ccui.ScrollView.prototype.init.call(this)) { - this.setDirection(ccui.ScrollView.DIR_VERTICAL); - return true; - } - return false; + this.setDirection(ccui.ScrollView.DIR_VERTICAL); }, /** diff --git a/extensions/ccui/uiwidgets/scroll-widget/UIPageView.js b/extensions/ccui/uiwidgets/scroll-widget/UIPageView.js index ee73541bca..1a4be2fc29 100644 --- a/extensions/ccui/uiwidgets/scroll-widget/UIPageView.js +++ b/extensions/ccui/uiwidgets/scroll-widget/UIPageView.js @@ -56,21 +56,10 @@ ccui.PageView = ccui.ListView.extend(/** @lends ccui.PageView# */{ this._indicatorPositionAsAnchorPoint = cc.p(0.5, 0.1); this._pageViewEventListener = null; this._pageViewEventSelector = null; - }, - - /** - * Initializes a ccui.PageView. Please do not call this function by yourself, you should pass the parameters to constructor to initialize it. - * @returns {boolean} - */ - init: function () { - if (ccui.ListView.prototype.init.call(this)) { - this.setDirection(ccui.ScrollView.DIR_HORIZONTAL); - this.setMagneticType(ccui.ListView.MAGNETIC_CENTER); - this.setScrollBarEnabled(false); - return true; - } - return false; + this.setDirection(ccui.ScrollView.DIR_HORIZONTAL); + this.setMagneticType(ccui.ListView.MAGNETIC_CENTER); + this.setScrollBarEnabled(false); }, /** diff --git a/extensions/ccui/uiwidgets/scroll-widget/UIPageViewIndicator.js b/extensions/ccui/uiwidgets/scroll-widget/UIPageViewIndicator.js index ae1c17364e..863a0743af 100644 --- a/extensions/ccui/uiwidgets/scroll-widget/UIPageViewIndicator.js +++ b/extensions/ccui/uiwidgets/scroll-widget/UIPageViewIndicator.js @@ -48,31 +48,17 @@ ccui.PageViewIndicator = ccui.ProtectedNode.extend(/** @lends ccui.PageViewIndic this._indexNodes = []; this._spaceBetweenIndexNodes = ccui.PageViewIndicator.SPACE_BETWEEN_INDEX_NODES_DEFAULT; - this.init(); + var image = new Image(); + image.src = ccui.PageViewIndicator.CIRCLE_IMAGE; + + this._currentIndexNode = new cc.Sprite(image); + this._currentIndexNode.setVisible(false); + this.addProtectedChild(this._currentIndexNode, 1); // this.setCascadeColorEnabled(true); // this.setCascadeOpacityEnabled(true); }, - /** - * Initializes a ccui.PageViewIndicator. Please do not call this function by yourself, you should pass the parameters to constructor to initialize it. - * @returns {boolean} - */ - init: function () { - if (cc.ProtectedNode.prototype.init.call(this)) { - - var image = new Image(); - image.src = ccui.PageViewIndicator.CIRCLE_IMAGE; - - this._currentIndexNode = new cc.Sprite(image); - this._currentIndexNode.setVisible(false); - this.addProtectedChild(this._currentIndexNode, 1); - - return true; - } - return false; - }, - /** * Sets direction of indicator * @param {ccui.ScrollView.DIR_NONE | ccui.ScrollView.DIR_VERTICAL | ccui.ScrollView.DIR_HORIZONTAL | ccui.ScrollView.DIR_BOTH} direction diff --git a/extensions/ccui/uiwidgets/scroll-widget/UIScrollView.js b/extensions/ccui/uiwidgets/scroll-widget/UIScrollView.js index a0ab5278c7..b298c6922d 100644 --- a/extensions/ccui/uiwidgets/scroll-widget/UIScrollView.js +++ b/extensions/ccui/uiwidgets/scroll-widget/UIScrollView.js @@ -87,6 +87,9 @@ ccui.ScrollView = ccui.Layout.extend(/** @lends ccui.ScrollView# */{ */ ctor: function () { ccui.Layout.prototype.ctor.call(this); + this.setClippingEnabled(true); + this._innerContainer.setTouchEnabled(false); + this._direction = ccui.ScrollView.DIR_NONE; this._childFocusCancelOffset = 5; @@ -101,6 +104,7 @@ ccui.ScrollView = ccui.Layout.extend(/** @lends ccui.ScrollView# */{ this._touchMovePreviousTimestamp = 0; this._scrollBarEnabled = true; + this._initScrollBar(); this.setTouchEnabled(true); }, @@ -111,12 +115,8 @@ ccui.ScrollView = ccui.Layout.extend(/** @lends ccui.ScrollView# */{ */ init: function () { if (ccui.Layout.prototype.init.call(this)) { - this.setClippingEnabled(true); - this._innerContainer.setTouchEnabled(false); - if(this._scrollBarEnabled) - { - this._initScrollBar(); - } + + return true; } return false; diff --git a/extensions/ccui/uiwidgets/scroll-widget/UIScrollViewBar.js b/extensions/ccui/uiwidgets/scroll-widget/UIScrollViewBar.js index 35d782744f..f379c6c3e2 100644 --- a/extensions/ccui/uiwidgets/scroll-widget/UIScrollViewBar.js +++ b/extensions/ccui/uiwidgets/scroll-widget/UIScrollViewBar.js @@ -70,7 +70,7 @@ ccui.ScrollViewBar = ccui.ProtectedNode.extend(/** @lends ccui.ScrollViewBar# */ this.autoHideTime = ccui.ScrollViewBar.DEFAULT_AUTO_HIDE_TIME; this._autoHideEnabled = true; - this.init(); + ccui.ScrollViewBar.prototype.init.call(this); this.setCascadeColorEnabled(true); this.setCascadeOpacityEnabled(true); @@ -81,41 +81,35 @@ ccui.ScrollViewBar = ccui.ProtectedNode.extend(/** @lends ccui.ScrollViewBar# */ * @returns {boolean} */ init: function () { - if (cc.ProtectedNode.prototype.init.call(this)) { + var halfPixelImage = new Image(); + halfPixelImage.src = ccui.ScrollViewBar.HALF_CIRCLE_IMAGE; - var halfPixelImage = new Image(); - halfPixelImage.src = ccui.ScrollViewBar.HALF_CIRCLE_IMAGE; + this._upperHalfCircle = new cc.Sprite(halfPixelImage); + this._upperHalfCircle.setAnchorPoint(cc.p(0.5, 0)); - this._upperHalfCircle = new cc.Sprite(halfPixelImage); - this._upperHalfCircle.setAnchorPoint(cc.p(0.5, 0)); + this._lowerHalfCircle = new cc.Sprite(halfPixelImage); + this._lowerHalfCircle.setAnchorPoint(cc.p(0.5, 0)); + this._lowerHalfCircle.setScaleY(-1); - this._lowerHalfCircle = new cc.Sprite(halfPixelImage); - this._lowerHalfCircle.setAnchorPoint(cc.p(0.5, 0)); - this._lowerHalfCircle.setScaleY(-1); + this.addProtectedChild(this._upperHalfCircle); + this.addProtectedChild(this._lowerHalfCircle); - this.addProtectedChild(this._upperHalfCircle); - this.addProtectedChild(this._lowerHalfCircle); + var bodyImage = new Image(); + bodyImage.src = ccui.ScrollViewBar.BODY_IMAGE_1_PIXEL_HEIGHT; - var bodyImage = new Image(); - bodyImage.src = ccui.ScrollViewBar.BODY_IMAGE_1_PIXEL_HEIGHT; + this._body = new cc.Sprite(bodyImage); + this._body.setAnchorPoint(cc.p(0.5, 0)); + this.addProtectedChild(this._body); - this._body = new cc.Sprite(bodyImage); - this._body.setAnchorPoint(cc.p(0.5, 0)); - this.addProtectedChild(this._body); + this.setColor(ccui.ScrollViewBar.DEFAULT_COLOR); + this.onScrolled(cc.p(0, 0)); + cc.ProtectedNode.prototype.setOpacity.call(this, 0); + this._autoHideRemainingTime = 0; - this.setColor(ccui.ScrollViewBar.DEFAULT_COLOR); - this.onScrolled(cc.p(0, 0)); - cc.ProtectedNode.prototype.setOpacity.call(this, 0); - this._autoHideRemainingTime = 0; - - if(this._direction === ccui.ScrollView.DIR_HORIZONTAL) - { - this.setRotation(90); - } - - return true; + if(this._direction === ccui.ScrollView.DIR_HORIZONTAL) + { + this.setRotation(90); } - return false; }, /** diff --git a/extensions/ccui/uiwidgets/scroll-widget/UIScrollViewWebGLRenderCmd.js b/extensions/ccui/uiwidgets/scroll-widget/UIScrollViewWebGLRenderCmd.js index ef1f2c8e41..25647d0e96 100644 --- a/extensions/ccui/uiwidgets/scroll-widget/UIScrollViewWebGLRenderCmd.js +++ b/extensions/ccui/uiwidgets/scroll-widget/UIScrollViewWebGLRenderCmd.js @@ -29,13 +29,15 @@ }; proto.rendering = function(ctx){ - var currentID = this._node.__instanceId; - var locCmds = cc.renderer._cacheToBufferCmds[currentID], - i, - len; - var context = ctx || cc._renderContext; + var currentID = this._node.__instanceId, + locCmds = cc.renderer._cacheToBufferCmds[currentID], + i, len, checkNode, + context = ctx || cc._renderContext; + if (!locCmds) { + return; + } for (i = 0, len = locCmds.length; i < len; i++) { - var checkNode = locCmds[i]._node; + checkNode = locCmds[i]._node; if(checkNode instanceof ccui.ScrollView) continue; if(checkNode && checkNode._parent && checkNode._parent._inViewRect === false) From c9de2b5c7d1ac63e4cf7aa6f58c50959fee202e2 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Tue, 26 Apr 2016 09:44:03 +0800 Subject: [PATCH 5/5] Limit device pixel ratio at 2, to avoid performance penalty in too high resolution --- cocos2d/core/platform/CCEGLView.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos2d/core/platform/CCEGLView.js b/cocos2d/core/platform/CCEGLView.js index 9f73ed913c..284f6178eb 100644 --- a/cocos2d/core/platform/CCEGLView.js +++ b/cocos2d/core/platform/CCEGLView.js @@ -900,7 +900,7 @@ cc.ContainerStrategy = cc.Class.extend(/** @lends cc.ContainerStrategy# */{ // Setup pixel ratio for retina display var devicePixelRatio = view._devicePixelRatio = 1; if (view.isRetinaEnabled()) - devicePixelRatio = view._devicePixelRatio = window.devicePixelRatio || 1; + devicePixelRatio = view._devicePixelRatio = Math.min(2, window.devicePixelRatio || 1); // Setup canvas locCanvasElement.width = w * devicePixelRatio; locCanvasElement.height = h * devicePixelRatio;