From 18fbcbb6d2f9386244257ea3528136dfc8dcddc4 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Mon, 13 Aug 2018 21:07:40 +0800 Subject: [PATCH] Add WXSubContextView to control sub context viewport (#3079) * Add WXSubContextView to control sub context viewport * Ignore WXSubContextView in other platform and provide docs --- cocos2d/core/components/WXSubContextView.js | 134 ++++++++++++++++++++ cocos2d/core/components/index.js | 3 +- cocos2d/core/platform/CCView.js | 10 -- editor/i18n/en/localization.js | 3 +- editor/i18n/zh/localization.js | 3 +- 5 files changed, 140 insertions(+), 13 deletions(-) create mode 100644 cocos2d/core/components/WXSubContextView.js diff --git a/cocos2d/core/components/WXSubContextView.js b/cocos2d/core/components/WXSubContextView.js new file mode 100644 index 0000000000..ea22559628 --- /dev/null +++ b/cocos2d/core/components/WXSubContextView.js @@ -0,0 +1,134 @@ +/**************************************************************************** + Copyright (c) 2018 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +const Component = require('./CCComponent'); + +let WXSubContextView; + +if (!CC_EDITOR && !CC_WECHATGAME) { + WXSubContextView = cc.Class({ + name: 'cc.WXSubContextView', + extends: Component, + }) +} +else { + /** + * !#en WXSubContextView is a view component which controls open data context viewport in Wechat game platform.
+ * The component's node size decide the viewport of the sub context content in main context, + * the entire sub context texture will be scaled to the node's bounding box area.
+ * This component provides multiple important features:
+ * 1. Sub context could use its own resolution size and policy.
+ * 2. Sub context could be minized to smallest size it needed.
+ * 3. Resolution of sub context content could be increased.
+ * 4. User touch input is transformed to the correct viewport.
+ * 5. Texture update is handled by this component. User don't need to worry.
+ * One important thing to be noted, whenever the node's bounding box change, + * you need to manually reset the viewport of sub context using updateSubContextViewport. + * !#zh WXSubContextView 可以用来控制微信小游戏平台开放数据域在主域中的视窗的位置。
+ * 这个组件的节点尺寸决定了开放数据域内容在主域中的尺寸,整个开放数据域会被缩放到节点的包围盒范围内。
+ * 在这个组件的控制下,用户可以更自由得控制开放数据域:
+ * 1. 子域中可以使用独立的设计分辨率和适配模式
+ * 2. 子域区域尺寸可以缩小到只容纳内容即可
+ * 3. 子域的分辨率也可以被放大,以便获得更清晰的显示效果
+ * 4. 用户输入坐标会被自动转换到正确的子域视窗中
+ * 5. 子域内容贴图的更新由组件负责,用户不需要处理
+ * 唯一需要注意的是,当子域节点的包围盒发生改变时,开发者需要使用 `updateSubContextViewport` 来手动更新子域视窗。 + * @class WXSubContextView + * @extends Component + */ + WXSubContextView = cc.Class({ + name: 'cc.WXSubContextView', + extends: Component, + + editor: CC_EDITOR && { + menu: 'i18n:MAIN_MENU.component.others/WXSubContextView', + help: 'i18n:COMPONENT.help_url.wxSubContextView' + }, + + ctor () { + this._sprite = null; + this._tex = new cc.Texture2D(); + this._context = null; + }, + + onLoad () { + // Setup subcontext canvas size + if (wx.getOpenDataContext) { + this._context = wx.getOpenDataContext(); + let sharedCanvas = this._context.canvas; + if (sharedCanvas) { + sharedCanvas.width = this.node.width; + sharedCanvas.height = this.node.height; + } + this._tex.initWithElement(sharedCanvas); + + this._sprite = this.node.getComponent(cc.Sprite); + if (!this._sprite) { + this._sprite = this.node.addComponent(cc.Sprite); + } + this._sprite.spriteFrame = new cc.SpriteFrame(this._tex); + } + else { + this.enabled = false; + } + }, + + onEnable () { + this.updateSubContextViewport(); + }, + + update () { + if (!this._tex || !this._context) { + return; + } + this._tex.initWithElement(this._context.canvas); + this._sprite._activateMaterial(); + }, + + /** + * !#en Update the sub context viewport manually, it should be called whenever the node's bounding box changes. + * !#zh 更新开放数据域相对于主域的 viewport,这个函数应该在节点包围盒改变时手动调用。 + * @method updateSubContextViewport + */ + updateSubContextViewport () { + if (this._context) { + let box = this.node.getBoundingBoxToWorld(); + let sx = cc.view._scaleX; + let sy = cc.view._scaleY; + this._context.postMessage({ + fromEngine: true, + event: 'viewport', + x: box.x * sx + cc.view._viewportRect.x, + y: box.y * sy + cc.view._viewportRect.y, + width: box.width * sx, + height: box.height * sy + }); + } + } + }); + +} + +cc.WXSubContextView = module.exports = WXSubContextView; \ No newline at end of file diff --git a/cocos2d/core/components/index.js b/cocos2d/core/components/index.js index c36c0c2f38..c268c1a4bf 100644 --- a/cocos2d/core/components/index.js +++ b/cocos2d/core/components/index.js @@ -51,7 +51,8 @@ var components = [ require('./CCToggleGroup'), require('./CCToggle'), require('./CCBlockInputEvents'), - require('./CCMotionStreak') + require('./CCMotionStreak'), + require('./WXSubContextView') ]; module.exports = components; \ No newline at end of file diff --git a/cocos2d/core/platform/CCView.js b/cocos2d/core/platform/CCView.js index 72aeb1b0b0..6df23024c6 100644 --- a/cocos2d/core/platform/CCView.js +++ b/cocos2d/core/platform/CCView.js @@ -1077,16 +1077,6 @@ cc.ContainerStrategy = cc.Class({ // Setup canvas locCanvas.width = w * devicePixelRatio; locCanvas.height = h * devicePixelRatio; - - // set sharedCanvas size - if (cc.sys.browserType === cc.sys.BROWSER_TYPE_WECHAT_GAME && wx.getOpenDataContext) { - var openDataContext = wx.getOpenDataContext(); - var sharedCanvas = openDataContext.canvas; - if (sharedCanvas) { - sharedCanvas.width = locCanvas.width; - sharedCanvas.height = locCanvas.height; - } - } }, _fixContainer: function () { diff --git a/editor/i18n/en/localization.js b/editor/i18n/en/localization.js index fae2bf513b..4048877775 100755 --- a/editor/i18n/en/localization.js +++ b/editor/i18n/en/localization.js @@ -22,7 +22,8 @@ module.exports = { "pageview": "http://www.cocos.com/docs/creator/components/pageview.html", "pageviewIndicator": "http://www.cocos.com/docs/creator/components/pageviewIndicator.html", "slider": "http://www.cocos.com/docs/creator/components/slider.html", - "block_input_events": "http://www.cocos.com/docs/creator/components/block-input-events.html" + "block_input_events": "http://www.cocos.com/docs/creator/components/block-input-events.html", + "wx_subcontext_view": "http://www.cocos.com/docs/creator/components/wx-subcontext-view.html" }, "animation": { "default_clip": "When checking, the deault animation clip is automatically played.", diff --git a/editor/i18n/zh/localization.js b/editor/i18n/zh/localization.js index bd85633cef..6eedb330e3 100755 --- a/editor/i18n/zh/localization.js +++ b/editor/i18n/zh/localization.js @@ -24,7 +24,8 @@ module.exports = { "toggleGroup": "http://www.cocos.com/docs/creator/components/toggleGroup.html", "toggleContainer": "http://www.cocos.com/docs/creator/components/toggleContainer.html", "slider": "http://www.cocos.com/docs/creator/components/slider.html", - "block_input_events": "http://www.cocos.com/docs/creator/components/block-input-events.html" + "block_input_events": "http://www.cocos.com/docs/creator/components/block-input-events.html", + "wx_subcontext_view": "http://www.cocos.com/docs/creator/components/wx-subcontext-view.html" }, 'animation': { 'default_clip': '在勾选自动播放或调用 play() 时默认播放的动画 clip。',