Skip to content

Commit eea7a86

Browse files
committed
Merge pull request #3271 from pandamicro/autobatch
Issue fixes and improvements
2 parents df32063 + c9de2b5 commit eea7a86

25 files changed

+210
-394
lines changed

Diff for: CCBoot.js

+70-17
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ cc.loader = (function () {
586586
_register = {}, //register of loaders
587587
_langPathCache = {}, //cache for lang path
588588
_aliases = {}, //aliases for res url
589+
_queue = {}, // Callback queue for resources already loading
589590
_urlRegExp = new RegExp(
590591
"^" +
591592
// protocol identifier
@@ -668,6 +669,10 @@ cc.loader = (function () {
668669
return results;
669670
},
670671

672+
isLoading: function (url) {
673+
return (_queue[url] !== undefined);
674+
},
675+
671676
/**
672677
* Load js files.
673678
* If the third parameter doesn't exist, then the baseDir turns to be "".
@@ -890,6 +895,12 @@ cc.loader = (function () {
890895
return img;
891896
}
892897

898+
var queue = _queue[url];
899+
if (queue) {
900+
queue.callbacks.push(callback);
901+
return queue.img;
902+
}
903+
893904
img = new Image();
894905
if (opt.isCrossOrigin && location.origin !== "file://")
895906
img.crossOrigin = "Anonymous";
@@ -898,24 +909,53 @@ cc.loader = (function () {
898909
this.removeEventListener('load', loadCallback, false);
899910
this.removeEventListener('error', errorCallback, false);
900911

901-
cc.loader.cache[url] = img;
902-
if (callback)
903-
callback(null, img);
912+
if (!_urlRegExp.test(url)) {
913+
cc.loader.cache[url] = img;
914+
}
915+
916+
var queue = _queue[url];
917+
if (queue) {
918+
callbacks = queue.callbacks;
919+
for (var i = 0; i < callbacks.length; ++i) {
920+
var callback = callbacks[i];
921+
if (callback) {
922+
callback(null, img);
923+
}
924+
}
925+
queue.img = null;
926+
delete _queue[url];
927+
}
904928
};
905929

906930
var self = this;
907931
var errorCallback = function () {
908932
this.removeEventListener('error', errorCallback, false);
909933

910-
if(img.crossOrigin && img.crossOrigin.toLowerCase() === "anonymous"){
934+
if (img.crossOrigin && img.crossOrigin.toLowerCase() === "anonymous") {
911935
opt.isCrossOrigin = false;
912936
self.release(url);
913937
cc.loader.loadImg(url, opt, callback);
914-
}else{
915-
typeof callback === "function" && callback("load image failed");
938+
} else {
939+
var queue = _queue[url];
940+
if (queue) {
941+
callbacks = queue.callbacks;
942+
for (var i = 0; i < callbacks.length; ++i) {
943+
var callback = callbacks[i];
944+
if (callback) {
945+
callback("load image failed");
946+
}
947+
}
948+
queue.img = null;
949+
delete _queue[url];
950+
}
916951
}
917952
};
918953

954+
_queue[url] = {
955+
img: img,
956+
callbacks: callback ? [callback] : []
957+
};
958+
919959
img.addEventListener("load", loadCallback);
920960
img.addEventListener("error", errorCallback);
921961
img.src = url;
@@ -1803,25 +1843,38 @@ var _initSys = function () {
18031843

18041844
var _supportCanvas = !!_tmpCanvas1.getContext("2d");
18051845
var _supportWebGL = false;
1806-
var tmpCanvas = document.createElement("CANVAS");
18071846
if (win.WebGLRenderingContext) {
1847+
var tmpCanvas = document.createElement("CANVAS");
18081848
try{
18091849
var context = cc.create3DContext(tmpCanvas, {'stencil': true, 'preserveDrawingBuffer': true });
18101850
if(context) {
18111851
_supportWebGL = true;
18121852
}
18131853

1814-
// Accept only Android 5+ default browser and QQ Browser 6.2+
18151854
if (_supportWebGL && sys.os === sys.OS_ANDROID) {
1816-
_supportWebGL = false;
1817-
// QQ Brwoser 6.2+
1818-
var browserVer = parseFloat(sys.browserVersion);
1819-
if (sys.browserType === sys.BROWSER_TYPE_MOBILE_QQ && browserVer >= 6.2) {
1820-
_supportWebGL = true;
1821-
}
1822-
// Android 5+ default browser
1823-
else if (sys.osMainVersion && sys.osMainVersion >= 5 && sys.browserType === sys.BROWSER_TYPE_ANDROID) {
1824-
_supportWebGL = true;
1855+
switch (sys.browserType) {
1856+
case sys.BROWSER_TYPE_MOBILE_QQ:
1857+
case sys.BROWSER_TYPE_BAIDU:
1858+
case sys.BROWSER_TYPE_BAIDU_APP:
1859+
// QQ & Baidu Brwoser 6.2+ (using blink kernel)
1860+
var browserVer = parseFloat(sys.browserVersion);
1861+
if (browserVer >= 6.2) {
1862+
_supportWebGL = true;
1863+
}
1864+
else {
1865+
_supportWebGL = false;
1866+
}
1867+
break;
1868+
case sys.BROWSER_TYPE_ANDROID:
1869+
// Android 5+ default browser
1870+
if (sys.osMainVersion && sys.osMainVersion >= 5) {
1871+
_supportWebGL = true;
1872+
}
1873+
break;
1874+
case sys.BROWSER_TYPE_UNKNOWN:
1875+
case sys.BROWSER_TYPE_360:
1876+
case sys.BROWSER_TYPE_MIUI:
1877+
_supportWebGL = false;
18251878
}
18261879
}
18271880
}

Diff for: cocos2d/core/CCDirector.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,6 @@ cc.Director = cc.Class.extend(/** @lends cc.Director# */{
218218
cc.eventManager.dispatchEvent(this._eventAfterUpdate);
219219
}
220220

221-
renderer.clear();
222-
223221
/* to avoid flickr, nextScene MUST be here: after tick and before draw.
224222
XXX: Which bug is this one. It seems that it can't be reproduced with v0.9 */
225223
if (this._nextScene) {
@@ -230,15 +228,25 @@ cc.Director = cc.Class.extend(/** @lends cc.Director# */{
230228
this._beforeVisitScene();
231229

232230
// draw the scene
231+
var skipRendering = false;
233232
if (this._runningScene) {
234233
if (renderer.childrenOrderDirty === true) {
235234
cc.renderer.clearRenderCommands();
236235
cc.renderer.assignedZ = 0;
237236
this._runningScene._renderCmd._curLevel = 0; //level start from 0;
238237
this._runningScene.visit();
239238
renderer.resetFlag();
240-
} else if (renderer.transformDirty() === true)
239+
}
240+
else if (renderer.transformDirty() === true) {
241241
renderer.transform();
242+
}
243+
else {
244+
skipRendering = true;
245+
}
246+
}
247+
248+
if (!skipRendering) {
249+
renderer.clear();
242250
}
243251

244252
// draw the notifications node
@@ -251,7 +259,9 @@ cc.Director = cc.Class.extend(/** @lends cc.Director# */{
251259
if (this._afterVisitScene)
252260
this._afterVisitScene();
253261

254-
renderer.rendering(cc._renderContext);
262+
if (!skipRendering) {
263+
renderer.rendering(cc._renderContext);
264+
}
255265
this._totalFrames++;
256266

257267
cc.eventManager.dispatchEvent(this._eventAfterDraw);

Diff for: cocos2d/core/platform/CCEGLView.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ cc.ContainerStrategy = cc.Class.extend(/** @lends cc.ContainerStrategy# */{
900900
// Setup pixel ratio for retina display
901901
var devicePixelRatio = view._devicePixelRatio = 1;
902902
if (view.isRetinaEnabled())
903-
devicePixelRatio = view._devicePixelRatio = window.devicePixelRatio || 1;
903+
devicePixelRatio = view._devicePixelRatio = Math.min(2, window.devicePixelRatio || 1);
904904
// Setup canvas
905905
locCanvasElement.width = w * devicePixelRatio;
906906
locCanvasElement.height = h * devicePixelRatio;

Diff for: extensions/ccui/base-classes/UIScale9SpriteCanvasRenderCmd.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
this._cacheScale9Sprite();
121121
this._dirtyFlag = this._dirtyFlag & flags.cacheDirty ^ this._dirtyFlag;
122122
}
123-
}
123+
};
124124

125125
proto._cacheScale9Sprite = function() {
126126
var node = this._node;

Diff for: extensions/ccui/base-classes/UIWidget.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
174174
this._positionPercent = cc.p(0, 0);
175175
this._nodes = [];
176176
this._layoutParameterType = ccui.LayoutParameter.NONE;
177-
this.init(); //TODO
177+
ccui.Widget.prototype.init.call(this);
178178
},
179179

180180
/**

Diff for: extensions/ccui/layouts/UIHBox.js

+3-28
Original file line numberDiff line numberDiff line change
@@ -35,37 +35,12 @@ ccui.HBox = ccui.Layout.extend(/** @lends ccui.HBox# */{
3535
* @param {cc.Size} [size]
3636
*/
3737
ctor: function(size){
38-
ccui.Layout.prototype.ctor.call(this, size);
39-
if(size !== undefined)
40-
this.initWithSize(size);
41-
else
42-
this.init();
43-
},
38+
ccui.Layout.prototype.ctor.call(this);
39+
this.setLayoutType(ccui.Layout.LINEAR_HORIZONTAL);
4440

45-
/**
46-
* Initialize a HBox. please do not call this function by yourself, you should pass the parameters to constructor to initialize it.
47-
* @override
48-
* @returns {boolean}
49-
*/
50-
init: function(){
51-
if(ccui.Layout.prototype.init.call(this)){
52-
this.setLayoutType(ccui.Layout.LINEAR_HORIZONTAL);
53-
return true;
54-
}
55-
return false;
56-
},
57-
58-
/**
59-
* Initializes a HBox with size.
60-
* @param size
61-
* @returns {boolean}
62-
*/
63-
initWithSize: function(size){
64-
if(this.init()){
41+
if(size) {
6542
this.setContentSize(size);
66-
return true;
6743
}
68-
return false;
6944
}
7045
});
7146

Diff for: extensions/ccui/layouts/UILayout.js

+11-23
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,16 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{
8181
ctor: function () {
8282
this._layoutType = ccui.Layout.ABSOLUTE;
8383
this._widgetType = ccui.Widget.TYPE_CONTAINER;
84-
this._clippingType = ccui.Layout.CLIPPING_STENCIL;
84+
this._clippingType = ccui.Layout.CLIPPING_SCISSOR;
8585
this._colorType = ccui.Layout.BG_COLOR_NONE;
8686

8787
ccui.Widget.prototype.ctor.call(this);
88+
89+
this.ignoreContentAdaptWithSize(false);
90+
this.setContentSize(cc.size(0, 0));
91+
this.setAnchorPoint(0, 0);
92+
this.onPassFocusToChild = this._findNearestChildWidgetIndex.bind(this);
93+
8894
this._backGroundImageCapInsets = cc.rect(0, 0, 0, 0);
8995

9096
this._color = cc.color(255, 255, 255, 255);
@@ -236,22 +242,6 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{
236242
*/
237243
onPassFocusToChild: null,
238244

239-
/**
240-
* override "init" method of widget. please do not call this function by yourself, you should pass the parameters to constructor to initialize it.
241-
* @returns {boolean}
242-
* @override
243-
*/
244-
init: function () {
245-
if (ccui.Widget.prototype.init.call(this)) {
246-
this.ignoreContentAdaptWithSize(false);
247-
this.setContentSize(cc.size(0, 0));
248-
this.setAnchorPoint(0, 0);
249-
this.onPassFocusToChild = this._findNearestChildWidgetIndex.bind(this);
250-
return true;
251-
}
252-
return false;
253-
},
254-
255245
/**
256246
* Adds a widget to the container.
257247
* @param {ccui.Widget} widget
@@ -330,8 +320,9 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{
330320
default:
331321
break;
332322
}
333-
} else
323+
} else {
334324
ccui.Widget.prototype.visit.call(this, parentCmd);
325+
}
335326
},
336327

337328
/**
@@ -344,6 +335,7 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{
344335
return;
345336
this._clippingEnabled = able;
346337
switch (this._clippingType) {
338+
case ccui.Layout.CLIPPING_SCISSOR:
347339
case ccui.Layout.CLIPPING_STENCIL:
348340
if (able){
349341
this._clippingStencil = new cc.DrawNode();
@@ -369,10 +361,6 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{
369361
setClippingType: function (type) {
370362
if (type === this._clippingType)
371363
return;
372-
if(cc._renderType === cc.game.RENDER_TYPE_CANVAS && type === ccui.Layout.CLIPPING_SCISSOR){
373-
cc.log("Only supports STENCIL on canvas mode.");
374-
return;
375-
}
376364
var clippingEnabled = this.isClippingEnabled();
377365
this.setClippingEnabled(false);
378366
this._clippingType = type;
@@ -388,7 +376,7 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{
388376
},
389377

390378
_setStencilClippingSize: function (size) {
391-
if (this._clippingEnabled && this._clippingType === ccui.Layout.CLIPPING_STENCIL) {
379+
if (this._clippingEnabled) {
392380
var rect = [];
393381
rect[0] = cc.p(0, 0);
394382
rect[1] = cc.p(size.width, 0);

Diff for: extensions/ccui/layouts/UILayoutCanvasRenderCmd.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@
5757
default:
5858
break;
5959
}
60-
} else
60+
} else {
6161
ccui.Widget.CanvasRenderCmd.prototype.visit.call(this, parentCmd);
62+
}
6263
};
6364

6465
proto._onRenderSaveCmd = function(ctx, scaleX, scaleY){
@@ -103,7 +104,7 @@
103104
context.globalCompositeOperation = "destination-over";
104105
context.drawImage(this._locCache, 0, 0);
105106
context.restore();
106-
}else{
107+
} else {
107108
wrapper.restore(); //use for restore clip operation
108109
}
109110
};
@@ -126,7 +127,7 @@
126127
}
127128
};
128129

129-
proto.stencilClippingVisit = proto.scissorClippingVisit = function(parentCmd){
130+
proto.stencilClippingVisit = proto.scissorClippingVisit = function (parentCmd) {
130131
var node = this._node;
131132
if (!node._clippingStencil || !node._clippingStencil.isVisible())
132133
return;

0 commit comments

Comments
 (0)