Skip to content

Commit 1d856ef

Browse files
author
pandamicro
committed
Improve screen adaptation logic
1 parent 6ed695c commit 1d856ef

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

cocos2d/core/platform/CCEGLView.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,14 @@ cc.EGLView = cc.Class.extend(/** @lends cc.view# */{
217217
return;
218218

219219
// Frame size changed, do resize works
220-
if (view._resizeCallback) {
221-
view._resizeCallback.call();
222-
}
223220
var width = view._originalDesignResolutionSize.width;
224221
var height = view._originalDesignResolutionSize.height;
225-
if (width > 0) {
222+
if (width > 0)
226223
view.setDesignResolutionSize(width, height, view._resolutionPolicy);
224+
225+
cc.eventManager.dispatchCustomEvent('canvas-resize');
226+
if (view._resizeCallback) {
227+
view._resizeCallback.call();
227228
}
228229
},
229230

@@ -287,7 +288,7 @@ cc.EGLView = cc.Class.extend(/** @lends cc.view# */{
287288
* @param {Function|null} callback The callback function
288289
*/
289290
setResizeCallback: function (callback) {
290-
if (cc.isFunction(callback) || callback == null) {
291+
if (typeof callback === 'function' || callback == null) {
291292
this._resizeCallback = callback;
292293
}
293294
},
@@ -304,6 +305,9 @@ cc.EGLView = cc.Class.extend(/** @lends cc.view# */{
304305
orientation = orientation & cc.ORIENTATION_AUTO;
305306
if (orientation) {
306307
this._orientation = orientation;
308+
var designWidth = this._originalDesignResolutionSize.width;
309+
var designHeight = this._originalDesignResolutionSize.height;
310+
this.setDesignResolutionSize(designWidth, designHeight, this._resolutionPolicy);
307311
}
308312
},
309313

@@ -428,7 +432,7 @@ cc.EGLView = cc.Class.extend(/** @lends cc.view# */{
428432
* @param {Boolean} enabled Enable or disable retina display
429433
*/
430434
enableRetina: function(enabled) {
431-
this._retinaEnabled = enabled ? true : false;
435+
this._retinaEnabled = !!enabled;
432436
},
433437

434438
/**
@@ -672,6 +676,8 @@ cc.EGLView = cc.Class.extend(/** @lends cc.view# */{
672676
if(cc.sys.isMobile)
673677
this._adjustViewportMeta();
674678

679+
// Permit to re-detect the orientation of device.
680+
this._orientationChanging = true;
675681
this._initFrameSize();
676682

677683
this._originalDesignResolutionSize.width = this._designResolutionSize.width = width;
@@ -713,6 +719,9 @@ cc.EGLView = cc.Class.extend(/** @lends cc.view# */{
713719
// reset director's member variables to fit visible rect
714720
director.setGLDefaultValues();
715721
}
722+
else if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
723+
cc.renderer._allNeedDraw = true;
724+
}
716725

717726
this._originalScaleX = this._scaleX;
718727
this._originalScaleY = this._scaleY;
@@ -747,7 +756,7 @@ cc.EGLView = cc.Class.extend(/** @lends cc.view# */{
747756
*/
748757
setRealPixelResolution: function (width, height, resolutionPolicy) {
749758
// Set viewport's width
750-
this._setViewportMeta({"width": width, "target-densitydpi": cc.DENSITYDPI_DEVICE}, true);
759+
this._setViewportMeta({"width": width}, true);
751760

752761
// Set body width to the exact pixel resolution
753762
document.body.style.width = width + "px";
@@ -871,10 +880,10 @@ cc.EGLView = cc.Class.extend(/** @lends cc.view# */{
871880
return this._isRotated ? {x: this._viewPortRect.width - y, y: x} : {x: x, y: y};
872881
},
873882

874-
_convertMouseToLocationInView: function(point, relatedPos) {
875-
var locViewPortRect = this._viewPortRect, _t = this;
876-
point.x = ((_t._devicePixelRatio * (point.x - relatedPos.left)) - locViewPortRect.x) / _t._scaleX;
877-
point.y = (_t._devicePixelRatio * (relatedPos.top + relatedPos.height - point.y) - locViewPortRect.y) / _t._scaleY;
883+
_convertMouseToLocationInView: function (point, relatedPos) {
884+
var viewport = this._viewPortRect, _t = this;
885+
point.x = ((_t._devicePixelRatio * (point.x - relatedPos.left)) - viewport.x) / _t._scaleX;
886+
point.y = (_t._devicePixelRatio * (relatedPos.top + relatedPos.height - point.y) - viewport.y) / _t._scaleY;
878887
},
879888

880889
_convertPointWithScale: function (point) {
@@ -945,6 +954,10 @@ cc.ContainerStrategy = cc.Class.extend(/** @lends cc.ContainerStrategy# */{
945954

946955
_setupContainer: function (view, w, h) {
947956
var locCanvas = cc.game.canvas, locContainer = cc.game.container;
957+
if (cc.sys.os === cc.sys.OS_ANDROID) {
958+
document.body.style.width = (view._isRotated ? h : w) + 'px';
959+
document.body.style.height = (view._isRotated ? w : h) + 'px';
960+
}
948961

949962
// Setup style
950963
locContainer.style.width = locCanvas.style.width = w + 'px';

0 commit comments

Comments
 (0)