Skip to content

Commit b1a84be

Browse files
committed
Issue cocos2d#2417: Unified management of browser data
1 parent ab54d4c commit b1a84be

File tree

3 files changed

+85
-50
lines changed

3 files changed

+85
-50
lines changed

cocos2d/core/platform/CCEGLView.js

+79-40
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,42 @@ cc.DENSITYDPI_HIGH = "high-dpi";
3535
cc.DENSITYDPI_MEDIUM = "medium-dpi";
3636
cc.DENSITYDPI_LOW = "low-dpi";
3737

38+
cc.__BrowserGetter = {
39+
init: function(){
40+
this.html = document.getElementsByTagName("html")[0];
41+
},
42+
avaWidth: function(frame){
43+
if(!frame || frame === this.html)
44+
return window.innerWidth;
45+
else
46+
return frame.clientWidth;
47+
},
48+
avaHeight: function(frame){
49+
if(!frame || frame === this.html)
50+
return window.innerHeight;
51+
else
52+
return frame.clientHeight;
53+
},
54+
meta: {
55+
"width": "device-width",
56+
"user-scalable": "no"
57+
}
58+
};
59+
60+
switch(cc.sys.browserType){
61+
case cc.sys.BROWSER_TYPE_CHROME:
62+
cc.__BrowserGetter.avaWidth = function(frame){
63+
return frame.clientWidth;
64+
};
65+
cc.__BrowserGetter.avaHeight = function(frame){
66+
return frame.clientHeight;
67+
};
68+
cc.__BrowserGetter.__defineGetter__("target-densitydpi", function(){
69+
return cc.view._targetDensityDPI;
70+
});
71+
break;
72+
}
73+
3874
/**
3975
* cc.view is the singleton object which represents the game window.<br/>
4076
* It's main task include: <br/>
@@ -205,8 +241,8 @@ cc.EGLView = cc.Class.extend(/** @lends cc.view# */{
205241

206242
_initFrameSize: function () {
207243
var locFrameSize = this._frameSize;
208-
locFrameSize.width = this._frame.clientWidth;
209-
locFrameSize.height = this._frame.clientHeight;
244+
locFrameSize.width = cc.__BrowserGetter.avaWidth(this._frame);
245+
locFrameSize.height = cc.__BrowserGetter.avaHeight(this._frame);
210246
},
211247

212248
// hack
@@ -234,14 +270,7 @@ cc.EGLView = cc.Class.extend(/** @lends cc.view# */{
234270
vp.name = "viewport";
235271
vp.content = "";
236272

237-
// For avoiding Android Firefox issue, to remove once firefox fixes its issue.
238-
if (cc.sys.isMobile && cc.sys.browserType == cc.sys.BROWSER_TYPE_FIREFOX) {
239-
viewportMetas = {"width": "device-width", "initial-scale": "1.0"};
240-
}else{
241-
viewportMetas = {"width": "device-width", "user-scalable": "no", "maximum-scale": "1.0", "initial-scale": "1.0"};
242-
}
243-
if(cc.sys.isMobile)
244-
viewportMetas["target-densitydpi"] = this._targetDensityDPI;
273+
viewportMetas = cc.__BrowserGetter.meta;
245274

246275
content = currentVP ? currentVP.content : "";
247276
for (var key in viewportMetas) {
@@ -250,7 +279,7 @@ cc.EGLView = cc.Class.extend(/** @lends cc.view# */{
250279
content += "," + key + "=" + viewportMetas[key];
251280
}
252281
}
253-
if(content != "")
282+
if(/^,/.test(content))
254283
content = content.substr(1);
255284

256285
vp.content = content;
@@ -494,46 +523,56 @@ cc.EGLView = cc.Class.extend(/** @lends cc.view# */{
494523
*/
495524
setDesignResolutionSize: function (width, height, resolutionPolicy) {
496525
// Defensive code
497-
if (isNaN(width) || width == 0 || isNaN(height) || height == 0) {
526+
if( !(width > 0 || height > 0) ){
498527
cc.log(cc._LogInfos.EGLView_setDesignResolutionSize);
499528
return;
500529
}
501-
var _t = this;
502-
_t.setResolutionPolicy(resolutionPolicy);
503-
var policy = _t._resolutionPolicy;
504-
if (policy)
505-
policy.preApply(_t);
506-
else {
530+
531+
this.setResolutionPolicy(resolutionPolicy);
532+
var policy = this._resolutionPolicy;
533+
if (!policy){
507534
cc.log(cc._LogInfos.EGLView_setDesignResolutionSize_2);
508535
return;
509536
}
537+
policy.preApply(this);
510538

511539
// Reinit frame size
512-
if (cc.sys.isMobile)
513-
_t._setViewPortMeta();
514-
_t._initFrameSize();
515-
_t._designResolutionSize = cc.size(width, height);
516-
_t._originalDesignResolutionSize = cc.size(width, height);
540+
if(cc.sys.isMobile)
541+
this._setViewPortMeta();
542+
543+
this._initFrameSize();
544+
545+
this._originalDesignResolutionSize.width = this._designResolutionSize.width = width;
546+
this._originalDesignResolutionSize.height = this._designResolutionSize.height = height;
547+
548+
var result = policy.apply(this, this._designResolutionSize);
517549

518-
var result = policy.apply(_t, _t._designResolutionSize);
519-
if (result.scale && result.scale.length == 2) {
520-
_t._scaleX = result.scale[0];
521-
_t._scaleY = result.scale[1];
550+
if(result.scale && result.scale.length == 2){
551+
this._scaleX = result.scale[0];
552+
this._scaleY = result.scale[1];
522553
}
523-
if (result.viewport) {
524-
var vp = _t._viewPortRect = result.viewport, visible = _t._visibleRect;
525-
visible.width = cc._canvas.width / _t._scaleX;
526-
visible.height = cc._canvas.height / _t._scaleY;
527-
visible.x = -vp.x/2 / _t._scaleX;
528-
visible.y = -vp.y/2 / _t._scaleY;
554+
555+
if(result.viewport){
556+
var vp = this._viewPortRect,
557+
vb = this._visibleRect,
558+
rv = result.viewport;
559+
560+
vp.x = rv.x;
561+
vp.y = rv.y;
562+
vp.width = rv.width;
563+
vp.height = rv.height;
564+
565+
vb.x = -vp.x / this._scaleX;
566+
vb.y = -vp.y / this._scaleY;
567+
vb.width = cc._canvas.width / this._scaleX;
568+
vb.height = cc._canvas.height / this._scaleY;
529569
}
530570

531571
// reset director's member variables to fit visible rect
532572
var director = cc.director;
533-
director._winSizeInPoints.width = _t._designResolutionSize.width;
534-
director._winSizeInPoints.height = _t._designResolutionSize.height;
535-
536-
policy.postApply(_t);
573+
director._winSizeInPoints.width = this._designResolutionSize.width;
574+
director._winSizeInPoints.height = this._designResolutionSize.height;
575+
policy.postApply(this);
537576
cc.winSize.width = director._winSizeInPoints.width;
538577
cc.winSize.height = director._winSizeInPoints.height;
539578

@@ -543,12 +582,12 @@ cc.EGLView = cc.Class.extend(/** @lends cc.view# */{
543582
director.setGLDefaultValues();
544583
}
545584

546-
_t._originalScaleX = _t._scaleX;
547-
_t._originalScaleY = _t._scaleY;
585+
this._originalScaleX = this._scaleX;
586+
this._originalScaleY = this._scaleY;
548587
// For editbox
549588
if (cc.DOM)
550589
cc.DOM._resetEGLViewDiv();
551-
cc.visibleRect && cc.visibleRect.init(_t._visibleRect);
590+
cc.visibleRect && cc.visibleRect.init(this._visibleRect);
552591
},
553592

554593
/**

cocos2d/core/platform/CCVisibleRect.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,13 @@ cc.visibleRect = {
6161
* @param {cc.Rect} visibleRect
6262
*/
6363
init:function(visibleRect){
64-
var view = cc.EGLView;
65-
var scaleX = view.getScaleX ? view.getScaleX() : 1;
66-
var scaleY = view.getScaleY ? view.getScaleY() : 1;
6764

68-
var w = this.width = visibleRect.width / scaleX;
69-
var h = this.height = visibleRect.height / scaleY;
70-
var l = visibleRect.x / scaleX,
71-
b = visibleRect.y / scaleY,
72-
t = (b + h) / scaleY,
73-
r = (l + w) / scaleX;
65+
var w = this.width = visibleRect.width;
66+
var h = this.height = visibleRect.height;
67+
var l = visibleRect.x,
68+
b = visibleRect.y,
69+
t = b + h,
70+
r = l + w;
7471

7572
//top
7673
this.topLeft.x = l;

cocos2d/core/scenes/CCLoaderScene.js

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ cc.LoaderScene = cc.Scene.extend({
4747

4848
// bg
4949
var bgLayer = self._bgLayer = new cc.LayerColor(cc.color(32, 32, 32, 255));
50-
bgLayer.setPosition(cc.visibleRect.bottomLeft);
5150
self.addChild(bgLayer, 0);
5251

5352
//image move to CCSceneFile.js

0 commit comments

Comments
 (0)