From dbb19e2362776c055bc273713349b72a6be92e89 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Thu, 22 Feb 2018 23:23:42 +0800 Subject: [PATCH] Support WebView in new renderer --- cocos2d/core/components/CCWebView.js | 108 ++--- cocos2d/core/components/index.js | 2 +- .../{CCSGWebView.js => webview-impl.js} | 432 ++++++++---------- 3 files changed, 250 insertions(+), 292 deletions(-) rename cocos2d/core/webview/{CCSGWebView.js => webview-impl.js} (53%) diff --git a/cocos2d/core/components/CCWebView.js b/cocos2d/core/components/CCWebView.js index 3dcbf13d0e..3ae4153b00 100644 --- a/cocos2d/core/components/CCWebView.js +++ b/cocos2d/core/components/CCWebView.js @@ -20,13 +20,15 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ -require('../webview/CCSGWebView'); + +var WebViewImpl = require('../webview/webview-impl'); + /** * !#en WebView event type * !#zh 网页视图事件类型 * @enum WebView.EventType */ -var EventType = _ccsg.WebView.EventType; +var EventType = WebViewImpl.EventType; /** @@ -81,9 +83,9 @@ var WebView = cc.Class({ }, set: function (url) { this._url = url; - var sgNode = this._sgNode; - if (sgNode) { - sgNode.loadURL(url); + var impl = this._impl; + if (impl) { + impl.loadURL(url); } } }, @@ -103,71 +105,57 @@ var WebView = cc.Class({ EventType: EventType, }, - onLoad: CC_JSB && function () { - if (cc.sys.os === cc.sys.OS_OSX || cc.sys.os === cc.sys.OS_WINDOWS) { - this.enabled = false; - } + ctor () { + this._impl = new WebViewImpl(); }, - _createSgNode: function () { - if (CC_JSB) { - if (cc.sys.os === cc.sys.OS_OSX || cc.sys.os === cc.sys.OS_WINDOWS) { - console.log('WebView is not supported on Mac and Windows!'); - return null; - } - } - return new _ccsg.WebView(); - }, - - _initSgNode: function () { - var sgNode = this._sgNode; - if (!sgNode) return; - - if (!CC_JSB) { - sgNode.createDomElementIfNeeded(); - } - - sgNode.loadURL(this._url); - - if (CC_EDITOR && this._useOriginalSize) { - this.node.setContentSize(sgNode.getContentSize()); - this._useOriginalSize = false; - } - else { - sgNode.setContentSize(this.node.getContentSize()); - } - }, - - onEnable: function () { + onEnable () { if (!CC_EDITOR) { - var sgNode = this._sgNode; - sgNode.setEventListener(EventType.LOADED, this._onWebViewLoaded.bind(this)); - sgNode.setEventListener(EventType.LOADING, this._onWebViewLoading.bind(this)); - sgNode.setEventListener(EventType.ERROR, this._onWebViewLoadError.bind(this)); + var impl = this._impl; + this._impl.createDomElementIfNeeded(this.node.width, this.node.height); + this._impl.loadURL(this._url); + this._impl.setVisible(true); + impl.setEventListener(EventType.LOADED, this._onWebViewLoaded.bind(this)); + impl.setEventListener(EventType.LOADING, this._onWebViewLoading.bind(this)); + impl.setEventListener(EventType.ERROR, this._onWebViewLoadError.bind(this)); } }, - onDisable: function () { + onDisable () { if (!CC_EDITOR) { - var sgNode = this._sgNode; - sgNode.setEventListener(EventType.LOADED, emptyCallback); - sgNode.setEventListener(EventType.LOADING, emptyCallback); - sgNode.setEventListener(EventType.ERROR, emptyCallback); + var impl = this._impl; + this._impl.setVisible(false); + impl.setEventListener(EventType.LOADED, emptyCallback); + impl.setEventListener(EventType.LOADING, emptyCallback); + impl.setEventListener(EventType.ERROR, emptyCallback); } }, - _onWebViewLoaded: function () { + onDestroy () { + if (this._impl) { + this._impl.destroy(); + this._impl = null; + } + }, + + update (dt) { + if (this._impl) { + this._impl.updateMatrix(this.node); + } + }, + + _onWebViewLoaded () { cc.Component.EventHandler.emitEvents(this.webviewEvents, this, EventType.LOADED); this.node.emit('loaded', this); }, - _onWebViewLoading: function () { + _onWebViewLoading () { cc.Component.EventHandler.emitEvents(this.webviewEvents, this, EventType.LOADING); this.node.emit('loading', this); return true; }, - _onWebViewLoadError: function () { + _onWebViewLoadError () { cc.Component.EventHandler.emitEvents(this.webviewEvents, this, EventType.ERROR); this.node.emit('error', this); }, @@ -184,9 +172,9 @@ var WebView = cc.Class({ * @method setJavascriptInterfaceScheme * @param {String} scheme */ - setJavascriptInterfaceScheme: function (scheme) { - if (this._sgNode) { - this._sgNode.setJavascriptInterfaceScheme(scheme); + setJavascriptInterfaceScheme (scheme) { + if (this._impl) { + this._impl.setJavascriptInterfaceScheme(scheme); } }, @@ -203,9 +191,9 @@ var WebView = cc.Class({ * @method setOnJSCallback * @param {Function} callback */ - setOnJSCallback: function (callback) { - if (this._sgNode) { - this._sgNode.setOnJSCallback(callback); + setOnJSCallback (callback) { + if (this._impl) { + this._impl.setOnJSCallback(callback); } }, @@ -220,9 +208,9 @@ var WebView = cc.Class({ * @method evaluateJS * @param {String} str */ - evaluateJS: function (str) { - if (this._sgNode) { - this._sgNode.evaluateJS(str); + evaluateJS (str) { + if (this._impl) { + this._impl.evaluateJS(str); } }, diff --git a/cocos2d/core/components/index.js b/cocos2d/core/components/index.js index 4666d89b3a..35d462953b 100644 --- a/cocos2d/core/components/index.js +++ b/cocos2d/core/components/index.js @@ -45,7 +45,7 @@ var components = [ require('./CCLayout'), require('./editbox/CCEditBox'), require('./CCVideoPlayer'), - // require('./CCWebView'), + require('./CCWebView'), require('./CCLabelOutline'), require('./CCRichText'), require('./CCToggleContainer'), diff --git a/cocos2d/core/webview/CCSGWebView.js b/cocos2d/core/webview/webview-impl.js similarity index 53% rename from cocos2d/core/webview/CCSGWebView.js rename to cocos2d/core/webview/webview-impl.js index 4ab1154c57..a1507167b8 100644 --- a/cocos2d/core/webview/CCSGWebView.js +++ b/cocos2d/core/webview/webview-impl.js @@ -20,46 +20,156 @@ ****************************************************************************/ var Utils = require('../platform/utils'); var eventManager = require('../event-manager'); +var sys = require('../platform/CCSys'); -_ccsg.WebView = _ccsg.Node.extend(/** @lends _ccsg.WebView# */{ +var WebViewImpl = cc.Class({ + name: "WebViewImpl", - ctor: function () { - _ccsg.Node.prototype.ctor.call(this); - this.setContentSize(cc.size(300, 200)); + ctor () { + // this.setContentSize(cc.size(300, 200)); this._EventList = {}; + + this._visible = false; + this._parent = null; + this._div = null; + this._iframe = null; + this._listener = null; + + // update matrix cache + this._forceUpdate = true; + this._m00 = 0; + this._m01 = 0; + this._m04 = 0; + this._m05 = 0; + this._m12 = 0; + this._m13 = 0; + this._w = 0; + this._h = 0; }, - createDomElementIfNeeded: function () { - if (!this._renderCmd._div) { - this._renderCmd.createNativeControl(); + _updateVisibility () { + if (!this._div) return; + var div = this._div; + if (this._visible) { + div.style.visibility = 'visible'; + } + else { + div.style.visibility = 'hidden'; + } + this._forceUpdate = true; + }, + + _updateSize (w, h) { + var div = this._div; + if (div) { + div.style["width"] = w + "px"; + div.style["height"] = h + "px"; } }, - setOnJSCallback: function (callback) {}, - setJavascriptInterfaceScheme: function (scheme) {}, - loadData: function (data, MIMEType, encoding, baseURL) {}, - loadHTMLString: function (string, baseURL) {}, + _initEvent () { + var self = this; + this._iframe.addEventListener("load", function () { + self._dispatchEvent(WebViewImpl.EventType.LOADED); + }); + this._iframe.addEventListener("error", function () { + self._dispatchEvent(WebViewImpl.EventType.ERROR); + }); + }, + + _initStyle () { + if (!this._div) return; + var div = this._div; + div.style.position = "absolute"; + div.style.bottom = "0px"; + div.style.left = "0px"; + }, + + _createDom (w, h) { + if (WebViewImpl._polyfill.enableDiv) { + this._div = document.createElement("div"); + this._div.style["-webkit-overflow"] = "auto"; + this._div.style["-webkit-overflow-scrolling"] = "touch"; + this._iframe = document.createElement("iframe"); + this._div.appendChild(this._iframe); + this._iframe.style.width = "100%"; + this._iframe.style.height = "100%"; + } + else { + this._div = this._iframe = document.createElement("iframe"); + } + + if (WebViewImpl._polyfill.enableBG) + this._div.style["background"] = "#FFF"; + + this._div.style["background"] = "#FFF"; + this._div.style.height = h + "px"; + this._div.style.width = w + "px"; + this._div.style.overflow = "scroll"; + this._iframe.style.border = "none"; + cc.game.container.appendChild(this._div); + this._updateVisibility(); + }, + + _createNativeControl (w, h) { + this._createDom(w, h); + this._initStyle(); + this._initEvent(); + }, + + createDomElementIfNeeded (w, h) { + if (!this._div) { + this._createNativeControl(w, h); + } + else { + this._updateSize(w, h); + } + }, + + removeDom () { + var div = this._div; + if (div) { + var hasChild = Utils.contains(cc.game.container, div); + if (hasChild) + cc.game.container.removeChild(div); + } + this._div = null; + }, + + setOnJSCallback (callback) {}, + setJavascriptInterfaceScheme (scheme) {}, + loadData (data, MIMEType, encoding, baseURL) {}, + loadHTMLString (string, baseURL) {}, + /** * Load an URL * @param {String} url */ - loadURL: function (url) { - this._renderCmd.updateURL(url); - this._dispatchEvent(_ccsg.WebView.EventType.LOADING); + loadURL (url) { + var iframe = this._iframe; + iframe.src = url; + var self = this; + var cb = function () { + self._loaded = true; + self._updateVisibility(); + iframe.removeEventListener("load", cb); + }; + iframe.addEventListener("load", cb); + this._dispatchEvent(WebViewImpl.EventType.LOADING); }, /** * Stop loading */ - stopLoading: function () { + stopLoading () { cc.logID(7800); }, /** * Reload the WebView */ - reload: function () { - var iframe = this._renderCmd._iframe; + reload () { + var iframe = this._iframe; if (iframe) { var win = iframe.contentWindow; if (win && win.location) @@ -70,7 +180,7 @@ _ccsg.WebView = _ccsg.Node.extend(/** @lends _ccsg.WebView# */{ /** * Determine whether to go back */ - canGoBack: function () { + canGoBack () { cc.logID(7801); return true; }, @@ -78,7 +188,7 @@ _ccsg.WebView = _ccsg.Node.extend(/** @lends _ccsg.WebView# */{ /** * Determine whether to go forward */ - canGoForward: function () { + canGoForward () { cc.logID(7802); return true; }, @@ -86,11 +196,11 @@ _ccsg.WebView = _ccsg.Node.extend(/** @lends _ccsg.WebView# */{ /** * go back */ - goBack: function () { + goBack () { try { - if (_ccsg.WebView._polyfill.closeHistory) + if (WebViewImpl._polyfill.closeHistory) return cc.logID(7803); - var iframe = this._renderCmd._iframe; + var iframe = this._iframe; if (iframe) { var win = iframe.contentWindow; if (win && win.location) @@ -104,11 +214,11 @@ _ccsg.WebView = _ccsg.Node.extend(/** @lends _ccsg.WebView# */{ /** * go forward */ - goForward: function () { + goForward () { try { - if (_ccsg.WebView._polyfill.closeHistory) + if (WebViewImpl._polyfill.closeHistory) return cc.logID(7804); - var iframe = this._renderCmd._iframe; + var iframe = this._iframe; if (iframe) { var win = iframe.contentWindow; if (win && win.location) @@ -123,13 +233,13 @@ _ccsg.WebView = _ccsg.Node.extend(/** @lends _ccsg.WebView# */{ * In the webview execution within a period of js string * @param {String} str */ - evaluateJS: function (str) { - var iframe = this._renderCmd._iframe; + evaluateJS (str) { + var iframe = this._iframe; if (iframe) { var win = iframe.contentWindow; try { win.eval(str); - this._dispatchEvent(_ccsg.WebView.EventType.JS_EVALUATED); + this._dispatchEvent(WebViewImpl.EventType.JS_EVALUATED); } catch (err) { console.error(err); } @@ -139,7 +249,7 @@ _ccsg.WebView = _ccsg.Node.extend(/** @lends _ccsg.WebView# */{ /** * Limited scale */ - setScalesPageToFit: function () { + setScalesPageToFit () { cc.logID(7805); }, @@ -148,7 +258,7 @@ _ccsg.WebView = _ccsg.Node.extend(/** @lends _ccsg.WebView# */{ * @param {_ccsg.WebView.EventType} event * @param {Function} callback */ - setEventListener: function (event, callback) { + setEventListener (event, callback) { this._EventList[event] = callback; }, @@ -156,241 +266,101 @@ _ccsg.WebView = _ccsg.Node.extend(/** @lends _ccsg.WebView# */{ * Delete events * @param {_ccsg.WebView.EventType} event */ - removeEventListener: function (event) { + removeEventListener (event) { this._EventList[event] = null; }, - _dispatchEvent: function (event) { + _dispatchEvent (event) { var callback = this._EventList[event]; if (callback) - callback.call(this, this, this._renderCmd._iframe.src); + callback.call(this, this, this._iframe.src); }, - _createRenderCmd: function () { - return new _ccsg.WebView.RenderCmd(this); + _createRenderCmd () { + return new WebViewImpl.RenderCmd(this); }, - /** - * Set the contentSize - * @param {Number} width - * @param {Number} height - */ - setContentSize: function (width, height) { - if (width.width !== undefined && width.height !== undefined) { - height = width.height; - width = width.width; - } - _ccsg.Node.prototype.setContentSize.call(this, width, height); - this._renderCmd.updateSize(width, height); + destroy () { + this.removeDom(); }, - cleanup: function () { - this._super(); - this._renderCmd.removeDom(); + setVisible (visible) { + if (this._visible !== visible) { + this._visible = !!visible; + this._updateVisibility(); + } }, - setVisible: function (visible) { - _ccsg.Node.prototype.setVisible.call(this, visible); - this._renderCmd.updateVisibility(); - } -}); + updateMatrix (node) { + if (!this._div || !this._visible) return; -_ccsg.WebView.EventType = { - LOADING: 0, - LOADED: 1, - ERROR: 2, - JS_EVALUATED: 3 -}; - -(function() { - - var polyfill = _ccsg.WebView._polyfill = { - devicePixelRatio: false, - enableDiv: false - }; - - if (cc.sys.os === cc.sys.OS_IOS) - polyfill.enableDiv = true; - - if (cc.sys.isMobile) { - if (cc.sys.browserType === cc.sys.BROWSER_TYPE_FIREFOX) { - polyfill.enableBG = true; + var mat = node._worldMatrix; + if (!this._forceUpdate && + this._m00 === mat.m00 && this._m01 === mat.m01 && this._m04 === mat.m04 && this._m05 === mat.m05 && this._m12 === mat.m12 && this._m13 === mat.m13 && + this._w === node._contentSize.width && this._h === node._contentSize.height) { + return; } - } - else { - if (cc.sys.browserType === cc.sys.BROWSER_TYPE_IE) { - polyfill.closeHistory = true; - } - } + // update matrix cache + this._m00 = mat.m00; + this._m01 = mat.m01; + this._m04 = mat.m04; + this._m05 = mat.m05; + this._m12 = mat.m12; + this._m13 = mat.m13; + this._w = node._contentSize.width; + this._h = node._contentSize.height; -})(); - -(function(polyfill) { - - var RenderCmd; - if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) { - RenderCmd = _ccsg.Node.CanvasRenderCmd; - } - else { - RenderCmd = _ccsg.Node.WebGLRenderCmd; - } - - _ccsg.WebView.RenderCmd = function (node) { - this._rootCtor(node); - - this._parent = null; - this._div = null; - this._iframe = null; - this._listener = null; - }; - - var proto = _ccsg.WebView.RenderCmd.prototype = Object.create(RenderCmd.prototype); - proto.constructor = _ccsg.WebView.RenderCmd; - - proto.transform = function (parentCmd, recursive) { - this.originTransform(parentCmd, recursive); - this.updateMatrix(); - }; - - proto.updateStatus = function () { - polyfill.devicePixelRatio = cc.view.isRetinaEnabled(); - var flags = _ccsg.Node._dirtyFlags, locFlag = this._dirtyFlag; - if (locFlag & flags.transformDirty) { - //update the transform - this.transform(this.getParentRenderCmd(), true); - this.updateMatrix(); - this._dirtyFlag &= ~_ccsg.Node._dirtyFlags.transformDirty; - } - }; - - proto.initEvent = function () { - var node = this._node; - this._iframe.addEventListener("load", function () { - node._dispatchEvent(_ccsg.WebView.EventType.LOADED); - }); - this._iframe.addEventListener("error", function () { - node._dispatchEvent(_ccsg.WebView.EventType.ERROR); - }); - }; - - proto.resize = function (view) { - view = view || cc.view; - var node = this._node; - if (node._parent && node._visible) - this.updateMatrix(); - else { - var list = eventManager._listenersMap[cc.game.EVENT_RESIZE].getFixedPriorityListeners(); - eventManager._removeListenerInVector(list, this._listener); - this._listener = null; - } - }; - - proto.updateMatrix = function () { - if (!this._div) return; - - var node = this._node, scaleX = cc.view._scaleX, scaleY = cc.view._scaleY; + var scaleX = cc.view._scaleX, scaleY = cc.view._scaleY; var dpr = cc.view._devicePixelRatio; - var t = this._worldTransform; scaleX /= dpr; scaleY /= dpr; var container = cc.game.container; - var a = t.a * scaleX, b = t.b, c = t.c, d = t.d * scaleY; + var a = mat.m00 * scaleX, b = mat.m01, c = mat.m04, d = mat.m05 * scaleY; var offsetX = container && container.style.paddingLeft && parseInt(container.style.paddingLeft); var offsetY = container && container.style.paddingBottom && parseInt(container.style.paddingBottom); - var tx = t.tx * scaleX + offsetX, ty = t.ty * scaleY + offsetY; + this._updateSize(this._w * a, this._h * d); + var w = this._div.clientWidth / scaleX; + var h = this._div.clientHeight / scaleY; + var appx = w * node._anchorPoint.x; + var appy = h - h * node._anchorPoint.y; + var tx = mat.m12 * scaleX - appx + offsetX, ty = mat.m13 * scaleY - appy + offsetY; var matrix = "matrix(" + a + "," + -b + "," + -c + "," + d + "," + tx + "," + -ty + ")"; this._div.style['transform'] = matrix; this._div.style['-webkit-transform'] = matrix; this._div.style['transform-origin'] = '0px 100% 0px'; this._div.style['-webkit-transform-origin'] = '0px 100% 0px'; - }; + } +}); - proto.initStyle = function () { - if (!this._div) return; - var div = this._div; - div.style.position = "absolute"; - div.style.bottom = "0px"; - div.style.left = "0px"; - }; +WebViewImpl.EventType = { + LOADING: 0, + LOADED: 1, + ERROR: 2, + JS_EVALUATED: 3 +}; - proto.updateURL = function (url) { - var iframe = this._iframe; - iframe.src = url; - var self = this; - var cb = function () { - self._loaded = true; - self.updateVisibility(); - iframe.removeEventListener("load", cb); - }; - iframe.addEventListener("load", cb); - }; +var polyfill = WebViewImpl._polyfill = { + devicePixelRatio: false, + enableDiv: false +}; - proto.updateSize = function (w, h) { - var div = this._div; - if (div) { - div.style["width"] = w + "px"; - div.style["height"] = h + "px"; - } - }; +if (sys.os === sys.OS_IOS) + polyfill.enableDiv = true; - proto.createDom = function () { - if (polyfill.enableDiv) { - this._div = document.createElement("div"); - this._div.style["-webkit-overflow"] = "auto"; - this._div.style["-webkit-overflow-scrolling"] = "touch"; - this._iframe = document.createElement("iframe"); - this._div.appendChild(this._iframe); - this._iframe.style.width = "100%"; - this._iframe.style.height = "100%"; - } - else { - this._div = this._iframe = document.createElement("iframe"); - } +if (sys.isMobile) { + if (sys.browserType === sys.BROWSER_TYPE_FIREFOX) { + polyfill.enableBG = true; + } +} +else { + if (sys.browserType === sys.BROWSER_TYPE_IE) { + polyfill.closeHistory = true; + } +} - if (polyfill.enableBG) - this._div.style["background"] = "#FFF"; - - this._div.style["background"] = "#FFF"; - var contentSize = this._node._contentSize; - this._div.style.height = contentSize.height + "px"; - this._div.style.width = contentSize.width + "px"; - this._div.style.overflow = "scroll"; - this._iframe.style.border = "none"; - cc.game.container.appendChild(this._div); - this.updateVisibility(); - }; - - proto.createNativeControl = function () { - this.createDom(); - this.initStyle(); - this.initEvent(); - }; - - proto.removeDom = function () { - var div = this._div; - if (div) { - var hasChild = Utils.contains(cc.game.container, div); - if (hasChild) - cc.game.container.removeChild(div); - } - this._div = null; - }; - - proto.updateVisibility = function () { - var node = this._node; - if (!this._div) return; - var div = this._div; - if (node.visible) { - div.style.visibility = 'visible'; - } - else { - div.style.visibility = 'hidden'; - } - }; - -})(_ccsg.WebView._polyfill); +module.exports = WebViewImpl; \ No newline at end of file