From 4ebe2bc450c99d7c1c9849d1b81172f94539a5c2 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Mon, 16 Jan 2017 16:03:32 +0800 Subject: [PATCH 1/3] fix editbox landscape issue --- cocos2d/core/editbox/CCSGEditBox.js | 75 +++++++++++++++++++++++------ cocos2d/core/platform/CCView.js | 3 +- 2 files changed, 62 insertions(+), 16 deletions(-) diff --git a/cocos2d/core/editbox/CCSGEditBox.js b/cocos2d/core/editbox/CCSGEditBox.js index 447ec2cc0c..d8719d4c09 100644 --- a/cocos2d/core/editbox/CCSGEditBox.js +++ b/cocos2d/core/editbox/CCSGEditBox.js @@ -31,23 +31,27 @@ var SCROLLY = 40; var TIMER_NAME = 400; var LEFT_PADDING = 2; +function adjustEditBoxPosition (editBox) { + var worldPos = editBox.convertToWorldSpace(cc.p(0,0)); + var windowHeight = cc.visibleRect.height; + var windowWidth = cc.visibleRect.width; + var factor = 0.5; + if(windowWidth > windowHeight) { + factor = 0.7; + } + setTimeout(function() { + if(window.scrollY < SCROLLY && worldPos.y < windowHeight * factor) { + var scrollOffset = windowHeight * factor - worldPos.y - window.scrollY; + if (scrollOffset < 35) scrollOffset = 35; + if (scrollOffset > 320) scrollOffset = 320; + window.scrollTo(scrollOffset, scrollOffset); + } + }, TIMER_NAME); +} + function scrollWindowUp(editBox) { if (cc.sys.os === cc.sys.OS_IOS && cc.sys.osMainVersion === 9) { - var worldPos = editBox.convertToWorldSpace(cc.p(0,0)); - var windowHeight = cc.visibleRect.height; - var windowWidth = cc.visibleRect.width; - var factor = 0.5; - if(windowWidth > windowHeight) { - factor = 0.7; - } - setTimeout(function() { - if(window.scrollY < SCROLLY && worldPos.y < windowHeight * factor) { - var scrollOffset = windowHeight * factor - worldPos.y - window.scrollY; - if (scrollOffset < 35) scrollOffset = 35; - if (scrollOffset > 320) scrollOffset = 320; - window.scrollTo(0, scrollOffset); - } - }, TIMER_NAME); + adjustEditBoxPosition(editBox); } } @@ -684,6 +688,28 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; this.style.color = cc.colorToHex(editBox._textColor); thisPointer._hiddenLabels(); + if (cc.sys.isMobile) { + if (cc.view._isRotated) { + cc.container.style['-webkit-transform'] = 'rotate(0deg)'; + cc.container.style.transform = 'rotate(0deg)'; + cc.view._isRotated = false; + var policy = cc.view.getResolutionPolicy(); + policy.apply(cc.view, cc.view.getDesignResolutionSize()); + cc.view._isRotated = true; + //use window scrollTo to adjust the input area + window.scrollTo(100, 100); + thisPointer.__rotateScreen = true; + } else { + thisPointer.__rotateScreen = false; + } + } + + this.__orientationChanged = function () { + adjustEditBoxPosition(editBox); + }; + + window.addEventListener('orientationchange', this.__orientationChanged); + if(cc.view.isAutoFullScreenEnabled()) { thisPointer.__fullscreen = true; cc.view.enableAutoFullScreen(false); @@ -694,6 +720,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; thisPointer.__autoResize = cc.view.__resizeWithBrowserSize; cc.view.resizeWithBrowserSize(false); + scrollWindowUp(editBox); if (editBox._delegate && editBox._delegate.editBoxEditingDidBegan) { @@ -704,6 +731,23 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; var editBox = thisPointer._editBox; editBox._text = this.value; thisPointer._updateEditBoxContentStyle(); + + if (cc.sys.isMobile) { + if (thisPointer.__rotateScreen) { + cc.container.style['-webkit-transform'] = 'rotate(90deg)'; + cc.container.style.transform = 'rotate(90deg)'; + + var view = cc.view; + var width = view._originalDesignResolutionSize.width; + var height = view._originalDesignResolutionSize.height; + if (width > 0) + view.setDesignResolutionSize(width, height, view._resolutionPolicy); + thisPointer.__rotateScreen = false; + } + } + + window.removeEventListener('orientationchange', this.__orientationChanged); + if(thisPointer.__fullscreen) { cc.view.enableAutoFullScreen(true); } @@ -711,6 +755,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; cc.view.resizeWithBrowserSize(true); } window.scrollY = 0; + if (editBox._delegate && editBox._delegate.editBoxEditingDidEnded) { editBox._delegate.editBoxEditingDidEnded(editBox); } diff --git a/cocos2d/core/platform/CCView.js b/cocos2d/core/platform/CCView.js index 7285c7220d..ea5ce9a381 100644 --- a/cocos2d/core/platform/CCView.js +++ b/cocos2d/core/platform/CCView.js @@ -66,6 +66,7 @@ switch(__BrowserGetter.adaptationType){ __BrowserGetter.__defineGetter__("target-densitydpi", function(){ return cc.view._targetDensityDPI; }); + break; case cc.sys.BROWSER_TYPE_SOUGOU: case cc.sys.BROWSER_TYPE_UC: __BrowserGetter.availWidth = function(frame){ @@ -524,7 +525,7 @@ var View = cc._Class.extend({ */ setFrameZoomFactor: function (zoomFactor) { this._frameZoomFactor = zoomFactor; - this.centerWindow(); + // this.centerWindow(); cc.director.setProjection(cc.director.getProjection()); }, From c2c4657e202ea94bc415aaefeb6fc406e8c646b3 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Tue, 17 Jan 2017 11:35:55 +0800 Subject: [PATCH 2/3] fix editbox landscape mode input issue --- cocos2d/core/editbox/CCSGEditBox.js | 46 +++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/editbox/CCSGEditBox.js b/cocos2d/core/editbox/CCSGEditBox.js index d8719d4c09..70ba6bb573 100644 --- a/cocos2d/core/editbox/CCSGEditBox.js +++ b/cocos2d/core/editbox/CCSGEditBox.js @@ -697,7 +697,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; policy.apply(cc.view, cc.view.getDesignResolutionSize()); cc.view._isRotated = true; //use window scrollTo to adjust the input area - window.scrollTo(100, 100); + window.scrollTo(35, 35); thisPointer.__rotateScreen = true; } else { thisPointer.__rotateScreen = false; @@ -740,8 +740,9 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; var view = cc.view; var width = view._originalDesignResolutionSize.width; var height = view._originalDesignResolutionSize.height; - if (width > 0) + if (width > 0) { view.setDesignResolutionSize(width, height, view._resolutionPolicy); + } thisPointer.__rotateScreen = false; } } @@ -813,6 +814,29 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; this.style.fontSize = thisPointer._edFontSize + 'px'; this.style.color = cc.colorToHex(editBox._textColor); + + if (cc.sys.isMobile) { + if (cc.view._isRotated) { + cc.container.style['-webkit-transform'] = 'rotate(0deg)'; + cc.container.style.transform = 'rotate(0deg)'; + cc.view._isRotated = false; + var policy = cc.view.getResolutionPolicy(); + policy.apply(cc.view, cc.view.getDesignResolutionSize()); + cc.view._isRotated = true; + //use window scrollTo to adjust the input area + window.scrollTo(35, 35); + thisPointer.__rotateScreen = true; + } else { + thisPointer.__rotateScreen = false; + } + } + + this.__orientationChanged = function () { + adjustEditBoxPosition(editBox); + }; + + window.addEventListener('orientationchange', this.__orientationChanged); + if(cc.view.isAutoFullScreenEnabled()) { thisPointer.__fullscreen = true; cc.view.enableAutoFullScreen(false); @@ -845,6 +869,24 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; var editBox = thisPointer._editBox; editBox._text = this.value; thisPointer._updateEditBoxContentStyle(); + + if (cc.sys.isMobile) { + if (thisPointer.__rotateScreen) { + cc.container.style['-webkit-transform'] = 'rotate(90deg)'; + cc.container.style.transform = 'rotate(90deg)'; + + var view = cc.view; + var width = view._originalDesignResolutionSize.width; + var height = view._originalDesignResolutionSize.height; + if (width > 0) { + view.setDesignResolutionSize(width, height, view._resolutionPolicy); + } + thisPointer.__rotateScreen = false; + } + } + + window.removeEventListener('orientationchange', this.__orientationChanged); + window.scrollY = 0; if(thisPointer.__fullscreen) { cc.view.enableAutoFullScreen(true); From 0a84b7837b345de8d3583e5bf325b051c9605edf Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Tue, 17 Jan 2017 14:57:30 +0800 Subject: [PATCH 3/3] apply DRY --- cocos2d/core/editbox/CCSGEditBox.js | 168 +++++++++++----------------- cocos2d/core/platform/CCView.js | 2 - 2 files changed, 63 insertions(+), 107 deletions(-) diff --git a/cocos2d/core/editbox/CCSGEditBox.js b/cocos2d/core/editbox/CCSGEditBox.js index 70ba6bb573..d910d9a50e 100644 --- a/cocos2d/core/editbox/CCSGEditBox.js +++ b/cocos2d/core/editbox/CCSGEditBox.js @@ -622,6 +622,65 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; } }; + proto._beginEditingOnMobile = function (editBox) { + if (cc.view._isRotated) { + cc.container.style['-webkit-transform'] = 'rotate(0deg)'; + cc.container.style.transform = 'rotate(0deg)'; + cc.view._isRotated = false; + var policy = cc.view.getResolutionPolicy(); + policy.apply(cc.view, cc.view.getDesignResolutionSize()); + cc.view._isRotated = true; + //use window scrollTo to adjust the input area + window.scrollTo(35, 35); + this.__rotateScreen = true; + } else { + this.__rotateScreen = false; + } + + this.__orientationChanged = function () { + adjustEditBoxPosition(editBox); + }; + + window.addEventListener('orientationchange', this.__orientationChanged); + + if(cc.view.isAutoFullScreenEnabled()) { + this.__fullscreen = true; + cc.view.enableAutoFullScreen(false); + cc.screen.exitFullScreen(); + } else { + this.__fullscreen = false; + } + this.__autoResize = cc.view.__resizeWithBrowserSize; + cc.view.resizeWithBrowserSize(false); + + scrollWindowUp(editBox); + }; + + proto._endEditingOnMobile = function () { + if (this.__rotateScreen) { + cc.container.style['-webkit-transform'] = 'rotate(90deg)'; + cc.container.style.transform = 'rotate(90deg)'; + + var view = cc.view; + var width = view._originalDesignResolutionSize.width; + var height = view._originalDesignResolutionSize.height; + if (width > 0) { + view.setDesignResolutionSize(width, height, view._resolutionPolicy); + } + this.__rotateScreen = false; + } + + window.removeEventListener('orientationchange', this.__orientationChanged); + + window.scrollY = 0; + if(this.__fullscreen) { + cc.view.enableAutoFullScreen(true); + } + if (this.__autoResize) { + cc.view.resizeWithBrowserSize(true); + } + }; + proto._createDomInput = function () { @@ -689,39 +748,9 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; thisPointer._hiddenLabels(); if (cc.sys.isMobile) { - if (cc.view._isRotated) { - cc.container.style['-webkit-transform'] = 'rotate(0deg)'; - cc.container.style.transform = 'rotate(0deg)'; - cc.view._isRotated = false; - var policy = cc.view.getResolutionPolicy(); - policy.apply(cc.view, cc.view.getDesignResolutionSize()); - cc.view._isRotated = true; - //use window scrollTo to adjust the input area - window.scrollTo(35, 35); - thisPointer.__rotateScreen = true; - } else { - thisPointer.__rotateScreen = false; - } + thisPointer._beginEditingOnMobile(editBox); } - this.__orientationChanged = function () { - adjustEditBoxPosition(editBox); - }; - - window.addEventListener('orientationchange', this.__orientationChanged); - - if(cc.view.isAutoFullScreenEnabled()) { - thisPointer.__fullscreen = true; - cc.view.enableAutoFullScreen(false); - cc.screen.exitFullScreen(); - } else { - thisPointer.__fullscreen = false; - } - thisPointer.__autoResize = cc.view.__resizeWithBrowserSize; - cc.view.resizeWithBrowserSize(false); - - - scrollWindowUp(editBox); if (editBox._delegate && editBox._delegate.editBoxEditingDidBegan) { editBox._delegate.editBoxEditingDidBegan(editBox); @@ -733,29 +762,9 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; thisPointer._updateEditBoxContentStyle(); if (cc.sys.isMobile) { - if (thisPointer.__rotateScreen) { - cc.container.style['-webkit-transform'] = 'rotate(90deg)'; - cc.container.style.transform = 'rotate(90deg)'; - - var view = cc.view; - var width = view._originalDesignResolutionSize.width; - var height = view._originalDesignResolutionSize.height; - if (width > 0) { - view.setDesignResolutionSize(width, height, view._resolutionPolicy); - } - thisPointer.__rotateScreen = false; - } + thisPointer._endEditingOnMobile(); } - window.removeEventListener('orientationchange', this.__orientationChanged); - - if(thisPointer.__fullscreen) { - cc.view.enableAutoFullScreen(true); - } - if (thisPointer.__autoResize) { - cc.view.resizeWithBrowserSize(true); - } - window.scrollY = 0; if (editBox._delegate && editBox._delegate.editBoxEditingDidEnded) { editBox._delegate.editBoxEditingDidEnded(editBox); @@ -816,39 +825,9 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; this.style.color = cc.colorToHex(editBox._textColor); if (cc.sys.isMobile) { - if (cc.view._isRotated) { - cc.container.style['-webkit-transform'] = 'rotate(0deg)'; - cc.container.style.transform = 'rotate(0deg)'; - cc.view._isRotated = false; - var policy = cc.view.getResolutionPolicy(); - policy.apply(cc.view, cc.view.getDesignResolutionSize()); - cc.view._isRotated = true; - //use window scrollTo to adjust the input area - window.scrollTo(35, 35); - thisPointer.__rotateScreen = true; - } else { - thisPointer.__rotateScreen = false; - } + thisPointer._beginEditingOnMobile(editBox); } - this.__orientationChanged = function () { - adjustEditBoxPosition(editBox); - }; - - window.addEventListener('orientationchange', this.__orientationChanged); - - if(cc.view.isAutoFullScreenEnabled()) { - thisPointer.__fullscreen = true; - cc.view.enableAutoFullScreen(false); - cc.screen.exitFullScreen(); - } else { - thisPointer.__fullscreen = false; - } - thisPointer.__autoResize = cc.view.__resizeWithBrowserSize; - cc.view.resizeWithBrowserSize(false); - - scrollWindowUp(editBox); - if (editBox._delegate && editBox._delegate.editBoxEditingDidBegan) { editBox._delegate.editBoxEditingDidBegan(editBox); } @@ -871,28 +850,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; thisPointer._updateEditBoxContentStyle(); if (cc.sys.isMobile) { - if (thisPointer.__rotateScreen) { - cc.container.style['-webkit-transform'] = 'rotate(90deg)'; - cc.container.style.transform = 'rotate(90deg)'; - - var view = cc.view; - var width = view._originalDesignResolutionSize.width; - var height = view._originalDesignResolutionSize.height; - if (width > 0) { - view.setDesignResolutionSize(width, height, view._resolutionPolicy); - } - thisPointer.__rotateScreen = false; - } - } - - window.removeEventListener('orientationchange', this.__orientationChanged); - - window.scrollY = 0; - if(thisPointer.__fullscreen) { - cc.view.enableAutoFullScreen(true); - } - if (thisPointer.__autoResize) { - cc.view.resizeWithBrowserSize(true); + thisPointer._endEditingOnMobile(); } if (editBox._delegate && editBox._delegate.editBoxEditingDidEnded) { diff --git a/cocos2d/core/platform/CCView.js b/cocos2d/core/platform/CCView.js index ea5ce9a381..3a9309a6a5 100644 --- a/cocos2d/core/platform/CCView.js +++ b/cocos2d/core/platform/CCView.js @@ -525,7 +525,6 @@ var View = cc._Class.extend({ */ setFrameZoomFactor: function (zoomFactor) { this._frameZoomFactor = zoomFactor; - // this.centerWindow(); cc.director.setProjection(cc.director.getProjection()); }, @@ -605,7 +604,6 @@ var View = cc._Class.extend({ this._frameSize.height = height; cc.game.frame.style.width = width + "px"; cc.game.frame.style.height = height + "px"; - //this.centerWindow(); this._resizeEvent(); cc.director.setProjection(cc.director.getProjection()); },