diff --git a/@types/globals.d.ts b/@types/globals.d.ts index e086fe1933..1e8688a0f4 100644 --- a/@types/globals.d.ts +++ b/@types/globals.d.ts @@ -67,6 +67,11 @@ interface Document{ webkitHidden: any; } +interface HTMLElement{ + content: any; + name: any; +} + interface ActiveXObject { new (s: string): any; } @@ -96,5 +101,9 @@ declare namespace Editor { function log (message?: any, ...optionalParams: any[]): void; function error (message?: any, ...optionalParams: any[]): void; function warn (message?: any, ...optionalParams: any[]): void; + function require(str:String): any; const isMainProcess: boolean | undefined; + const Ipc: any; + const Utils: any; + const assets: any; } diff --git a/@types/wx.d.ts b/@types/wx.d.ts index 4f536a1dbe..9decc0368b 100644 --- a/@types/wx.d.ts +++ b/@types/wx.d.ts @@ -16,6 +16,7 @@ declare namespace wx { function getFileSystemManager(): FileSystemManager; function getSharedCanvas(): any; + function getOpenDataContext(): any; const onShow: any; const onHide: any; diff --git a/cocos/assets/CCAssetLibrary.js b/cocos/assets/CCAssetLibrary.ts similarity index 78% rename from cocos/assets/CCAssetLibrary.js rename to cocos/assets/CCAssetLibrary.ts index 039b398037..1ba74b4bc4 100644 --- a/cocos/assets/CCAssetLibrary.js +++ b/cocos/assets/CCAssetLibrary.ts @@ -24,15 +24,17 @@ THE SOFTWARE. ****************************************************************************/ -import {createMap, isChildClassOf, _getClassById} from '../core/utils/js'; -import { Asset } from './asset'; -import {callInNextTick} from '../core/utils/misc'; import decodeUuid from '../core/utils/decode-uuid'; -import Loader from '../load-pipeline/CCLoader'; -import {initPacks} from '../load-pipeline/pack-downloader'; -import {getDependsRecursively} from '../load-pipeline/auto-release-utils'; -import MD5Pipe from '../load-pipeline/md5-pipe'; +import {_getClassById, createMap, isChildClassOf} from '../core/utils/js'; +import {callInNextTick} from '../core/utils/misc'; import AssetTable from '../load-pipeline/asset-table'; +import {getDependsRecursively} from '../load-pipeline/auto-release-utils'; +import Loader from '../load-pipeline/CCLoader'; +import MD5Pipe from '../load-pipeline/md5-pipe'; +import {initPacks} from '../load-pipeline/pack-downloader'; +import { Asset } from './asset'; + +// tslint:disable: max-line-length /** * The asset library which managing loading/unloading assets in project. @@ -43,9 +45,9 @@ import AssetTable from '../load-pipeline/asset-table'; // configs -var _libraryBase = ''; -var _rawAssetsBase = ''; // The base dir for raw assets in runtime -var _uuidToRawAsset = createMap(true); +let _libraryBase = ''; +let _rawAssetsBase = ''; // The base dir for raw assets in runtime +const _uuidToRawAsset = createMap(true); function isScene (asset) { return asset && (asset.constructor === cc.SceneAsset || asset instanceof cc.Scene); @@ -60,7 +62,7 @@ function RawAssetEntry (url, type) { // publics -let AssetLibrary = { +const AssetLibrary = { /** * !#en Caches uuid to all loaded assets in scenes. * @@ -93,20 +95,20 @@ let AssetLibrary = { * @param {Asset} options.existingAsset - load to existing asset, this argument is only available in editor * @private */ - loadAsset: function (uuid, callback, options) { + loadAsset (uuid, callback, options) { if (typeof uuid !== 'string') { return callInNextTick(callback, new Error('[AssetLibrary] uuid must be string'), null); } // var readMainCache = typeof (options && options.readMainCache) !== 'undefined' ? readMainCache : true; // var writeMainCache = typeof (options && options.writeMainCache) !== 'undefined' ? writeMainCache : true; - var item = { - uuid: uuid, - type: 'uuid' + const item: any = { + uuid, + type: 'uuid', }; if (options && options.existingAsset) { item.existingAsset = options.existingAsset; } - cc.loader.load(item, function (error, asset) { + cc.loader.load(item, (error, asset) => { if (error || !asset) { error = new Error('[AssetLibrary] loading JSON or dependencies failed: ' + (error ? error.message : 'Unknown error')); } @@ -116,12 +118,12 @@ let AssetLibrary = { Editor.error('Sorry, the scene data of "%s" is corrupted!', uuid); } else { - var key = cc.loader._getReferenceKey(uuid); + const key = cc.loader._getReferenceKey(uuid); asset.scene.dependAssets = getDependsRecursively(key); } } if (isScene(asset)) { - var id = cc.loader._getReferenceKey(uuid); + const id = cc.loader._getReferenceKey(uuid); cc.loader.removeItem(id); } } @@ -131,22 +133,22 @@ let AssetLibrary = { }); }, - getLibUrlNoExt: function (uuid, inRawAssetsDir) { + getLibUrlNoExt (uuid, inRawAssetsDir?: any) { if (CC_BUILD) { uuid = decodeUuid(uuid); } - var base = (CC_BUILD && inRawAssetsDir) ? (_rawAssetsBase + 'assets/') : _libraryBase; + const base = (CC_BUILD && inRawAssetsDir) ? (_rawAssetsBase + 'assets/') : _libraryBase; return base + uuid.slice(0, 2) + '/' + uuid; }, - _queryAssetInfoInEditor: function (uuid, callback) { + _queryAssetInfoInEditor (uuid, callback) { if (CC_EDITOR) { - Editor.Ipc.sendToMain('scene:query-asset-info-by-uuid', uuid, function (err, info) { + Editor.Ipc.sendToMain('scene:query-asset-info-by-uuid', uuid, (err, info) => { if (info) { Editor.Utils.UuidCache.cache(info.url, uuid); - var ctor = Editor.assets[info.type]; + const ctor = Editor.assets[info.type]; if (ctor) { - var isRawAsset = !isChildClassOf(ctor, Asset); + const isRawAsset = !isChildClassOf(ctor, Asset); callback(null, info.url, isRawAsset, ctor); } else { @@ -154,7 +156,7 @@ let AssetLibrary = { } } else { - var error = new Error('Can not get asset url by uuid "' + uuid + '", the asset may be deleted.'); + const error: any = new Error('Can not get asset url by uuid "' + uuid + '", the asset may be deleted.'); error.errorCode = 'db.NOTFOUND'; callback(error); } @@ -162,9 +164,9 @@ let AssetLibrary = { } }, - _getAssetInfoInRuntime: function (uuid, result) { + _getAssetInfoInRuntime (uuid, result?: any) { result = result || {url: null, raw: false}; - var info = _uuidToRawAsset[uuid]; + const info = _uuidToRawAsset[uuid]; if (info && !isChildClassOf(info.type, cc.Asset)) { // backward compatibility since 1.10 result.url = _rawAssetsBase + info.url; @@ -177,7 +179,7 @@ let AssetLibrary = { return result; }, - _uuidInSettings: function (uuid) { + _uuidInSettings (uuid) { return uuid in _uuidToRawAsset; }, @@ -190,29 +192,29 @@ let AssetLibrary = { * @param {Boolean} callback.raw - indicates whether the asset is raw asset * @param {Function} callback.ctorInEditor - the actual type of asset, used in editor only */ - queryAssetInfo: function (uuid, callback) { + queryAssetInfo (uuid, callback) { if (CC_EDITOR && !CC_TEST) { this._queryAssetInfoInEditor(uuid, callback); } else { - var info = this._getAssetInfoInRuntime(uuid); + const info = this._getAssetInfoInRuntime(uuid); callback(null, info.url, info.raw); } }, // parse uuid out of url - parseUuidInEditor: function (url) { + parseUuidInEditor (url) { if (CC_EDITOR) { - var uuid = ''; - var isImported = url.startsWith(_libraryBase); + let uuid = ''; + const isImported = url.startsWith(_libraryBase); if (isImported) { - var dir = cc.path.dirname(url); - var dirBasename = cc.path.basename(dir); + const dir = cc.path.dirname(url); + const dirBasename = cc.path.basename(dir); - var isAssetUrl = dirBasename.length === 2; + const isAssetUrl = dirBasename.length === 2; if (isAssetUrl) { uuid = cc.path.basename(url); - var index = uuid.indexOf('.'); + const index = uuid.indexOf('.'); if (index !== -1) { uuid = uuid.slice(0, index); } @@ -234,25 +236,25 @@ let AssetLibrary = { * @return {LoadingHandle} * @private */ - loadJson: function (json, callback) { - var randomUuid = '' + ((new Date()).getTime() + Math.random()); - var item = { + loadJson (json, callback) { + const randomUuid = '' + ((new Date()).getTime() + Math.random()); + const item = { uuid: randomUuid, type: 'uuid', content: json, - skips: [ cc.loader.assetLoader.id, cc.loader.downloader.id ] + skips: [ cc.loader.assetLoader.id, cc.loader.downloader.id ], }; - cc.loader.load(item, function (error, asset) { + cc.loader.load(item, (error, asset) => { if (error) { error = new Error('[AssetLibrary] loading JSON or dependencies failed: ' + error.message); } else { if (asset.constructor === cc.SceneAsset) { - var key = cc.loader._getReferenceKey(randomUuid); + const key = cc.loader._getReferenceKey(randomUuid); asset.scene.dependAssets = getDependsRecursively(key); } if (CC_EDITOR || isScene(asset)) { - var id = cc.loader._getReferenceKey(randomUuid); + const id = cc.loader._getReferenceKey(randomUuid); cc.loader.removeItem(id); } } @@ -271,10 +273,11 @@ let AssetLibrary = { * @return {Asset} - the existing asset, if not loaded, just returns null. * @private */ - getAssetByUuid: function (uuid) { + getAssetByUuid (uuid) { return AssetLibrary._uuidToAsset[uuid] || null; }, + // tslint:disable: no-shadowed-variable /** * init the asset library * @@ -286,60 +289,61 @@ let AssetLibrary = { * @param {String} [options.rawAssetsBase] - base of raw asset's urls (only used in runtime) * @param {String} [options.packedAssets] - packed assets (only used in runtime) */ - init: function (options) { + init (options) { if (CC_EDITOR && _libraryBase) { cc.errorID(6402); return; } - // 这里将路径转 url,不使用路径的原因是有的 runtime 不能解析 "\" 符号。 // 不使用 url.format 的原因是 windows 不支持 file:// 和 /// 开头的协议,所以只能用 replace 操作直接把路径转成 URL。 - var libraryPath = options.libraryPath; + let libraryPath = options.libraryPath; libraryPath = libraryPath.replace(/\\/g, '/'); _libraryBase = cc.path.stripSep(libraryPath) + '/'; _rawAssetsBase = options.rawAssetsBase; - var md5AssetsMap = options.md5AssetsMap; + const md5AssetsMap = options.md5AssetsMap; if (md5AssetsMap && md5AssetsMap.import) { // decode uuid - var i = 0, uuid = 0; - var md5ImportMap = createMap(true); - var md5Entries = md5AssetsMap.import; + let i = 0; + let uuid = 0; + const md5ImportMap = createMap(true); + let md5Entries = md5AssetsMap.import; for (i = 0; i < md5Entries.length; i += 2) { uuid = decodeUuid(md5Entries[i]); md5ImportMap[uuid] = md5Entries[i + 1]; } - var md5RawAssetsMap = createMap(true); + const md5RawAssetsMap = createMap(true); md5Entries = md5AssetsMap['raw-assets']; for (i = 0; i < md5Entries.length; i += 2) { uuid = decodeUuid(md5Entries[i]); md5RawAssetsMap[uuid] = md5Entries[i + 1]; } - var md5Pipe = new MD5Pipe(md5ImportMap, md5RawAssetsMap, _libraryBase); + const md5Pipe = new MD5Pipe(md5ImportMap, md5RawAssetsMap, _libraryBase); cc.loader.insertPipeAfter(cc.loader.assetLoader, md5Pipe); cc.loader.md5Pipe = md5Pipe; } // init raw assets - var assetTables = Loader._assetTables; - for (var mount in assetTables) { + const assetTables = Loader._assetTables; + // tslint:disable: forin + for (const mount in assetTables) { assetTables[mount].reset(); } - var rawAssets = options.rawAssets; + const rawAssets = options.rawAssets; if (rawAssets) { - for (var mountPoint in rawAssets) { - var assets = rawAssets[mountPoint]; - for (var uuid in assets) { - var info = assets[uuid]; - var url = info[0]; - var typeId = info[1]; - var type = _getClassById(typeId); + for (const mountPoint in rawAssets) { + const assets = rawAssets[mountPoint]; + for (const uuid in assets) { + const info = assets[uuid]; + const url = info[0]; + const typeId = info[1]; + const type = _getClassById(typeId); if (!type) { cc.error('Cannot get', typeId); continue; @@ -347,7 +351,7 @@ let AssetLibrary = { // backward compatibility since 1.10 _uuidToRawAsset[uuid] = new RawAssetEntry(mountPoint + '/' + url, type); // init resources - var isSubAsset = info[2] === 1; + const isSubAsset = info[2] === 1; if (!assetTables[mountPoint]) { assetTables[mountPoint] = new AssetTable(); @@ -364,18 +368,18 @@ let AssetLibrary = { // init cc.url cc.url._init((options.mountPaths && options.mountPaths.assets) || _rawAssetsBase + 'assets'); - } -} + }, +}; -//暂时屏蔽,因为目前没有缓存任何asset -//if (CC_DEV && Asset.prototype._onPreDestroy) { +// 暂时屏蔽,因为目前没有缓存任何asset +// if (CC_DEV && Asset.prototype._onPreDestroy) { // cc.error('_onPreDestroy of Asset has already defined'); -//} -//Asset.prototype._onPreDestroy = function () { +// } +// Asset.prototype._onPreDestroy = function () { // if (AssetLibrary._uuidToAsset[this._uuid] === this) { // AssetLibrary.unloadAsset(this); // } -//}; +// }; cc.AssetLibrary = AssetLibrary; export default AssetLibrary; diff --git a/cocos/assets/CCPrefab.js b/cocos/assets/CCPrefab.ts similarity index 90% rename from cocos/assets/CCPrefab.js rename to cocos/assets/CCPrefab.ts index c53d4c4b79..3e2ace45a5 100644 --- a/cocos/assets/CCPrefab.js +++ b/cocos/assets/CCPrefab.ts @@ -24,11 +24,11 @@ THE SOFTWARE. ****************************************************************************/ -import {compile} from '../core/data/instantiate-jit'; -import Enum from '../core/value-types/enum'; -import {obsolete} from '../core/utils/js'; -import { Asset } from './asset'; import {ccclass, property} from '../core/data/class-decorator'; +import {compile} from '../core/data/instantiate-jit'; +import {obsolete} from '../core/utils/js'; +import Enum from '../core/value-types/enum'; +import { Asset } from './asset'; /** * !#zh @@ -40,7 +40,7 @@ import {ccclass, property} from '../core/data/class-decorator'; * @enum Prefab.OptimizationPolicy * @since 1.10.0 */ -var OptimizationPolicy = Enum({ +const OptimizationPolicy = Enum({ /** * !#zh * 根据创建次数自动调整优化策略。初次创建实例时,行为等同 SINGLE_INSTANCE,多次创建后将自动采用 MULTI_INSTANCE。 @@ -83,23 +83,15 @@ var OptimizationPolicy = Enum({ */ @ccclass('cc.Prefab') export default class Prefab extends Asset { - constructor () { - super(); - /** - * Cache function to optimize instance creaton. - * @property {Function} _createFunction - * @private - */ - this._createFunction = null; - this._instantiatedTimes = 0; - } + public static OptimizationPolicy = OptimizationPolicy; + public static OptimizationPolicyThreshold = 3; /** * @property {Node} data - the main cc.Node in the prefab */ @property - data = null; + public data: any = null; /** * !#zh @@ -115,7 +107,7 @@ export default class Prefab extends Asset { * prefab.optimizationPolicy = cc.Prefab.OptimizationPolicy.MULTI_INSTANCE; */ @property - optimizationPolicy = OptimizationPolicy.AUTO; + public optimizationPolicy = OptimizationPolicy.AUTO; /** * !#en Indicates the raw assets of this prefab can be load after prefab loaded. @@ -124,14 +116,24 @@ export default class Prefab extends Asset { * @default false */ @property - asyncLoadAssets = false; + public asyncLoadAssets = false; - static OptimizationPolicy = OptimizationPolicy; + private _createFunction: Function | null; + private _instantiatedTimes: number; + constructor () { + super(); + /** + * Cache function to optimize instance creaton. + * @property {Function} _createFunction + * @private + */ + this._createFunction = null; - static OptimizationPolicyThreshold = 3; + this._instantiatedTimes = 0; + } - createNode (cb) { - var node = cc.instantiate(this); + public createNode (cb: Function): void { + const node = cc.instantiate(this); node.name = this.name; cb(null, node); } @@ -142,14 +144,14 @@ export default class Prefab extends Asset { * but you can re-call to refresh the create function once you modified the original prefab data in script. * @method compileCreateFunction */ - compileCreateFunction () { + public compileCreateFunction (): void { this._createFunction = compile(this.data); } // just instantiate, will not initialize the Node, this will be called during Node's initialization. // @param {Node} [rootToRedirect] - specify an instantiated prefabRoot that all references to prefabRoot in prefab // will redirect to - _doInstantiate (rootToRedirect) { + private _doInstantiate (rootToRedirect?: any) { if (this.data._prefab) { // prefab asset is always synced this.data._prefab._synced = true; @@ -161,11 +163,12 @@ export default class Prefab extends Asset { if (!this._createFunction) { this.compileCreateFunction(); } - return this._createFunction(rootToRedirect); // this.data._instantiate(); + return this._createFunction!(rootToRedirect); // this.data._instantiate(); } - _instantiate () { - var node, useJit = false; + private _instantiate () { + let node; + let useJit: Boolean = false; if (CC_SUPPORT_JIT) { if (this.optimizationPolicy === OptimizationPolicy.SINGLE_INSTANCE) { useJit = false; @@ -194,7 +197,7 @@ export default class Prefab extends Asset { // link prefab in editor if (CC_EDITOR || CC_TEST) { - var PrefabUtils = Editor.require('scene://utils/prefab'); + const PrefabUtils = Editor.require('scene://utils/prefab'); // This operation is not necessary, but some old prefab asset may not contain complete data. PrefabUtils.linkPrefab(this, node); } diff --git a/cocos/components/WXSubContextView.js b/cocos/components/WXSubContextView.ts similarity index 90% rename from cocos/components/WXSubContextView.js rename to cocos/components/WXSubContextView.ts index cf1fc17dd2..231bf72432 100644 --- a/cocos/components/WXSubContextView.js +++ b/cocos/components/WXSubContextView.ts @@ -1,137 +1,139 @@ -/**************************************************************************** - 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. - ****************************************************************************/ - -import { Component } from './component'; -const { ImageAsset } = require('../assets/image-asset'); - -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.setPremultiplyAlpha(true); - this._tex.image = new ImageAsset(sharedCanvas); - - this._sprite = this.node.getComponent(cc.Sprite); - if (!this._sprite) { - this._sprite = this.node.addComponent(cc.Sprite); - this._sprite.srcBlendFactor = cc.macro.BlendFactor.ONE; - } - this._sprite.spriteFrame = new cc.SpriteFrame(this._tex); - } - else { - this.enabled = false; - } - }, - - onEnable () { - this.updateSubContextViewport(); - }, - - update () { - if (!this._tex || !this._context) { - return; - } - this._tex.updateImage(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; +/**************************************************************************** + 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. + ****************************************************************************/ + +import { Component } from './component'; + +import { ImageAsset } from '../assets/image-asset'; + +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(); + const sharedCanvas = this._context.canvas; + if (sharedCanvas) { + sharedCanvas.width = this.node.width; + sharedCanvas.height = this.node.height; + } + this._tex.setPremultiplyAlpha(true); + this._tex.image = new ImageAsset(sharedCanvas); + + this._sprite = this.node.getComponent(cc.Sprite); + if (!this._sprite) { + this._sprite = this.node.addComponent(cc.Sprite); + this._sprite.srcBlendFactor = cc.macro.BlendFactor.ONE; + } + this._sprite.spriteFrame = new cc.SpriteFrame(this._tex); + } + else { + this.enabled = false; + } + }, + + onEnable () { + this.updateSubContextViewport(); + }, + + update () { + if (!this._tex || !this._context) { + return; + } + this._tex.updateImage(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) { + const box = this.node.getBoundingBoxToWorld(); + const sx = cc.view._scaleX; + const 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, + }); + } + }, + }); + +} + +export default WXSubContextView; +cc.WXSubContextView = WXSubContextView; diff --git a/cocos/components/missing-script.js b/cocos/components/missing-script.ts similarity index 87% rename from cocos/components/missing-script.js rename to cocos/components/missing-script.ts index 4ee8d0ceab..f270e0c05a 100644 --- a/cocos/components/missing-script.js +++ b/cocos/components/missing-script.ts @@ -1,130 +1,131 @@ -/**************************************************************************** - Copyright (c) 2013-2016 Chukong Technologies Inc. - Copyright (c) 2017-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. - ****************************************************************************/ - -import {_getClassById} from '../core/utils/js'; -import {BUILTIN_CLASSID_RE} from '../core/utils/misc'; -import { Component } from './component'; -import {ccclass, property, inspector} from '../core/data/class-decorator'; - -/* - * A temp fallback to contain the original serialized data which can not be loaded. - */ -@ccclass('cc.MissingClass') -class MissingClass { - // the serialized data for original object - @property({ - visible: false, - editorOnly: true - }) - _$erialized = null; -} - -/* - * A temp fallback to contain the original component which can not be loaded. - */ -@ccclass('cc.MissingScript') -@inspector('packages://inspector/inspectors/comps/missing-script.js') -export default class MissingScript extends Component { - - //_scriptUuid: { - // get: function () { - // var id = this._$erialized.__type__; - // if (Editor.Utils.UuidUtils.isUuid(id)) { - // return Editor.Utils.UuidUtils.decompressUuid(id); - // } - // return ''; - // }, - // set: function (value) { - // if ( !sandbox.compiled ) { - // cc.error('Scripts not yet compiled, please fix script errors and compile first.'); - // return; - // } - // if (value && Editor.Utils.UuidUtils.isUuid(value._uuid)) { - // var classId = Editor.Utils.UuidUtils.compressUuid(value); - // if (_getClassById(classId)) { - // this._$erialized.__type__ = classId; - // Editor.Ipc.sendToWins('reload:window-scripts', sandbox.compiled); - // } - // else { - // cc.error('Can not find a component in the script which uuid is "%s".', value); - // } - // } - // else { - // cc.error('invalid script'); - // } - // } - //}, - - @property({ - serializable: false - }) - compiled = false; - - // the serialized data for original script object - @property({ - visible: false, - editorOnly: true - }) - _$erialized = null; - - constructor () { - super(); - if (CC_EDITOR) { - this.compiled = _Scene.Sandbox.compiled; - } - } - - /* - * @param {string} id - * @return {function} constructor - */ - static safeFindClass (id, data) { - var cls = _getClassById(id); - if (cls) { - return cls; - } - if (id) { - cc.deserialize.reportMissingClass(id); - return MissingScript.getMissingWrapper(id, data); - } - return null; - } - static getMissingWrapper (id, data) { - if (data.node && (/^[0-9a-zA-Z+/]{23}$/.test(id) || BUILTIN_CLASSID_RE.test(id))) { - // is component - return MissingScript; - } - else { - return MissingClass; - } - } - - onLoad () { - cc.warnID(4600, this.node.name); - } -} - -cc._MissingScript = MissingScript; +/**************************************************************************** + Copyright (c) 2013-2016 Chukong Technologies Inc. + Copyright (c) 2017-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. + ****************************************************************************/ + +import {ccclass, inspector, property} from '../core/data/class-decorator'; +import {_getClassById} from '../core/utils/js'; +import {BUILTIN_CLASSID_RE} from '../core/utils/misc'; +import { Component } from './component'; + +/* + * A temp fallback to contain the original serialized data which can not be loaded. + */ +@ccclass('cc.MissingClass') +class MissingClass { + // the serialized data for original object + @property({ + visible: false, + editorOnly: true, + }) + public _$erialized = null; +} + +/* + * A temp fallback to contain the original component which can not be loaded. + */ +@ccclass('cc.MissingScript') +@inspector('packages://inspector/inspectors/comps/missing-script.js') +export default class MissingScript extends Component { + + // _scriptUuid: { + // get: function () { + // var id = this._$erialized.__type__; + // if (Editor.Utils.UuidUtils.isUuid(id)) { + // return Editor.Utils.UuidUtils.decompressUuid(id); + // } + // return ''; + // }, + // set: function (value) { + // if ( !sandbox.compiled ) { + // cc.error('Scripts not yet compiled, please fix script errors and compile first.'); + // return; + // } + // if (value && Editor.Utils.UuidUtils.isUuid(value._uuid)) { + // var classId = Editor.Utils.UuidUtils.compressUuid(value); + // if (_getClassById(classId)) { + // this._$erialized.__type__ = classId; + // Editor.Ipc.sendToWins('reload:window-scripts', sandbox.compiled); + // } + // else { + // cc.error('Can not find a component in the script which uuid is "%s".', value); + // } + // } + // else { + // cc.error('invalid script'); + // } + // } + // }, + + /* + * @param {string} id + * @return {function} constructor + */ + public static safeFindClass (id, data) { + const cls = _getClassById(id); + if (cls) { + return cls; + } + if (id) { + cc.deserialize.reportMissingClass(id); + return MissingScript.getMissingWrapper(id, data); + } + return null; + } + public static getMissingWrapper (id, data) { + if (data.node && (/^[0-9a-zA-Z+/]{23}$/.test(id) || BUILTIN_CLASSID_RE.test(id))) { + // is component + return MissingScript; + } + else { + return MissingClass; + } + } + + @property({ + serializable: false, + }) + public compiled = false; + + // the serialized data for original script object + @property({ + visible: false, + editorOnly: true, + }) + public _$erialized = null; + + constructor () { + super(); + if (CC_EDITOR) { + // @ts-ignore + this.compiled = _Scene.Sandbox.compiled; + } + } + + public onLoad () { + cc.warnID(4600, this.node.name); + } +} + +cc._MissingScript = MissingScript; diff --git a/cocos/core/platform/CCView.js b/cocos/core/platform/CCView.ts similarity index 70% rename from cocos/core/platform/CCView.js rename to cocos/core/platform/CCView.ts index ff56b87e67..59a1144367 100644 --- a/cocos/core/platform/CCView.js +++ b/cocos/core/platform/CCView.ts @@ -1,1499 +1,1623 @@ -/**************************************************************************** - Copyright (c) 2008-2010 Ricardo Quesada - Copyright (c) 2011-2012 cocos2d-x.org - Copyright (c) 2013-2016 Chukong Technologies Inc. - Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. - - http://www.cocos2d-x.org - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - 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. - ****************************************************************************/ - -import { EventTarget } from '../event/event-target'; -import '../data/class'; -import Game from '../game'; -import Size from '../value-types/size'; -import Rect from '../value-types/rect'; - -let _currentFrame = 0; - -var __BrowserGetter = { - init(){ - if (!CC_WECHATGAME && !CC_QQPLAY) { - this.html = document.getElementsByTagName("html")[0]; - } - }, - availWidth(frame){ - if (cc.sys.isMobile || !frame || frame === this.html) - return window.innerWidth; - else - return frame.clientWidth; - }, - availHeight(frame){ - if (cc.sys.isMobile || !frame || frame === this.html) - return window.innerHeight; - else - return frame.clientHeight; - }, - meta: { - "width": "device-width" - }, - adaptationType: cc.sys.browserType -}; - -if (cc.sys.os === cc.sys.OS_IOS) // All browsers are WebView - __BrowserGetter.adaptationType = cc.sys.BROWSER_TYPE_SAFARI; - -if (CC_WECHATGAME) { - if (cc.sys.browserType === cc.sys.BROWSER_TYPE_WECHAT_GAME_SUB) { - __BrowserGetter.adaptationType = cc.sys.BROWSER_TYPE_WECHAT_GAME_SUB; - } - else { - __BrowserGetter.adaptationType = cc.sys.BROWSER_TYPE_WECHAT_GAME; - } -} - -if (CC_QQPLAY) { - __BrowserGetter.adaptationType = cc.sys.BROWSER_TYPE_QQ_PLAY; -} - -switch (__BrowserGetter.adaptationType) { - case cc.sys.BROWSER_TYPE_SAFARI: - __BrowserGetter.meta["minimal-ui"] = "true"; - case cc.sys.BROWSER_TYPE_SOUGOU: - case cc.sys.BROWSER_TYPE_WECHAT_GAME: - __BrowserGetter.availWidth = function(){ - return window.innerWidth; - }; - __BrowserGetter.availHeight = function(){ - return window.innerHeight; - }; - break; - case cc.sys.BROWSER_TYPE_WECHAT_GAME_SUB: - var sharedCanvas = window.sharedCanvas || wx.getSharedCanvas(); - __BrowserGetter.availWidth = function(){ - return sharedCanvas.width; - }; - __BrowserGetter.availHeight = function(){ - return sharedCanvas.height; - }; - break; -} - -var _scissorRect = null; - -/** - * cc.view is the singleton object which represents the game window.
- * It's main task include:
- * - Apply the design resolution policy
- * - Provide interaction with the window, like resize event on web, retina display support, etc...
- * - Manage the game view port which can be different with the window
- * - Manage the content scale and translation
- *
- * Since the cc.view is a singleton, you don't need to call any constructor or create functions,
- * the standard way to use it is by calling:
- * - cc.view.methodName();
- * - * @class View - */ -class View extends EventTarget { - constructor () { - super(); - - var _t = this, _strategyer = ContainerStrategy, _strategy = ContentStrategy; - - // Size of parent node that contains cc.game.container and cc.game.canvas - _t._frameSize = new Size(0, 0); - - // resolution size, it is the size appropriate for the app resources. - _t._designResolutionSize = new Size(0, 0); - _t._originalDesignResolutionSize = new Size(0, 0); - _t._scaleX = 1; - _t._scaleY = 1; - // Viewport is the container's rect related to content's coordinates in pixel - _t._viewportRect = new Rect(0, 0, 0, 0); - // The visible rect in content's coordinate in point - _t._visibleRect = new Rect(0, 0, 0, 0); - // Auto full screen disabled by default - _t._autoFullScreen = false; - // The device's pixel ratio (for retina displays) - _t._devicePixelRatio = 1; - // Retina disabled by default - _t._retinaEnabled = false; - // Custom callback for resize event - _t._resizeCallback = null; - _t._resizing = false; - _t._resizeWithBrowserSize = false; - _t._orientationChanging = true; - _t._isRotated = false; - _t._orientation = cc.macro.ORIENTATION_AUTO; - _t._isAdjustViewport = true; - _t._antiAliasEnabled = false; - - // Setup system default resolution policies - _t._resolutionPolicy = null; - _t._rpExactFit = new ResolutionPolicy(_strategyer.EQUAL_TO_FRAME, _strategy.EXACT_FIT); - _t._rpShowAll = new ResolutionPolicy(_strategyer.EQUAL_TO_FRAME, _strategy.SHOW_ALL); - _t._rpNoBorder = new ResolutionPolicy(_strategyer.EQUAL_TO_FRAME, _strategy.NO_BORDER); - _t._rpFixedHeight = new ResolutionPolicy(_strategyer.EQUAL_TO_FRAME, _strategy.FIXED_HEIGHT); - _t._rpFixedWidth = new ResolutionPolicy(_strategyer.EQUAL_TO_FRAME, _strategy.FIXED_WIDTH); - - cc.game.once(Game.EVENT_ENGINE_INITED, this.init, this); - } - - init () { - __BrowserGetter.init(this); - - this._initFrameSize(); - this.enableAntiAlias(true); - - var w = cc.game.canvas.width, h = cc.game.canvas.height; - this._designResolutionSize.width = w; - this._designResolutionSize.height = h; - this._originalDesignResolutionSize.width = w; - this._originalDesignResolutionSize.height = h; - this._viewportRect.width = w; - this._viewportRect.height = h; - this._visibleRect.width = w; - this._visibleRect.height = h; - - cc.winSize.width = this._visibleRect.width; - cc.winSize.height = this._visibleRect.height; - cc.visibleRect && cc.visibleRect.init(this._visibleRect); - } - - // Resize helper functions - _resizeEvent () { - let frameId = cc.director.getTotalFrames(); - if (_currentFrame !== frameId) { - _currentFrame = frameId; - } - else { - // Avoid adapte multiple times in a frame - return; - } - var view; - if (this.setDesignResolutionSize) { - view = this; - } else { - view = cc.view; - } - - // Check frame size changed or not - var prevFrameW = view._frameSize.width, prevFrameH = view._frameSize.height, prevRotated = view._isRotated; - if (cc.sys.isMobile) { - var containerStyle = cc.game.container.style, - margin = containerStyle.margin; - containerStyle.margin = '0'; - containerStyle.display = 'none'; - view._initFrameSize(); - containerStyle.margin = margin; - containerStyle.display = 'block'; - } - else { - view._initFrameSize(); - } - if (!view._orientationChanging && view._isRotated === prevRotated && view._frameSize.width === prevFrameW && view._frameSize.height === prevFrameH) - return; - - // Frame size changed, do resize works - var width = view._originalDesignResolutionSize.width; - var height = view._originalDesignResolutionSize.height; - - view._resizing = true; - if (width > 0) - view.setDesignResolutionSize(width, height, view._resolutionPolicy); - - view._resizing = false; - - if (view._resizeCallback) { - view._resizeCallback.call(); - } - } - - _orientationChange () { - cc.view._orientationChanging = true; - cc.view._resizeEvent(); - } - - /** - * !#en - * Sets view's target-densitydpi for android mobile browser. it can be set to:
- * 1. macro.DENSITYDPI_DEVICE, value is "device-dpi"
- * 2. macro.DENSITYDPI_HIGH, value is "high-dpi" (default value)
- * 3. macro.DENSITYDPI_MEDIUM, value is "medium-dpi" (browser's default value)
- * 4. macro.DENSITYDPI_LOW, value is "low-dpi"
- * 5. Custom value, e.g: "480"
- * !#zh 设置目标内容的每英寸像素点密度。 - * - * @method setTargetDensityDPI - * @param {String} densityDPI - * @deprecated since v2.0 - */ - - /** - * !#en - * Returns the current target-densitydpi value of cc.view. - * !#zh 获取目标内容的每英寸像素点密度。 - * @method getTargetDensityDPI - * @returns {String} - * @deprecated since v2.0 - */ - - /** - * !#en - * Sets whether resize canvas automatically when browser's size changed.
- * Useful only on web. - * !#zh 设置当发现浏览器的尺寸改变时,是否自动调整 canvas 尺寸大小。 - * 仅在 Web 模式下有效。 - * @method resizeWithBrowserSize - * @param {Boolean} enabled - Whether enable automatic resize with browser's resize event - */ - resizeWithBrowserSize (enabled) { - if (enabled) { - //enable - if (!this._resizeWithBrowserSize) { - this._resizeWithBrowserSize = true; - window.addEventListener('resize', this._resizeEvent); - window.addEventListener('orientationchange', this._orientationChange); - } - } else { - //disable - if (this._resizeWithBrowserSize) { - this._resizeWithBrowserSize = false; - window.removeEventListener('resize', this._resizeEvent); - window.removeEventListener('orientationchange', this._orientationChange); - } - } - } - - /** - * !#en - * Sets the callback function for cc.view's resize action,
- * this callback will be invoked before applying resolution policy,
- * so you can do any additional modifications within the callback.
- * Useful only on web. - * !#zh 设置 cc.view 调整视窗尺寸行为的回调函数, - * 这个回调函数会在应用适配模式之前被调用, - * 因此你可以在这个回调函数内添加任意附加改变, - * 仅在 Web 平台下有效。 - * @method setResizeCallback - * @param {Function|Null} callback - The callback function - */ - setResizeCallback (callback) { - if (typeof callback === 'function' || callback == null) { - this._resizeCallback = callback; - } - } - - /** - * !#en - * Sets the orientation of the game, it can be landscape, portrait or auto. - * When set it to landscape or portrait, and screen w/h ratio doesn't fit, - * cc.view will automatically rotate the game canvas using CSS. - * Note that this function doesn't have any effect in native, - * in native, you need to set the application orientation in native project settings - * !#zh 设置游戏屏幕朝向,它能够是横版,竖版或自动。 - * 当设置为横版或竖版,并且屏幕的宽高比例不匹配时, - * cc.view 会自动用 CSS 旋转游戏场景的 canvas, - * 这个方法不会对 native 部分产生任何影响,对于 native 而言,你需要在应用设置中的设置排版。 - * @method setOrientation - * @param {Number} orientation - Possible values: macro.ORIENTATION_LANDSCAPE | macro.ORIENTATION_PORTRAIT | macro.ORIENTATION_AUTO - */ - setOrientation (orientation) { - orientation = orientation & cc.macro.ORIENTATION_AUTO; - if (orientation && this._orientation !== orientation) { - this._orientation = orientation; - } - } - - _initFrameSize () { - var locFrameSize = this._frameSize; - var w = __BrowserGetter.availWidth(cc.game.frame); - var h = __BrowserGetter.availHeight(cc.game.frame); - var isLandscape = w >= h; - - if (CC_EDITOR || !cc.sys.isMobile || - (isLandscape && this._orientation & cc.macro.ORIENTATION_LANDSCAPE) || - (!isLandscape && this._orientation & cc.macro.ORIENTATION_PORTRAIT)) { - locFrameSize.width = w; - locFrameSize.height = h; - cc.game.container.style['-webkit-transform'] = 'rotate(0deg)'; - cc.game.container.style.transform = 'rotate(0deg)'; - this._isRotated = false; - } - else { - locFrameSize.width = h; - locFrameSize.height = w; - cc.game.container.style['-webkit-transform'] = 'rotate(90deg)'; - cc.game.container.style.transform = 'rotate(90deg)'; - cc.game.container.style['-webkit-transform-origin'] = '0px 0px 0px'; - cc.game.container.style.transformOrigin = '0px 0px 0px'; - this._isRotated = true; - - // Fix for issue: https://github.com/cocos-creator/fireball/issues/8365 - // Reference: https://www.douban.com/note/343402554/ - // For Chrome, z-index not working after container transform rotate 90deg. - // Because 'transform' style adds canvas (the top-element of container) to a new stack context. - // That causes the DOM Input was hidden under canvas. - // This should be done after container rotated, instead of in style-mobile.css. - cc.game.canvas.style['-webkit-transform'] = 'translateZ(0px)'; - cc.game.canvas.style.transform = 'translateZ(0px)'; - } - if (this._orientationChanging) { - setTimeout(function () { - cc.view._orientationChanging = false; - }, 1000); - } - } - - // hack - _adjustSizeKeepCanvasSize () { - var designWidth = this._originalDesignResolutionSize.width; - var designHeight = this._originalDesignResolutionSize.height; - if (designWidth > 0) - this.setDesignResolutionSize(designWidth, designHeight, this._resolutionPolicy); - } - - _setViewportMeta (metas, overwrite) { - var vp = document.getElementById("cocosMetaElement"); - if(vp && overwrite){ - document.head.removeChild(vp); - } - - var elems = document.getElementsByName("viewport"), - currentVP = elems ? elems[0] : null, - content, key, pattern; - - content = currentVP ? currentVP.content : ""; - vp = vp || document.createElement("meta"); - vp.id = "cocosMetaElement"; - vp.name = "viewport"; - vp.content = ""; - - for (key in metas) { - if (content.indexOf(key) == -1) { - content += "," + key + "=" + metas[key]; - } - else if (overwrite) { - pattern = new RegExp(key+"\s*=\s*[^,]+"); - content.replace(pattern, key + "=" + metas[key]); - } - } - if(/^,/.test(content)) - content = content.substr(1); - - vp.content = content; - // For adopting certain android devices which don't support second viewport - if (currentVP) - currentVP.content = content; - - document.head.appendChild(vp); - } - - _adjustViewportMeta () { - if (this._isAdjustViewport && !CC_JSB && !CC_WECHATGAME && !CC_QQPLAY) { - this._setViewportMeta(__BrowserGetter.meta, false); - this._isAdjustViewport = false; - } - } - - /** - * !#en - * Sets whether the engine modify the "viewport" meta in your web page.
- * It's enabled by default, we strongly suggest you not to disable it.
- * And even when it's enabled, you can still set your own "viewport" meta, it won't be overridden
- * Only useful on web - * !#zh 设置引擎是否调整 viewport meta 来配合屏幕适配。 - * 默认设置为启动,我们强烈建议你不要将它设置为关闭。 - * 即使当它启动时,你仍然能够设置你的 viewport meta,它不会被覆盖。 - * 仅在 Web 模式下有效 - * @method adjustViewportMeta - * @param {Boolean} enabled - Enable automatic modification to "viewport" meta - */ - adjustViewportMeta (enabled) { - this._isAdjustViewport = enabled; - } - - /** - * !#en - * Retina support is enabled by default for Apple device but disabled for other devices,
- * it takes effect only when you called setDesignResolutionPolicy
- * Only useful on web - * !#zh 对于 Apple 这种支持 Retina 显示的设备上默认进行优化而其他类型设备默认不进行优化, - * 它仅会在你调用 setDesignResolutionPolicy 方法时有影响。 - * 仅在 Web 模式下有效。 - * @method enableRetina - * @param {Boolean} enabled - Enable or disable retina display - */ - enableRetina(enabled) { - this._retinaEnabled = !!enabled; - } - - /** - * !#en - * Check whether retina display is enabled.
- * Only useful on web - * !#zh 检查是否对 Retina 显示设备进行优化。 - * 仅在 Web 模式下有效。 - * @method isRetinaEnabled - * @return {Boolean} - */ - isRetinaEnabled() { - return this._retinaEnabled; - } - - /** - * !#en Whether to Enable on anti-alias - * !#zh 控制抗锯齿是否开启 - * @method enableAntiAlias - * @param {Boolean} enabled - Enable or not anti-alias - */ - enableAntiAlias (enabled) { - if (this._antiAliasEnabled === enabled) { - return; - } - this._antiAliasEnabled = enabled; - if (cc.game.renderType === Game.RENDER_TYPE_WEBGL) { - var cache = cc.loader._cache; - for (var key in cache) { - var item = cache[key]; - var tex = item && item.content instanceof cc.Texture2D ? item.content : null; - if (tex) { - var Filter = cc.Texture2D.Filter; - if (enabled) { - tex.setFilters(Filter.LINEAR, Filter.LINEAR); - } - else { - tex.setFilters(Filter.NEAREST, Filter.NEAREST); - } - } - } - } - else if (cc.game.renderType === Game.RENDER_TYPE_CANVAS) { - var ctx = cc.game.canvas.getContext('2d'); - ctx.imageSmoothingEnabled = enabled; - ctx.mozImageSmoothingEnabled = enabled; - } - } - - /** - * !#en Returns whether the current enable on anti-alias - * !#zh 返回当前是否抗锯齿 - * @method isAntiAliasEnabled - * @return {Boolean} - */ - isAntiAliasEnabled () { - return this._antiAliasEnabled; - } - /** - * !#en - * If enabled, the application will try automatically to enter full screen mode on mobile devices
- * You can pass true as parameter to enable it and disable it by passing false.
- * Only useful on web - * !#zh 启动时,移动端游戏会在移动端自动尝试进入全屏模式。 - * 你能够传入 true 为参数去启动它,用 false 参数来关闭它。 - * @method enableAutoFullScreen - * @param {Boolean} enabled - Enable or disable auto full screen on mobile devices - */ - enableAutoFullScreen(enabled) { - if (enabled && - enabled !== this._autoFullScreen && - cc.sys.isMobile && - cc.sys.browserType !== cc.sys.BROWSER_TYPE_WECHAT) { - // Automatically full screen when user touches on mobile version - this._autoFullScreen = true; - cc.screen.autoFullScreen(cc.game.frame); - } - else { - this._autoFullScreen = false; - } - } - - /** - * !#en - * Check whether auto full screen is enabled.
- * Only useful on web - * !#zh 检查自动进入全屏模式是否启动。 - * 仅在 Web 模式下有效。 - * @method isAutoFullScreenEnabled - * @return {Boolean} Auto full screen enabled or not - */ - isAutoFullScreenEnabled() { - return this._autoFullScreen; - } - - /* - * Not support on native.
- * On web, it sets the size of the canvas. - * !#zh 这个方法并不支持 native 平台,在 Web 平台下,可以用来设置 canvas 尺寸。 - * @method setCanvasSize - * @param {Number} width - * @param {Number} height - */ - setCanvasSize (width, height) { - var canvas = cc.game.canvas; - var container = cc.game.container; - - canvas.width = width * this._devicePixelRatio; - canvas.height = height * this._devicePixelRatio; - - // canvas.width = width; - // canvas.height = height; - - canvas.style.width = width + 'px'; - canvas.style.height = height + 'px'; - - container.style.width = width + 'px'; - container.style.height = height + 'px'; - - this._resizeEvent(); - } - - /** - * !#en - * Returns the canvas size of the view.
- * On native platforms, it returns the screen size since the view is a fullscreen view.
- * On web, it returns the size of the canvas element. - * !#zh 返回视图中 canvas 的尺寸。 - * 在 native 平台下,它返回全屏视图下屏幕的尺寸。 - * 在 Web 平台下,它返回 canvas 元素尺寸。 - * @method getCanvasSize - * @return {Size} - */ - getCanvasSize () { - return cc.size(cc.game.canvas.width, cc.game.canvas.height); - } - - /** - * !#en - * Returns the frame size of the view.
- * On native platforms, it returns the screen size since the view is a fullscreen view.
- * On web, it returns the size of the canvas's outer DOM element. - * !#zh 返回视图中边框尺寸。 - * 在 native 平台下,它返回全屏视图下屏幕的尺寸。 - * 在 web 平台下,它返回 canvas 元素的外层 DOM 元素尺寸。 - * @method getFrameSize - * @return {Size} - */ - getFrameSize () { - return cc.size(this._frameSize.width, this._frameSize.height); - } - - /** - * !#en - * On native, it sets the frame size of view.
- * On web, it sets the size of the canvas's outer DOM element. - * !#zh 在 native 平台下,设置视图框架尺寸。 - * 在 web 平台下,设置 canvas 外层 DOM 元素尺寸。 - * @method setFrameSize - * @param {Number} width - * @param {Number} height - */ - setFrameSize (width, height) { - this._frameSize.width = width; - this._frameSize.height = height; - cc.frame.style.width = width + "px"; - cc.frame.style.height = height + "px"; - this._resizeEvent(); - } - - /** - * !#en - * Returns the visible area size of the view port. - * !#zh 返回视图窗口可见区域尺寸。 - * @method getVisibleSize - * @return {Size} - */ - getVisibleSize () { - return cc.size(this._visibleRect.width,this._visibleRect.height); - } - - /** - * !#en - * Returns the visible area size of the view port. - * !#zh 返回视图窗口可见区域像素尺寸。 - * @method getVisibleSizeInPixel - * @return {Size} - */ - getVisibleSizeInPixel () { - return cc.size( this._visibleRect.width * this._scaleX, - this._visibleRect.height * this._scaleY ); - } - - /** - * !#en - * Returns the visible origin of the view port. - * !#zh 返回视图窗口可见区域原点。 - * @method getVisibleOrigin - * @return {Vec2} - */ - getVisibleOrigin () { - return cc.v2(this._visibleRect.x,this._visibleRect.y); - } - - /** - * !#en - * Returns the visible origin of the view port. - * !#zh 返回视图窗口可见区域像素原点。 - * @method getVisibleOriginInPixel - * @return {Vec2} - */ - getVisibleOriginInPixel () { - return cc.v2(this._visibleRect.x * this._scaleX, - this._visibleRect.y * this._scaleY); - } - - /** - * !#en - * Returns the current resolution policy - * !#zh 返回当前分辨率方案 - * @see ResolutionPolicy - * @method getResolutionPolicy - * @return {ResolutionPolicy} - */ - getResolutionPolicy () { - return this._resolutionPolicy; - } - - /** - * !#en - * Sets the current resolution policy - * !#zh 设置当前分辨率模式 - * @see ResolutionPolicy - * @method setResolutionPolicy - * @param {ResolutionPolicy|Number} resolutionPolicy - */ - setResolutionPolicy (resolutionPolicy) { - var _t = this; - if (resolutionPolicy instanceof ResolutionPolicy) { - _t._resolutionPolicy = resolutionPolicy; - } - // Ensure compatibility with JSB - else { - var _locPolicy = ResolutionPolicy; - if(resolutionPolicy === _locPolicy.EXACT_FIT) - _t._resolutionPolicy = _t._rpExactFit; - if(resolutionPolicy === _locPolicy.SHOW_ALL) - _t._resolutionPolicy = _t._rpShowAll; - if(resolutionPolicy === _locPolicy.NO_BORDER) - _t._resolutionPolicy = _t._rpNoBorder; - if(resolutionPolicy === _locPolicy.FIXED_HEIGHT) - _t._resolutionPolicy = _t._rpFixedHeight; - if(resolutionPolicy === _locPolicy.FIXED_WIDTH) - _t._resolutionPolicy = _t._rpFixedWidth; - } - } - - /** - * !#en - * Sets the resolution policy with designed view size in points.
- * The resolution policy include:
- * [1] ResolutionExactFit Fill screen by stretch-to-fit: if the design resolution ratio of width to height is different from the screen resolution ratio, your game view will be stretched.
- * [2] ResolutionNoBorder Full screen without black border: if the design resolution ratio of width to height is different from the screen resolution ratio, two areas of your game view will be cut.
- * [3] ResolutionShowAll Full screen with black border: if the design resolution ratio of width to height is different from the screen resolution ratio, two black borders will be shown.
- * [4] ResolutionFixedHeight Scale the content's height to screen's height and proportionally scale its width
- * [5] ResolutionFixedWidth Scale the content's width to screen's width and proportionally scale its height
- * [ResolutionPolicy] [Web only feature] Custom resolution policy, constructed by ResolutionPolicy
- * !#zh 通过设置设计分辨率和匹配模式来进行游戏画面的屏幕适配。 - * @method setDesignResolutionSize - * @param {Number} width Design resolution width. - * @param {Number} height Design resolution height. - * @param {ResolutionPolicy|Number} resolutionPolicy The resolution policy desired - */ - setDesignResolutionSize (width, height, resolutionPolicy) { - // Defensive code - if( !(width > 0 || height > 0) ){ - cc.logID(2200); - return; - } - - this.setResolutionPolicy(resolutionPolicy); - var policy = this._resolutionPolicy; - if (policy) { - policy.preApply(this); - } - - // Reinit frame size - if (cc.sys.isMobile) - this._adjustViewportMeta(); - - // Permit to re-detect the orientation of device. - this._orientationChanging = true; - // If resizing, then frame size is already initialized, this logic should be improved - if (!this._resizing) - this._initFrameSize(); - - if (!policy) { - cc.logID(2201); - return; - } - - this._originalDesignResolutionSize.width = this._designResolutionSize.width = width; - this._originalDesignResolutionSize.height = this._designResolutionSize.height = height; - - var result = policy.apply(this, this._designResolutionSize); - - if(result.scale && result.scale.length === 2){ - this._scaleX = result.scale[0]; - this._scaleY = result.scale[1]; - } - - if(result.viewport){ - var vp = this._viewportRect, - vb = this._visibleRect, - rv = result.viewport; - - vp.x = rv.x; - vp.y = rv.y; - vp.width = rv.width; - vp.height = rv.height; - - vb.x = 0; - vb.y = 0; - vb.width = rv.width / this._scaleX; - vb.height = rv.height / this._scaleY; - } - - policy.postApply(this); - cc.winSize.width = this._visibleRect.width; - cc.winSize.height = this._visibleRect.height; - - cc.visibleRect && cc.visibleRect.init(this._visibleRect); - - this.emit('design-resolution-changed'); - } - - /** - * !#en - * Returns the designed size for the view. - * Default resolution size is the same as 'getFrameSize'. - * !#zh 返回视图的设计分辨率。 - * 默认下分辨率尺寸同 `getFrameSize` 方法相同 - * @method getDesignResolutionSize - * @return {Size} - */ - getDesignResolutionSize () { - return cc.size(this._designResolutionSize.width, this._designResolutionSize.height); - } - - /** - * !#en - * Sets the container to desired pixel resolution and fit the game content to it. - * This function is very useful for adaptation in mobile browsers. - * In some HD android devices, the resolution is very high, but its browser performance may not be very good. - * In this case, enabling retina display is very costy and not suggested, and if retina is disabled, the image may be blurry. - * But this API can be helpful to set a desired pixel resolution which is in between. - * This API will do the following: - * 1. Set viewport's width to the desired width in pixel - * 2. Set body width to the exact pixel resolution - * 3. The resolution policy will be reset with designed view size in points. - * !#zh 设置容器(container)需要的像素分辨率并且适配相应分辨率的游戏内容。 - * @method setRealPixelResolution - * @param {Number} width Design resolution width. - * @param {Number} height Design resolution height. - * @param {ResolutionPolicy|Number} resolutionPolicy The resolution policy desired - */ - setRealPixelResolution (width, height, resolutionPolicy) { - if (!CC_JSB && !CC_WECHATGAME && !CC_QQPLAY) { - // Set viewport's width - this._setViewportMeta({"width": width}, true); - - // Set body width to the exact pixel resolution - document.documentElement.style.width = width + "px"; - document.body.style.width = width + "px"; - document.body.style.left = "0px"; - document.body.style.top = "0px"; - } - - // Reset the resolution size and policy - this.setDesignResolutionSize(width, height, resolutionPolicy); - } - - /** - * !#en - * Sets view port rectangle with points. - * !#zh 用设计分辨率下的点尺寸来设置视窗。 - * @method setViewportInPoints - * @deprecated since v2.0 - * @param {Number} x - * @param {Number} y - * @param {Number} w width - * @param {Number} h height - */ - setViewportInPoints (x, y, w, h) { - var locScaleX = this._scaleX, locScaleY = this._scaleY; - cc.game._renderContext.viewport((x * locScaleX + this._viewportRect.x), - (y * locScaleY + this._viewportRect.y), - (w * locScaleX), - (h * locScaleY)); - } - - /** - * !#en - * Sets Scissor rectangle with points. - * !#zh 用设计分辨率下的点的尺寸来设置 scissor 剪裁区域。 - * @method setScissorInPoints - * @deprecated since v2.0 - * @param {Number} x - * @param {Number} y - * @param {Number} w - * @param {Number} h - */ - setScissorInPoints (x, y, w, h) { - let scaleX = this._scaleX, scaleY = this._scaleY; - let sx = Math.ceil(x * scaleX + this._viewportRect.x); - let sy = Math.ceil(y * scaleY + this._viewportRect.y); - let sw = Math.ceil(w * scaleX); - let sh = Math.ceil(h * scaleY); - let gl = cc.game._renderContext; - - if (!_scissorRect) { - var boxArr = gl.getParameter(gl.SCISSOR_BOX); - _scissorRect = new Rect(boxArr[0], boxArr[1], boxArr[2], boxArr[3]); - } - - if (_scissorRect.x !== sx || _scissorRect.y !== sy || _scissorRect.width !== sw || _scissorRect.height !== sh) { - _scissorRect.x = sx; - _scissorRect.y = sy; - _scissorRect.width = sw; - _scissorRect.height = sh; - gl.scissor(sx, sy, sw, sh); - } - } - - /** - * !#en - * Returns whether GL_SCISSOR_TEST is enable - * !#zh 检查 scissor 是否生效。 - * @method isScissorEnabled - * @deprecated since v2.0 - * @return {Boolean} - */ - isScissorEnabled () { - return cc.game._renderContext.isEnabled(gl.SCISSOR_TEST); - } - - /** - * !#en - * Returns the current scissor rectangle - * !#zh 返回当前的 scissor 剪裁区域。 - * @method getScissorRect - * @deprecated since v2.0 - * @return {Rect} - */ - getScissorRect () { - if (!_scissorRect) { - var boxArr = gl.getParameter(gl.SCISSOR_BOX); - _scissorRect = new Rect(boxArr[0], boxArr[1], boxArr[2], boxArr[3]); - } - var scaleXFactor = 1 / this._scaleX; - var scaleYFactor = 1 / this._scaleY; - return new Rect( - (_scissorRect.x - this._viewportRect.x) * scaleXFactor, - (_scissorRect.y - this._viewportRect.y) * scaleYFactor, - _scissorRect.width * scaleXFactor, - _scissorRect.height * scaleYFactor - ); - } - - /** - * !#en - * Returns the view port rectangle. - * !#zh 返回视窗剪裁区域。 - * @method getViewportRect - * @return {Rect} - */ - getViewportRect () { - return this._viewportRect; - } - - /** - * !#en - * Returns scale factor of the horizontal direction (X axis). - * !#zh 返回横轴的缩放比,这个缩放比是将画布像素分辨率放到设计分辨率的比例。 - * @method getScaleX - * @return {Number} - */ - getScaleX () { - return this._scaleX; - } - - /** - * !#en - * Returns scale factor of the vertical direction (Y axis). - * !#zh 返回纵轴的缩放比,这个缩放比是将画布像素分辨率缩放到设计分辨率的比例。 - * @method getScaleY - * @return {Number} - */ - getScaleY () { - return this._scaleY; - } - - /** - * !#en - * Returns device pixel ratio for retina display. - * !#zh 返回设备或浏览器像素比例。 - * @method getDevicePixelRatio - * @return {Number} - */ - getDevicePixelRatio() { - return this._devicePixelRatio; - } - - /** - * !#en - * Returns the real location in view for a translation based on a related position - * !#zh 将屏幕坐标转换为游戏视图下的坐标。 - * @method convertToLocationInView - * @param {Number} tx - The X axis translation - * @param {Number} ty - The Y axis translation - * @param {Object} relatedPos - The related position object including "left", "top", "width", "height" informations - * @return {Vec2} - */ - convertToLocationInView (tx, ty, relatedPos, out) { - let result = out || cc.v2(); - let x = this._devicePixelRatio * (tx - relatedPos.left); - let y = this._devicePixelRatio * (relatedPos.top + relatedPos.height - ty); - if (this._isRotated) { - result.x = cc.game.canvas.width - y; - result.y = x; - } - else { - result.x = x; - result.y = y; - } - return result; - } - - _convertMouseToLocation(in_out_point, relatedPos){ - in_out_point.x = this._devicePixelRatio * (in_out_point.x - relatedPos.left); - in_out_point.y = this._devicePixelRatio * (relatedPos.top + relatedPos.height - in_out_point.y); - } - - // _convertMouseToLocationInView (in_out_point, relatedPos) { - // var viewport = this._viewportRect, _t = this; - // in_out_point.x = ((_t._devicePixelRatio * (in_out_point.x - relatedPos.left)) - viewport.x) / _t._scaleX; - // in_out_point.y = (_t._devicePixelRatio * (relatedPos.top + relatedPos.height - in_out_point.y) - viewport.y) / _t._scaleY; - // } - - _convertPointWithScale (point) { - var viewport = this._viewportRect; - point.x = (point.x - viewport.x) / this._scaleX; - point.y = (point.y - viewport.y) / this._scaleY; - } - - _convertTouchWidthScale(selTouch){ - const viewport = this._viewportRect; - const scaleX = this._scaleX; - const scaleY = this._scaleY; - - selTouch._point.x = (selTouch._point.x - viewport.x) / scaleX; - selTouch._point.y = (selTouch._point.y - viewport.y) / scaleY; - selTouch._prevPoint.x = (selTouch._prevPoint.x - viewport.x) / scaleX; - selTouch._prevPoint.y = (selTouch._prevPoint.y - viewport.y) / scaleY; - } - - _convertTouchesWithScale (touches) { - var viewport = this._viewportRect, scaleX = this._scaleX, scaleY = this._scaleY, - selTouch, selPoint, selPrePoint; - for (var i = 0; i < touches.length; i++) { - selTouch = touches[i]; - selPoint = selTouch._point; - selPrePoint = selTouch._prevPoint; - - selPoint.x = (selPoint.x - viewport.x) / scaleX; - selPoint.y = (selPoint.y - viewport.y) / scaleY; - selPrePoint.x = (selPrePoint.x - viewport.x) / scaleX; - selPrePoint.y = (selPrePoint.y - viewport.y) / scaleY; - } - } -}; - -/** - * !en - * Emit when design resolution changed. - * !zh - * 当设计分辨率改变时发送。 - * @event design-resolution-changed - - -/** - *

cc.game.containerStrategy class is the root strategy class of container's scale strategy, - * it controls the behavior of how to scale the cc.game.container and cc.game.canvas object

- * - * @class ContainerStrategy - */ -class ContainerStrategy { - /** - * !#en - * Manipulation before appling the strategy - * !#zh 在应用策略之前的操作 - * @method preApply - * @param {View} view - The target view - */ - preApply (view) { - } - - /** - * !#en - * Function to apply this strategy - * !#zh 策略应用方法 - * @method apply - * @param {View} view - * @param {Size} designedResolution - */ - apply (view, designedResolution) { - } - - /** - * !#en - * Manipulation after applying the strategy - * !#zh 策略调用之后的操作 - * @method postApply - * @param {View} view The target view - */ - postApply (view) { - - } - - _setupContainer (view, w, h) { - var locCanvas = cc.game.canvas, locContainer = cc.game.container; - - if (cc.sys.platform !== cc.sys.WECHAT_GAME) { - if (cc.sys.os === cc.sys.OS_ANDROID) { - document.body.style.width = (view._isRotated ? h : w) + 'px'; - document.body.style.height = (view._isRotated ? w : h) + 'px'; - } - // Setup style - locContainer.style.width = locCanvas.style.width = w + 'px'; - locContainer.style.height = locCanvas.style.height = h + 'px'; - } - // Setup pixel ratio for retina display - var devicePixelRatio = view._devicePixelRatio = 1; - if (view.isRetinaEnabled()) - devicePixelRatio = view._devicePixelRatio = Math.min(2, window.devicePixelRatio || 1); - // Setup canvas - locCanvas.width = w * devicePixelRatio; - locCanvas.height = h * devicePixelRatio; - } - - _fixContainer () { - // Add container to document body - document.body.insertBefore(cc.game.container, document.body.firstChild); - // Set body's width height to window's size, and forbid overflow, so that game will be centered - var bs = document.body.style; - bs.width = window.innerWidth + "px"; - bs.height = window.innerHeight + "px"; - bs.overflow = "hidden"; - // Body size solution doesn't work on all mobile browser so this is the aleternative: fixed container - var contStyle = cc.game.container.style; - contStyle.position = "fixed"; - contStyle.left = contStyle.top = "0px"; - // Reposition body - document.body.scrollTop = 0; - } -} -ContainerStrategy.prototype.name = "ContainerStrategy"; - -/** - *

ContentStrategy class is the root strategy class of content's scale strategy, - * it controls the behavior of how to scale the scene and setup the viewport for the game

- * - * @class ContentStrategy - */ -class ContentStrategy { - constructor () { - this._result = { - scale: [1, 1], - viewport: null - }; - } - - _buildResult (containerW, containerH, contentW, contentH, scaleX, scaleY) { - // Makes content fit better the canvas - Math.abs(containerW - contentW) < 2 && (contentW = containerW); - Math.abs(containerH - contentH) < 2 && (contentH = containerH); - - var viewport = new Rect(Math.round((containerW - contentW) / 2), - Math.round((containerH - contentH) / 2), - contentW, contentH); - - // Translate the content - if (cc.game.renderType === Game.RENDER_TYPE_CANVAS){ - //TODO: modify something for setTransform - //game._renderContext.translate(viewport.x, viewport.y + contentH); - } - - this._result.scale = [scaleX, scaleY]; - this._result.viewport = viewport; - return this._result; - } - - /** - * !#en - * Manipulation before applying the strategy - * !#zh 策略应用前的操作 - * @method preApply - * @param {View} view - The target view - */ - preApply (view) { - } - - /** - * !#en Function to apply this strategy - * The return value is {scale: [scaleX, scaleY], viewport: {new Rect}}, - * The target view can then apply these value to itself, it's preferred not to modify directly its private variables - * !#zh 调用策略方法 - * @method apply - * @param {View} view - * @param {Size} designedResolution - * @return {Object} scaleAndViewportRect - */ - apply (view, designedResolution) { - return {"scale": [1, 1]}; - } - - /** - * !#en - * Manipulation after applying the strategy - * !#zh 策略调用之后的操作 - * @method postApply - * @param {View} view - The target view - */ - postApply (view) { - } -} -ContentStrategy.prototype.name = "ContentStrategy"; - -(function () { - -// Container scale strategys - /** - * @class EqualToFrame - * @extends ContainerStrategy - */ - class EqualToFrame extends ContainerStrategy { - apply (view) { - var frameH = view._frameSize.height, containerStyle = cc.game.container.style; - this._setupContainer(view, view._frameSize.width, view._frameSize.height); - // Setup container's margin and padding - if (view._isRotated) { - containerStyle.margin = '0 0 0 ' + frameH + 'px'; - } - else { - containerStyle.margin = '0px'; - } - containerStyle.padding = "0px"; - } - } - EqualToFrame.prototype.name = "EqualToFrame"; - - /** - * @class ProportionalToFrame - * @extends ContainerStrategy - */ - class ProportionalToFrame extends ContainerStrategy { - apply (view, designedResolution) { - var frameW = view._frameSize.width, frameH = view._frameSize.height, containerStyle = cc.game.container.style, - designW = designedResolution.width, designH = designedResolution.height, - scaleX = frameW / designW, scaleY = frameH / designH, - containerW, containerH; - - scaleX < scaleY ? (containerW = frameW, containerH = designH * scaleX) : (containerW = designW * scaleY, containerH = frameH); - - // Adjust container size with integer value - var offx = Math.round((frameW - containerW) / 2); - var offy = Math.round((frameH - containerH) / 2); - containerW = frameW - 2 * offx; - containerH = frameH - 2 * offy; - - this._setupContainer(view, containerW, containerH); - if (!CC_EDITOR) { - // Setup container's margin and padding - if (view._isRotated) { - containerStyle.margin = '0 0 0 ' + frameH + 'px'; - } - else { - containerStyle.margin = '0px'; - } - containerStyle.paddingLeft = offx + "px"; - containerStyle.paddingRight = offx + "px"; - containerStyle.paddingTop = offy + "px"; - containerStyle.paddingBottom = offy + "px"; - } - } - }; - ProportionalToFrame.prototype.name = "ProportionalToFrame"; - -// Alias: Strategy that makes the container's size equals to the frame's size - ContainerStrategy.EQUAL_TO_FRAME = new EqualToFrame(); -// Alias: Strategy that scale proportionally the container's size to frame's size - ContainerStrategy.PROPORTION_TO_FRAME = new ProportionalToFrame(); - -// Content scale strategys - class ExactFit extends ContentStrategy { - apply (view, designedResolution) { - var containerW = cc.game.canvas.width, containerH = cc.game.canvas.height, - scaleX = containerW / designedResolution.width, scaleY = containerH / designedResolution.height; - - return this._buildResult(containerW, containerH, containerW, containerH, scaleX, scaleY); - } - } - ExactFit.prototype.name = "ExactFit"; - - class ShowAll extends ContentStrategy { - apply (view, designedResolution) { - var containerW = cc.game.canvas.width, containerH = cc.game.canvas.height, - designW = designedResolution.width, designH = designedResolution.height, - scaleX = containerW / designW, scaleY = containerH / designH, scale = 0, - contentW, contentH; - - scaleX < scaleY ? (scale = scaleX, contentW = containerW, contentH = designH * scale) - : (scale = scaleY, contentW = designW * scale, contentH = containerH); - - return this._buildResult(containerW, containerH, contentW, contentH, scale, scale); - } - } - ShowAll.prototype.name = "ShowAll"; - - class NoBorder extends ContentStrategy { - apply (view, designedResolution) { - var containerW = cc.game.canvas.width, containerH = cc.game.canvas.height, - designW = designedResolution.width, designH = designedResolution.height, - scaleX = containerW / designW, scaleY = containerH / designH, scale, - contentW, contentH; - - scaleX < scaleY ? (scale = scaleY, contentW = designW * scale, contentH = containerH) - : (scale = scaleX, contentW = containerW, contentH = designH * scale); - - return this._buildResult(containerW, containerH, contentW, contentH, scale, scale); - } - } - NoBorder.prototype.name = "NoBorder"; - - class FixedHeight extends ContentStrategy { - apply (view, designedResolution) { - var containerW = cc.game.canvas.width, containerH = cc.game.canvas.height, - designH = designedResolution.height, scale = containerH / designH, - contentW = containerW, contentH = containerH; - - return this._buildResult(containerW, containerH, contentW, contentH, scale, scale); - } - } - FixedHeight.prototype.name = "FixedHeight"; - - class FixedWidth extends ContentStrategy { - apply (view, designedResolution) { - var containerW = cc.game.canvas.width, containerH = cc.game.canvas.height, - designW = designedResolution.width, scale = containerW / designW, - contentW = containerW, contentH = containerH; - - return this._buildResult(containerW, containerH, contentW, contentH, scale, scale); - } - } - FixedWidth.prototype.name = "FixedWidth"; - -// Alias: Strategy to scale the content's size to container's size, non proportional - ContentStrategy.EXACT_FIT = new ExactFit(); -// Alias: Strategy to scale the content's size proportionally to maximum size and keeps the whole content area to be visible - ContentStrategy.SHOW_ALL = new ShowAll(); -// Alias: Strategy to scale the content's size proportionally to fill the whole container area - ContentStrategy.NO_BORDER = new NoBorder(); -// Alias: Strategy to scale the content's height to container's height and proportionally scale its width - ContentStrategy.FIXED_HEIGHT = new FixedHeight(); -// Alias: Strategy to scale the content's width to container's width and proportionally scale its height - ContentStrategy.FIXED_WIDTH = new FixedWidth(); - -})(); - -/** - *

ResolutionPolicy class is the root strategy class of scale strategy, - * its main task is to maintain the compatibility with Cocos2d-x

- * - * @class ResolutionPolicy - */ -/** - * @method constructor - * @param {ContainerStrategy} containerStg The container strategy - * @param {ContentStrategy} contentStg The content strategy - */ -class ResolutionPolicy { - /** - * Constructor of ResolutionPolicy - * @param {ContainerStrategy} containerStg - * @param {ContentStrategy} contentStg - */ - constructor (containerStg, contentStg) { - this._containerStrategy = null; - this._contentStrategy = null; - this.setContainerStrategy(containerStg); - this.setContentStrategy(contentStg); - } - - get canvasSize () { - return cc.v2(cc.game.canvas.width, cc.game.canvas.height); - } - - /** - * !#en Manipulation before applying the resolution policy - * !#zh 策略应用前的操作 - * @method preApply - * @param {View} view The target view - */ - preApply (view) { - this._containerStrategy.preApply(view); - this._contentStrategy.preApply(view); - } - - /** - * !#en Function to apply this resolution policy - * The return value is {scale: [scaleX, scaleY], viewport: {new Rect}}, - * The target view can then apply these value to itself, it's preferred not to modify directly its private variables - * !#zh 调用策略方法 - * @method apply - * @param {View} view - The target view - * @param {Size} designedResolution - The user defined design resolution - * @return {Object} An object contains the scale X/Y values and the viewport rect - */ - apply (view, designedResolution) { - this._containerStrategy.apply(view, designedResolution); - return this._contentStrategy.apply(view, designedResolution); - } - - /** - * !#en Manipulation after appyling the strategy - * !#zh 策略应用之后的操作 - * @method postApply - * @param {View} view - The target view - */ - postApply (view) { - this._containerStrategy.postApply(view); - this._contentStrategy.postApply(view); - } - - /** - * !#en - * Setup the container's scale strategy - * !#zh 设置容器的适配策略 - * @method setContainerStrategy - * @param {ContainerStrategy} containerStg - */ - setContainerStrategy (containerStg) { - if (containerStg instanceof ContainerStrategy) - this._containerStrategy = containerStg; - } - - /** - * !#en - * Setup the content's scale strategy - * !#zh 设置内容的适配策略 - * @method setContentStrategy - * @param {ContentStrategy} contentStg - */ - setContentStrategy (contentStg) { - if (contentStg instanceof ContentStrategy) - this._contentStrategy = contentStg; - } -} -ResolutionPolicy.prototype.name = "ResolutionPolicy"; - -/** - * The entire application is visible in the specified area without trying to preserve the original aspect ratio.
- * Distortion can occur, and the application may appear stretched or compressed. - * @property {Number} EXACT_FIT - * @readonly - * @static - */ -ResolutionPolicy.EXACT_FIT = 0; - -/** - * The entire application fills the specified area, without distortion but possibly with some cropping,
- * while maintaining the original aspect ratio of the application. - * @property {Number} NO_BORDER - * @readonly - * @static - */ -ResolutionPolicy.NO_BORDER = 1; - -/** - * The entire application is visible in the specified area without distortion while maintaining the original
- * aspect ratio of the application. Borders can appear on two sides of the application. - * @property {Number} SHOW_ALL - * @readonly - * @static - */ -ResolutionPolicy.SHOW_ALL = 2; - -/** - * The application takes the height of the design resolution size and modifies the width of the internal
- * canvas so that it fits the aspect ratio of the device
- * no distortion will occur however you must make sure your application works on different
- * aspect ratios - * @property {Number} FIXED_HEIGHT - * @readonly - * @static - */ -ResolutionPolicy.FIXED_HEIGHT = 3; - -/** - * The application takes the width of the design resolution size and modifies the height of the internal
- * canvas so that it fits the aspect ratio of the device
- * no distortion will occur however you must make sure your application works on different
- * aspect ratios - * @property {Number} FIXED_WIDTH - * @readonly - * @static - */ -ResolutionPolicy.FIXED_WIDTH = 4; - -/** - * Unknow policy - * @property {Number} UNKNOWN - * @readonly - * @static - */ -ResolutionPolicy.UNKNOWN = 5; - -ResolutionPolicy.ContainerStrategy = ContainerStrategy; -ResolutionPolicy.ContentStrategy = ContentStrategy; -cc.ResolutionPolicy = ResolutionPolicy; - -/** - * @module cc - */ - -/** - * !#en cc.view is the shared view object. - * !#zh cc.view 是全局的视图对象。 - * @property view - * @static - * @type {View} - */ -let view = cc.view = new View(); - -/** - * !#en cc.winSize is the alias object for the size of the current game window. - * !#zh cc.winSize 为当前的游戏窗口的大小。 - * @property winSize - * @type Size - */ -cc.winSize = cc.v2(); - -export default view; +/**************************************************************************** + Copyright (c) 2008-2010 Ricardo Quesada + Copyright (c) 2011-2012 cocos2d-x.org + Copyright (c) 2013-2016 Chukong Technologies Inc. + Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + 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. + ****************************************************************************/ + +import '../data/class'; +import { EventTarget } from '../event/event-target'; +import Game from '../game'; +import Rect from '../value-types/rect'; +import Size from '../value-types/size'; + +let _currentFrame = 0; + +class BrowserGetter { + + public html: HTMLHtmlElement | undefined; + + public meta!: { + 'width': 'device-width'; + }; + + public adaptationType: any; + + public init () { + if (!CC_WECHATGAME && !CC_QQPLAY) { + this.html = document.getElementsByTagName('html')[0]; + } + } + + public availWidth (frame) { + if (cc.sys.isMobile || !frame || frame === this.html) { + return window.innerWidth; + } + else { + return frame.clientWidth; + } + } + + public availHeight (frame) { + if (cc.sys.isMobile || !frame || frame === this.html) { + return window.innerHeight; + } + else { + return frame.clientHeight; + } + } +} + +const __BrowserGetter = new BrowserGetter(); + +if (cc.sys.os === cc.sys.OS_IOS) { // All browsers are WebView + __BrowserGetter.adaptationType = cc.sys.BROWSER_TYPE_SAFARI; +} + +if (CC_WECHATGAME) { + if (cc.sys.browserType === cc.sys.BROWSER_TYPE_WECHAT_GAME_SUB) { + __BrowserGetter.adaptationType = cc.sys.BROWSER_TYPE_WECHAT_GAME_SUB; + } + else { + __BrowserGetter.adaptationType = cc.sys.BROWSER_TYPE_WECHAT_GAME; + } +} + +if (CC_QQPLAY) { + __BrowserGetter.adaptationType = cc.sys.BROWSER_TYPE_QQ_PLAY; +} + +switch (__BrowserGetter.adaptationType) { + case cc.sys.BROWSER_TYPE_SAFARI: + __BrowserGetter.meta['minimal-ui'] = 'true'; + case cc.sys.BROWSER_TYPE_SOUGOU: + case cc.sys.BROWSER_TYPE_WECHAT_GAME: + __BrowserGetter.availWidth = () => { + return window.innerWidth; + }; + __BrowserGetter.availHeight = () => { + return window.innerHeight; + }; + break; + case cc.sys.BROWSER_TYPE_WECHAT_GAME_SUB: + const sharedCanvas = window.sharedCanvas || wx.getSharedCanvas(); + __BrowserGetter.availWidth = () => { + return sharedCanvas.width; + }; + __BrowserGetter.availHeight = () => { + return sharedCanvas.height; + }; + break; +} + +let _scissorRect: Rect | null = null; + +/** + * cc.view is the singleton object which represents the game window.
+ * It's main task include:
+ * - Apply the design resolution policy
+ * - Provide interaction with the window, like resize event on web, retina display support, etc...
+ * - Manage the game view port which can be different with the window
+ * - Manage the content scale and translation
+ *
+ * Since the cc.view is a singleton, you don't need to call any constructor or create functions,
+ * the standard way to use it is by calling:
+ * - cc.view.methodName();
+ * + * @class View + */ +class View extends EventTarget { + + private _frameSize: Size; + private _designResolutionSize: Size; + private _originalDesignResolutionSize: Size; + private _scaleX: number; + private _scaleY: number; + private _viewportRect: Rect; + private _visibleRect: Rect; + private _autoFullScreen: boolean; + private _devicePixelRatio: number; + private _retinaEnabled: boolean; + private _resizeCallback: null; + private _resizing: boolean; + private _resizeWithBrowserSize: boolean; + private _orientationChanging: boolean; + private _isRotated: boolean; + private _orientation: any; + private _isAdjustViewport: boolean; + private _antiAliasEnabled: boolean; + private _resolutionPolicy: any; + private _rpExactFit: ResolutionPolicy; + private _rpShowAll: ResolutionPolicy; + private _rpNoBorder: ResolutionPolicy; + private _rpFixedHeight: ResolutionPolicy; + private _rpFixedWidth: ResolutionPolicy; + + constructor () { + super(); + + const _t = this; + const _strategyer = ContainerStrategy; + const _strategy = ContentStrategy; + + // Size of parent node that contains cc.game.container and cc.game.canvas + this._frameSize = new Size(0, 0); + + // resolution size, it is the size appropriate for the app resources. + this._designResolutionSize = new Size(0, 0); + this._originalDesignResolutionSize = new Size(0, 0); + this._scaleX = 1; + this._scaleY = 1; + // Viewport is the container's rect related to content's coordinates in pixel + this._viewportRect = new Rect(0, 0, 0, 0); + // The visible rect in content's coordinate in point + this._visibleRect = new Rect(0, 0, 0, 0); + // Auto full screen disabled by default + this._autoFullScreen = false; + // The device's pixel ratio (for retina displays) + this._devicePixelRatio = 1; + // Retina disabled by default + this._retinaEnabled = false; + // Custom callback for resize event + this._resizeCallback = null; + this._resizing = false; + this._resizeWithBrowserSize = false; + this._orientationChanging = true; + this._isRotated = false; + this._orientation = cc.macro.ORIENTATION_AUTO; + this._isAdjustViewport = true; + this._antiAliasEnabled = false; + + // Setup system default resolution policies + this._resolutionPolicy = null; + this._rpExactFit = new ResolutionPolicy(_strategyer.EQUAL_TO_FRAME, _strategy.EXACT_FIT); + this._rpShowAll = new ResolutionPolicy(_strategyer.EQUAL_TO_FRAME, _strategy.SHOW_ALL); + this._rpNoBorder = new ResolutionPolicy(_strategyer.EQUAL_TO_FRAME, _strategy.NO_BORDER); + this._rpFixedHeight = new ResolutionPolicy(_strategyer.EQUAL_TO_FRAME, _strategy.FIXED_HEIGHT); + this._rpFixedWidth = new ResolutionPolicy(_strategyer.EQUAL_TO_FRAME, _strategy.FIXED_WIDTH); + + cc.game.once(Game.EVENT_ENGINE_INITED, this.init, this); + } + + public init () { + __BrowserGetter.init(); + + this._initFrameSize(); + this.enableAntiAlias(true); + + const w = cc.game.canvas.width; + const h = cc.game.canvas.height; + this._designResolutionSize.width = w; + this._designResolutionSize.height = h; + this._originalDesignResolutionSize.width = w; + this._originalDesignResolutionSize.height = h; + this._viewportRect.width = w; + this._viewportRect.height = h; + this._visibleRect.width = w; + this._visibleRect.height = h; + + cc.winSize.width = this._visibleRect.width; + cc.winSize.height = this._visibleRect.height; + if (cc.visibleRect) { + cc.visibleRect.init(this._visibleRect); + } + } + + /** + * !#en + * Sets view's target-densitydpi for android mobile browser. it can be set to:
+ * 1. macro.DENSITYDPI_DEVICE, value is "device-dpi"
+ * 2. macro.DENSITYDPI_HIGH, value is "high-dpi" (default value)
+ * 3. macro.DENSITYDPI_MEDIUM, value is "medium-dpi" (browser's default value)
+ * 4. macro.DENSITYDPI_LOW, value is "low-dpi"
+ * 5. Custom value, e.g: "480"
+ * !#zh 设置目标内容的每英寸像素点密度。 + * + * @method setTargetDensityDPI + * @param {String} densityDPI + * @deprecated since v2.0 + */ + + /** + * !#en + * Returns the current target-densitydpi value of cc.view. + * !#zh 获取目标内容的每英寸像素点密度。 + * @method getTargetDensityDPI + * @returns {String} + * @deprecated since v2.0 + */ + + /** + * !#en + * Sets whether resize canvas automatically when browser's size changed.
+ * Useful only on web. + * !#zh 设置当发现浏览器的尺寸改变时,是否自动调整 canvas 尺寸大小。 + * 仅在 Web 模式下有效。 + * @method resizeWithBrowserSize + * @param {Boolean} enabled - Whether enable automatic resize with browser's resize event + */ + public resizeWithBrowserSize (enabled) { + if (enabled) { + // enable + if (!this._resizeWithBrowserSize) { + this._resizeWithBrowserSize = true; + window.addEventListener('resize', this._resizeEvent); + window.addEventListener('orientationchange', this._orientationChange); + } + } else { + // disable + if (this._resizeWithBrowserSize) { + this._resizeWithBrowserSize = false; + window.removeEventListener('resize', this._resizeEvent); + window.removeEventListener('orientationchange', this._orientationChange); + } + } + } + + /** + * !#en + * Sets the callback function for cc.view's resize action,
+ * this callback will be invoked before applying resolution policy,
+ * so you can do any additional modifications within the callback.
+ * Useful only on web. + * !#zh 设置 cc.view 调整视窗尺寸行为的回调函数, + * 这个回调函数会在应用适配模式之前被调用, + * 因此你可以在这个回调函数内添加任意附加改变, + * 仅在 Web 平台下有效。 + * @method setResizeCallback + * @param {Function|Null} callback - The callback function + */ + public setResizeCallback (callback) { + if (typeof callback === 'function' || callback == null) { + this._resizeCallback = callback; + } + } + + /** + * !#en + * Sets the orientation of the game, it can be landscape, portrait or auto. + * When set it to landscape or portrait, and screen w/h ratio doesn't fit, + * cc.view will automatically rotate the game canvas using CSS. + * Note that this function doesn't have any effect in native, + * in native, you need to set the application orientation in native project settings + * !#zh 设置游戏屏幕朝向,它能够是横版,竖版或自动。 + * 当设置为横版或竖版,并且屏幕的宽高比例不匹配时, + * cc.view 会自动用 CSS 旋转游戏场景的 canvas, + * 这个方法不会对 native 部分产生任何影响,对于 native 而言,你需要在应用设置中的设置排版。 + * @method setOrientation + * @param {Number} orientation - Possible values: macro.ORIENTATION_LANDSCAPE | macro.ORIENTATION_PORTRAIT | macro.ORIENTATION_AUTO + */ + public setOrientation (orientation) { + orientation = orientation & cc.macro.ORIENTATION_AUTO; + if (orientation && this._orientation !== orientation) { + this._orientation = orientation; + } + } + + /** + * !#en + * Sets whether the engine modify the "viewport" meta in your web page.
+ * It's enabled by default, we strongly suggest you not to disable it.
+ * And even when it's enabled, you can still set your own "viewport" meta, it won't be overridden
+ * Only useful on web + * !#zh 设置引擎是否调整 viewport meta 来配合屏幕适配。 + * 默认设置为启动,我们强烈建议你不要将它设置为关闭。 + * 即使当它启动时,你仍然能够设置你的 viewport meta,它不会被覆盖。 + * 仅在 Web 模式下有效 + * @method adjustViewportMeta + * @param {Boolean} enabled - Enable automatic modification to "viewport" meta + */ + public adjustViewportMeta (enabled) { + this._isAdjustViewport = enabled; + } + + /** + * !#en + * Retina support is enabled by default for Apple device but disabled for other devices,
+ * it takes effect only when you called setDesignResolutionPolicy
+ * Only useful on web + * !#zh 对于 Apple 这种支持 Retina 显示的设备上默认进行优化而其他类型设备默认不进行优化, + * 它仅会在你调用 setDesignResolutionPolicy 方法时有影响。 + * 仅在 Web 模式下有效。 + * @method enableRetina + * @param {Boolean} enabled - Enable or disable retina display + */ + public enableRetina (enabled) { + this._retinaEnabled = !!enabled; + } + + /** + * !#en + * Check whether retina display is enabled.
+ * Only useful on web + * !#zh 检查是否对 Retina 显示设备进行优化。 + * 仅在 Web 模式下有效。 + * @method isRetinaEnabled + * @return {Boolean} + */ + public isRetinaEnabled () { + return this._retinaEnabled; + } + + /** + * !#en Whether to Enable on anti-alias + * !#zh 控制抗锯齿是否开启 + * @method enableAntiAlias + * @param {Boolean} enabled - Enable or not anti-alias + */ + public enableAntiAlias (enabled) { + if (this._antiAliasEnabled === enabled) { + return; + } + this._antiAliasEnabled = enabled; + if (cc.game.renderType === Game.RENDER_TYPE_WEBGL) { + const cache = cc.loader._cache; + // tslint:disable-next-line: forin + for (const key in cache) { + const item = cache[key]; + const tex = item && item.content instanceof cc.Texture2D ? item.content : null; + if (tex) { + const Filter = cc.Texture2D.Filter; + if (enabled) { + tex.setFilters(Filter.LINEAR, Filter.LINEAR); + } + else { + tex.setFilters(Filter.NEAREST, Filter.NEAREST); + } + } + } + } + else if (cc.game.renderType === Game.RENDER_TYPE_CANVAS) { + const ctx = cc.game.canvas.getContext('2d'); + ctx.imageSmoothingEnabled = enabled; + ctx.mozImageSmoothingEnabled = enabled; + } + } + + /** + * !#en Returns whether the current enable on anti-alias + * !#zh 返回当前是否抗锯齿 + * @method isAntiAliasEnabled + * @return {Boolean} + */ + public isAntiAliasEnabled () { + return this._antiAliasEnabled; + } + /** + * !#en + * If enabled, the application will try automatically to enter full screen mode on mobile devices
+ * You can pass true as parameter to enable it and disable it by passing false.
+ * Only useful on web + * !#zh 启动时,移动端游戏会在移动端自动尝试进入全屏模式。 + * 你能够传入 true 为参数去启动它,用 false 参数来关闭它。 + * @method enableAutoFullScreen + * @param {Boolean} enabled - Enable or disable auto full screen on mobile devices + */ + public enableAutoFullScreen (enabled) { + if (enabled && + enabled !== this._autoFullScreen && + cc.sys.isMobile && + cc.sys.browserType !== cc.sys.BROWSER_TYPE_WECHAT) { + // Automatically full screen when user touches on mobile version + this._autoFullScreen = true; + cc.screen.autoFullScreen(cc.game.frame); + } + else { + this._autoFullScreen = false; + } + } + + /** + * !#en + * Check whether auto full screen is enabled.
+ * Only useful on web + * !#zh 检查自动进入全屏模式是否启动。 + * 仅在 Web 模式下有效。 + * @method isAutoFullScreenEnabled + * @return {Boolean} Auto full screen enabled or not + */ + public isAutoFullScreenEnabled () { + return this._autoFullScreen; + } + + /* + * Not support on native.
+ * On web, it sets the size of the canvas. + * !#zh 这个方法并不支持 native 平台,在 Web 平台下,可以用来设置 canvas 尺寸。 + * @method setCanvasSize + * @param {Number} width + * @param {Number} height + */ + public setCanvasSize (width, height) { + const canvas = cc.game.canvas; + const container = cc.game.container; + + canvas.width = width * this._devicePixelRatio; + canvas.height = height * this._devicePixelRatio; + + // canvas.width = width; + // canvas.height = height; + + canvas.style.width = width + 'px'; + canvas.style.height = height + 'px'; + + container.style.width = width + 'px'; + container.style.height = height + 'px'; + + this._resizeEvent(); + } + + /** + * !#en + * Returns the canvas size of the view.
+ * On native platforms, it returns the screen size since the view is a fullscreen view.
+ * On web, it returns the size of the canvas element. + * !#zh 返回视图中 canvas 的尺寸。 + * 在 native 平台下,它返回全屏视图下屏幕的尺寸。 + * 在 Web 平台下,它返回 canvas 元素尺寸。 + * @method getCanvasSize + * @return {Size} + */ + public getCanvasSize () { + return cc.size(cc.game.canvas.width, cc.game.canvas.height); + } + + /** + * !#en + * Returns the frame size of the view.
+ * On native platforms, it returns the screen size since the view is a fullscreen view.
+ * On web, it returns the size of the canvas's outer DOM element. + * !#zh 返回视图中边框尺寸。 + * 在 native 平台下,它返回全屏视图下屏幕的尺寸。 + * 在 web 平台下,它返回 canvas 元素的外层 DOM 元素尺寸。 + * @method getFrameSize + * @return {Size} + */ + public getFrameSize () { + return cc.size(this._frameSize.width, this._frameSize.height); + } + + /** + * !#en + * On native, it sets the frame size of view.
+ * On web, it sets the size of the canvas's outer DOM element. + * !#zh 在 native 平台下,设置视图框架尺寸。 + * 在 web 平台下,设置 canvas 外层 DOM 元素尺寸。 + * @method setFrameSize + * @param {Number} width + * @param {Number} height + */ + public setFrameSize (width, height) { + this._frameSize.width = width; + this._frameSize.height = height; + cc.frame.style.width = width + 'px'; + cc.frame.style.height = height + 'px'; + this._resizeEvent(); + } + + /** + * !#en + * Returns the visible area size of the view port. + * !#zh 返回视图窗口可见区域尺寸。 + * @method getVisibleSize + * @return {Size} + */ + public getVisibleSize () { + return cc.size(this._visibleRect.width, this._visibleRect.height); + } + + /** + * !#en + * Returns the visible area size of the view port. + * !#zh 返回视图窗口可见区域像素尺寸。 + * @method getVisibleSizeInPixel + * @return {Size} + */ + public getVisibleSizeInPixel () { + return cc.size( this._visibleRect.width * this._scaleX, + this._visibleRect.height * this._scaleY ); + } + + /** + * !#en + * Returns the visible origin of the view port. + * !#zh 返回视图窗口可见区域原点。 + * @method getVisibleOrigin + * @return {Vec2} + */ + public getVisibleOrigin () { + return cc.v2(this._visibleRect.x, this._visibleRect.y); + } + + /** + * !#en + * Returns the visible origin of the view port. + * !#zh 返回视图窗口可见区域像素原点。 + * @method getVisibleOriginInPixel + * @return {Vec2} + */ + public getVisibleOriginInPixel () { + return cc.v2(this._visibleRect.x * this._scaleX, + this._visibleRect.y * this._scaleY); + } + + /** + * !#en + * Returns the current resolution policy + * !#zh 返回当前分辨率方案 + * @see ResolutionPolicy + * @method getResolutionPolicy + * @return {ResolutionPolicy} + */ + public getResolutionPolicy () { + return this._resolutionPolicy; + } + + /** + * !#en + * Sets the current resolution policy + * !#zh 设置当前分辨率模式 + * @see ResolutionPolicy + * @method setResolutionPolicy + * @param {ResolutionPolicy|Number} resolutionPolicy + */ + public setResolutionPolicy (resolutionPolicy) { + const _t = this; + if (resolutionPolicy instanceof ResolutionPolicy) { + _t._resolutionPolicy = resolutionPolicy; + } + // Ensure compatibility with JSB + else { + const _locPolicy = ResolutionPolicy; + if (resolutionPolicy === _locPolicy.EXACT_FIT) { + _t._resolutionPolicy = _t._rpExactFit; + } + if (resolutionPolicy === _locPolicy.SHOW_ALL) { + _t._resolutionPolicy = _t._rpShowAll; + } + if (resolutionPolicy === _locPolicy.NO_BORDER) { + _t._resolutionPolicy = _t._rpNoBorder; + } + if (resolutionPolicy === _locPolicy.FIXED_HEIGHT) { + _t._resolutionPolicy = _t._rpFixedHeight; + } + if (resolutionPolicy === _locPolicy.FIXED_WIDTH) { + _t._resolutionPolicy = _t._rpFixedWidth; + } + } + } + + // tslint:disable: max-line-length + /** + * !#en + * Sets the resolution policy with designed view size in points.
+ * The resolution policy include:
+ * [1] ResolutionExactFit Fill screen by stretch-to-fit: if the design resolution ratio of width to height is different from the screen resolution ratio, your game view will be stretched.
+ * [2] ResolutionNoBorder Full screen without black border: if the design resolution ratio of width to height is different from the screen resolution ratio, two areas of your game view will be cut.
+ * [3] ResolutionShowAll Full screen with black border: if the design resolution ratio of width to height is different from the screen resolution ratio, two black borders will be shown.
+ * [4] ResolutionFixedHeight Scale the content's height to screen's height and proportionally scale its width
+ * [5] ResolutionFixedWidth Scale the content's width to screen's width and proportionally scale its height
+ * [ResolutionPolicy] [Web only feature] Custom resolution policy, constructed by ResolutionPolicy
+ * !#zh 通过设置设计分辨率和匹配模式来进行游戏画面的屏幕适配。 + * @method setDesignResolutionSize + * @param {Number} width Design resolution width. + * @param {Number} height Design resolution height. + * @param {ResolutionPolicy|Number} resolutionPolicy The resolution policy desired + */ + public setDesignResolutionSize (width, height, resolutionPolicy) { + // Defensive code + if ( !(width > 0 || height > 0) ){ + cc.logID(2200); + return; + } + + this.setResolutionPolicy(resolutionPolicy); + const policy = this._resolutionPolicy; + if (policy) { + policy.preApply(this); + } + + // Reinit frame size + if (cc.sys.isMobile) { + this._adjustViewportMeta(); + } + + // Permit to re-detect the orientation of device. + this._orientationChanging = true; + // If resizing, then frame size is already initialized, this logic should be improved + if (!this._resizing) { + this._initFrameSize(); + } + + if (!policy) { + cc.logID(2201); + return; + } + + this._originalDesignResolutionSize.width = this._designResolutionSize.width = width; + this._originalDesignResolutionSize.height = this._designResolutionSize.height = height; + + const result = policy.apply(this, this._designResolutionSize); + + if (result.scale && result.scale.length === 2){ + this._scaleX = result.scale[0]; + this._scaleY = result.scale[1]; + } + + if (result.viewport){ + const vp = this._viewportRect; + const vb = this._visibleRect; + const rv = result.viewport; + + vp.x = rv.x; + vp.y = rv.y; + vp.width = rv.width; + vp.height = rv.height; + + vb.x = 0; + vb.y = 0; + vb.width = rv.width / this._scaleX; + vb.height = rv.height / this._scaleY; + } + + policy.postApply(this); + cc.winSize.width = this._visibleRect.width; + cc.winSize.height = this._visibleRect.height; + + if (cc.visibleRect) { + cc.visibleRect.init(this._visibleRect); + } + + this.emit('design-resolution-changed'); + } + + /** + * !#en + * Returns the designed size for the view. + * Default resolution size is the same as 'getFrameSize'. + * !#zh 返回视图的设计分辨率。 + * 默认下分辨率尺寸同 `getFrameSize` 方法相同 + * @method getDesignResolutionSize + * @return {Size} + */ + public getDesignResolutionSize () { + return cc.size(this._designResolutionSize.width, this._designResolutionSize.height); + } + + /** + * !#en + * Sets the container to desired pixel resolution and fit the game content to it. + * This function is very useful for adaptation in mobile browsers. + * In some HD android devices, the resolution is very high, but its browser performance may not be very good. + * In this case, enabling retina display is very costy and not suggested, and if retina is disabled, the image may be blurry. + * But this API can be helpful to set a desired pixel resolution which is in between. + * This API will do the following: + * 1. Set viewport's width to the desired width in pixel + * 2. Set body width to the exact pixel resolution + * 3. The resolution policy will be reset with designed view size in points. + * !#zh 设置容器(container)需要的像素分辨率并且适配相应分辨率的游戏内容。 + * @method setRealPixelResolution + * @param {Number} width Design resolution width. + * @param {Number} height Design resolution height. + * @param {ResolutionPolicy|Number} resolutionPolicy The resolution policy desired + */ + public setRealPixelResolution (width, height, resolutionPolicy) { + if (!CC_JSB && !CC_WECHATGAME && !CC_QQPLAY) { + // Set viewport's width + this._setViewportMeta({width}, true); + + // Set body width to the exact pixel resolution + document.documentElement.style.width = width + 'px'; + document.body.style.width = width + 'px'; + document.body.style.left = '0px'; + document.body.style.top = '0px'; + } + + // Reset the resolution size and policy + this.setDesignResolutionSize(width, height, resolutionPolicy); + } + + /** + * !#en + * Sets view port rectangle with points. + * !#zh 用设计分辨率下的点尺寸来设置视窗。 + * @method setViewportInPoints + * @deprecated since v2.0 + * @param {Number} x + * @param {Number} y + * @param {Number} w width + * @param {Number} h height + */ + public setViewportInPoints (x, y, w, h) { + const locScaleX = this._scaleX; + const locScaleY = this._scaleY; + cc.game._renderContext.viewport((x * locScaleX + this._viewportRect.x), + (y * locScaleY + this._viewportRect.y), + (w * locScaleX), + (h * locScaleY)); + } + + /** + * !#en + * Sets Scissor rectangle with points. + * !#zh 用设计分辨率下的点的尺寸来设置 scissor 剪裁区域。 + * @method setScissorInPoints + * @deprecated since v2.0 + * @param {Number} x + * @param {Number} y + * @param {Number} w + * @param {Number} h + */ + public setScissorInPoints (x, y, w, h) { + const scaleX = this._scaleX; + const scaleY = this._scaleY; + const sx = Math.ceil(x * scaleX + this._viewportRect.x); + const sy = Math.ceil(y * scaleY + this._viewportRect.y); + const sw = Math.ceil(w * scaleX); + const sh = Math.ceil(h * scaleY); + const gl = cc.game._renderContext; + + if (!_scissorRect) { + const boxArr = gl.getParameter(gl.SCISSOR_BOX); + _scissorRect = new Rect(boxArr[0], boxArr[1], boxArr[2], boxArr[3]); + } + + if (_scissorRect.x !== sx || _scissorRect.y !== sy || _scissorRect.width !== sw || _scissorRect.height !== sh) { + _scissorRect.x = sx; + _scissorRect.y = sy; + _scissorRect.width = sw; + _scissorRect.height = sh; + gl.scissor(sx, sy, sw, sh); + } + } + + /** + * !#en + * Returns whether GL_SCISSOR_TEST is enable + * !#zh 检查 scissor 是否生效。 + * @method isScissorEnabled + * @deprecated since v2.0 + * @return {Boolean} + */ + public isScissorEnabled () { + const gl = cc.game._renderContext; + return gl.isEnabled(gl.SCISSOR_TEST); + } + + /** + * !#en + * Returns the current scissor rectangle + * !#zh 返回当前的 scissor 剪裁区域。 + * @method getScissorRect + * @deprecated since v2.0 + * @return {Rect} + */ + public getScissorRect () { + const gl = cc.game._renderContext; + if (!_scissorRect) { + const boxArr = gl.getParameter(gl.SCISSOR_BOX); + _scissorRect = new Rect(boxArr[0], boxArr[1], boxArr[2], boxArr[3]); + } + const scaleXFactor = 1 / this._scaleX; + const scaleYFactor = 1 / this._scaleY; + return new Rect( + (_scissorRect.x - this._viewportRect.x) * scaleXFactor, + (_scissorRect.y - this._viewportRect.y) * scaleYFactor, + _scissorRect.width * scaleXFactor, + _scissorRect.height * scaleYFactor, + ); + } + + /** + * !#en + * Returns the view port rectangle. + * !#zh 返回视窗剪裁区域。 + * @method getViewportRect + * @return {Rect} + */ + public getViewportRect () { + return this._viewportRect; + } + + /** + * !#en + * Returns scale factor of the horizontal direction (X axis). + * !#zh 返回横轴的缩放比,这个缩放比是将画布像素分辨率放到设计分辨率的比例。 + * @method getScaleX + * @return {Number} + */ + public getScaleX () { + return this._scaleX; + } + + /** + * !#en + * Returns scale factor of the vertical direction (Y axis). + * !#zh 返回纵轴的缩放比,这个缩放比是将画布像素分辨率缩放到设计分辨率的比例。 + * @method getScaleY + * @return {Number} + */ + public getScaleY () { + return this._scaleY; + } + + /** + * !#en + * Returns device pixel ratio for retina display. + * !#zh 返回设备或浏览器像素比例。 + * @method getDevicePixelRatio + * @return {Number} + */ + public getDevicePixelRatio () { + return this._devicePixelRatio; + } + + /** + * !#en + * Returns the real location in view for a translation based on a related position + * !#zh 将屏幕坐标转换为游戏视图下的坐标。 + * @method convertToLocationInView + * @param {Number} tx - The X axis translation + * @param {Number} ty - The Y axis translation + * @param {Object} relatedPos - The related position object including "left", "top", "width", "height" informations + * @return {Vec2} + */ + public convertToLocationInView (tx, ty, relatedPos, out) { + const result = out || cc.v2(); + const x = this._devicePixelRatio * (tx - relatedPos.left); + const y = this._devicePixelRatio * (relatedPos.top + relatedPos.height - ty); + if (this._isRotated) { + result.x = cc.game.canvas.width - y; + result.y = x; + } + else { + result.x = x; + result.y = y; + } + return result; + } + + // Resize helper functions + private _resizeEvent () { + const frameId = cc.director.getTotalFrames(); + if (_currentFrame !== frameId) { + _currentFrame = frameId; + } + else { + // Avoid adapte multiple times in a frame + return; + } + // tslint:disable: no-shadowed-variable + let view; + if (this.setDesignResolutionSize) { + view = this; + } else { + view = cc.view; + } + + // Check frame size changed or not + const prevFrameW = view._frameSize.width; + const prevFrameH = view._frameSize.height; + const prevRotated = view._isRotated; + if (cc.sys.isMobile) { + const containerStyle = cc.game.container.style; + const margin = containerStyle.margin; + containerStyle.margin = '0'; + containerStyle.display = 'none'; + view._initFrameSize(); + containerStyle.margin = margin; + containerStyle.display = 'block'; + } + else { + view._initFrameSize(); + } + if (!view._orientationChanging && view._isRotated === prevRotated && view._frameSize.width === prevFrameW && view._frameSize.height === prevFrameH) { + return; + } + + // Frame size changed, do resize works + const width = view._originalDesignResolutionSize.width; + const height = view._originalDesignResolutionSize.height; + + view._resizing = true; + if (width > 0) { + view.setDesignResolutionSize(width, height, view._resolutionPolicy); + } + view._resizing = false; + + if (view._resizeCallback) { + view._resizeCallback.call(); + } + } + + private _orientationChange () { + cc.view._orientationChanging = true; + cc.view._resizeEvent(); + } + + private _initFrameSize () { + const locFrameSize = this._frameSize; + const w = __BrowserGetter.availWidth(cc.game.frame); + const h = __BrowserGetter.availHeight(cc.game.frame); + const isLandscape: Boolean = w >= h; + + if (CC_EDITOR || !cc.sys.isMobile || + (isLandscape && this._orientation & cc.macro.ORIENTATION_LANDSCAPE) || + (!isLandscape && this._orientation & cc.macro.ORIENTATION_PORTRAIT)) { + locFrameSize.width = w; + locFrameSize.height = h; + cc.game.container.style['-webkit-transform'] = 'rotate(0deg)'; + cc.game.container.style.transform = 'rotate(0deg)'; + this._isRotated = false; + } + else { + locFrameSize.width = h; + locFrameSize.height = w; + cc.game.container.style['-webkit-transform'] = 'rotate(90deg)'; + cc.game.container.style.transform = 'rotate(90deg)'; + cc.game.container.style['-webkit-transform-origin'] = '0px 0px 0px'; + cc.game.container.style.transformOrigin = '0px 0px 0px'; + this._isRotated = true; + + // Fix for issue: https://github.com/cocos-creator/fireball/issues/8365 + // Reference: https://www.douban.com/note/343402554/ + // For Chrome, z-index not working after container transform rotate 90deg. + // Because 'transform' style adds canvas (the top-element of container) to a new stack context. + // That causes the DOM Input was hidden under canvas. + // This should be done after container rotated, instead of in style-mobile.css. + cc.game.canvas.style['-webkit-transform'] = 'translateZ(0px)'; + cc.game.canvas.style.transform = 'translateZ(0px)'; + } + if (this._orientationChanging) { + setTimeout(() => { + cc.view._orientationChanging = false; + }, 1000); + } + } + + // hack + private _adjustSizeKeepCanvasSize () { + const designWidth = this._originalDesignResolutionSize.width; + const designHeight = this._originalDesignResolutionSize.height; + if (designWidth > 0) { + this.setDesignResolutionSize(designWidth, designHeight, this._resolutionPolicy); + } + } + + private _setViewportMeta (metas, overwrite) { + let vp = document.getElementById('cocosMetaElement'); + if (vp && overwrite) { + document.head.removeChild(vp); + } + + const elems = document.getElementsByName('viewport'); + const currentVP = elems ? elems[0] : null; + let content; + let key; + let pattern; + + content = currentVP ? currentVP.content : ''; + vp = vp || document.createElement('meta'); + vp.id = 'cocosMetaElement'; + vp.name = 'viewport'; + vp.content = ''; + + for (key in metas) { + if (content.indexOf(key) === -1) { + content += ',' + key + '=' + metas[key]; + } + else if (overwrite) { + pattern = new RegExp(key + '\s*=\s*[^,]+'); + content.replace(pattern, key + '=' + metas[key]); + } + } + if (/^,/.test(content)) { + content = content.substr(1); + } + + vp.content = content; + // For adopting certain android devices which don't support second viewport + if (currentVP) { + currentVP.content = content; + } + + document.head.appendChild(vp); + } + + private _adjustViewportMeta () { + if (this._isAdjustViewport && !CC_JSB && !CC_WECHATGAME && !CC_QQPLAY) { + this._setViewportMeta(__BrowserGetter.meta, false); + this._isAdjustViewport = false; + } + } + + private _convertMouseToLocation (in_out_point, relatedPos){ + in_out_point.x = this._devicePixelRatio * (in_out_point.x - relatedPos.left); + in_out_point.y = this._devicePixelRatio * (relatedPos.top + relatedPos.height - in_out_point.y); + } + + // _convertMouseToLocationInView (in_out_point, relatedPos) { + // var viewport = this._viewportRect, _t = this; + // in_out_point.x = ((_t._devicePixelRatio * (in_out_point.x - relatedPos.left)) - viewport.x) / _t._scaleX; + // in_out_point.y = (_t._devicePixelRatio * (relatedPos.top + relatedPos.height - in_out_point.y) - viewport.y) / _t._scaleY; + // } + + private _convertPointWithScale (point) { + const viewport = this._viewportRect; + point.x = (point.x - viewport.x) / this._scaleX; + point.y = (point.y - viewport.y) / this._scaleY; + } + + private _convertTouchWidthScale (selTouch){ + const viewport = this._viewportRect; + const scaleX = this._scaleX; + const scaleY = this._scaleY; + + selTouch._point.x = (selTouch._point.x - viewport.x) / scaleX; + selTouch._point.y = (selTouch._point.y - viewport.y) / scaleY; + selTouch._prevPoint.x = (selTouch._prevPoint.x - viewport.x) / scaleX; + selTouch._prevPoint.y = (selTouch._prevPoint.y - viewport.y) / scaleY; + } + + private _convertTouchesWithScale (touches) { + const viewport = this._viewportRect; + const scaleX = this._scaleX; + const scaleY = this._scaleY; + let selPoint; + let selPrePoint; + for (const selTouch of touches) { + selPoint = selTouch._point; + selPrePoint = selTouch._prevPoint; + + selPoint.x = (selPoint.x - viewport.x) / scaleX; + selPoint.y = (selPoint.y - viewport.y) / scaleY; + selPrePoint.x = (selPrePoint.x - viewport.x) / scaleX; + selPrePoint.y = (selPrePoint.y - viewport.y) / scaleY; + } + } +} + +/** + * !en + * Emit when design resolution changed. + * !zh + * 当设计分辨率改变时发送。 + * @event design-resolution-changed + *

cc.game.containerStrategy class is the root strategy class of container's scale strategy, + * it controls the behavior of how to scale the cc.game.container and cc.game.canvas object

+ * + * @class ContainerStrategy + */ +class ContainerStrategy { + public static EQUAL_TO_FRAME: any; + public static PROPORTION_TO_FRAME: any; + + public name: string = 'ContainerStrategy'; + + /** + * !#en + * Manipulation before appling the strategy + * !#zh 在应用策略之前的操作 + * @method preApply + * @param {View} view - The target view + */ + public preApply (view) { + } + + /** + * !#en + * Function to apply this strategy + * !#zh 策略应用方法 + * @method apply + * @param {View} view + * @param {Size} designedResolution + */ + public apply (view, designedResolution) { + } + + /** + * !#en + * Manipulation after applying the strategy + * !#zh 策略调用之后的操作 + * @method postApply + * @param {View} view The target view + */ + public postApply (view) { + + } + + protected _setupContainer (view, w, h) { + const locCanvas = cc.game.canvas; + const locContainer = cc.game.container; + + if (cc.sys.platform !== cc.sys.WECHAT_GAME) { + if (cc.sys.os === cc.sys.OS_ANDROID) { + document.body.style.width = (view._isRotated ? h : w) + 'px'; + document.body.style.height = (view._isRotated ? w : h) + 'px'; + } + // Setup style + locContainer.style.width = locCanvas.style.width = w + 'px'; + locContainer.style.height = locCanvas.style.height = h + 'px'; + } + // Setup pixel ratio for retina display + let devicePixelRatio = view._devicePixelRatio = 1; + if (view.isRetinaEnabled()) { + devicePixelRatio = view._devicePixelRatio = Math.min(2, window.devicePixelRatio || 1); + } + // Setup canvas + locCanvas.width = w * devicePixelRatio; + locCanvas.height = h * devicePixelRatio; + } + + protected _fixContainer () { + // Add container to document body + document.body.insertBefore(cc.game.container, document.body.firstChild); + // Set body's width height to window's size, and forbid overflow, so that game will be centered + const bs = document.body.style; + bs.width = window.innerWidth + 'px'; + bs.height = window.innerHeight + 'px'; + bs.overflow = 'hidden'; + // Body size solution doesn't work on all mobile browser so this is the aleternative: fixed container + const contStyle = cc.game.container.style; + contStyle.position = 'fixed'; + contStyle.left = contStyle.top = '0px'; + // Reposition body + document.body.scrollTop = 0; + } +} + +/** + *

ContentStrategy class is the root strategy class of content's scale strategy, + * it controls the behavior of how to scale the scene and setup the viewport for the game

+ * + * @class ContentStrategy + */ +class ContentStrategy { + public static EXACT_FIT: any; + public static SHOW_ALL: any; + public static NO_BORDER: any; + public static FIXED_HEIGHT: any; + public static FIXED_WIDTH: any; + + public name = 'ContentStrategy'; + private _result: { scale: number[]; viewport: null | Rect; }; + constructor () { + this._result = { + scale: [1, 1], + viewport: null, + }; + } + + /** + * !#en + * Manipulation before applying the strategy + * !#zh 策略应用前的操作 + * @method preApply + * @param {View} view - The target view + */ + public preApply (view) { + } + + /** + * !#en Function to apply this strategy + * The return value is {scale: [scaleX, scaleY], viewport: {new Rect}}, + * The target view can then apply these value to itself, it's preferred not to modify directly its private variables + * !#zh 调用策略方法 + * @method apply + * @param {View} view + * @param {Size} designedResolution + * @return {Object} scaleAndViewportRect + */ + public apply (view, designedResolution) { + return {scale: [1, 1]}; + } + + /** + * !#en + * Manipulation after applying the strategy + * !#zh 策略调用之后的操作 + * @method postApply + * @param {View} view - The target view + */ + public postApply (view) { + } + + public _buildResult (containerW, containerH, contentW, contentH, scaleX, scaleY) { + // Makes content fit better the canvas + if ( Math.abs(containerW - contentW) < 2 ) { + contentW = containerW; + } + if ( Math.abs(containerH - contentH) < 2 ) { + contentH = containerH; + } + + const viewport = new Rect(Math.round((containerW - contentW) / 2), + Math.round((containerH - contentH) / 2), + contentW, contentH); + + // Translate the content + if (cc.game.renderType === Game.RENDER_TYPE_CANVAS){ + // TODO: modify something for setTransform + // game._renderContext.translate(viewport.x, viewport.y + contentH); + } + + this._result.scale = [scaleX, scaleY]; + this._result.viewport = viewport; + return this._result; + } +} + +(() => { + +// Container scale strategys + /** + * @class EqualToFrame + * @extends ContainerStrategy + */ + class EqualToFrame extends ContainerStrategy { + public name = 'EqualToFrame'; + public apply (view) { + const frameH = view._frameSize.height; + const containerStyle = cc.game.container.style; + this._setupContainer(view, view._frameSize.width, view._frameSize.height); + // Setup container's margin and padding + if (view._isRotated) { + containerStyle.margin = '0 0 0 ' + frameH + 'px'; + } + else { + containerStyle.margin = '0px'; + } + containerStyle.padding = '0px'; + } + } + + /** + * @class ProportionalToFrame + * @extends ContainerStrategy + */ + class ProportionalToFrame extends ContainerStrategy { + public name = 'ProportionalToFrame'; + public apply (view, designedResolution) { + const frameW = view._frameSize.width; + const frameH = view._frameSize.height; + const containerStyle = cc.game.container.style; + const designW = designedResolution.width; + const designH = designedResolution.height; + const scaleX = frameW / designW; + const scaleY = frameH / designH; + let containerW; + let containerH; + + scaleX < scaleY ? (containerW = frameW, containerH = designH * scaleX) : (containerW = designW * scaleY, containerH = frameH); + + // Adjust container size with integer value + const offx = Math.round((frameW - containerW) / 2); + const offy = Math.round((frameH - containerH) / 2); + containerW = frameW - 2 * offx; + containerH = frameH - 2 * offy; + + this._setupContainer(view, containerW, containerH); + if (!CC_EDITOR) { + // Setup container's margin and padding + if (view._isRotated) { + containerStyle.margin = '0 0 0 ' + frameH + 'px'; + } + else { + containerStyle.margin = '0px'; + } + containerStyle.paddingLeft = offx + 'px'; + containerStyle.paddingRight = offx + 'px'; + containerStyle.paddingTop = offy + 'px'; + containerStyle.paddingBottom = offy + 'px'; + } + } + } + +// Alias: Strategy that makes the container's size equals to the frame's size + ContainerStrategy.EQUAL_TO_FRAME = new EqualToFrame(); +// Alias: Strategy that scale proportionally the container's size to frame's size + ContainerStrategy.PROPORTION_TO_FRAME = new ProportionalToFrame(); + +// Content scale strategys + class ExactFit extends ContentStrategy { + public name = 'ExactFit'; + public apply (view, designedResolution) { + const containerW = cc.game.canvas.width; + const containerH = cc.game.canvas.height; + const scaleX = containerW / designedResolution.width; + const scaleY = containerH / designedResolution.height; + + return this._buildResult(containerW, containerH, containerW, containerH, scaleX, scaleY); + } + } + + class ShowAll extends ContentStrategy { + public name = 'ShowAll'; + public apply (view, designedResolution) { + const containerW = cc.game.canvas.width; + const containerH = cc.game.canvas.height; + const designW = designedResolution.width; + const designH = designedResolution.height; + const scaleX = containerW / designW; + const scaleY = containerH / designH; + let scale = 0; + let contentW; + let contentH; + + scaleX < scaleY ? (scale = scaleX, contentW = containerW, contentH = designH * scale) + : (scale = scaleY, contentW = designW * scale, contentH = containerH); + + return this._buildResult(containerW, containerH, contentW, contentH, scale, scale); + } + } + + class NoBorder extends ContentStrategy { + public name = 'NoBorder'; + public apply (view, designedResolution) { + const containerW = cc.game.canvas.width; + const containerH = cc.game.canvas.height; + const designW = designedResolution.width; + const designH = designedResolution.height; + const scaleX = containerW / designW; + const scaleY = containerH / designH; + let scale; + let contentW; + let contentH; + + scaleX < scaleY ? (scale = scaleY, contentW = designW * scale, contentH = containerH) + : (scale = scaleX, contentW = containerW, contentH = designH * scale); + + return this._buildResult(containerW, containerH, contentW, contentH, scale, scale); + } + } + + class FixedHeight extends ContentStrategy { + public name = 'FixedHeight'; + public apply (view, designedResolution) { + const containerW = cc.game.canvas.width; + const containerH = cc.game.canvas.height; + const designH = designedResolution.height; + const scale = containerH / designH; + const contentW = containerW; + const contentH = containerH; + + return this._buildResult(containerW, containerH, contentW, contentH, scale, scale); + } + } + + class FixedWidth extends ContentStrategy { + public name = 'FixedWidth'; + public apply (view, designedResolution) { + const containerW = cc.game.canvas.width; + const containerH = cc.game.canvas.height; + const designW = designedResolution.width; + const scale = containerW / designW; + const contentW = containerW; + const contentH = containerH; + + return this._buildResult(containerW, containerH, contentW, contentH, scale, scale); + } + } + +// Alias: Strategy to scale the content's size to container's size, non proportional + ContentStrategy.EXACT_FIT = new ExactFit(); +// Alias: Strategy to scale the content's size proportionally to maximum size and keeps the whole content area to be visible + ContentStrategy.SHOW_ALL = new ShowAll(); +// Alias: Strategy to scale the content's size proportionally to fill the whole container area + ContentStrategy.NO_BORDER = new NoBorder(); +// Alias: Strategy to scale the content's height to container's height and proportionally scale its width + ContentStrategy.FIXED_HEIGHT = new FixedHeight(); +// Alias: Strategy to scale the content's width to container's width and proportionally scale its height + ContentStrategy.FIXED_WIDTH = new FixedWidth(); + +})(); + +/** + *

ResolutionPolicy class is the root strategy class of scale strategy, + * its main task is to maintain the compatibility with Cocos2d-x

+ * + * @class ResolutionPolicy + */ +/** + * @method constructor + * @param {ContainerStrategy} containerStg The container strategy + * @param {ContentStrategy} contentStg The content strategy + */ +class ResolutionPolicy { + public static EXACT_FIT: number; + public static SHOW_ALL: number; + public static NO_BORDER: number; + public static FIXED_HEIGHT: number; + public static FIXED_WIDTH: number; + public static UNKNOWN: number; + public static ContainerStrategy: typeof ContainerStrategy; + public static ContentStrategy: typeof ContentStrategy; + + public name = 'ResolutionPolicy'; + + private _containerStrategy: null | ContainerStrategy; + private _contentStrategy: null | ContentStrategy; + + /** + * Constructor of ResolutionPolicy + * @param {ContainerStrategy} containerStg + * @param {ContentStrategy} contentStg + */ + constructor (containerStg, contentStg) { + this._containerStrategy = null; + this._contentStrategy = null; + this.setContainerStrategy(containerStg); + this.setContentStrategy(contentStg); + } + + get canvasSize () { + return cc.v2(cc.game.canvas.width, cc.game.canvas.height); + } + + /** + * !#en Manipulation before applying the resolution policy + * !#zh 策略应用前的操作 + * @method preApply + * @param {View} view The target view + */ + public preApply (view) { + this._containerStrategy!.preApply(view); + this._contentStrategy!.preApply(view); + } + + /** + * !#en Function to apply this resolution policy + * The return value is {scale: [scaleX, scaleY], viewport: {new Rect}}, + * The target view can then apply these value to itself, it's preferred not to modify directly its private variables + * !#zh 调用策略方法 + * @method apply + * @param {View} view - The target view + * @param {Size} designedResolution - The user defined design resolution + * @return {Object} An object contains the scale X/Y values and the viewport rect + */ + public apply (view, designedResolution) { + this._containerStrategy!.apply(view, designedResolution); + return this._contentStrategy!.apply(view, designedResolution); + } + + /** + * !#en Manipulation after appyling the strategy + * !#zh 策略应用之后的操作 + * @method postApply + * @param {View} view - The target view + */ + public postApply (view) { + this._containerStrategy!.postApply(view); + this._contentStrategy!.postApply(view); + } + + /** + * !#en + * Setup the container's scale strategy + * !#zh 设置容器的适配策略 + * @method setContainerStrategy + * @param {ContainerStrategy} containerStg + */ + public setContainerStrategy (containerStg) { + if (containerStg instanceof ContainerStrategy) { + this._containerStrategy = containerStg; + } + } + + /** + * !#en + * Setup the content's scale strategy + * !#zh 设置内容的适配策略 + * @method setContentStrategy + * @param {ContentStrategy} contentStg + */ + public setContentStrategy (contentStg) { + if (contentStg instanceof ContentStrategy) { + this._contentStrategy = contentStg; + } + } +} + +/** + * The entire application is visible in the specified area without trying to preserve the original aspect ratio.
+ * Distortion can occur, and the application may appear stretched or compressed. + * @property {Number} EXACT_FIT + * @readonly + * @static + */ +ResolutionPolicy.EXACT_FIT = 0; + +/** + * The entire application fills the specified area, without distortion but possibly with some cropping,
+ * while maintaining the original aspect ratio of the application. + * @property {Number} NO_BORDER + * @readonly + * @static + */ +ResolutionPolicy.NO_BORDER = 1; + +/** + * The entire application is visible in the specified area without distortion while maintaining the original
+ * aspect ratio of the application. Borders can appear on two sides of the application. + * @property {Number} SHOW_ALL + * @readonly + * @static + */ +ResolutionPolicy.SHOW_ALL = 2; + +/** + * The application takes the height of the design resolution size and modifies the width of the internal
+ * canvas so that it fits the aspect ratio of the device
+ * no distortion will occur however you must make sure your application works on different
+ * aspect ratios + * @property {Number} FIXED_HEIGHT + * @readonly + * @static + */ +ResolutionPolicy.FIXED_HEIGHT = 3; + +/** + * The application takes the width of the design resolution size and modifies the height of the internal
+ * canvas so that it fits the aspect ratio of the device
+ * no distortion will occur however you must make sure your application works on different
+ * aspect ratios + * @property {Number} FIXED_WIDTH + * @readonly + * @static + */ +ResolutionPolicy.FIXED_WIDTH = 4; + +/** + * Unknow policy + * @property {Number} UNKNOWN + * @readonly + * @static + */ +ResolutionPolicy.UNKNOWN = 5; + +ResolutionPolicy.ContainerStrategy = ContainerStrategy; +ResolutionPolicy.ContentStrategy = ContentStrategy; +cc.ResolutionPolicy = ResolutionPolicy; + +/** + * @module cc + */ + +/** + * !#en cc.view is the shared view object. + * !#zh cc.view 是全局的视图对象。 + * @property view + * @static + * @type {View} + */ +const view = cc.view = new View(); + +/** + * !#en cc.winSize is the alias object for the size of the current game window. + * !#zh cc.winSize 为当前的游戏窗口的大小。 + * @property winSize + * @type Size + */ +cc.winSize = cc.v2(); + +export default view; diff --git a/cocos/load-pipeline/CCLoader.ts b/cocos/load-pipeline/CCLoader.ts index 881ff7e9d6..d7676c73e8 100644 --- a/cocos/load-pipeline/CCLoader.ts +++ b/cocos/load-pipeline/CCLoader.ts @@ -100,8 +100,8 @@ class CCLoader extends Pipeline { public downloader: Downloader; public loader: Loader; public onProgress: null; + public _assetTables: any; - private _assetTables: any; private _autoReleaseSetting: any; private _releasedAssetChecker_DEBUG: any; private _ownerQueue: any;