Partial API translation for core and related tweaks (#6534)

* Event api translation

* Event manager API translation

* Macro cleanup and API doc improve

* Code cleanup and API translation in core/platform,

* Use macro.ENABLE_WEBGL_ANTIALIAS for initialization

* tweak

* Improve singleton ref in view

* Fix ci

* Export sys without default
This commit is contained in:
pandamicro
2020-04-16 11:38:24 +08:00
committed by GitHub
parent 12e1e20fb3
commit 276a7f200a
33 changed files with 1389 additions and 2200 deletions
+1 -1
View File
@@ -28,7 +28,7 @@
*/
import { clamp } from '../../core/math/utils';
import sys from '../../core/platform/sys';
import { sys } from '../../core/platform/sys';
import { AudioPlayer, IAudioInfo, PlayingState } from './player';
const audioSupport = sys.__audioSupport;
+1 -1
View File
@@ -28,7 +28,7 @@
*/
import { getError, log } from '../core/platform/debug';
import sys from '../core/platform/sys';
import { sys } from '../core/platform/sys';
import { AudioClip, AudioType } from './assets/clip';
const __audioSupport = sys.__audioSupport;
+1 -1
View File
@@ -1,5 +1,5 @@
import { GFXFormat, GFXFormatInfos, GFXFormatType, IGFXFormatInfo } from '../../gfx/define';
import sys from '../../platform/sys';
import { sys } from '../../platform/sys';
const _typeMap = {
[GFXFormatType.UNORM]: 'Uint',
+1 -2
View File
@@ -46,13 +46,12 @@ import {
import { GFXDevice, GFXFeature } from '../gfx/device';
import { IGFXAttribute } from '../gfx/input-assembler';
import { warnID } from '../platform/debug';
import sys from '../platform/sys';
import { sys } from '../platform/sys';
import { murmurhash2_32_gc } from '../utils/murmurhash2_gc';
import { Asset } from './asset';
import { Skeleton } from './skeleton';
import { postLoadMesh } from './utils/mesh-utils';
import { Morph, createMorphRendering, MorphRendering } from './morph';
import { js } from '../utils/js';
function getIndexStrideCtor (stride: number) {
switch (stride) {
@@ -36,7 +36,7 @@ import { game } from '../../game';
import { GFXClearFlag } from '../../gfx/define';
import { GFXWindow } from '../../gfx/window';
import { Color, Vec3, Rect } from '../../math';
import { ResolutionPolicy, view } from '../../platform/view';
import { view } from '../../platform/view';
import visibleRect from '../../platform/visible-rect';
import { Camera } from '../../renderer';
import { Node } from '../../scene-graph/node';
@@ -343,7 +343,7 @@ export class CanvasComponent extends Component {
designSize = view.getDesignResolutionSize();
const policy = view.getResolutionPolicy();
// const clipTopRight = !this.fitHeight && !this.fitWidth;
const clipTopRight = policy === ResolutionPolicy.NO_BORDER;
const clipTopRight = policy === cc.view._rpNoBorder;
let offsetX = 0;
let offsetY = 0;
if (clipTopRight) {
+15 -1
View File
@@ -6,6 +6,7 @@ import { replaceProperty } from './utils/deprecated';
import * as math from './math';
import { Scheduler } from './scheduler';
import { CameraComponent } from './3d';
import { EventTouch } from './platform';
// VMATH
@@ -177,6 +178,8 @@ replaceProperty(Scheduler.prototype, 'Scheduler.prototype', [
}
]);
// Camera
replaceProperty(CameraComponent.prototype, 'CameraComponent.prototype', [
{
name: 'color',
@@ -190,4 +193,15 @@ replaceProperty(CameraComponent.prototype, 'CameraComponent.prototype', [
name: 'stencil',
newName: 'clearStencil',
},
])
]);
// Events
replaceProperty(EventTouch.prototype, 'EventTouch.prototype', [
{
name: 'getUILocationInView',
newName: 'getLocationInView',
target: EventTouch,
targetName: 'EventTouch'
}
]);
+46 -53
View File
@@ -1,6 +1,6 @@
/*
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
@@ -51,16 +51,20 @@ const callbackInfoPool = new Pool(() => {
return new CallbackInfo();
}, 32);
/**
* @zh 事件监听器列表的简单封装。
* @en A simple list of event callbacks
*/
export class CallbackList {
public callbackInfos: Array<CallbackInfo | null> = [];
public isInvoking = false;
public containCanceled = false;
/**
* @zh
* 从列表中移除与指定目标相同回调函数的事件。
* @zh 从列表中移除与指定目标相同回调函数的事件。
* @en Remove the event listeners with the given callback from the list
*
* @param cb - 指定回调函数
* @param cb - The callback to be removed
*/
public removeByCallback (cb: Function) {
for (let i = 0; i < this.callbackInfos.length; ++i) {
@@ -73,10 +77,9 @@ export class CallbackList {
}
}
/**
* @zh
* 从列表中移除与指定目标相同调用者的事件。
*
* @param target - 指定调用者
* @zh 从列表中移除与指定目标相同调用者的事件。
* @en Remove the event listeners with the given target from the list
* @param target
*/
public removeByTarget (target: Object) {
for (let i = 0; i < this.callbackInfos.length; ++i) {
@@ -90,10 +93,9 @@ export class CallbackList {
}
/**
* @zh
* 移除指定编号事件。
*
* @param index - 指定编号。
* @zh 移除指定编号事件。
* @en Remove the event listener at the given index
* @param index
*/
public cancel (index: number) {
const info = this.callbackInfos[index];
@@ -105,8 +107,8 @@ export class CallbackList {
}
/**
* @zh
* 注销所有事件。
* @zh 注销所有事件。
* @en Cancel all event listeners
*/
public cancelAll () {
for (let i = 0; i < this.callbackInfos.length; i++) {
@@ -120,8 +122,8 @@ export class CallbackList {
}
/**
* @zh
* 取消所有回调。(在移除过程中会更加紧凑的排列数组)
* @zh 立即删除所有取消的回调。(在移除过程中会更加紧凑的排列数组)
* @en Delete all canceled callbacks and compact array
*/
public purgeCanceled () {
for (let i = this.callbackInfos.length - 1; i >= 0; --i) {
@@ -134,8 +136,8 @@ export class CallbackList {
}
/**
* @zh
* 清除并重置所有数据。
* @zh 清除并重置所有数据。
* @en Clear all data
*/
public clear () {
this.cancelAll();
@@ -155,21 +157,21 @@ export interface ICallbackTable {
}
/**
* @zh
* CallbacksInvoker 用来根据 Key 管理事件监听器列表并调用回调方法。
* @class CallbacksInvoker
* @zh CallbacksInvoker 用来根据事件名(Key)管理事件监听器列表并调用回调方法。
* @en CallbacksInvoker is used to manager and invoke event listeners with different event keys,
* each key is mapped to a CallbackList.
*/
export class CallbacksInvoker {
public _callbackTable: ICallbackTable = createMap(true);
/**
* @zh
* 事件添加管理
* @zh 向一个事件名注册一个新的事件监听器,包含回调函数和调用者
* @en Register an event listener to a given event key with callback and target.
*
* @param key - 一个监听事件类型的字符串。
* @param callback - 事件分派时将被调用的回调函数。
* @param arget - 调用回调的目标。可以为空。
* @param once - 是否只调用一次。
* @param key - Event type
* @param callback - Callback function when event triggered
* @param target - Callback callee
* @param once - Whether invoke the callback only once (and remove it)
*/
public on (key: string, callback: Function, target?: Object, once?: boolean) {
let list = this._callbackTable[key];
@@ -182,13 +184,11 @@ export class CallbacksInvoker {
}
/**
* @zh
* 检查指定事件是否已注册回调。
*
* @param key - 一个监听事件类型的字符串。
* @param callback - 事件分派时将被调用的回调函数。
* @param target - 调用回调的目标。
* @return - 指定事件是否已注册回调。
* @zh 检查指定事件是否已注册回调。
* @en Checks whether there is correspond event listener registered on the given event
* @param key - Event type
* @param callback - Callback function when event triggered
* @param target - Callback callee
*/
public hasEventListener (key: string, callback?: Function, target: Object | null = null) {
const list = this._callbackTable[key];
@@ -222,10 +222,9 @@ export class CallbacksInvoker {
}
/**
* @zh
* 移除在特定事件类型中注册的所有回调或在某个目标中注册的所有回调。
*
* @param keyOrTarget - 要删除的事件键或要删除的目标。
* @zh 移除在特定事件类型中注册的所有回调或在某个目标中注册的所有回调。
* @en Removes all callbacks registered in a certain event type or all callbacks registered with a certain target
* @param keyOrTarget - The event type or target with which the listeners will be removed
*/
public removeAll (keyOrTarget?: string | Object) {
if (typeof keyOrTarget === 'string') {
@@ -263,12 +262,11 @@ export class CallbacksInvoker {
}
/**
* @zh
* 删除之前与同类型,回调,目标注册的回调。
*
* @param key - 一个监听事件类型的字符串。
* @param callback - 移除指定注册回调。如果没有给,则删除全部同事件类型的监听。
* @param target - 调用回调的目标。
* @zh 删除以指定事件,回调函数,目标注册的回调。
* @en Remove event listeners registered with the given event key, callback and target
* @param key - Event type
* @param callback - The callback function of the event listener, if absent all event listeners for the given type will be removed
* @param target - The callback callee of the event listener
*/
public off (key: string, callback?: Function, target?: Object) {
const list = this._callbackTable[key];
@@ -296,15 +294,10 @@ export class CallbacksInvoker {
}
/**
* @zh
* 事件派发
*
* @param key - 一个监听事件类型的字符串
* @param p1 - 派发的第一个参数。
* @param p2 - 派发的第二个参数。
* @param p3 - 派发的第三个参数。
* @param p4 - 派发的第四个参数。
* @param p5 - 派发的第五个参数。
* @zh 派发一个指定事件,并传递需要的参数
* @en Trigger an event directly with the event name and necessary arguments.
* @param key - event type
* @param args - Arguments when the event triggered
*/
public emit (key: string, ...args: any[]) {
const list: CallbackList = this._callbackTable[key]!;
+25
View File
@@ -1,3 +1,28 @@
/*
Copyright (c) 2019-2020 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.
*/
/**
* @hidden
*/
+6 -3
View File
@@ -1,6 +1,5 @@
/*
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2019-2020 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
@@ -41,5 +40,9 @@ export function applyMixins (derivedCtor: any, baseCtors: any[]) {
});
}
export interface IEventTarget extends EventTarget{
/**
* @en Interface for all classes that self implement the EventTarget protocol, they are normally not sub class of EventTarget
* @zh 所有自己实现 EventTarget 功能的类都实现了这个 Interface,他们可能无法直接继承自 EventTarget
*/
export interface IEventTarget extends EventTarget {
}
+52 -37
View File
@@ -39,32 +39,49 @@ export interface ITargetImpl extends Object {
}
/**
* @zh
* 事件目标是事件触发时,分派的事件对象,Node 是最常见的事件目标,
* 但是其他对象也可以是事件目标。
* 可通过 cc.EventTarget 获得该对象。
* !#en
* EventTarget is an object to which an event is dispatched when something has occurred.
* Entity are the most common event targets, but other objects can be event targets too.
*
* Event targets are an important part of the Fireball event model.
* The event target serves as the focal point for how events flow through the scene graph.
* When an event such as a mouse click or a keypress occurs, Fireball dispatches an event object
* into the event flow from the root of the hierarchy. The event object then makes its way through
* the scene graph until it reaches the event target, at which point it begins its return trip through
* the scene graph. This round-trip journey to the event target is conceptually divided into three phases:
* - The capture phase comprises the journey from the root to the last node before the event target's node
* - The target phase comprises only the event target node
* - The bubbling phase comprises any subsequent nodes encountered on the return trip to the root of the tree
* See also: http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
*
* Event targets can implement the following methods:
* - _getCapturingTargets
* - _getBubblingTargets
*
* If a class cannot extend from EventTarget, it can consider implements IEventTarget interface.
*
* !#zh
* 事件目标是具有注册监听器、派发事件能力的类,Node 是最常见的事件目标,
* 但是其他类也可以继承自事件目标以获得管理监听器和派发事件的能力。
* 如果无法继承自 EventTarget,也可以考虑自实现 IEventTarget
*/
export class EventTarget extends CallbacksInvoker {
/**
* @en
* Register an callback of a specific event type on the EventTarget.
* This type of event should be triggered via `emit`.
* @zh
* 注册事件目标的特定事件类型回调。这种类型的事件应该被 `emit` 触发。
*
* @param type - 一个监听事件类型的字符串.
* @param callback - 事件分派时将被调用的回调函数。如果该回调存在则不会重复添加.
* @param callback.arg1 回调的第一个参数
* @param callback.arg2 回调的第二个参数
* @param callback.arg3 回调的第三个参数
* @param callback.arg4 回调的第四个参数
* @param callback.arg5 回调的第五个参数
* @param target - 回调的目标。可以为空。
* @return - 返回监听回调函数自身。
*
* @param type - A string representing the event type to listen for.
* @param callback - The callback that will be invoked when the event is dispatched.
* The callback is ignored if it is a duplicate (the callbacks are unique).
* @param target - The target (this object) to invoke the callback, can be null
* @return - Just returns the incoming callback so you can save the anonymous function easier.
* @example
* ```typescript
* eventTarget.on('fire', function () {
* cc.log("fire in the hole");
* }, node);
* ```
*/
public on (type: string, callback: Function, target?: Object) {
if (!callback) {
@@ -88,15 +105,16 @@ export class EventTarget extends CallbacksInvoker {
}
/**
* @en
* Removes the listeners previously registered with the same type, callback, target and or useCapture,
* if only type is passed as parameter, all listeners registered with that type will be removed.
* @zh
* 删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。
*
* @param type - 一个监听事件类型的字符串。
* @param callback - 事件分派时将被调用的回调函数。
* @param target - 调用回调的目标。如果为空, 只有没有目标的事件会被移除。
*
* @param type - A string representing the event type being removed.
* @param callback - The callback to remove.
* @param target - The target (this object) to invoke the callback, if it's not given, only callback without target will be removed
* @example
* ```typescript
* // register fire eventListener
* var callback = eventTarget.on('fire', function () {
* cc.log("fire in the hole");
@@ -105,7 +123,6 @@ export class EventTarget extends CallbacksInvoker {
* eventTarget.off('fire', callback, target);
* // remove all fire event listeners
* eventTarget.off('fire');
* ```
*/
public off (type: string, callback?: Function, target?: Object) {
if (!callback) {
@@ -126,36 +143,34 @@ export class EventTarget extends CallbacksInvoker {
}
/**
* @zh
* 在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。
* @en Removes all callbacks previously registered with the same target (passed as parameter).
* This is not for removing all listeners in the current event target,
* and this is not for removing all listeners the target parameter have registered.
* It's only for removing all listeners (callback and target couple) registered on the current event target by the target parameter.
* @zh 在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。
* 这个函数无法删除当前 EventTarget 的所有事件监听器,也无法删除 target 参数所注册的所有事件监听器。
* 这个函数只能删除 target 参数在当前 EventTarget 上注册的所有事件监听器。
*
* @param target - 注销所有指定目标的监听
* @param target - The target to be searched for all related listeners
*/
public targetOff (keyOrTarget?: string | Object) {
this.removeAll(keyOrTarget);
}
/**
* @en
* Register an callback of a specific event type on the EventTarget,
* the callback will remove itself after the first time it is triggered.
* @zh
* 注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。
*
* @param type - 一个监听事件类型的字符串。
* @param callback - 事件分派时将被调用的回调函数。如果该回调存在则不会重复添加。
* @param callback.arg1 回调的第一个参数。
* @param callback.arg2 第二个参数。
* @param callback.arg3 第三个参数。
* @param callback.arg4 第四个参数。
* @param callback.arg5 第五个参数。
* @param target - 调用回调的目标。可以为空。
*
* @param type - A string representing the event type to listen for.
* @param callback - The callback that will be invoked when the event is dispatched.
* The callback is ignored if it is a duplicate (the callbacks are unique).
* @param target - The target (this object) to invoke the callback, can be null
* @example
* ```typescript
* eventTarget.once('fire', function () {
* cc.log("this is the callback and will be invoked only once");
* }, node);
* ```
*/
public once (type: string, callback: Function, target?: Object) {
if (!callback) {
+10 -14
View File
@@ -34,8 +34,7 @@
* Base class of all kinds of events.
*
* @zh
* 包含事件相关信息的对象。
* 可通过 cc.Event 获得该事件信息。
* 所有事件对象的基类,包含事件相关基本信息。
*/
export default class Event {
// Event types
@@ -134,7 +133,7 @@ export default class Event {
/**
* @en
* Indicate whether the event bubbles up through the tree or not.
* Indicate whether the event bubbles up through the hierarchy or not.
*
* @zh
* 表示该事件是否进行冒泡。
@@ -208,7 +207,7 @@ export default class Event {
* Reset the event for being stored in the object pool.
*
* @zh
* 重置对象池中存储的事件。
* 重置事件对象以便在对象池中存储。
*/
public unuse () {
this.type = Event.NO_TYPE;
@@ -221,10 +220,11 @@ export default class Event {
/**
* @en
* Reuse the event for being used again by the object pool.
*
* Reinitialize the event for being used again after retrieved from the object pool.
* @zh
* 用于对象池再次使用的事件。
* 重新初始化让对象池中取出的事件可再次使用。
* @param type - The name of the event (case-sensitive), e.g. "click", "fire", or "submit"
* @param bubbles - A boolean indicating whether the event bubbles up through the tree or not
*/
public reuse (type: string, bubbles?: boolean) {
this.type = type;
@@ -261,12 +261,9 @@ export default class Event {
/**
* @en
* <p>
* Gets current target of the event <br/>
* note: It only be available when the event listener is associated with node. <br/>
* It returns 0 when the listener is associated with fixed priority.
* </p>
*
* Gets current target of the event <br/>
* note: It only be available when the event listener is associated with node. <br/>
* It returns 0 when the listener is associated with fixed priority.
* @zh
* 获取当前目标节点
* @returns - The target with which the event associates.
@@ -278,7 +275,6 @@ export default class Event {
/**
* @en
* Gets the event type.
*
* @zh
* 获取事件类型。
*/
+3 -1
View File
@@ -35,7 +35,8 @@ import { WebGL2GFXDevice } from './gfx/webgl2/webgl2-device';
import { ForwardPipeline, RenderPipeline } from './pipeline';
import * as debug from './platform/debug';
import inputManager from './platform/event-manager/input-manager';
import sys from './platform/sys';
import { sys } from './platform/sys';
import { macro } from './platform/macro';
import { ICustomJointTextureLayout } from './renderer';
/**
@@ -895,6 +896,7 @@ export class Game extends EventTarget {
const opts = {
canvasElm: this.canvas as HTMLCanvasElement,
debug: true,
isAntialias: EDITOR || macro.ENABLE_WEBGL_ANTIALIAS,
devicePixelRatio: window.devicePixelRatio,
nativeWidth: Math.floor(screen.width * window.devicePixelRatio),
nativeHeight: Math.floor(screen.height * window.devicePixelRatio),
+1 -1
View File
@@ -1,6 +1,6 @@
import { ALIPAY, RUNTIME_BASED } from 'internal:constants';
import { macro } from '../../platform';
import sys from '../../platform/sys';
import { sys } from '../../platform/sys';
import { GFXBindingLayout, IGFXBindingLayoutInfo } from '../binding-layout';
import { GFXBuffer, IGFXBufferInfo } from '../buffer';
import { GFXCommandAllocator, IGFXCommandAllocatorInfo } from '../command-allocator';
-1
View File
@@ -158,7 +158,6 @@ export class SubContextView extends Component {
/**
* @en Reset open data context size and viewport
* @zh 重置开放数据域的尺寸和视窗
* @method reset
*/
public reset () {
if (this._context) {
+14 -4
View File
@@ -96,6 +96,16 @@ export function error (message?: any, ...optionalParams: any[]) {
return ccError(message, ...optionalParams);
}
/**
* @en
* Assert the condition and output error messages if the condition is not true.
* @zh
* 对检查测试条件进行检查,如果条件不为 true 则输出错误消息
* @param value - The condition to check on
* @param message - A JavaScript string containing zero or more substitution strings.
* @param optionalParams - JavaScript objects with which to replace substitution strings within msg.
* This gives you additional control over the format of the output.
*/
export function assert (value: any, message?: string, ...optionalParams: any[]) {
return ccAssert(value, message, ...optionalParams);
}
@@ -345,16 +355,16 @@ export function getError (errorId: any, ...param: any[]): string {
}
/**
* @en Returns whether or not to display the FPS information.
* @zh 是否显示 FPS 信息。
* @en Returns whether or not to display the FPS and debug information.
* @zh 是否显示 FPS 信息和部分调试信息。
*/
export function isDisplayStats (): boolean {
return cc.profiler ? cc.profiler.isShowingStats() : false;
}
/**
* @en Sets whether display the FPS on the bottom-left corner.
* @zh 设置是否在左下角显示 FPS。
* @en Sets whether display the FPS and debug informations on the bottom-left corner.
* @zh 设置是否在左下角显示 FPS 和部分调试。
*/
export function setDisplayStats (displayStats: boolean) {
if (cc.profiler) {
+14 -15
View File
@@ -4,8 +4,8 @@
import { ccenum } from '../../value-types/enum';
/**
* @zh
* 一般用于系统事件或者节点事件的事件枚举
* @en The event type supported by SystemEvent and Node events
* @zh SystemEvent 支持的事件类型以及节点事件类型
*/
export enum SystemEventType {
/**
@@ -136,8 +136,8 @@ export enum SystemEventType {
TRANSFORM_CHANGED = 'transform-changed',
/**
* @en The event type for press the devicemotion event, you can use its value directly: 'devicemotion'.
* @zh
* @en The event type for notifying the host scene has been changed for a persist node.
* @zh 当场景常驻节点的场景发生改变时触发的事件,一般在切换场景过程中触发。
*/
SCENE_CHANGED_FOR_PERSISTS = 'scene-changed-for-persists',
@@ -160,40 +160,39 @@ export enum SystemEventType {
* if the event callback have heavy logic it may have great performance impact, try to avoid such scenario.
*
* @zh
* 当节点锚点改变时触发的事件。
* 当节点的 UITransform 锚点改变时触发的事件。
* 性能警告:这个事件会在每次对应的属性被修改时触发,如果事件回调损耗较高,有可能对性能有很大的负面影响,请尽量避免这种情况。
* @deprecated
*/
ANCHOR_CHANGED = 'anchor-changed',
/**
* @en
* The event type for add children.
* The event type for adding a new child node to the target node.
*
* @zh
* 节点子类添加。
* 给目标节点添加子节点时触发的事件。
*/
CHILD_ADDED = 'child-added',
/**
* @en
* The event type for removed children.
* The event type for removing a child node from the target node.
*
* @zh
* 节点子类移除。
* 给目标节点移除子节点时触发的事件。
*/
CHILD_REMOVED = 'child-removed',
/**
* @en
* @zh
* 父节点改变时触发的事件。
* @en The event type for changing the parent of the target node
* @zh 目标节点的父节点改变时触发的事件。
*/
PARENT_CHANGED = 'parent-changed',
/**
* @en
* @zh
* 节点销毁时触发的事件。
* @en The event type for destroying the target node
* @zh 目标节点被销毁时触发的事件。
*/
NODE_DESTROYED = 'node-destroyed',
}
@@ -1,7 +1,6 @@
/*
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
@@ -26,7 +25,7 @@
*/
/**
* @category event
* @hidden
*/
import { EventKeyboard, EventAcceleration, EventMouse } from './events';
@@ -44,20 +43,14 @@ export interface IListenerMask {
}
/**
* <p><br/>
* The base class of event listener. <br/>
* If you need custom listener which with different callback, you need to inherit this class. <br/>
* For instance, you could refer to EventListenerAcceleration, EventListenerKeyboard, <br/>
* EventListenerTouchOneByOne, EventListenerCustom.<br/>
* </p><br/>
* 封装用户的事件处理逻辑。<br/>
* @en The base class of event listener. <br/>
* If you need custom listener which with different callback, you need to inherit this class. <br/>
* For instance, you could refer to EventListenerAcceleration, EventListenerKeyboard, <br/>
* EventListenerTouchOneByOne, EventListenerCustom.<br/>
* @zh 封装用户的事件处理逻辑
* 注意:这是一个抽象类,开发者不应该直接实例化这个类,请参考 [[create]] 。
*/
export class EventListener {
public get onEvent () {
return this._onEvent;
}
/**
* @en The type code of unknown event listener.<br/>
* @zh 未知的事件监听器类型
@@ -114,7 +107,7 @@ export class EventListener {
* You can also pass custom parameters in the configuration object,<br/>
* all custom parameters will be polyfilled into the event listener object and can be accessed in handlers.<br/>
* @zh 通过指定不同的 Event 对象来设置想要创建的事件监听器。
* @param {Object} argObj a json object
* @param argObj a json object
*/
public static create (argObj: IEventListenerCreateInfo): EventListener {
cc.assertID(argObj && argObj.event, 1900);
@@ -178,6 +171,10 @@ export class EventListener {
// Whether the listener is enabled
private _isEnabled = true; // Whether the listener is enabled
public get onEvent () {
return this._onEvent;
}
constructor (type: number, listenerID: string, callback: ((...args: any[]) => any) | null) {
this._onEvent = callback;
this._type = type || 0;
@@ -34,6 +34,7 @@ import { Event } from '../../event';
import { EventTouch } from './events';
import { EventListener, TouchOneByOne } from './event-listener';
import { Node } from '../../scene-graph';
import { macro } from '../macro';
const ListenerID = EventListener.ListenerID;
function checkUINode (node) {
@@ -946,7 +947,7 @@ class EventManager {
const getCode = event.getEventCode();
// const EventTouch = cc.Event.EventTouch;
if (getCode === EventTouch.BEGAN) {
if (!cc.macro.ENABLE_MULTI_TOUCH && eventManager._currentTouch) {
if (!macro.ENABLE_MULTI_TOUCH && eventManager._currentTouch) {
const node = eventManager._currentTouchListener!._node;
if (!node || node.activeInHierarchy) {
return false;
@@ -964,7 +965,7 @@ class EventManager {
removedIdx = listener._claimedTouches.indexOf(selTouch);
if (removedIdx !== -1) {
isClaimed = true;
if (!cc.macro.ENABLE_MULTI_TOUCH && eventManager._currentTouch && eventManager._currentTouch !== selTouch) {
if (!macro.ENABLE_MULTI_TOUCH && eventManager._currentTouch && eventManager._currentTouch !== selTouch) {
return false;
}
if (getCode === EventTouch.MOVED && listener.onTouchMoved) {
+185 -309
View File
@@ -1,6 +1,6 @@
/*
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
@@ -32,139 +32,118 @@ import Event from '../../event/event';
import { Vec2 } from '../../math/vec2';
import { Touch } from './touch';
let _vec2 = new Vec2();
/**
* @en The mouse event
* @zh 鼠标事件类型
* @class Event.EventMouse
* @extends Event
*/
export class EventMouse extends Event {
// Inner event types of MouseEvent
/**
* @en
* The none event code of mouse event.
*
* @zh
* 无。
* @en The none event code of mouse event.
* @zh 无效事件代码
*/
public static NONE = 0;
/**
* @en
* The event type code of mouse down event.
*
* @zh
* 鼠标按下事件。
* @en The event code of mouse down event.
* @zh 鼠标按下事件代码。
*/
public static DOWN = 1;
/**
* @en
* The event type code of mouse up event.
*
* @zh
* 鼠标按下后释放事件。
* @en The event code of mouse up event.
* @zh 鼠标按下后释放事件代码。
*/
public static UP = 2;
/**
* @en
* The event type code of mouse move event.
*
* @zh
* 鼠标移动事件。
* @en The event code of mouse move event.
* @zh 鼠标移动事件。
*/
public static MOVE = 3;
/**
* @en
* The event type code of mouse scroll event.
*
* @zh
* 鼠标滚轮事件。
* @en The event code of mouse scroll event.
* @zh 鼠标滚轮事件。
*/
public static SCROLL = 4;
/**
* @en
* The tag of Mouse left button.
*
* @zh
* 鼠标左键的标签。
* @en The default tag when no button is pressed
* @zh 按键默认的缺省状态
*/
public static BUTTON_MISSING = -1;
/**
* @en The tag of mouse's left button.
* @zh 鼠标左键的标签。
*/
public static BUTTON_LEFT = 0;
/**
* @en
* The tag of Mouse right button (The right button number is 2 on browser).
*
* @zh
* 鼠标右键的标签。
* @en The tag of mouse's right button (The right button number is 2 on browser).
* @zh 鼠标右键的标签。
*/
public static BUTTON_RIGHT = 2;
/**
* @en
* The tag of Mouse middle button (The right button number is 1 on browser).
*
* @zh
* 鼠标中键的标签。
* @en The tag of mouse's middle button.
* @zh 鼠标中键的标签。
*/
public static BUTTON_MIDDLE = 1;
/**
* @en
* The tag of Mouse button 4.
*
* @zh
* 鼠标按键 4 的标签。
* @en The tag of mouse's button 4.
* @zh 鼠标按键 4 的标签。
*/
public static BUTTON_4 = 3;
/**
* @en
* The tag of Mouse button 5.
*
* @zh
* 鼠标按键 5 的标签。
* @en The tag of mouse's button 5.
* @zh 鼠标按键 5 的标签。
*/
public static BUTTON_5 = 4;
/**
* @en
* The tag of Mouse button 6.
*
* @zh
* 鼠标按键 6 的标签。
* @en The tag of mouse's button 6.
* @zh 鼠标按键 6 的标签。
*/
public static BUTTON_6 = 5;
/**
* @en
* The tag of Mouse button 7.
*
* @zh
* 鼠标按键 7 的标签。
* @en The tag of mouse's button 7.
* @zh 鼠标按键 7 的标签。
*/
public static BUTTON_7 = 6;
/**
* @en
* The tag of Mouse button 8.
*
* @zh
* 鼠标按键 8 的标签。
* @en The tag of mouse's button 8.
* @zh 鼠标按键 8 的标签。
*/
public static BUTTON_8 = 7;
/**
* @en Mouse movement on x axis of the UI coordinate system.
* @zh 鼠标在 UI 坐标系下 X 轴上的移动距离
*/
public movementX: number = 0;
/**
* @en Mouse movement on y axis of the UI coordinate system.
* @zh 鼠标在 UI 坐标系下 Y 轴上的移动距离
*/
public movementY: number = 0;
/**
* @en The type of the event, possible values are UP, DOWN, MOVE, SCROLL
* @zh 鼠标事件类型,可以是 UP, DOWN, MOVE, CANCELED。
*/
public eventType: number;
private _button: number | null = 0;
private _button: number = EventMouse.BUTTON_MISSING;
private _x: number = 0;
@@ -179,8 +158,8 @@ export class EventMouse extends Event {
private _scrollY: number = 0;
/**
* @param eventType - 鼠标时间类型 UP, DOWN, MOVE, CANCELED。
* @param bubbles - 事件是否通过树结构冒泡。默认为 false。
* @param eventType - The type of the event, possible values are UP, DOWN, MOVE, SCROLL
* @param bubbles - Indicate whether the event bubbles up through the hierarchy or not.
*/
constructor (eventType: number, bubbles?: boolean, prevLoc?: Vec2) {
super(Event.MOUSE, bubbles);
@@ -192,11 +171,10 @@ export class EventMouse extends Event {
}
/**
* @en
* Sets scroll data.
*
* @zh
* 设置鼠标的滚动数据。
* @en Sets scroll data of the mouse.
* @zh 设置鼠标滚轮的滚动数据。
* @param scrollX - The scroll value on x axis
* @param scrollY - The scroll value on y axis
*/
public setScrollData (scrollX: number, scrollY: number) {
this._scrollX = scrollX;
@@ -204,33 +182,26 @@ export class EventMouse extends Event {
}
/**
* @en
* Returns the x axis scroll value.
*
* @zh
* 获取鼠标滚动的X轴距离,只有滚动时才有效。
* @en Returns the scroll value on x axis.
* @zh 获取鼠标滚动的 X 轴距离,只有滚动时才有效。
*/
public getScrollX () {
return this._scrollX;
}
/**
* @en
* Returns the y axis scroll value.
*
* @zh
* 获取滚轮滚动的 Y 轴距离,只有滚动时才有效。
* @en Returns the scroll value on y axis.
* @zh 获取滚轮滚动的 Y 轴距离,只有滚动时才有效。
*/
public getScrollY () {
return this._scrollY;
}
/**
* @en
* Sets cursor location.
*
* @zh
* 设置当前鼠标位置。
* @en Sets cursor location.
* @zh 设置当前鼠标位置。
* @param x - The location on x axis
* @param y - The location on y axis
*/
public setLocation (x: number, y: number) {
this._x = x;
@@ -238,11 +209,9 @@ export class EventMouse extends Event {
}
/**
* @en
* Returns cursor location.
*
* @zh
* 获取鼠标相对于左下角位置对象,对象包含 x 和 y 属性。
* @en Returns cursor location.
* @zh 获取鼠标相对于左下角位置对象,对象包含 x 和 y 属性。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getLocation (out?: Vec2) {
if (!out) {
@@ -254,11 +223,9 @@ export class EventMouse extends Event {
}
/**
* @en
* Returns the current cursor location in screen coordinates.
*
* @zh
* 获取当前事件在游戏窗口内的坐标位置对象,对象包含 x 和 y 属性。
* @en Returns the current cursor location in game view coordinates.
* @zh 获取当前事件在游戏窗口内的坐标位置对象,对象包含 x 和 y 属性。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getLocationInView (out?: Vec2) {
if (!out) {
@@ -270,11 +237,9 @@ export class EventMouse extends Event {
}
/**
* @en
* Returns the current cursor location in ui coordinates.
*
* @zh
* 获取当前事件在 UI 窗口内的坐标位置,对象包含 x 和 y 属性。
* @en Returns the current cursor location in ui coordinates.
* @zh 获取当前事件在 UI 窗口内的坐标位置,对象包含 x 和 y 属性。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getUILocation (out?: Vec2){
if (!out) {
@@ -287,11 +252,9 @@ export class EventMouse extends Event {
}
/**
* @en
* Returns the previous touch location.
*
* @zh
* 获取鼠标点击在上一次事件时的位置对象,对象包含 x 和 y 属性。
* @en Returns the previous touch location.
* @zh 获取鼠标点击在上一次事件时的位置对象,对象包含 x 和 y 属性。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getPreviousLocation (out?: Vec2) {
if (!out) {
@@ -303,11 +266,9 @@ export class EventMouse extends Event {
}
/**
* @en
* Returns the previous touch location.
*
* @zh
* 获取鼠标点击在上一次事件时的位置对象,对象包含 x 和 y 属性。
* @en Returns the previous touch location.
* @zh 获取鼠标点击在上一次事件时的位置对象,对象包含 x 和 y 属性。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getUIPreviousLocation (out?: Vec2) {
if (!out) {
@@ -320,11 +281,9 @@ export class EventMouse extends Event {
}
/**
* @en
* Returns the delta distance from the previous location to current location.
*
* @zh
* 获取鼠标距离上一次事件移动的距离对象,对象包含 x 和 y 属性。
* @en Returns the delta distance from the previous location to current location.
* @zh 获取鼠标距离上一次事件移动的距离对象,对象包含 x 和 y 属性。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getDelta (out?: Vec2) {
if (!out) {
@@ -336,33 +295,25 @@ export class EventMouse extends Event {
}
/**
* @en
* Returns the X axis delta distance from the previous location to current location.
*
* @zh
* 获取鼠标距离上一次事件移动的 X 轴距离。
* @en Returns the X axis delta distance from the previous location to current location.
* @zh 获取鼠标距离上一次事件移动的 X 轴距离。
*/
public getDeltaX () {
return this._x - this._prevX;
}
/**
* @en
* Returns the Y axis delta distance from the previous location to current location.
*
* @zh
* 获取鼠标距离上一次事件移动的 Y 轴距离。
* @en Returns the Y axis delta distance from the previous location to current location.
* @zh 获取鼠标距离上一次事件移动的 Y 轴距离。
*/
public getDeltaY () {
return this._y - this._prevY;
}
/**
* @en
* Returns the delta distance from the previous location to current location.
*
* @zh
* 获取鼠标距离上一次事件移动的距离对象,对象包含 x 和 y 属性。
* @en Returns the delta distance from the previous location to current location in the UI coordinates.
* @zh 获取鼠标距离上一次事件移动在 UI 坐标系下的距离对象,对象包含 x 和 y 属性。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getUIDelta (out?: Vec2) {
if (!out) {
@@ -374,74 +325,57 @@ export class EventMouse extends Event {
}
/**
* @en
* Returns the X axis delta distance from the previous location to current location.
*
* @zh
* 获取鼠标距离上一次事件移动的 X 轴距离。
* @en Returns the X axis delta distance from the previous location to current location in the UI coordinates.
* @zh 获取鼠标距离上一次事件移动在 UI 坐标系下的 X 轴距离。
*/
public getUIDeltaX () {
return (this._x - this._prevX) / cc.view.getScaleX();
}
/**
* @en
* Returns the Y axis delta distance from the previous location to current location.
*
* @zh
* 获取鼠标距离上一次事件移动的 Y 轴距离。
* @en Returns the Y axis delta distance from the previous location to current location in the UI coordinates.
* @zh 获取鼠标距离上一次事件移动在 UI 坐标系下的 Y 轴距离。
*/
public getUIDeltaY () {
return (this._y - this._prevY) / cc.view.getScaleY();
}
/**
* @en
* Sets mouse button.
*
* @zh
* 设置鼠标按键。
* @en Sets mouse button code.
* @zh 设置鼠标按键。
* @param button - The button code
*/
public setButton (button: number | null) {
public setButton (button: number) {
this._button = button;
}
/**
* @en
* Returns mouse button.
*
* @zh
* 获取鼠标按键。
* @en Returns mouse button code.
* @zh 获取鼠标按键。
*/
public getButton () {
return this._button;
}
/**
* @en
* Returns location X axis data.
*
* @zh
* 获取鼠标当前位置 X 轴。
* @en Returns location data on X axis.
* @zh 获取鼠标当前 X 轴位置。
*/
public getLocationX () {
return this._x;
}
/**
* @en Returns location Y axis data.
* @zh 获取鼠标当前位置 Y 轴。
* @en Returns location data on Y axis.
* @zh 获取鼠标当前 Y 轴位置。
*/
public getLocationY () {
return this._y;
}
/**
* @en
* Returns location X axis data.
*
* @zh
* 获取鼠标当前位置 X 轴。
* @en Returns location data on X axis.
* @zh 获取鼠标当前 X 轴位置。
*/
public getUILocationX () {
const viewport = cc.view.getViewportRect();
@@ -449,11 +383,8 @@ export class EventMouse extends Event {
}
/**
* @en
* Returns location Y axis data.
*
* @zh
* 获取鼠标当前位置 Y 轴。
* @en Returns location data on Y axis.
* @zh 获取鼠标当前 Y 轴位置。
*/
public getUILocationY () {
const viewport = cc.view.getViewportRect();
@@ -470,55 +401,41 @@ export class EventMouse extends Event {
*/
export class EventTouch extends Event {
/**
* @en
* The maximum touch numbers
*
* @zh
* 最大触摸数量。
* @en The maximum touch point numbers simultaneously
* @zh 同时存在的最大触点数量。
*/
public static MAX_TOUCHES = 5;
/**
* @en
* The event type code of touch began event.
*
* @zh
* 开始触摸事件。
* @en The event type code of touch began event.
* @zh 开始触摸事件。
*/
public static BEGAN = 0;
/**
* @en
* The event type code of touch moved event.
*
* @zh
* 触摸后移动事件。
* @en The event type code of touch moved event.
* @zh 触摸后移动事件。
*/
public static MOVED = 1;
/**
* @en
* The event type code of touch ended event.
*
* @zh
* 结束触摸事件。
* @en The event type code of touch ended event.
* @zh 结束触摸事件。
*/
public static ENDED = 2;
/**
* @en
* The event type code of touch cancelled event.
*
* @en The event type code of touch canceled event.
* @zh 取消触摸事件。
*/
public static CANCELLED = 3;
/**
* @en
* The current touch object
*
* @zh
* 当前触点对象
* @en The current touch object
* @zh 当前触点对象
*/
public touch: Touch | null = null;
/**
* @en Indicate whether the touch event is simulated or real
* @zh 表示触摸事件是真实触点触发的还是模拟的
*/
public simulate = false;
private _eventCode: number;
@@ -526,8 +443,9 @@ export class EventTouch extends Event {
private _touches: Touch[];
/**
* @param touches - touch 数组
* @param bubbles - 事件是否通过树结构冒泡。默认为 false。
* @param touches - An array of current touches
* @param bubbles - Indicate whether the event bubbles up through the hierarchy or not.
* @param eventCode - The type code of the touch event
*/
constructor (touches?: Touch[], bubbles?: boolean, eventCode?: number) {
super(Event.TOUCH, bubbles);
@@ -536,33 +454,26 @@ export class EventTouch extends Event {
}
/**
* @en
* Returns event code.
*
* @zh
* 获取事件类型。
* @en Returns event type code.
* @zh 获取触摸事件类型。
*/
public getEventCode () {
return this._eventCode;
}
/**
* @en
* Returns touches of event.
*
* @zh
* 获取触摸点的列表。
* @en Returns touches of event.
* @zh 获取触摸点的列表。
*/
public getTouches () {
return this._touches;
}
/**
* @en
* Sets touch location.
*
* @zh
* 设置当前触点位置
* @en Sets touch location.
* @zh 设置当前触点位置
* @param x - The current touch location on the x axis
* @param y - The current touch location on the y axis
*/
public setLocation (x: number, y: number) {
if (this.touch) {
@@ -571,151 +482,112 @@ export class EventTouch extends Event {
}
/**
* @en
* Returns touch location.
*
* @zh
* 获取触点位置。
* @en Returns the current touch location.
* @zh 获取触点位置。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getLocation (out?: Vec2) {
return this.touch ? this.touch.getLocation(out) : new Vec2();
}
/**
* @en
* Returns touch location.
*
* @zh
* 获取触点位置。
* @en Returns the current touch location in UI coordinates.
* @zh 获取 UI 坐标系下的触点位置。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getUILocation(out?: Vec2) {
return this.touch ? this.touch.getUILocation(out) : new Vec2();
}
/**
* @en
* Returns the current touch location in screen coordinates.
*
* @zh
* 获取当前触点在游戏窗口中的位置。
* @en Returns the current touch location in game screen coordinates.
* @zh 获取当前触点在游戏窗口中的位置。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getLocationInView (out?: Vec2) {
return this.touch ? this.touch.getLocationInView(out) : new Vec2();
}
/**
* @en
* Returns the current touch location in screen coordinates.
*
* @zh
* 获取当前触点在游戏窗口中的位置。
*/
public getUILocationInView(out?: Vec2) {
return this.touch ? this.touch.getLocationInView(out) : new Vec2();
}
/**
* @en Returns the previous touch location.
* @zh 获取触点在上一次事件时的位置对象,对象包含 x 和 y 属性。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getPreviousLocation (out?: Vec2) {
return this.touch ? this.touch.getPreviousLocation(out) : new Vec2();
}
/**
* @en
* Returns the start touch location.
*
* @zh
* 获获取触点落下时的位置对象,对象包含 x 和 y 属性。
* @en Returns the start touch location.
* @zh 获获取触点落下时的位置对象,对象包含 x 和 y 属性。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getStartLocation (out?: Vec2) {
return this.touch ? this.touch.getStartLocation(out) : new Vec2();
}
/**
* @en
* Returns the start touch location.
*
* @zh
* 获获取触点落下时的 UI 世界下位置对象,对象包含 x 和 y 属性。
* @en Returns the start touch location in UI coordinates.
* @zh 获获取触点落下时的 UI 世界下位置对象,对象包含 x 和 y 属性。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getUIStartLocation(out?: Vec2) {
return this.touch ? this.touch.getUIStartLocation(out) : new Vec2();
}
/**
* @en
* Returns the id of cc.Touch.
*
* @zh
* 触点的标识 ID,可以用来在多点触摸中跟踪触点。
* @en Returns the id of the current touch point.
* @zh 获取触点的标识 ID,可以用来在多点触摸中跟踪触点。
*/
public getID () {
return this.touch ? this.touch.getID() : null;
}
/**
* @en
* Returns the delta distance from the previous location to current location.
*
* @zh
* 获取触点距离上一次事件移动的距离对象,对象包含 x 和 y 属性。
* @en Returns the delta distance from the previous location to current location.
* @zh 获取触点距离上一次事件移动的距离对象,对象包含 x 和 y 属性。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getDelta (out?: Vec2) {
return this.touch ? this.touch.getDelta(out) : new Vec2();
}
/**
* @en
* Returns the delta distance from the previous location to current location.
*
* @zh
* 获取触点距离上一次事件 UI 世界下移动的距离对象,对象包含 x 和 y 属性。
* @en Returns the delta distance from the previous location to current location.
* @zh 获取触点距离上一次事件 UI 世界下移动的距离对象,对象包含 x 和 y 属性。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getUIDelta(out?: Vec2) {
return this.touch ? this.touch.getUIDelta(out) : new Vec2();
}
/**
* @en
* Returns the X axis delta distance from the previous location to current location.
*
* @zh
* 获取触点距离上一次事件移动的 x 轴距离。
* @en Returns the X axis delta distance from the previous location to current location.
* @zh 获取触点距离上一次事件移动的 x 轴距离。
*/
public getDeltaX (out?: Vec2) {
return this.touch ? this.touch.getDelta(out).x : 0;
public getDeltaX () {
return this.touch ? this.touch.getDelta(_vec2).x : 0;
}
/**
* @en
* Returns the Y axis delta distance from the previous location to current location.
*
* @zh
* 获取触点距离上一次事件移动的 y 轴距离。
* @en Returns the Y axis delta distance from the previous location to current location.
* @zh 获取触点距离上一次事件移动的 y 轴距离。
*/
public getDeltaY (out?: Vec2) {
return this.touch ? this.touch.getDelta(out).y : 0;
public getDeltaY () {
return this.touch ? this.touch.getDelta(_vec2).y : 0;
}
/**
* @en
* Returns location X axis data.
*
* @zh
* 获取当前触点 X 轴位置。
* @en Returns location X axis data.
* @zh 获取当前触点 X 轴位置。
*/
public getLocationX () {
return this.touch ? this.touch.getLocationX() : 0;
}
/**
* @en
* Returns location Y axis data.
*
* @zh
* 获取当前触点 Y 轴位置。
* @en Returns location Y axis data.
* @zh 获取当前触点 Y 轴位置。
*/
public getLocationY () {
return this.touch ? this.touch.getLocationY() : 0;
@@ -725,16 +597,19 @@ export class EventTouch extends Event {
/**
* @en
* The acceleration event.
*
* @zh
* 加速度事件。
* 加速计事件。
*/
export class EventAcceleration extends Event {
/**
* @en The acceleration object
* @zh 加速度对象
*/
public acc: Object;
/**
* @param acc - 加速度
* @param bubbles - 事件是否通过树结构冒泡。默认为 false。
* @param acc - The acceleration
* @param bubbles - Indicate whether the event bubbles up through the hierarchy or not.
*/
constructor (acc: Object, bubbles?: boolean) {
super(Event.ACCELERATION, bubbles);
@@ -745,36 +620,37 @@ export class EventAcceleration extends Event {
/**
* @en
* The keyboard event.
*
* @zh
* 键盘事件。
*/
export class EventKeyboard extends Event {
/**
* @en
* The keyCode read-only property represents a system and implementation dependent numerical code
* @en The keyCode read-only property represents a system and implementation dependent numerical code
* identifying the unmodified value of the pressed key.
* This is usually the decimal ASCII (RFC 20) or Windows 1252 code corresponding to the key.
* If the key can't be identified, this value is 0.
*
* @zh
* keyCode 是只读属性它表示一个系统和依赖于实现的数字代码,可以识别按键的未修改值。
* @zh keyCode 是只读属性它表示一个系统和依赖于实现的数字代码,可以识别按键的未修改值。
* 这通常是十进制 ASCII (RFC20) 或者 Windows 1252 代码,所对应的密钥。
* 如果无法识别该键,则该值为 0。
*/
public keyCode: number;
/**
* Raw DOM event.
* @en Raw DOM KeyboardEvent.
* @zh 原始 DOM KeyboardEvent 事件对象
*/
public rawEvent?: KeyboardEvent;
/**
* @en Indicates whether the current key is being pressed
* @zh 表示当前按键是否正在被按下
*/
public isPressed: boolean;
/**
* @param keyCode - 事件触发的键值。
* @param isPressed - 指示键是否已按下。
* @param bubbles - 事件是否通过树结构冒泡。默认为 false。
* @param keyCode - The key code of the current key or the DOM KeyboardEvent
* @param isPressed - Indicates whether the current key is being pressed
* @param bubbles - Indicates whether the event bubbles up through the hierarchy or not.
*/
constructor (keyCode: number | KeyboardEvent, isPressed: boolean, bubbles?: boolean) {
super(Event.KEYBOARD, bubbles);
@@ -31,7 +31,7 @@
import { Vec2 } from '../../math/index';
import { rect } from '../../math/rect';
import { macro } from '../macro';
import sys from '../sys';
import { sys } from '../sys';
import eventManager from './event-manager';
import { EventAcceleration, EventKeyboard, EventMouse, EventTouch } from './events';
import { Touch } from './touch';
@@ -651,7 +651,7 @@ class InputManager {
listenDOMMouseEvent('mousemove', EventMouse.MOVE, (event, mouseEvent, location, pos) => {
this.handleTouchesMove([this.getTouchByXY(event, location.x, location.y, pos)]);
if (!this._mousePressed) {
mouseEvent.setButton(null);
mouseEvent.setButton(EventMouse.BUTTON_MISSING);
}
if (event.movementX !== undefined && event.movementY !== undefined) {
mouseEvent.movementX = event.movementX;
@@ -46,11 +46,6 @@ let mouseListener: EventListener | null = null;
* @en
* The System event, it currently supports keyboard events and accelerometer events.<br/>
* You can get the SystemEvent instance with cc.systemEvent.<br/>
* @example
* ```
* cc.systemEvent.on(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);
* cc.systemEvent.off(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);
* ```
* @zh
* 系统事件,它目前支持按键事件和重力感应事件。<br/>
* 你可以通过 cc.systemEvent 获取到 SystemEvent 的实例。<br/>
@@ -59,8 +54,6 @@ let mouseListener: EventListener | null = null;
* cc.systemEvent.on(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);
* cc.systemEvent.off(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);
* ```
* @class SystemEvent
* @extends EventTarget
*/
export class SystemEvent extends EventTarget {
@@ -70,29 +63,24 @@ export class SystemEvent extends EventTarget {
}
/**
* @en
* whether enable accelerometer event.
* Sets whether to enable the accelerometer event listener or not.
*
* @zh
* 是否启用加速度计事件。
*
* @param {Boolean} isEnable
*/
public setAccelerometerEnabled (isEnable: boolean) {
public setAccelerometerEnabled (isEnabled: boolean) {
if (EDITOR) {
return;
}
inputManager.setAccelerometerEnabled(isEnable);
inputManager.setAccelerometerEnabled(isEnabled);
}
/**
* @en
* set accelerometer interval value.
* Sets the accelerometer interval value.
*
* @zh
* 设置加速度计间隔值。
*
* @method setAccelerometerInterval
* @param {Number} interval
*/
public setAccelerometerInterval (interval: number) {
if (EDITOR) {
@@ -109,12 +97,14 @@ export class SystemEvent extends EventTarget {
callback: (touch?: Touch, event?: EventTouch) => void, target?: Object);
public on (type: SystemEventType.DEVICEMOTION, callback: (event?: EventAcceleration) => void, target?: Object);
/**
* @en
* Register an callback of a specific system event type.
* @zh
* 系统事件注册。
* 注册特定事件类型回调。
*
* @param type - 事件名。
* @param callback - 事件回调。
* @param target - 接收事件目标。
* @param type - The event type
* @param callback - The event listener's callback
* @param target - The event listener's target and callee
*/
public on (type: string, callback: Function, target?: Object) {
if (EDITOR) {
@@ -219,12 +209,15 @@ export class SystemEvent extends EventTarget {
}
/**
* @en
* Removes the listeners previously registered with the same type, callback, target and or useCapture,
* if only type is passed as parameter, all listeners registered with that type will be removed.
* @zh
* 注销事件。
* 删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。
*
* @param type - 事件名。
* @param callback - 事件回调。
* @param target - 回调接收对象。
* @param type - A string representing the event type being removed.
* @param callback - The callback to remove.
* @param target - The target (this object) to invoke the callback, if it's not given, only callback without target will be removed
*/
public off (type: string, callback?: Function, target?: Object) {
if (EDITOR) {
@@ -282,7 +275,8 @@ cc.SystemEvent = SystemEvent;
*/
/**
* 系统事件单例,方便全局使用。
* @en The singleton of the SystemEvent, there should only be one instance to be used globally
* @zh 系统事件单例,方便全局使用。
*/
export const systemEvent = new SystemEvent();
cc.systemEvent = systemEvent;
+64 -28
View File
@@ -32,19 +32,14 @@ import { Vec2 } from '../../math';
const _vec2 = new Vec2();
/**
* @en The touch event class
* @zh 封装了触摸相关的信息。
* @class Touch
*
* @param {Number} x
* @param {Number} y
* @param {Number} id
* @en The touch point class
* @zh 封装了触点相关的信息。
*/
export class Touch {
private _point: Vec2 = new Vec2();
private _prevPoint: Vec2 = new Vec2();
private _lastModified = 0;
private _id: number | null = null;
private _id: number = 0;
private _startPoint: Vec2 = new Vec2();
private _startPointCaptured: boolean = false;
@@ -52,13 +47,19 @@ export class Touch {
return this._lastModified;
}
constructor (x: number, y: number, id: number | null = null) {
/**
* @param x - x position of the touch point
* @param y - y position of the touch point
* @param id - The id of the touch point
*/
constructor (x: number, y: number, id: number = 0) {
this.setTouchInfo(id, x, y);
}
/**
* @en Returns the current touch location in OpenGL coordinates.、
* @zh 获取当前触点位置。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getLocation (out?: Vec2) {
if (!out){
@@ -86,8 +87,9 @@ export class Touch {
}
/**
* @en Returns the current touch location in OpenGL coordinates.、
* @zh 获取当前触点位置。
* @en Returns the current touch location in UI coordinates.、
* @zh 获取当前触点在 UI 坐标系中的位置。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getUILocation (out?: Vec2) {
if (!out) {
@@ -100,8 +102,8 @@ export class Touch {
}
/**
* @en Returns X axis location value.
* @zh 获取当前触点 X 轴位置。
* @en Returns X axis location value in UI coordinates.
* @zh 获取当前触点在 UI 坐标系中 X 轴位置。
*/
public getUILocationX () {
const viewport = cc.view.getViewportRect();
@@ -109,8 +111,8 @@ export class Touch {
}
/**
* @en Returns Y axis location value.
* @zh 获取当前触点 Y 轴位置。
* @en Returns Y axis location value in UI coordinates.
* @zh 获取当前触点在 UI 坐标系中 Y 轴位置。
*/
public getUILocationY () {
const viewport = cc.view.getViewportRect();
@@ -118,8 +120,9 @@ export class Touch {
}
/**
* @en Returns the previous touch location in OpenGL coordinates.
* @en Returns the previous touch location.
* @zh 获取触点在上一次事件时的位置对象,对象包含 x 和 y 属性。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getPreviousLocation (out?: Vec2) {
if (!out) {
@@ -131,8 +134,9 @@ export class Touch {
}
/**
* @en Returns the previous touch location in OpenGL coordinates.
* @zh 获取触点在上一次事件时的位置对象,对象包含 x 和 y 属性。
* @en Returns the previous touch location in UI coordinates.
* @zh 获取触点在上一次事件时在 UI 坐标系中的位置对象,对象包含 x 和 y 属性。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getUIPreviousLocation (out?: Vec2) {
if (!out) {
@@ -145,8 +149,9 @@ export class Touch {
}
/**
* @en Returns the start touch location in OpenGL coordinates.
* @en Returns the start touch location.
* @zh 获获取触点落下时的位置对象,对象包含 x 和 y 属性。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getStartLocation (out?: Vec2) {
if (!out) {
@@ -158,8 +163,9 @@ export class Touch {
}
/**
* @en Returns the start touch location in OpenGL coordinates.
* @zh 获获取触点落下时的位置对象,对象包含 x 和 y 属性。
* @en Returns the start touch location in UI coordinates.
* @zh 获获取触点落下时在 UI 坐标系中的位置对象,对象包含 x 和 y 属性。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getUIStartLocation (out?: Vec2) {
if (!out) {
@@ -172,8 +178,9 @@ export class Touch {
}
/**
* @en Returns the delta distance from the previous touche to the current one in screen coordinates.
* @en Returns the delta distance from the previous touche to the current one.
* @zh 获取触点距离上一次事件移动的距离对象,对象包含 x 和 y 属性。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getDelta (out?: Vec2) {
if (!out){
@@ -186,8 +193,9 @@ export class Touch {
}
/**
* @en Returns the delta distance from the previous touche to the current one in screen coordinates.
* @zh 获取触点距离上一次事件移动的距离对象,对象包含 x 和 y 属性。
* @en Returns the delta distance from the previous touche to the current one in UI coordinates.
* @zh 获取触点距离上一次事件移动在 UI 坐标系中的距离对象,对象包含 x 和 y 属性。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getUIDelta (out?: Vec2) {
if (!out) {
@@ -204,6 +212,7 @@ export class Touch {
/**
* @en Returns the current touch location in screen coordinates.
* @zh 获取当前事件在游戏窗口内的坐标位置对象,对象包含 x 和 y 属性。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getLocationInView (out?: Vec2) {
if (!out) {
@@ -217,6 +226,7 @@ export class Touch {
/**
* @en Returns the previous touch location in screen coordinates.
* @zh 获取触点在上一次事件时在游戏窗口中的位置对象,对象包含 x 和 y 属性。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getPreviousLocationInView (out?: Vec2) {
if (!out) {
@@ -230,6 +240,7 @@ export class Touch {
/**
* @en Returns the start touch location in screen coordinates.
* @zh 获取触点落下时在游戏窗口中的位置对象,对象包含 x 和 y 属性。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getStartLocationInView (out?: Vec2) {
if (!out) {
@@ -241,7 +252,7 @@ export class Touch {
}
/**
* @en Returns the id of cc.Touch.
* @en Returns the id of the touch point.
* @zh 触点的标识 ID,可以用来在多点触摸中跟踪触点。
*/
public getID () {
@@ -249,10 +260,13 @@ export class Touch {
}
/**
* @en Sets information to touch.
* @zh 设置触摸相关的信息。用于监控触摸事件。
* @en Resets touch point information.
* @zh 重置触点相关的信息。
* @param id - The id of the touch point
* @param x - x position of the touch point
* @param y - y position of the touch point
*/
public setTouchInfo (id: number | null = null, x?: number, y?: number) {
public setTouchInfo (id: number = 0, x?: number, y?: number) {
this._prevPoint = this._point;
this._point = new Vec2(x || 0, y || 0);
this._id = id;
@@ -263,8 +277,19 @@ export class Touch {
}
}
/**
* @en Sets touch point location.
* @zh 设置触点位置。
* @param point - The location
*/
public setPoint (point: Vec2): void;
/**
* @en Sets touch point location.
* @zh 设置触点位置。
* @param x - x position
* @param y - y position
*/
public setPoint (x: number, y: number): void;
public setPoint (x: number | Vec2, y?: number) {
@@ -278,8 +303,19 @@ export class Touch {
this._lastModified = cc.director.getCurrentTime();
}
/**
* @en Sets the location previously registered for the current touch.
* @zh 设置触点在前一次触发时收集的位置。
* @param point - The location
*/
public setPrevPoint (point: Vec2): void;
/**
* @en Sets the location previously registered for the current touch.
* @zh 设置触点在前一次触发时收集的位置。
* @param x - x position
* @param y - y position
*/
public setPrevPoint (x: number, y: number): void;
public setPrevPoint (x: number | Vec2, y?: number) {
File diff suppressed because it is too large Load Diff
+24 -23
View File
@@ -28,12 +28,10 @@
* @category core
*/
// tslint:disable
/**
* The fullscreen API provides an easy way for web content to be presented using the user's entire screen.
* It's invalid on safari, QQbrowser and android browser
* @class screen
* @en The screen API provides an easy way for web content to be presented using the user's entire screen.
* It's designed for web platforms and some mobile browsers don't provide such behavior, e.g. Safari
* @zh screen 单例对象提供简单的方法来尝试让 Web 内容进入全屏模式。这是 Web 平台特有的行为,在部分浏览器上并不支持这样的功能。
*/
const screen = {
_supportsFullScreen: false,
@@ -81,8 +79,8 @@ const screen = {
],
/**
* initialize
* @method init
* @en Initialization
* @zh 初始化函数
*/
init () {
this._fn = {};
@@ -102,8 +100,8 @@ const screen = {
},
/**
* return true if it's full now.
* @method fullScreen
* @en Return true if it's in full screen state now.
* @zh 当前是否处在全屏状态下
* @returns {Boolean}
*/
fullScreen () {
@@ -117,12 +115,15 @@ const screen = {
},
/**
* change the screen to full mode.
* @method requestFullScreen
* @param {Element} element
* @param {Function} onFullScreenChange
* @en Request to enter full screen mode with the given element.
* Many browser forbid to enter full screen mode without an user intended interaction.
* For simplify the process, you can try to use {{autoFullScreen}} which will try to enter full screen mode during the next user touch event.
* @zh 尝试使当前节点进入全屏模式,很多浏览器不允许程序触发这样的行为,必须在一个用户交互回调中才会生效。
* 如果希望更简单一些,可以尝试用 {{autoFullScreen}} 来自动监听用户触摸事件并在下一次触摸事件中尝试进入全屏模式。
* @param element The element to request full screen state
* @param onFullScreenChange callback function when full screen state changed
*/
requestFullScreen (element, onFullScreenChange) {
requestFullScreen (element: HTMLElement, onFullScreenChange: (this: Document, ev: any) => any) {
if (!this._supportsFullScreen) {
return;
}
@@ -142,21 +143,21 @@ const screen = {
},
/**
* exit the full mode.
* @method exitFullScreen
* @return {Boolean}
* @en Exit the full mode.
* @zh 退出全屏模式
* @return Success or not
*/
exitFullScreen () {
exitFullScreen (): boolean {
return this._supportsFullScreen ? document[this._fn.exitFullscreen]() : true;
},
/**
* Automatically request full screen with a touch/click event
* @method autoFullScreen
* @param {Element} element
* @param {Function} onFullScreenChange
* @en Automatically request full screen during the next touch/click event
* @zh 自动监听触摸、鼠标事件并在下一次事件触发时尝试进入全屏模式
* @param element The element to request full screen state
* @param onFullScreenChange callback function when full screen state changed
*/
autoFullScreen (element, onFullScreenChange) {
autoFullScreen (element: HTMLElement, onFullScreenChange: (this: Document, ev: any) => any) {
element = element || document.body;
const touchTarget = cc.game.canvas || element;
const theScreen = this;
+663 -747
View File
File diff suppressed because it is too large Load Diff
+177 -407
View File
@@ -32,7 +32,7 @@
import '../data/class';
import { EventTarget } from '../event/event-target';
import '../game';
import { Rect, Size } from '../math';
import { Rect, Size, Vec2 } from '../math';
import visibleRect from './visible-rect';
import { EDITOR, MINIGAME, WECHAT, JSB } from 'internal:constants';
@@ -112,21 +112,21 @@ switch (__BrowserGetter.adaptationType) {
break;
}
let _scissorRect: Rect | null = null;
/**
* cc.view is the singleton object which represents the game window.<br/>
* @en View represents the game window.<br/>
* It's main task include: <br/>
* - Apply the design resolution policy<br/>
* - Apply the design resolution policy to the UI Canvas<br/>
* - Provide interaction with the window, like resize event on web, retina display support, etc...<br/>
* - Manage the game view port which can be different with the window<br/>
* - Manage the content scale and translation<br/>
* - Manage the scale and translation of canvas related to the frame on Web<br/>
* <br/>
* Since the cc.view is a singleton, you don't need to call any constructor or create functions,<br/>
* With {{view}} as its singleton initialized by the engine, you don't need to call any constructor or create functions,<br/>
* the standard way to use it is by calling:<br/>
* - cc.view.methodName(); <br/>
*
* @class View
* - view.methodName(); <br/>
* @zh View 代表游戏窗口视图,它的核心功能包括:
* - 对所有 UI Canvas 进行设计分辨率适配。
* - 提供窗口视图的交互,比如监听 resize 事件,控制 retina 屏幕适配,等等。
* - 控制 Canvas 节点相对于外层 DOM 节点的缩放和偏移。
* 引擎会自动初始化它的单例对象 {{view}},所以你不需要实例化任何 View,只需要直接使用 `view.methodName();`
*/
export class View extends EventTarget {
@@ -144,14 +144,14 @@ export class View extends EventTarget {
private _devicePixelRatio: number;
private _maxPixelRatio: number;
private _retinaEnabled: boolean;
private _resizeCallback: null;
private _resizeCallback: Function | null;
private _resizing: boolean;
private _orientationChanging: boolean;
private _isRotated: boolean;
private _orientation: any;
private _isAdjustViewport: boolean;
private _antiAliasEnabled: boolean;
private _resolutionPolicy: any;
private _resolutionPolicy: ResolutionPolicy;
private _rpExactFit: ResolutionPolicy;
private _rpShowAll: ResolutionPolicy;
private _rpNoBorder: ResolutionPolicy;
@@ -199,12 +199,12 @@ export class View extends EventTarget {
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);
this._resolutionPolicy = this._rpShowAll;
cc.game.once(cc.Game.EVENT_ENGINE_INITED, this.init, this);
}
@@ -233,40 +233,15 @@ export class View extends EventTarget {
}
}
/**
* @en
* Sets view's target-densitydpi for android mobile browser. it can be set to: <br/>
* 1. macro.DENSITYDPI_DEVICE, value is "device-dpi" <br/>
* 2. macro.DENSITYDPI_HIGH, value is "high-dpi" (default value) <br/>
* 3. macro.DENSITYDPI_MEDIUM, value is "medium-dpi" (browser's default value) <br/>
* 4. macro.DENSITYDPI_LOW, value is "low-dpi" <br/>
* 5. Custom value, e.g: "480" <br/>
* @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.<br/>
* Useful only on web.
* @zh 设置当发现浏览器的尺寸改变时,是否自动调整 canvas 尺寸大小。
* 仅在 Web 模式下有效。
* @method resizeWithBrowserSize
* @param {Boolean} enabled - Whether enable automatic resize with browser's resize event
* @param enabled - Whether enable automatic resize with browser's resize event
*/
public resizeWithBrowserSize (enabled) {
public resizeWithBrowserSize (enabled: boolean) {
if (enabled) {
// enable
if (!this._resizeWithBrowserSize) {
@@ -294,10 +269,9 @@ export class View extends EventTarget {
* 这个回调函数会在应用适配模式之前被调用,
* 因此你可以在这个回调函数内添加任意附加改变,
* 仅在 Web 平台下有效。
* @method setResizeCallback
* @param {Function|Null} callback - The callback function
* @param callback - The callback function
*/
public setResizeCallback (callback) {
public setResizeCallback (callback: Function | null) {
if (typeof callback === 'function' || callback == null) {
this._resizeCallback = callback;
}
@@ -314,10 +288,9 @@ export class View extends EventTarget {
* 当设置为横版或竖版,并且屏幕的宽高比例不匹配时,
* cc.view 会自动用 CSS 旋转游戏场景的 canvas,
* 这个方法不会对 native 部分产生任何影响,对于 native 而言,你需要在应用设置中的设置排版。
* @method setOrientation
* @param {Number} orientation - Possible values: macro.ORIENTATION_LANDSCAPE | macro.ORIENTATION_PORTRAIT | macro.ORIENTATION_AUTO
* @param orientation - Possible values: macro.ORIENTATION_LANDSCAPE | macro.ORIENTATION_PORTRAIT | macro.ORIENTATION_AUTO
*/
public setOrientation (orientation) {
public setOrientation (orientation: number) {
orientation = orientation & cc.macro.ORIENTATION_AUTO;
if (orientation && this._orientation !== orientation) {
this._orientation = orientation;
@@ -334,10 +307,9 @@ export class View extends EventTarget {
* 默认设置为启动,我们强烈建议你不要将它设置为关闭。
* 即使当它启动时,你仍然能够设置你的 viewport meta,它不会被覆盖。
* 仅在 Web 模式下有效
* @method adjustViewportMeta
* @param {Boolean} enabled - Enable automatic modification to "viewport" meta
* @param enabled - Enable automatic modification to "viewport" meta
*/
public adjustViewportMeta (enabled) {
public adjustViewportMeta (enabled: boolean) {
this._isAdjustViewport = enabled;
}
@@ -349,10 +321,9 @@ export class View extends EventTarget {
* @zh 对于 Apple 这种支持 Retina 显示的设备上默认进行优化而其他类型设备默认不进行优化,
* 它仅会在你调用 setDesignResolutionPolicy 方法时有影响。
* 仅在 Web 模式下有效。
* @method enableRetina
* @param {Boolean} enabled - Enable or disable retina display
* @param enabled - Enable or disable retina display
*/
public enableRetina (enabled) {
public enableRetina (enabled: boolean) {
this._retinaEnabled = !!enabled;
}
@@ -362,20 +333,17 @@ export class View extends EventTarget {
* Only useful on web
* @zh 检查是否对 Retina 显示设备进行优化。
* 仅在 Web 模式下有效。
* @method isRetinaEnabled
* @return {Boolean}
*/
public isRetinaEnabled () {
public isRetinaEnabled (): boolean {
return this._retinaEnabled;
}
/**
* @en Whether to Enable on anti-alias
* @zh 控制抗锯齿是否开启
* @method enableAntiAlias
* @param {Boolean} enabled - Enable or not anti-alias
* @param enabled - Enable or not anti-alias
*/
public enableAntiAlias (enabled) {
public enableAntiAlias (enabled: boolean) {
if (this._antiAliasEnabled === enabled) {
return;
}
@@ -407,10 +375,8 @@ export class View extends EventTarget {
/**
* @en Returns whether the current enable on anti-alias
* @zh 返回当前是否抗锯齿
* @method isAntiAliasEnabled
* @return {Boolean}
*/
public isAntiAliasEnabled () {
public isAntiAliasEnabled (): boolean {
return this._antiAliasEnabled;
}
/**
@@ -420,10 +386,9 @@ export class View extends EventTarget {
* Only useful on web
* @zh 启动时,移动端游戏会在移动端自动尝试进入全屏模式。
* 你能够传入 true 为参数去启动它,用 false 参数来关闭它。
* @method enableAutoFullScreen
* @param {Boolean} enabled - Enable or disable auto full screen on mobile devices
* @param enabled - Enable or disable auto full screen on mobile devices
*/
public enableAutoFullScreen (enabled) {
public enableAutoFullScreen (enabled: boolean) {
if (enabled &&
enabled !== this._autoFullScreen &&
cc.sys.isMobile &&
@@ -443,10 +408,9 @@ export class View extends EventTarget {
* Only useful on web
* @zh 检查自动进入全屏模式是否启动。
* 仅在 Web 模式下有效。
* @method isAutoFullScreenEnabled
* @return {Boolean} Auto full screen enabled or not
* @return Auto full screen enabled or not
*/
public isAutoFullScreenEnabled () {
public isAutoFullScreenEnabled (): boolean {
return this._autoFullScreen;
}
@@ -454,11 +418,11 @@ export class View extends EventTarget {
* Not support on native.<br/>
* On web, it sets the size of the canvas.
* @zh 这个方法并不支持 native 平台,在 Web 平台下,可以用来设置 canvas 尺寸。
* @method setCanvasSize
* @private
* @param {Number} width
* @param {Number} height
*/
public setCanvasSize (width, height) {
public setCanvasSize (width: number, height: number) {
const canvas = cc.game.canvas;
const container = cc.game.container;
this._devicePixelRatio = window.devicePixelRatio;
@@ -486,11 +450,9 @@ export class View extends EventTarget {
* @zh 返回视图中 canvas 的尺寸。
* 在 native 平台下,它返回全屏视图下屏幕的尺寸。
* 在 Web 平台下,它返回 canvas 元素尺寸。
* @method getCanvasSize
* @return {Size}
*/
public getCanvasSize () {
return cc.size(cc.game.canvas.width, cc.game.canvas.height);
public getCanvasSize (): Size {
return new Size(cc.game.canvas.width, cc.game.canvas.height);
}
/**
@@ -501,24 +463,20 @@ export class View extends EventTarget {
* @zh 返回视图中边框尺寸。
* 在 native 平台下,它返回全屏视图下屏幕的尺寸。
* 在 web 平台下,它返回 canvas 元素的外层 DOM 元素尺寸。
* @method getFrameSize
* @return {Size}
*/
public getFrameSize () {
return cc.size(this._frameSize.width, this._frameSize.height);
public getFrameSize (): Size {
return new Size(this._frameSize.width, this._frameSize.height);
}
/**
* @en
* On native, it sets the frame size of view.<br/>
* @en On native, it sets the frame size of view.<br/>
* 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) {
public setFrameSize (width: number, height: number) {
this._frameSize.width = width;
this._frameSize.height = height;
cc.frame.style.width = width + 'px';
@@ -527,72 +485,54 @@ export class View extends EventTarget {
}
/**
* @en
* Returns the visible area size of the view port.
* @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);
public getVisibleSize (): Size {
return new Size(this._visibleRect.width, this._visibleRect.height);
}
/**
* @en
* Returns the visible area size of the view port.
* @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,
public getVisibleSizeInPixel (): Size {
return new Size( this._visibleRect.width * this._scaleX,
this._visibleRect.height * this._scaleY );
}
/**
* @en
* Returns the visible origin of the view port.
* @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);
public getVisibleOrigin (): Vec2 {
return new Vec2(this._visibleRect.x, this._visibleRect.y);
}
/**
* @en
* Returns the visible origin of the view port.
* @en Returns the visible origin of the view port.
* @zh 返回视图窗口可见区域像素原点。
* @method getVisibleOriginInPixel
* @return {Vec2}
*/
public getVisibleOriginInPixel () {
return cc.v2(this._visibleRect.x * this._scaleX,
public getVisibleOriginInPixel (): Vec2 {
return new Vec2(this._visibleRect.x * this._scaleX,
this._visibleRect.y * this._scaleY);
}
/**
* @en
* Returns the current resolution policy
* @en Returns the current resolution policy
* @zh 返回当前分辨率方案
* @see ResolutionPolicy
* @method getResolutionPolicy
* @return {ResolutionPolicy}
* @see {{ResolutionPolicy}}
*/
public getResolutionPolicy () {
public getResolutionPolicy (): ResolutionPolicy {
return this._resolutionPolicy;
}
/**
* @en
* Sets the current resolution policy
* @en Sets the current resolution policy
* @zh 设置当前分辨率模式
* @see ResolutionPolicy
* @method setResolutionPolicy
* @param {ResolutionPolicy|Number} resolutionPolicy
* @see {{ResolutionPolicy}}
*/
public setResolutionPolicy (resolutionPolicy) {
public setResolutionPolicy (resolutionPolicy: ResolutionPolicy|number) {
const _t = this;
if (resolutionPolicy instanceof ResolutionPolicy) {
_t._resolutionPolicy = resolutionPolicy;
@@ -620,8 +560,7 @@ export class View extends EventTarget {
// tslint:disable: max-line-length
/**
* @en
* Sets the resolution policy with designed view size in points.<br/>
* @en Sets the resolution policy with designed view size in points.<br/>
* The resolution policy include: <br/>
* [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.<br/>
* [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.<br/>
@@ -630,12 +569,11 @@ export class View extends EventTarget {
* [5] ResolutionFixedWidth Scale the content's width to screen's width and proportionally scale its height<br/>
* [ResolutionPolicy] [Web only feature] Custom resolution policy, constructed by ResolutionPolicy<br/>
* @zh 通过设置设计分辨率和匹配模式来进行游戏画面的屏幕适配。
* @method setDesignResolutionSize
* @param {Number} width Design resolution width.
* @param {Number} height Design resolution height.
* @param {ResolutionPolicy|Number} resolutionPolicy The resolution policy desired
* @param width Design resolution width.
* @param height Design resolution height.
* @param resolutionPolicy The resolution policy desired
*/
public setDesignResolutionSize (width, height, resolutionPolicy) {
public setDesignResolutionSize (width: number, height: number, resolutionPolicy: ResolutionPolicy|number) {
// Defensive code
if ( !(width > 0 || height > 0) ){
cc.logID(2200);
@@ -703,21 +641,17 @@ export class View extends EventTarget {
}
/**
* @en
* Returns the designed size for the view.
* @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);
public getDesignResolutionSize (): Size {
return new Size(this._designResolutionSize.width, this._designResolutionSize.height);
}
/**
* @en
* Sets the container to desired pixel resolution and fit the game content to it.
* @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.
@@ -727,12 +661,11 @@ export class View extends EventTarget {
* 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
* @param width Design resolution width.
* @param height Design resolution height.
* @param resolutionPolicy The resolution policy desired
*/
public setRealPixelResolution (width, height, resolutionPolicy) {
public setRealPixelResolution (width: number, height: number, resolutionPolicy: ResolutionPolicy|number) {
if (!JSB && !MINIGAME) {
// Set viewport's width
this._setViewportMeta({width}, true);
@@ -749,152 +682,47 @@ export class View extends EventTarget {
}
/**
* @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.
* @en Returns the view port rectangle.
* @zh 返回视窗剪裁区域。
* @method getViewportRect
* @return {Rect}
*/
public getViewportRect () {
public getViewportRect (): Rect {
return this._viewportRect;
}
/**
* @en
* Returns scale factor of the horizontal direction (X axis).
* @en Returns scale factor of the horizontal direction (X axis).
* @zh 返回横轴的缩放比,这个缩放比是将画布像素分辨率放到设计分辨率的比例。
* @method getScaleX
* @return {Number}
*/
public getScaleX () {
public getScaleX (): number {
return this._scaleX;
}
/**
* @en
* Returns scale factor of the vertical direction (Y axis).
* @en Returns scale factor of the vertical direction (Y axis).
* @zh 返回纵轴的缩放比,这个缩放比是将画布像素分辨率缩放到设计分辨率的比例。
* @method getScaleY
* @return {Number}
*/
public getScaleY () {
public getScaleY (): number {
return this._scaleY;
}
/**
* @en
* Returns device pixel ratio for retina display.
* @en Returns device pixel ratio for retina display.
* @zh 返回设备或浏览器像素比例。
* @method getDevicePixelRatio
* @return {Number}
*/
public getDevicePixelRatio () {
public getDevicePixelRatio (): number {
return this._devicePixelRatio;
}
/**
* @en
* Returns the real location in view for a translation based on a related position
* @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}
* @param tx - The X axis translation
* @param ty - The Y axis translation
* @param relatedPos - The related position object including "left", "top", "width", "height" informations
* @param out - The out object to save the conversion result
*/
public convertToLocationInView (tx, ty, relatedPos, out) {
const result = out || cc.v2();
public convertToLocationInView (tx: number, ty: number, relatedPos: any, out: Vec2): Vec2 {
const result = out || new Vec2();
const x = this._devicePixelRatio * (tx - relatedPos.left);
const y = this._devicePixelRatio * (relatedPos.top + relatedPos.height - ty);
if (this._isRotated) {
@@ -914,7 +742,7 @@ export class View extends EventTarget {
// in_out_point.y = (_t._devicePixelRatio * (relatedPos.top + relatedPos.height - in_out_point.y) - viewport.y) / _t._scaleY;
// }
public _convertPointWithScale (point) {
private _convertPointWithScale (point) {
const viewport = this._viewportRect;
point.x = (point.x - viewport.x) / this._scaleX;
point.y = (point.y - viewport.y) / this._scaleY;
@@ -1103,10 +931,16 @@ export class View extends EventTarget {
* !zh
* 当设计分辨率改变时发送。
* @event design-resolution-changed
* <p>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</p>
*
* @class ContainerStrategy
*/
interface AdaptResult {
scale: number[];
viewport?: null | Rect;
}
/**
* 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 {
public static EQUAL_TO_FRAME: any;
@@ -1115,34 +949,29 @@ class ContainerStrategy {
public name: string = 'ContainerStrategy';
/**
* @en
* Manipulation before appling the strategy
* @en Manipulation before appling the strategy
* @zh 在应用策略之前的操作
* @method preApply
* @param {View} view - The target view
* @param view - The target view
*/
public preApply (_view) {
public preApply (_view: View) {
}
/**
* @en
* Function to apply this strategy
* @en Function to apply this strategy
* @zh 策略应用方法
* @method apply
* @param {View} view
* @param {Size} designedResolution
* @param view
* @param designedResolution
*/
public apply (_view, designedResolution) {
public apply (_view: View, designedResolution: Size) {
}
/**
* @en
* Manipulation after applying the strategy
* @zh 策略调用之后的操作
* @method postApply
* @param {View} view The target view
* @param view The target view
*/
public postApply (_view) {
public postApply (_view: View) {
}
@@ -1187,8 +1016,8 @@ class ContainerStrategy {
}
/**
* <p>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</p>
* 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
*/
@@ -1200,7 +1029,7 @@ class ContentStrategy {
public static FIXED_WIDTH: any;
public name = 'ContentStrategy';
private _result: { scale: number[]; viewport: null | Rect; };
private _result: AdaptResult;
constructor () {
this._result = {
scale: [1, 1],
@@ -1209,13 +1038,11 @@ class ContentStrategy {
}
/**
* @en
* Manipulation before applying the strategy
* @en Manipulation before applying the strategy
* @zh 策略应用前的操作
* @method preApply
* @param {View} view - The target view
* @param view - The target view
*/
public preApply (_view) {
public preApply (_view: View) {
}
/**
@@ -1223,26 +1050,21 @@ class ContentStrategy {
* 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
* @return The result scale and viewport rect
*/
public apply (_view, designedResolution) {
public apply (_view: View, designedResolution: Size): AdaptResult {
return {scale: [1, 1]};
}
/**
* @en
* Manipulation after applying the strategy
* @en Manipulation after applying the strategy
* @zh 策略调用之后的操作
* @method postApply
* @param {View} view - The target view
* @param view - The target view
*/
public postApply (_view) {
public postApply (_view: View) {
}
public _buildResult (containerW, containerH, contentW, contentH, scaleX, scaleY) {
public _buildResult (containerW, containerH, contentW, contentH, scaleX, scaleY): AdaptResult {
// Makes content fit better the canvas
if ( Math.abs(containerW - contentW) < 2 ) {
contentW = containerW;
@@ -1262,7 +1084,6 @@ class ContentStrategy {
}
(() => {
// Container scale strategys
/**
* @class EqualToFrame
@@ -1348,7 +1169,7 @@ class ContentStrategy {
// Content scale strategys
class ExactFit extends ContentStrategy {
public name = 'ExactFit';
public apply (_view, designedResolution) {
public apply (_view: View, designedResolution: Size) {
const containerW = cc.game.canvas.width;
const containerH = cc.game.canvas.height;
const scaleX = containerW / designedResolution.width;
@@ -1440,25 +1261,45 @@ class ContentStrategy {
})();
/**
* <p>ResolutionPolicy class is the root strategy class of scale strategy,
* ResolutionPolicy class is the root strategy class of scale strategy,
* its main task is to maintain the compatibility with Cocos2d-x</p>
*
* @class ResolutionPolicy
*/
/**
* @method constructor
* @param {ContainerStrategy} containerStg The container strategy
* @param {ContentStrategy} contentStg The content strategy
*/
export 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;
/**
* The entire application is visible in the specified area without trying to preserve the original aspect ratio.<br/>
* Distortion can occur, and the application may appear stretched or compressed.
*/
public static EXACT_FIT: number = 0;
/**
* The entire application fills the specified area, without distortion but possibly with some cropping,<br/>
* while maintaining the original aspect ratio of the application.
*/
public static NO_BORDER: number = 1;
/**
* The entire application is visible in the specified area without distortion while maintaining the original<br/>
* aspect ratio of the application. Borders can appear on two sides of the application.
*/
public static SHOW_ALL: number = 2;
/**
* The application takes the height of the design resolution size and modifies the width of the internal<br/>
* canvas so that it fits the aspect ratio of the device<br/>
* no distortion will occur however you must make sure your application works on different<br/>
* aspect ratios
*/
public static FIXED_HEIGHT: number = 3;
/**
* The application takes the width of the design resolution size and modifies the height of the internal<br/>
* canvas so that it fits the aspect ratio of the device<br/>
* no distortion will occur however you must make sure your application works on different<br/>
* aspect ratios
*/
public static FIXED_WIDTH: number = 4;
/**
* Unknown policy
*/
public static UNKNOWN: number = 5;
public static ContainerStrategy: typeof ContainerStrategy = ContainerStrategy;
public static ContentStrategy: typeof ContentStrategy = ContentStrategy;
public name = 'ResolutionPolicy';
@@ -1467,10 +1308,10 @@ export class ResolutionPolicy {
/**
* Constructor of ResolutionPolicy
* @param {ContainerStrategy} containerStg
* @param {ContentStrategy} contentStg
* @param containerStg
* @param contentStg
*/
constructor (containerStg, contentStg) {
constructor (containerStg: ContainerStrategy, contentStg: ContentStrategy) {
this._containerStrategy = null;
this._contentStrategy = null;
this.setContainerStrategy(containerStg);
@@ -1484,10 +1325,9 @@ export class ResolutionPolicy {
/**
* @en Manipulation before applying the resolution policy
* @zh 策略应用前的操作
* @method preApply
* @param {View} _view The target view
* @param _view The target view
*/
public preApply (_view) {
public preApply (_view: View) {
this._containerStrategy!.preApply(_view);
this._contentStrategy!.preApply(_view);
}
@@ -1497,12 +1337,11 @@ export class ResolutionPolicy {
* 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
* @param _view - The target view
* @param designedResolution - The user defined design resolution
* @return An object contains the scale X/Y values and the viewport rect
*/
public apply (_view, designedResolution) {
public apply (_view: View, designedResolution: Size) {
this._containerStrategy!.apply(_view, designedResolution);
return this._contentStrategy!.apply(_view, designedResolution);
}
@@ -1510,114 +1349,45 @@ export class ResolutionPolicy {
/**
* @en Manipulation after appyling the strategy
* @zh 策略应用之后的操作
* @method postApply
* @param {View} _view - The target view
* @param _view - The target view
*/
public postApply (_view) {
public postApply (_view: View) {
this._containerStrategy!.postApply(_view);
this._contentStrategy!.postApply(_view);
}
/**
* @en
* Setup the container's scale strategy
* @en Setup the container's scale strategy
* @zh 设置容器的适配策略
* @method setContainerStrategy
* @param {ContainerStrategy} containerStg
* @param containerStg The container strategy
*/
public setContainerStrategy (containerStg) {
public setContainerStrategy (containerStg: ContainerStrategy) {
if (containerStg instanceof ContainerStrategy) {
this._containerStrategy = containerStg;
}
}
/**
* @en
* Setup the content's scale strategy
* @en Setup the content's scale strategy
* @zh 设置内容的适配策略
* @method setContentStrategy
* @param {ContentStrategy} contentStg
* @param contentStg The content strategy
*/
public setContentStrategy (contentStg) {
public setContentStrategy (contentStg: ContentStrategy) {
if (contentStg instanceof ContentStrategy) {
this._contentStrategy = contentStg;
}
}
}
/**
* The entire application is visible in the specified area without trying to preserve the original aspect ratio.<br/>
* 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,<br/>
* 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<br/>
* 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<br/>
* canvas so that it fits the aspect ratio of the device<br/>
* no distortion will occur however you must make sure your application works on different<br/>
* 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<br/>
* canvas so that it fits the aspect ratio of the device<br/>
* no distortion will occur however you must make sure your application works on different<br/>
* 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 是全局的视图对象。
* @en view is the singleton view object.
* @zh view 是全局的视图单例对象。
*/
export const view = View.instance = cc.view = new View();
/**
* @en cc.winSize is the alias object for the size of the current game window.
* @zh cc.winSize 为当前的游戏窗口的大小。
* @en winSize is the alias object for the size of the current game window.
* @zh winSize 为当前的游戏窗口的大小。
*/
cc.winSize = cc.v2();
cc.winSize = new Vec2();
+1 -1
View File
@@ -6,7 +6,7 @@ import { IGFXColor } from "./gfx";
import { clamp01 } from "./math";
import { easing } from "./animation";
import { macro } from "./platform";
import sys from "./platform/sys";
import { sys } from "./platform/sys";
import { COCOSPLAY, XIAOMI, JSB } from 'internal:constants';
type SplashEffectType = 'none' | 'Fade-InOut';
+1 -1
View File
@@ -34,7 +34,7 @@ import { bmfont } from './bmfont';
import { CanvasPool } from './font-utils';
import { letter } from './letter';
import { ttf } from './ttf';
import sys from '../../../core/platform/sys';
import { sys } from '../../../core/platform/sys';
import { warn } from '../../../core/platform/debug';
const labelAssembler: IAssemblerManager = {
@@ -42,7 +42,7 @@ import { SpriteComponent } from '../sprite-component';
import { EditBoxImpl } from './edit-box-impl';
import { EditBoxImplBase } from './edit-box-impl-base';
import { InputFlag, InputMode, KeyboardReturnType } from './types';
import sys from '../../../core/platform/sys';
import { sys } from '../../../core/platform/sys';
import { EDITOR } from 'internal:constants';
const LEFT_PADDING = 2;
+1 -1
View File
@@ -41,7 +41,7 @@ import { LabelComponent } from '../label-component';
import { EditBoxComponent} from './edit-box-component';
import { tabIndexUtil } from './tabIndexUtil';
import { InputFlag, InputMode, KeyboardReturnType } from './types';
import sys from '../../../core/platform/sys';
import { sys } from '../../../core/platform/sys';
import visibleRect from '../../../core/platform/visible-rect';
import { Node } from '../../../core';
import { EditBoxImplBase } from './edit-box-impl-base';
+1 -1
View File
@@ -37,7 +37,7 @@ import { CanvasPool, ISharedLabelData } from '../assembler/label/font-utils';
import { LetterRenderTexture } from '../assembler/label/letter-font';
import { UIRenderComponent } from '../../core/components/ui-base/ui-render-component';
import { warnID } from '../../core/platform/debug';
import sys from '../../core/platform/sys';
import { sys } from '../../core/platform/sys';
import { EDITOR } from 'internal:constants';
/**
+1 -1
View File
@@ -28,7 +28,7 @@
*/
import { ccclass } from '../../../core/data/class-decorator';
import sys from '../../../core/platform/sys';
import { sys } from '../../../core/platform/sys';
import { misc } from '../../../core/utils';
import { Mat4 } from '../../../core/math';
import { UIRenderComponent } from '../../../core/components/ui-base/ui-render-component';
+1 -2
View File
@@ -29,10 +29,9 @@
*/
import { CanvasComponent } from '../../core/components/ui-base/canvas-component';
import { UIRenderComponent } from '../../core/components/ui-base/ui-render-component';
import { Director, director } from '../../core/director';
import { Vec2, Vec3 } from '../../core/math';
import sys from '../../core/platform/sys';
import { sys } from '../../core/platform/sys';
import { View } from '../../core/platform/view';
import visibleRect from '../../core/platform/visible-rect';
import { Scene } from '../../core/scene-graph';